Skip to content

Commit

Permalink
feat:[close #281] Allow creating rootfull containers in Apx core
Browse files Browse the repository at this point in the history
  • Loading branch information
mirkobrombin authored Sep 2, 2023
2 parents 3a7d607 + 9486bfb commit 38939a1
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 44 deletions.
2 changes: 1 addition & 1 deletion cmd/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/subsyStems.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
56 changes: 31 additions & 25 deletions core/dbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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" {
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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,
Expand All @@ -229,24 +235,24 @@ 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,
"--",
}

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,
Expand All @@ -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 {
Expand All @@ -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...)
}
29 changes: 16 additions & 13 deletions core/subSystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,18 @@ 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,
Name: name,
Stack: stack,
HasInit: hasInit,
IsManaged: isManaged,
IsRootfull: IsRootfull,
}, nil
}

Expand Down Expand Up @@ -130,6 +132,7 @@ func (s *SubSystem) Create() error {
s.Stack.Packages,
labels,
s.HasInit,
s.IsRootfull,
)
if err != nil {
return err
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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) {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand All @@ -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)
}

0 comments on commit 38939a1

Please sign in to comment.