From 9486bfb40cabf2557470bf233e940f24ef8eda69 Mon Sep 17 00:00:00 2001 From: mirkobrombin Date: Sat, 2 Sep 2023 15:28:08 +0200 Subject: [PATCH] feat Allow creating rootfull containers in Apx core --- cmd/runtime.go | 2 +- cmd/subsyStems.go | 10 ++++----- core/dbox.go | 56 ++++++++++++++++++++++++++--------------------- core/subSystem.go | 29 +++++++++++++----------- 4 files changed, 53 insertions(+), 44 deletions(-) diff --git a/cmd/runtime.go b/cmd/runtime.go index edb8d009..6f85a992 100644 --- a/cmd/runtime.go +++ b/cmd/runtime.go @@ -19,7 +19,7 @@ import ( func NewRuntimeCommands() []*cmdr.Command { var commands []*cmdr.Command - subSystems, err := core.ListSubSystems(false) + subSystems, err := core.ListSubSystems(false, false) if err != nil { return []*cmdr.Command{} } diff --git a/cmd/subsyStems.go b/cmd/subsyStems.go index 6d772793..a5949b21 100644 --- a/cmd/subsyStems.go +++ b/cmd/subsyStems.go @@ -133,7 +133,7 @@ func NewSubSystemsCommand() *cmdr.Command { func listSubSystems(cmd *cobra.Command, args []string) error { jsonFlag, _ := cmd.Flags().GetBool("json") - subSystems, err := core.ListSubSystems(false) + subSystems, err := core.ListSubSystems(false, false) if err != nil { return err } @@ -213,7 +213,7 @@ func newSubSystem(cmd *cobra.Command, args []string) error { stackName = stacks[stackIndex-1].Name } - checkSubSystem, err := core.LoadSubSystem(subSystemName) + checkSubSystem, err := core.LoadSubSystem(subSystemName, false) if err == nil { cmdr.Error.Printf(apx.Trans("subsystems.new.error.alreadyExists"), checkSubSystem.Name) return nil @@ -224,7 +224,7 @@ func newSubSystem(cmd *cobra.Command, args []string) error { return err } - subSystem, err := core.NewSubSystem(subSystemName, stack, false, false) + subSystem, err := core.NewSubSystem(subSystemName, stack, false, false, false) if err != nil { return err } @@ -260,7 +260,7 @@ func rmSubSystem(cmd *cobra.Command, args []string) error { } } - subSystem, err := core.LoadSubSystem(subSystemName) + subSystem, err := core.LoadSubSystem(subSystemName, false) if err != nil { return err } @@ -294,7 +294,7 @@ func resetSubSystem(cmd *cobra.Command, args []string) error { } } - subSystem, err := core.LoadSubSystem(subSystemName) + subSystem, err := core.LoadSubSystem(subSystemName, false) if err != nil { return err } diff --git a/core/dbox.go b/core/dbox.go index d03a8e54..5bbb9584 100644 --- a/core/dbox.go +++ b/core/dbox.go @@ -76,7 +76,7 @@ func dboxGetVersion() (version string, err error) { return splitted[1], nil } -func (d *dbox) RunCommand(command string, args []string, engineFlags []string, useEngine bool, captureOutput bool, muteOutput bool) ([]byte, error) { +func (d *dbox) RunCommand(command string, args []string, engineFlags []string, useEngine bool, captureOutput bool, muteOutput bool, rootFull bool) ([]byte, error) { entrypoint := apx.Cnf.DistroboxPath if useEngine { entrypoint = d.EngineBinary @@ -113,6 +113,12 @@ func (d *dbox) RunCommand(command string, args []string, engineFlags []string, u cmd.Args = append(cmd.Args, strings.Join(engineFlags, " ")) } + // NOTE: the root flag is not being used by the Apx CLI, but it's useful + // for those using Apx as a library, e.g. VSO. + if rootFull { + cmd.Args = append(cmd.Args, "--root") + } + cmd.Args = append(cmd.Args, args...) if os.Getenv("APX_VERBOSE") == "1" { @@ -129,11 +135,11 @@ func (d *dbox) RunCommand(command string, args []string, engineFlags []string, u return nil, err } -func (d *dbox) ListContainers() ([]dboxContainer, error) { +func (d *dbox) ListContainers(rootFull bool) ([]dboxContainer, error) { output, err := d.RunCommand("ps", []string{ "-a", "--format", "{{.ID}}|{{.CreatedAt}}|{{.Status}}|{{.Labels}}|{{.Names}}", - }, []string{}, true, true, false) + }, []string{}, true, true, false, rootFull) if err != nil { return nil, err } @@ -178,8 +184,8 @@ func (d *dbox) ListContainers() ([]dboxContainer, error) { return containers, nil } -func (d *dbox) GetContainer(name string) (*dboxContainer, error) { - containers, err := d.ListContainers() +func (d *dbox) GetContainer(name string, rootFull bool) (*dboxContainer, error) { + containers, err := d.ListContainers(rootFull) if err != nil { return nil, err } @@ -194,15 +200,15 @@ func (d *dbox) GetContainer(name string) (*dboxContainer, error) { return nil, errors.New("container not found") } -func (d *dbox) ContainerDelete(name string) error { +func (d *dbox) ContainerDelete(name string, rootFull bool) error { _, err := d.RunCommand("rm", []string{ "--force", name, - }, []string{}, false, false, true) + }, []string{}, false, false, true, rootFull) return err } -func (d *dbox) CreateContainer(name string, image string, additionalPackages []string, labels map[string]string, withInit bool) error { +func (d *dbox) CreateContainer(name string, image string, additionalPackages []string, labels map[string]string, withInit bool, rootFull bool) error { args := []string{ "--image", image, "--name", name, @@ -229,12 +235,12 @@ func (d *dbox) CreateContainer(name string, image string, additionalPackages []s } engineFlags = append(engineFlags, "--label=manager=apx") - _, err := d.RunCommand("create", args, engineFlags, false, true, true) + _, err := d.RunCommand("create", args, engineFlags, false, true, true, rootFull) // fmt.Println(string(out)) return err } -func (d *dbox) RunContainerCommand(name string, command []string) error { +func (d *dbox) RunContainerCommand(name string, command []string, rootFull bool) error { args := []string{ "--name", name, "--", @@ -242,11 +248,11 @@ func (d *dbox) RunContainerCommand(name string, command []string) error { args = append(args, command...) - _, err := d.RunCommand("run", args, []string{}, false, false, false) + _, err := d.RunCommand("run", args, []string{}, false, false, false, rootFull) return err } -func (d *dbox) ContainerExec(name string, captureOutput bool, muteOutput bool, args ...string) (string, error) { +func (d *dbox) ContainerExec(name string, captureOutput bool, muteOutput bool, rootFull bool, args ...string) (string, error) { finalArgs := []string{ // "--verbose", name, @@ -256,22 +262,22 @@ func (d *dbox) ContainerExec(name string, captureOutput bool, muteOutput bool, a finalArgs = append(finalArgs, args...) engineFlags := []string{} - out, err := d.RunCommand("enter", finalArgs, engineFlags, false, captureOutput, muteOutput) + out, err := d.RunCommand("enter", finalArgs, engineFlags, false, captureOutput, muteOutput, rootFull) return string(out), err } -func (d *dbox) ContainerEnter(name string) error { +func (d *dbox) ContainerEnter(name string, rootFull bool) error { finalArgs := []string{ name, } engineFlags := []string{} - _, err := d.RunCommand("enter", finalArgs, engineFlags, false, false, false) + _, err := d.RunCommand("enter", finalArgs, engineFlags, false, false, false, rootFull) return err } -func (d *dbox) ContainerExport(name string, delete bool, args ...string) error { +func (d *dbox) ContainerExport(name string, delete bool, rootFull bool, args ...string) error { finalArgs := []string{"distrobox-export"} if delete { @@ -280,26 +286,26 @@ func (d *dbox) ContainerExport(name string, delete bool, args ...string) error { finalArgs = append(finalArgs, args...) - _, err := d.ContainerExec(name, false, true, finalArgs...) + _, err := d.ContainerExec(name, false, true, rootFull, finalArgs...) return err } -func (d *dbox) ContainerExportDesktopEntry(containerName string, appName string, label string) error { +func (d *dbox) ContainerExportDesktopEntry(containerName string, appName string, label string, rootFull bool) error { args := []string{"--app", appName, "--export-label", label} - return d.ContainerExport(containerName, false, args...) + return d.ContainerExport(containerName, false, rootFull, args...) } -func (d *dbox) ContainerUnexportDesktopEntry(containerName string, appName string) error { +func (d *dbox) ContainerUnexportDesktopEntry(containerName string, appName string, rootFull bool) error { args := []string{"--app", appName} - return d.ContainerExport(containerName, true, args...) + return d.ContainerExport(containerName, true, rootFull, args...) } -func (d *dbox) ContainerExportBin(containerName string, binary string, exportPath string) error { +func (d *dbox) ContainerExportBin(containerName string, binary string, exportPath string, rootFull bool) error { args := []string{"--bin", binary, "--export-path", exportPath} - return d.ContainerExport(containerName, false, args...) + return d.ContainerExport(containerName, false, rootFull, args...) } -func (d *dbox) ContainerUnexportBin(containerName string, binary string, exportPath string) error { +func (d *dbox) ContainerUnexportBin(containerName string, binary string, exportPath string, rootFull bool) error { args := []string{"--bin", binary, "--export-path", exportPath} - return d.ContainerExport(containerName, true, args...) + return d.ContainerExport(containerName, true, rootFull, args...) } diff --git a/core/subSystem.go b/core/subSystem.go index 042e143c..277f59c9 100644 --- a/core/subSystem.go +++ b/core/subSystem.go @@ -28,9 +28,10 @@ type SubSystem struct { ExportedPrograms map[string]map[string]string HasInit bool IsManaged bool + IsRootfull bool } -func NewSubSystem(name string, stack *Stack, hasInit bool, isManaged bool) (*SubSystem, error) { +func NewSubSystem(name string, stack *Stack, hasInit bool, isManaged bool, IsRootfull bool) (*SubSystem, error) { internalName := genInternalName(name) return &SubSystem{ InternalName: internalName, @@ -38,6 +39,7 @@ func NewSubSystem(name string, stack *Stack, hasInit bool, isManaged bool) (*Sub Stack: stack, HasInit: hasInit, IsManaged: isManaged, + IsRootfull: IsRootfull, }, nil } @@ -130,6 +132,7 @@ func (s *SubSystem) Create() error { s.Stack.Packages, labels, s.HasInit, + s.IsRootfull, ) if err != nil { return err @@ -138,14 +141,14 @@ func (s *SubSystem) Create() error { return nil } -func LoadSubSystem(name string) (*SubSystem, error) { +func LoadSubSystem(name string, isRootFull bool) (*SubSystem, error) { dbox, err := NewDbox() if err != nil { return nil, err } internalName := genInternalName(name) - container, err := dbox.GetContainer(internalName) + container, err := dbox.GetContainer(internalName, isRootFull) if err != nil { return nil, err } @@ -164,13 +167,13 @@ func LoadSubSystem(name string) (*SubSystem, error) { }, nil } -func ListSubSystems(includeManaged bool) ([]*SubSystem, error) { +func ListSubSystems(includeManaged bool, includeRootFull bool) ([]*SubSystem, error) { dbox, err := NewDbox() if err != nil { return nil, err } - containers, err := dbox.ListContainers() + containers, err := dbox.ListContainers(includeRootFull) if err != nil { return nil, err } @@ -215,7 +218,7 @@ func (s *SubSystem) Exec(captureOutput bool, args ...string) (string, error) { return "", err } - out, err := dbox.ContainerExec(s.InternalName, captureOutput, false, args...) + out, err := dbox.ContainerExec(s.InternalName, captureOutput, false, s.IsRootfull, args...) if err != nil { return "", err } @@ -232,7 +235,7 @@ func (s *SubSystem) Enter() error { if err != nil { return err } - return dbox.ContainerEnter(s.InternalName) + return dbox.ContainerEnter(s.InternalName, s.IsRootfull) } func (s *SubSystem) Remove() error { @@ -241,7 +244,7 @@ func (s *SubSystem) Remove() error { return err } - return dbox.ContainerDelete(s.InternalName) + return dbox.ContainerDelete(s.InternalName, s.IsRootfull) } func (s *SubSystem) Reset() error { @@ -259,7 +262,7 @@ func (s *SubSystem) ExportDesktopEntry(appName string) error { return err } - return dbox.ContainerExportDesktopEntry(s.InternalName, appName, fmt.Sprintf("on %s", s.Name)) + return dbox.ContainerExportDesktopEntry(s.InternalName, appName, fmt.Sprintf("on %s", s.Name), s.IsRootfull) } func (s *SubSystem) ExportDesktopEntries(args ...string) (int, error) { @@ -327,7 +330,7 @@ func (s *SubSystem) ExportBin(binary string, exportPath string) error { return err } - err = dbox.ContainerExportBin(s.InternalName, binary, tmpExportPath) + err = dbox.ContainerExportBin(s.InternalName, binary, tmpExportPath, s.IsRootfull) if err != nil { return err } @@ -355,7 +358,7 @@ func (s *SubSystem) ExportBin(binary string, exportPath string) error { return err } - err = dbox.ContainerExportBin(s.InternalName, binary, exportPath) + err = dbox.ContainerExportBin(s.InternalName, binary, exportPath, s.IsRootfull) if err != nil { return err } @@ -369,7 +372,7 @@ func (s *SubSystem) UnexportDesktopEntry(appName string) error { return err } - return dbox.ContainerUnexportDesktopEntry(s.InternalName, appName) + return dbox.ContainerUnexportDesktopEntry(s.InternalName, appName, s.IsRootfull) } func (s *SubSystem) UnexportBin(binary string, exportPath string) error { @@ -378,5 +381,5 @@ func (s *SubSystem) UnexportBin(binary string, exportPath string) error { return err } - return dbox.ContainerUnexportBin(s.InternalName, binary, exportPath) + return dbox.ContainerUnexportBin(s.InternalName, binary, exportPath, s.IsRootfull) }