Skip to content

Commit

Permalink
Add volume support and clone container
Browse files Browse the repository at this point in the history
  • Loading branch information
robertjndw committed Sep 23, 2023
1 parent aee8d0a commit 4dd322c
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 49 deletions.
48 changes: 0 additions & 48 deletions HadesScheduler/docker.go

This file was deleted.

17 changes: 17 additions & 0 deletions HadesScheduler/docker/container.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package docker

import (
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
)

var defaultHostConfig = container.HostConfig{
Mounts: []mount.Mount{
{
Type: mount.TypeVolume,
Source: sharedVolumeName,
Target: "/shared",
},
},
AutoRemove: true,
}
90 changes: 90 additions & 0 deletions HadesScheduler/docker/docker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package docker

import (
"context"
"github.com/Mtze/HadesCI/shared/payload"
"github.com/Mtze/HadesCI/shared/utils"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
_ "github.com/docker/docker/client"
log "github.com/sirupsen/logrus"
)

const (
cloneContainerImage = "alpine/git:latest"
sharedVolumeName = "shared"
)

type DockerScheduler struct{}

func (d *DockerScheduler) ScheduleJob(job payload.BuildJob) error {
ctx := context.Background()
// Create a new Docker client
cli, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
return err
}
defer cli.Close()

// Create the shared volume
err = createSharedVolume(ctx, cli, sharedVolumeName)
if err != nil {
log.WithError(err).Error("Failed to create shared volume")
return err
}

// Clone the repository
err = cloneRepository(ctx, cli, job.BuildConfig.Repositories...)
if err != nil {
log.WithError(err).Error("Failed to clone repository")
return err
}

// TODO enable deletion of shared volume
//time.Sleep(5 * time.Second)
//err = deleteSharedVolume(ctx, cli, sharedVolumeName)
//if err != nil {
// log.WithError(err).Error("Failed to delete shared volume")
// return err
//}

return nil
}

func cloneRepository(ctx context.Context, client *client.Client, repositories ...payload.Repository) error {
// Pull the image
_, err := client.ImagePull(ctx, cloneContainerImage, types.ImagePullOptions{})
if err != nil {
return err
}

// Use the index to modify the slice in place
for i := range repositories {
repositories[i].Path = "/shared" + repositories[i].Path
}
commandStr := utils.BuildCloneCommands(repositories...)
log.Debug(commandStr)

// Create the container
resp, err := client.ContainerCreate(ctx, &container.Config{
Image: cloneContainerImage,
Entrypoint: []string{"/bin/sh", "-c"},
Cmd: []string{commandStr},
Volumes: map[string]struct{}{
"/shared": {},
},
}, &defaultHostConfig, nil, nil, "")
if err != nil {
return err
}

// Start the container
err = client.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{})
if err != nil {
return err
}

log.Infof("Container %s started with ID: %s\n", cloneContainerImage, resp.ID)
return nil
}
34 changes: 34 additions & 0 deletions HadesScheduler/docker/volume.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package docker

import (
"context"
"github.com/docker/docker/api/types/volume"
"github.com/docker/docker/client"
log "github.com/sirupsen/logrus"
)

func createSharedVolume(ctx context.Context, client *client.Client, name string) error {
// Create the volume
_, err := client.VolumeCreate(ctx, volume.CreateOptions{
Name: name,
})
if err != nil {
log.WithError(err).Error("Failed to create shared volume")
return err
}

log.Debugf("Volume %s created\n", name)
return nil
}

func deleteSharedVolume(ctx context.Context, client *client.Client, name string) error {
// Delete the volume
err := client.VolumeRemove(ctx, name, true)
if err != nil {
log.WithError(err).Error("Failed to delete shared volume")
return err
}

log.Debugf("Volume %s deleted\n", name)
return nil
}
3 changes: 2 additions & 1 deletion HadesScheduler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"github.com/Mtze/HadesCI/hadesScheduler/docker"
"os"
"path/filepath"

Expand Down Expand Up @@ -62,7 +63,7 @@ func main() {

var forever chan struct{}

scheduler := DockerScheduler{}
scheduler := docker.DockerScheduler{}
BuildQueue.Dequeue(scheduler.ScheduleJob)

log.Printf(" [*] Waiting for messages. To exit press CTRL+C")
Expand Down
22 changes: 22 additions & 0 deletions shared/utils/git.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package utils

import (
"fmt"
"github.com/Mtze/HadesCI/shared/payload"
"strings"
)

func BuildCloneCommand(repo payload.Repository) string {
return fmt.Sprintf("git clone %s %s", repo.URL, repo.Path)
}

func BuildCloneCommands(repos ...payload.Repository) string {
var builder strings.Builder
for i, repo := range repos {
if i > 0 {
builder.WriteString(" && ")
}
builder.WriteString(BuildCloneCommand(repo))
}
return builder.String()
}

0 comments on commit 4dd322c

Please sign in to comment.