diff --git a/pkg/container/executions_environment.go b/pkg/container/executions_environment.go index 41e3b57..87e652b 100644 --- a/pkg/container/executions_environment.go +++ b/pkg/container/executions_environment.go @@ -5,6 +5,8 @@ import "context" type ExecutionsEnvironment interface { Container ToContainerPath(string) string + GetName() string + GetRoot() string GetActPath() string GetPathVariableName() string DefaultPathVariable() string diff --git a/pkg/container/host_environment.go b/pkg/container/host_environment.go index 4801b27..42dbaa9 100644 --- a/pkg/container/host_environment.go +++ b/pkg/container/host_environment.go @@ -25,16 +25,18 @@ import ( ) type HostEnvironment struct { + Name string Path string TmpDir string ToolCache string Workdir string ActPath string + Root string CleanUp func() StdOut io.Writer } -func (e *HostEnvironment) Create(_ []string, _ []string) common.Executor { +func (e *HostEnvironment) Create(_, _ []string) common.Executor { return func(ctx context.Context) error { return nil } @@ -66,7 +68,7 @@ func (e *HostEnvironment) Copy(destPath string, files ...*FileEntry) common.Exec } } -func (e *HostEnvironment) CopyDir(destPath string, srcPath string, useGitIgnore bool) common.Executor { +func (e *HostEnvironment) CopyDir(destPath, srcPath string, useGitIgnore bool) common.Executor { return func(ctx context.Context) error { logger := common.Logger(ctx) srcPrefix := filepath.Dir(srcPath) @@ -260,7 +262,7 @@ func getEnvListFromMap(env map[string]string) []string { return envList } -func (e *HostEnvironment) exec(ctx context.Context, command []string, cmdline string, env map[string]string, _, workdir string) error { +func (e *HostEnvironment) exec(ctx context.Context, commandparam []string, cmdline string, env map[string]string, user, workdir string) error { envList := getEnvListFromMap(env) var wd string if workdir != "" { @@ -272,6 +274,19 @@ func (e *HostEnvironment) exec(ctx context.Context, command []string, cmdline st } else { wd = e.Path } + if _, err := os.Stat(wd); err != nil { + common.Logger(ctx).Debugf("Failed to stat working directory %s %v\n", wd, err.Error()) + } + + command := make([]string, len(commandparam)) + copy(command, commandparam) + if user == "root" { + command = append([]string{"/usr/bin/sudo"}, command...) + } else { + common.Logger(ctx).Debugf("lxc-attach --name %v %v", e.Name, command) + command = append([]string{"/usr/bin/sudo", "--preserve-env", "--preserve-env=PATH", "/usr/bin/lxc-attach", "--keep-env", "--name", e.Name, "--"}, command...) + } + f, err := lookupPathHost(command[0], env, e.StdOut) if err != nil { return err @@ -314,7 +329,7 @@ func (e *HostEnvironment) exec(ctx context.Context, command []string, cmdline st } err = cmd.Run() if err != nil { - return err + return fmt.Errorf("RUN %w", err) } if tty != nil { writer.AutoStop = true @@ -367,6 +382,14 @@ func (e *HostEnvironment) ToContainerPath(path string) string { return path } +func (e *HostEnvironment) GetName() string { + return e.Name +} + +func (e *HostEnvironment) GetRoot() string { + return e.Root +} + func (e *HostEnvironment) GetActPath() string { actPath := e.ActPath if runtime.GOOS == "windows" { @@ -426,7 +449,7 @@ func (e *HostEnvironment) GetRunnerContext(_ context.Context) map[string]interfa } } -func (e *HostEnvironment) ReplaceLogWriter(stdout io.Writer, _ io.Writer) (io.Writer, io.Writer) { +func (e *HostEnvironment) ReplaceLogWriter(stdout, _ io.Writer) (io.Writer, io.Writer) { org := e.StdOut e.StdOut = stdout return org, org diff --git a/pkg/container/linux_container_environment_extensions.go b/pkg/container/linux_container_environment_extensions.go index d673451..3518554 100644 --- a/pkg/container/linux_container_environment_extensions.go +++ b/pkg/container/linux_container_environment_extensions.go @@ -10,8 +10,7 @@ import ( log "github.com/sirupsen/logrus" ) -type LinuxContainerEnvironmentExtensions struct { -} +type LinuxContainerEnvironmentExtensions struct{} // Resolves the equivalent host path inside the container // This is required for windows and WSL 2 to translate things like C:\Users\Myproject to /mnt/users/Myproject @@ -47,6 +46,14 @@ func (*LinuxContainerEnvironmentExtensions) ToContainerPath(path string) string return result } +func (*LinuxContainerEnvironmentExtensions) GetName() string { + return "NAME" +} + +func (*LinuxContainerEnvironmentExtensions) GetRoot() string { + return "/var/run" +} + func (*LinuxContainerEnvironmentExtensions) GetActPath() string { return "/var/run/act" } diff --git a/pkg/runner/run_context.go b/pkg/runner/run_context.go index d34cc13..48753a0 100644 --- a/pkg/runner/run_context.go +++ b/pkg/runner/run_context.go @@ -3,9 +3,11 @@ package runner import ( "archive/tar" "bufio" + "bytes" "context" "crypto/rand" "crypto/sha256" + _ "embed" "encoding/hex" "encoding/json" "errors" @@ -16,6 +18,7 @@ import ( "regexp" "runtime" "strings" + "text/template" "time" "github.com/opencontainers/selinux/go-selinux" @@ -178,6 +181,89 @@ func (rc *RunContext) GetBindsAndMounts() ([]string, map[string]string) { return binds, mounts } +//go:embed lxc-helpers-lib.sh +var lxcHelpersLib string + +//go:embed lxc-helpers.sh +var lxcHelpers string + +var startTemplate = template.Must(template.New("start").Parse(`#!/bin/bash -xe +source $(dirname $0)/lxc-helpers-lib.sh + +LXC_CONTAINER_RELEASE="{{.Release}}" + +function template_act() { + echo $(lxc_template_release)-act +} + +function install_nodejs() { + local name="$1" + + local script=/usr/local/bin/lxc-helpers-install-node.sh + + cat > $(lxc_root $name)/$script <