Skip to content

Commit

Permalink
refactor: remove unused runner cache and id
Browse files Browse the repository at this point in the history
  • Loading branch information
aweris committed Nov 23, 2023
1 parent 5d36aee commit 6096592
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 65 deletions.
2 changes: 1 addition & 1 deletion daggerverse/gale/gale.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
75 changes: 11 additions & 64 deletions daggerverse/gale/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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"
Expand All @@ -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"
}

0 comments on commit 6096592

Please sign in to comment.