From 6096592d5bddbb32ed9b6f5c20ea14703f1c8130 Mon Sep 17 00:00:00 2001 From: Ali AKCA Date: Thu, 23 Nov 2023 22:43:37 +0100 Subject: [PATCH] refactor: remove unused runner cache and id --- daggerverse/gale/gale.go | 2 +- daggerverse/gale/runner.go | 75 ++++++-------------------------------- 2 files changed, 12 insertions(+), 65 deletions(-) diff --git a/daggerverse/gale/gale.go b/daggerverse/gale/gale.go index 5c90589..5c07bc3 100644 --- a/daggerverse/gale/gale.go +++ b/daggerverse/gale/gale.go @@ -118,7 +118,7 @@ func (g *Gale) Run( return nil, err } - runner, err := g.runner().Container(ctx, container, info) + runner, err := g.runner().Container(ctx, info, container) if err != nil { return nil, err } diff --git a/daggerverse/gale/runner.go b/daggerverse/gale/runner.go index 86a2dbc..ea91d5f 100644 --- a/daggerverse/gale/runner.go +++ b/daggerverse/gale/runner.go @@ -3,57 +3,36 @@ package main import ( "context" "fmt" - - "github.com/google/uuid" ) type Runner struct{} // RunnerContainer represents a container to run a Github Actions workflow in. type RunnerContainer struct { - // Unique identifier for the runner container. - RunnerID string - // Initialized container to run the workflow in. Ctr *Container } -// getRunnerContainer returns a runner container for the given container if it is initialized. -func (r *Runner) getRunnerContainer(ctx context.Context, container *Container) *RunnerContainer { - id := isContainerInitialized(ctx, container) - if id == "" { - return nil - } - - return &RunnerContainer{RunnerID: id, Ctr: container} -} - // Container initializes a new runner container with the given options. func (r *Runner) Container( // context to use for the operation ctx context.Context, - // Container to use for the runner. - container Optional[*Container], // repository information to use for the runner repo *RepoInfo, + // Container to use for the runner. + container Optional[*Container], ) (*RunnerContainer, error) { // check if container is already initialized if ctr, ok := container.Get(); ok { - id := isContainerInitialized(ctx, ctr) - if id != "" { - println(fmt.Sprintf("skip container initialization, container already initialized with id: %s", id)) - println(fmt.Sprintf("WARNING: given source and repo options are ignored, using the initialized container")) + if isContainerInitialized(ctx, ctr) { + fmt.Println("skipping container initialization, container already initialized") + fmt.Println("WARNING: given source and repo options are ignored, using the initialized container") - return &RunnerContainer{RunnerID: id, Ctr: ctr}, nil + return &RunnerContainer{Ctr: ctr}, nil } } - var ( - id = uuid.New().String() - ctr = container.GetOr(dag.Container().From("ghcr.io/catthehacker/ubuntu:act-latest")) - path = getRunnerCacheVolumeMountPath(id) - cache = getRunnerCacheVolume(id) - ) + ctr := container.GetOr(dag.Container().From("ghcr.io/catthehacker/ubuntu:act-latest")) // configure internal components ctr = ctr.With(dag.Ghx().Source().Binary) @@ -77,10 +56,6 @@ func (r *Runner) Container( ctr = ctr.WithEnvVariable("GITHUB_REF_TYPE", repo.RefType) ctr = ctr.WithEnvVariable("GITHUB_SHA", repo.SHA) - // configure runner cache - ctr = ctr.WithEnvVariable("GALE_RUNNER_CACHE", path) - ctr = ctr.WithMountedCache(path, cache, ContainerWithMountedCacheOpts{Sharing: Shared}) - // ghx specific directory configuration -- TODO: refactor this later to be more generic for runners var ( metadata = "/home/runner/_temp/gale/metadata" @@ -95,45 +70,17 @@ func (r *Runner) Container( ctr = ctr.WithMountedCache(actions, dag.CacheVolume("gale-actions"), cacheOpts) // add env variable to the container to indicate container is configured - ctr = ctr.WithEnvVariable("GALE_RUNNER_ID", id) ctr = ctr.WithEnvVariable("GALE_CONFIGURED", "true") - return &RunnerContainer{RunnerID: id, Ctr: ctr}, nil -} - -func (rc *RunnerContainer) Container() *Container { - return rc.Ctr -} - -func (rc *RunnerContainer) getRunnerCachePath() string { - return getRunnerCacheVolumeMountPath(rc.RunnerID) + return &RunnerContainer{Ctr: ctr}, nil } // isContainerInitialized checks if the given container is initialized and returns the runner id if it is. -func isContainerInitialized(ctx context.Context, container *Container) string { +func isContainerInitialized(ctx context.Context, container *Container) bool { val, err := container.EnvVariable(ctx, "GALE_CONFIGURED") if err != nil { - return "" + return false } - if val != "true" { - return "" - } - - runnerID, err := container.EnvVariable(ctx, "GALE_RUNNER_ID") - if err != nil { - return "" - } - - return runnerID -} - -// getRunnerCacheVolumeMountPath returns the mount path for the cache volume with the given id. -func getRunnerCacheVolumeMountPath(id string) string { - return fmt.Sprintf("/home/runner/_temp/gale/%s", id) -} - -// getRunnerCacheVolume returns a cache volume for the given id. -func getRunnerCacheVolume(id string) *CacheVolume { - return dag.CacheVolume(fmt.Sprintf("gale-runner-%s", id)) + return val == "true" }