[FORGEJO] implement lxc separately from self-hosted

This commit is contained in:
Earl Warren 2023-11-09 03:36:09 +01:00
parent 8c59ad2ab3
commit 77ceaf4e5d
No known key found for this signature in database
GPG key ID: 0579CB2928A78A00
4 changed files with 89 additions and 62 deletions

View file

@ -7,6 +7,7 @@ type ExecutionsEnvironment interface {
ToContainerPath(string) string
GetName() string
GetRoot() string
GetLXC() bool
GetActPath() string
GetPathVariableName() string
DefaultPathVariable() string

View file

@ -35,6 +35,7 @@ type HostEnvironment struct {
Root string
CleanUp func()
StdOut io.Writer
LXC bool
}
func (e *HostEnvironment) Create(_, _ []string) common.Executor {
@ -302,18 +303,22 @@ func (e *HostEnvironment) exec(ctx context.Context, commandparam []string, cmdli
} 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 e.GetLXC() {
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 {
@ -414,6 +419,10 @@ func (e *HostEnvironment) ToContainerPath(path string) string {
return path
}
func (e *HostEnvironment) GetLXC() bool {
return e.LXC
}
func (e *HostEnvironment) GetName() string {
return e.Name
}

View file

@ -50,6 +50,10 @@ func (*LinuxContainerEnvironmentExtensions) GetName() string {
return "NAME"
}
func (*LinuxContainerEnvironmentExtensions) GetLXC() bool {
return false
}
func (*LinuxContainerEnvironmentExtensions) GetRoot() string {
return "/var/run"
}

View file

@ -247,8 +247,7 @@ source $(dirname $0)/lxc-helpers-lib.sh
lxc_container_destroy "{{.Name}}"
`))
func (rc *RunContext) stopHostEnvironment() common.Executor {
return func(ctx context.Context) error {
func (rc *RunContext) stopHostEnvironment(ctx context.Context) error {
logger := common.Logger(ctx)
logger.Debugf("stopHostEnvironment")
@ -272,7 +271,6 @@ func (rc *RunContext) stopHostEnvironment() common.Executor {
rc.JobContainer.Exec([]string{rc.JobContainer.GetActPath() + "/workflow/stop-lxc.sh"}, map[string]string{}, "root", "/tmp"),
)(ctx)
}
}
func (rc *RunContext) startHostEnvironment() common.Executor {
return func(ctx context.Context) error {
@ -316,6 +314,7 @@ func (rc *RunContext) startHostEnvironment() common.Executor {
os.RemoveAll(miscpath)
},
StdOut: logWriter,
LXC: rc.IsLXCHostEnv(ctx),
}
rc.cleanUpJobContainer = rc.JobContainer.Remove()
for k, v := range rc.JobContainer.GetRunnerContext(ctx) {
@ -353,7 +352,10 @@ func (rc *RunContext) startHostEnvironment() common.Executor {
return err
}
return common.NewPipelineExecutor(
executors := make([]common.Executor, 0, 10)
if rc.IsLXCHostEnv(ctx) {
executors = append(executors,
rc.JobContainer.Copy(rc.JobContainer.GetActPath()+"/", &container.FileEntry{
Name: "workflow/lxc-helpers-lib.sh",
Mode: 0755,
@ -370,7 +372,10 @@ func (rc *RunContext) startHostEnvironment() common.Executor {
Body: startScript.String(),
}),
rc.JobContainer.Exec([]string{rc.JobContainer.GetActPath() + "/workflow/start-lxc.sh"}, map[string]string{}, "root", "/tmp"),
rc.JobContainer.Copy(rc.JobContainer.GetActPath()+"/", &container.FileEntry{
)
}
executors = append(executors, rc.JobContainer.Copy(rc.JobContainer.GetActPath()+"/", &container.FileEntry{
Name: "workflow/event.json",
Mode: 0o644,
Body: rc.EventJSON,
@ -378,8 +383,9 @@ func (rc *RunContext) startHostEnvironment() common.Executor {
Name: "workflow/envs.txt",
Mode: 0o666,
Body: "",
}),
)(ctx)
}))
return common.NewPipelineExecutor(executors...)(ctx)
}
}
@ -725,17 +731,25 @@ func (rc *RunContext) startContainer() common.Executor {
}
}
func (rc *RunContext) IsHostEnv(ctx context.Context) bool {
func (rc *RunContext) IsBareHostEnv(ctx context.Context) bool {
platform := rc.runsOnImage(ctx)
image := rc.containerImage(ctx)
return image == "" && strings.EqualFold(platform, "-self-hosted")
}
func (rc *RunContext) IsLXCHostEnv(ctx context.Context) bool {
platform := rc.runsOnImage(ctx)
return strings.HasPrefix(platform, "lxc:")
}
func (rc *RunContext) IsHostEnv(ctx context.Context) bool {
return rc.IsBareHostEnv(ctx) || rc.IsLXCHostEnv(ctx)
}
func (rc *RunContext) stopContainer() common.Executor {
return func(ctx context.Context) error {
image := rc.platformImage(ctx)
if strings.EqualFold(image, "-self-hosted") {
return rc.stopHostEnvironment()(ctx)
if rc.IsLXCHostEnv(ctx) {
return rc.stopHostEnvironment(ctx)
}
return rc.stopJobContainer()(ctx)
}
@ -744,9 +758,8 @@ func (rc *RunContext) stopContainer() common.Executor {
func (rc *RunContext) closeContainer() common.Executor {
return func(ctx context.Context) error {
if rc.JobContainer != nil {
image := rc.platformImage(ctx)
if strings.EqualFold(image, "-self-hosted") {
return rc.stopHostEnvironment()(ctx)
if rc.IsLXCHostEnv(ctx) {
return rc.stopHostEnvironment(ctx)
}
return rc.JobContainer.Close()(ctx)
}