Skip to content

Commit

Permalink
Merge pull request #5 from Mtze/docker-prototype
Browse files Browse the repository at this point in the history
Docker Prototype
  • Loading branch information
robertjndw authored Sep 23, 2023
2 parents fb9166f + 2ee24fe commit 1cb3b08
Show file tree
Hide file tree
Showing 13 changed files with 349 additions and 47 deletions.
13 changes: 13 additions & 0 deletions .idea/runConfigurations/HadesAPI.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/runConfigurations/HadesCI.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/runConfigurations/HadesScheduler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions .idea/runConfigurations/RabbitMQ.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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,
}
95 changes: 95 additions & 0 deletions HadesScheduler/docker/docker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
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"
)

var cli *client.Client

type Scheduler struct{}

func init() {
var err error
// Create a new Docker client
cli, err = client.NewClientWithOpts(client.FromEnv)
if err != nil {
log.WithError(err).Fatal("Failed to create Docker client")
}
}

func (d *Scheduler) ScheduleJob(job payload.BuildJob) error {
ctx := context.Background()

// 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", 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", name)
return nil
}
38 changes: 30 additions & 8 deletions HadesScheduler/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,67 @@ module github.com/Mtze/HadesCI/hadesScheduler

go 1.21.0

replace github.com/Mtze/HadesCI/shared => ../shared

require (
github.com/Mtze/HadesCI/shared v0.0.0-00010101000000-000000000000
github.com/docker/docker v24.0.6+incompatible
github.com/sirupsen/logrus v1.9.3
k8s.io/client-go v0.28.2
)

require (
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/caarlos0/env/v9 v9.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/distribution v2.8.2+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/go-openapi/swag v0.22.4 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/imdario/mergo v0.3.6 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/joho/godotenv v1.5.1 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/rabbitmq/amqp091-go v1.8.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/mod v0.10.0 // indirect
golang.org/x/net v0.13.0 // indirect
golang.org/x/oauth2 v0.8.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/term v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
golang.org/x/tools v0.8.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gotest.tools/v3 v3.5.1 // indirect
k8s.io/api v0.28.2 // indirect
k8s.io/apimachinery v0.28.2 // indirect
k8s.io/client-go v0.28.2 // indirect
k8s.io/klog/v2 v2.100.1 // indirect
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 // indirect
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 // indirect
k8s.io/kube-openapi v0.0.0-20230918164632-68afd615200d // indirect
k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
Expand Down
Loading

0 comments on commit 1cb3b08

Please sign in to comment.