diff --git a/go.mod b/go.mod index 9fd25e0..bc8539c 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,7 @@ require ( github.com/graphql-go/graphql v0.8.0 github.com/graphql-go/handler v0.2.3 github.com/jackc/pgx/v5 v5.2.0 + github.com/jhoonb/archivex v0.0.0-20201016144719-6a343cdae81d github.com/op/go-logging v0.0.0-20160315200505-970db520ece7 golang.org/x/net v0.2.0 google.golang.org/protobuf v1.28.1 diff --git a/go.sum b/go.sum index 1bc8c09..f111696 100644 --- a/go.sum +++ b/go.sum @@ -858,6 +858,8 @@ github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOl github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo= github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4= +github.com/jhoonb/archivex v0.0.0-20201016144719-6a343cdae81d h1:q7n+5taxmM+9T2Q7Ydo7YN90FkoDuR5bbzByZwkQqPo= +github.com/jhoonb/archivex v0.0.0-20201016144719-6a343cdae81d/go.mod h1:GN1Mg/uXQ6qwXA0HypnUO3xlcQJS9/y68EsHNeuuRa4= github.com/jhump/protoreflect v1.6.1/go.mod h1:RZQ/lnuN+zqeRVpQigTwO6o0AJUkxbnSnpuG7toUTG4= github.com/jhump/protoreflect v1.8.2/go.mod h1:7GcYQDdMU/O/BBrl/cX6PNHpXh6cenjd8pneu5yW7Tg= github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= diff --git a/pipeline_executor/pipeline_executor.go b/pipeline_executor/pipeline_executor.go index 206d9bf..3c4d492 100644 --- a/pipeline_executor/pipeline_executor.go +++ b/pipeline_executor/pipeline_executor.go @@ -5,10 +5,13 @@ import ( "context" "fmt" "io" + "io/ioutil" "os" "path/filepath" "strings" + "github.com/jhoonb/archivex" + "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/mount" @@ -34,6 +37,7 @@ type PipelineExecution struct { func ExecutePipeline(pe PipelineExecution, db database.Database, pipelineConf config.PipelineConf) { jobFolder := filepath.Join(pipelineConf.WorkingDir, pe.Pipeline.Id.String(), pe.Run.Id.String()) + cloneFolder := filepath.Join(jobFolder, "repo") log.Debugf("Job %v configured with URL \"%v\"", pe.Pipeline.Name, pe.Pipeline.Url) @@ -45,7 +49,7 @@ func ExecutePipeline(pe PipelineExecution, db database.Database, pipelineConf co return } - err = os.MkdirAll(jobFolder, 0755) + err = os.MkdirAll(cloneFolder, 0755) if err != nil { log.Errorf("could not create working directory for job %v: %w", pe.Pipeline.Name, err) return @@ -84,7 +88,7 @@ func ExecutePipeline(pe PipelineExecution, db database.Database, pipelineConf co auth = nil } - _, err = git.PlainClone(jobFolder, false, &git.CloneOptions{ + _, err = git.PlainClone(cloneFolder, false, &git.CloneOptions{ URL: pe.Pipeline.Url, Auth: auth, }) @@ -102,6 +106,49 @@ func ExecutePipeline(pe PipelineExecution, db database.Database, pipelineConf co ctx := context.Background() + log.Info("Building container") + + tarFile := filepath.Join(jobFolder, "archive.tar") + tar := new(archivex.TarFile) + err = tar.Create(tarFile) + if err != nil { + log.Errorf("could not create tarfile: %w", err) + return + } + + err = tar.AddAll(cloneFolder, false) + if err != nil { + log.Errorf("could not add repo to tarfile: %w", err) + return + } + + err = tar.Close() + if err != nil { + log.Errorf("could not close tarfile: %w", err) + return + } + + dockerBuildContext, err := os.Open(tarFile) + defer dockerBuildContext.Close() + + buildResponse, err := cli.ImageBuild(context.Background(), dockerBuildContext, types.ImageBuildOptions{ + Dockerfile: ".cursorius/Dockerfile", + }) + if err != nil { + log.Errorf("could not build container: %w", err) + return + } + + log.Debugf("********* %s **********", buildResponse.OSType) + response, err := ioutil.ReadAll(buildResponse.Body) + if err != nil { + log.Errorf("could no read build response: %w", err) + return + } + log.Debugf("build log: %v", string(response)) + + return + imageName := "git.ohea.xyz/cursorius/pipeline-api/cursorius-pipeline:v2" log.Infof("Pulling image %v", imageName)