-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add volume support and clone container
- Loading branch information
1 parent
aee8d0a
commit 4dd322c
Showing
6 changed files
with
165 additions
and
49 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |