Skip to content

Commit 38939a1

Browse files
authored
feat:[close #281] Allow creating rootfull containers in Apx core
2 parents 3a7d607 + 9486bfb commit 38939a1

File tree

4 files changed

+53
-44
lines changed

4 files changed

+53
-44
lines changed

cmd/runtime.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
func NewRuntimeCommands() []*cmdr.Command {
2020
var commands []*cmdr.Command
2121

22-
subSystems, err := core.ListSubSystems(false)
22+
subSystems, err := core.ListSubSystems(false, false)
2323
if err != nil {
2424
return []*cmdr.Command{}
2525
}

cmd/subsyStems.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func NewSubSystemsCommand() *cmdr.Command {
133133
func listSubSystems(cmd *cobra.Command, args []string) error {
134134
jsonFlag, _ := cmd.Flags().GetBool("json")
135135

136-
subSystems, err := core.ListSubSystems(false)
136+
subSystems, err := core.ListSubSystems(false, false)
137137
if err != nil {
138138
return err
139139
}
@@ -213,7 +213,7 @@ func newSubSystem(cmd *cobra.Command, args []string) error {
213213
stackName = stacks[stackIndex-1].Name
214214
}
215215

216-
checkSubSystem, err := core.LoadSubSystem(subSystemName)
216+
checkSubSystem, err := core.LoadSubSystem(subSystemName, false)
217217
if err == nil {
218218
cmdr.Error.Printf(apx.Trans("subsystems.new.error.alreadyExists"), checkSubSystem.Name)
219219
return nil
@@ -224,7 +224,7 @@ func newSubSystem(cmd *cobra.Command, args []string) error {
224224
return err
225225
}
226226

227-
subSystem, err := core.NewSubSystem(subSystemName, stack, false, false)
227+
subSystem, err := core.NewSubSystem(subSystemName, stack, false, false, false)
228228
if err != nil {
229229
return err
230230
}
@@ -260,7 +260,7 @@ func rmSubSystem(cmd *cobra.Command, args []string) error {
260260
}
261261
}
262262

263-
subSystem, err := core.LoadSubSystem(subSystemName)
263+
subSystem, err := core.LoadSubSystem(subSystemName, false)
264264
if err != nil {
265265
return err
266266
}
@@ -294,7 +294,7 @@ func resetSubSystem(cmd *cobra.Command, args []string) error {
294294
}
295295
}
296296

297-
subSystem, err := core.LoadSubSystem(subSystemName)
297+
subSystem, err := core.LoadSubSystem(subSystemName, false)
298298
if err != nil {
299299
return err
300300
}

core/dbox.go

+31-25
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func dboxGetVersion() (version string, err error) {
7676
return splitted[1], nil
7777
}
7878

79-
func (d *dbox) RunCommand(command string, args []string, engineFlags []string, useEngine bool, captureOutput bool, muteOutput bool) ([]byte, error) {
79+
func (d *dbox) RunCommand(command string, args []string, engineFlags []string, useEngine bool, captureOutput bool, muteOutput bool, rootFull bool) ([]byte, error) {
8080
entrypoint := apx.Cnf.DistroboxPath
8181
if useEngine {
8282
entrypoint = d.EngineBinary
@@ -113,6 +113,12 @@ func (d *dbox) RunCommand(command string, args []string, engineFlags []string, u
113113
cmd.Args = append(cmd.Args, strings.Join(engineFlags, " "))
114114
}
115115

116+
// NOTE: the root flag is not being used by the Apx CLI, but it's useful
117+
// for those using Apx as a library, e.g. VSO.
118+
if rootFull {
119+
cmd.Args = append(cmd.Args, "--root")
120+
}
121+
116122
cmd.Args = append(cmd.Args, args...)
117123

118124
if os.Getenv("APX_VERBOSE") == "1" {
@@ -129,11 +135,11 @@ func (d *dbox) RunCommand(command string, args []string, engineFlags []string, u
129135
return nil, err
130136
}
131137

132-
func (d *dbox) ListContainers() ([]dboxContainer, error) {
138+
func (d *dbox) ListContainers(rootFull bool) ([]dboxContainer, error) {
133139
output, err := d.RunCommand("ps", []string{
134140
"-a",
135141
"--format", "{{.ID}}|{{.CreatedAt}}|{{.Status}}|{{.Labels}}|{{.Names}}",
136-
}, []string{}, true, true, false)
142+
}, []string{}, true, true, false, rootFull)
137143
if err != nil {
138144
return nil, err
139145
}
@@ -178,8 +184,8 @@ func (d *dbox) ListContainers() ([]dboxContainer, error) {
178184
return containers, nil
179185
}
180186

181-
func (d *dbox) GetContainer(name string) (*dboxContainer, error) {
182-
containers, err := d.ListContainers()
187+
func (d *dbox) GetContainer(name string, rootFull bool) (*dboxContainer, error) {
188+
containers, err := d.ListContainers(rootFull)
183189
if err != nil {
184190
return nil, err
185191
}
@@ -194,15 +200,15 @@ func (d *dbox) GetContainer(name string) (*dboxContainer, error) {
194200
return nil, errors.New("container not found")
195201
}
196202

197-
func (d *dbox) ContainerDelete(name string) error {
203+
func (d *dbox) ContainerDelete(name string, rootFull bool) error {
198204
_, err := d.RunCommand("rm", []string{
199205
"--force",
200206
name,
201-
}, []string{}, false, false, true)
207+
}, []string{}, false, false, true, rootFull)
202208
return err
203209
}
204210

205-
func (d *dbox) CreateContainer(name string, image string, additionalPackages []string, labels map[string]string, withInit bool) error {
211+
func (d *dbox) CreateContainer(name string, image string, additionalPackages []string, labels map[string]string, withInit bool, rootFull bool) error {
206212
args := []string{
207213
"--image", image,
208214
"--name", name,
@@ -229,24 +235,24 @@ func (d *dbox) CreateContainer(name string, image string, additionalPackages []s
229235
}
230236
engineFlags = append(engineFlags, "--label=manager=apx")
231237

232-
_, err := d.RunCommand("create", args, engineFlags, false, true, true)
238+
_, err := d.RunCommand("create", args, engineFlags, false, true, true, rootFull)
233239
// fmt.Println(string(out))
234240
return err
235241
}
236242

237-
func (d *dbox) RunContainerCommand(name string, command []string) error {
243+
func (d *dbox) RunContainerCommand(name string, command []string, rootFull bool) error {
238244
args := []string{
239245
"--name", name,
240246
"--",
241247
}
242248

243249
args = append(args, command...)
244250

245-
_, err := d.RunCommand("run", args, []string{}, false, false, false)
251+
_, err := d.RunCommand("run", args, []string{}, false, false, false, rootFull)
246252
return err
247253
}
248254

249-
func (d *dbox) ContainerExec(name string, captureOutput bool, muteOutput bool, args ...string) (string, error) {
255+
func (d *dbox) ContainerExec(name string, captureOutput bool, muteOutput bool, rootFull bool, args ...string) (string, error) {
250256
finalArgs := []string{
251257
// "--verbose",
252258
name,
@@ -256,22 +262,22 @@ func (d *dbox) ContainerExec(name string, captureOutput bool, muteOutput bool, a
256262
finalArgs = append(finalArgs, args...)
257263
engineFlags := []string{}
258264

259-
out, err := d.RunCommand("enter", finalArgs, engineFlags, false, captureOutput, muteOutput)
265+
out, err := d.RunCommand("enter", finalArgs, engineFlags, false, captureOutput, muteOutput, rootFull)
260266
return string(out), err
261267
}
262268

263-
func (d *dbox) ContainerEnter(name string) error {
269+
func (d *dbox) ContainerEnter(name string, rootFull bool) error {
264270
finalArgs := []string{
265271
name,
266272
}
267273

268274
engineFlags := []string{}
269275

270-
_, err := d.RunCommand("enter", finalArgs, engineFlags, false, false, false)
276+
_, err := d.RunCommand("enter", finalArgs, engineFlags, false, false, false, rootFull)
271277
return err
272278
}
273279

274-
func (d *dbox) ContainerExport(name string, delete bool, args ...string) error {
280+
func (d *dbox) ContainerExport(name string, delete bool, rootFull bool, args ...string) error {
275281
finalArgs := []string{"distrobox-export"}
276282

277283
if delete {
@@ -280,26 +286,26 @@ func (d *dbox) ContainerExport(name string, delete bool, args ...string) error {
280286

281287
finalArgs = append(finalArgs, args...)
282288

283-
_, err := d.ContainerExec(name, false, true, finalArgs...)
289+
_, err := d.ContainerExec(name, false, true, rootFull, finalArgs...)
284290
return err
285291
}
286292

287-
func (d *dbox) ContainerExportDesktopEntry(containerName string, appName string, label string) error {
293+
func (d *dbox) ContainerExportDesktopEntry(containerName string, appName string, label string, rootFull bool) error {
288294
args := []string{"--app", appName, "--export-label", label}
289-
return d.ContainerExport(containerName, false, args...)
295+
return d.ContainerExport(containerName, false, rootFull, args...)
290296
}
291297

292-
func (d *dbox) ContainerUnexportDesktopEntry(containerName string, appName string) error {
298+
func (d *dbox) ContainerUnexportDesktopEntry(containerName string, appName string, rootFull bool) error {
293299
args := []string{"--app", appName}
294-
return d.ContainerExport(containerName, true, args...)
300+
return d.ContainerExport(containerName, true, rootFull, args...)
295301
}
296302

297-
func (d *dbox) ContainerExportBin(containerName string, binary string, exportPath string) error {
303+
func (d *dbox) ContainerExportBin(containerName string, binary string, exportPath string, rootFull bool) error {
298304
args := []string{"--bin", binary, "--export-path", exportPath}
299-
return d.ContainerExport(containerName, false, args...)
305+
return d.ContainerExport(containerName, false, rootFull, args...)
300306
}
301307

302-
func (d *dbox) ContainerUnexportBin(containerName string, binary string, exportPath string) error {
308+
func (d *dbox) ContainerUnexportBin(containerName string, binary string, exportPath string, rootFull bool) error {
303309
args := []string{"--bin", binary, "--export-path", exportPath}
304-
return d.ContainerExport(containerName, true, args...)
310+
return d.ContainerExport(containerName, true, rootFull, args...)
305311
}

core/subSystem.go

+16-13
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,18 @@ type SubSystem struct {
2828
ExportedPrograms map[string]map[string]string
2929
HasInit bool
3030
IsManaged bool
31+
IsRootfull bool
3132
}
3233

33-
func NewSubSystem(name string, stack *Stack, hasInit bool, isManaged bool) (*SubSystem, error) {
34+
func NewSubSystem(name string, stack *Stack, hasInit bool, isManaged bool, IsRootfull bool) (*SubSystem, error) {
3435
internalName := genInternalName(name)
3536
return &SubSystem{
3637
InternalName: internalName,
3738
Name: name,
3839
Stack: stack,
3940
HasInit: hasInit,
4041
IsManaged: isManaged,
42+
IsRootfull: IsRootfull,
4143
}, nil
4244
}
4345

@@ -130,6 +132,7 @@ func (s *SubSystem) Create() error {
130132
s.Stack.Packages,
131133
labels,
132134
s.HasInit,
135+
s.IsRootfull,
133136
)
134137
if err != nil {
135138
return err
@@ -138,14 +141,14 @@ func (s *SubSystem) Create() error {
138141
return nil
139142
}
140143

141-
func LoadSubSystem(name string) (*SubSystem, error) {
144+
func LoadSubSystem(name string, isRootFull bool) (*SubSystem, error) {
142145
dbox, err := NewDbox()
143146
if err != nil {
144147
return nil, err
145148
}
146149

147150
internalName := genInternalName(name)
148-
container, err := dbox.GetContainer(internalName)
151+
container, err := dbox.GetContainer(internalName, isRootFull)
149152
if err != nil {
150153
return nil, err
151154
}
@@ -164,13 +167,13 @@ func LoadSubSystem(name string) (*SubSystem, error) {
164167
}, nil
165168
}
166169

167-
func ListSubSystems(includeManaged bool) ([]*SubSystem, error) {
170+
func ListSubSystems(includeManaged bool, includeRootFull bool) ([]*SubSystem, error) {
168171
dbox, err := NewDbox()
169172
if err != nil {
170173
return nil, err
171174
}
172175

173-
containers, err := dbox.ListContainers()
176+
containers, err := dbox.ListContainers(includeRootFull)
174177
if err != nil {
175178
return nil, err
176179
}
@@ -215,7 +218,7 @@ func (s *SubSystem) Exec(captureOutput bool, args ...string) (string, error) {
215218
return "", err
216219
}
217220

218-
out, err := dbox.ContainerExec(s.InternalName, captureOutput, false, args...)
221+
out, err := dbox.ContainerExec(s.InternalName, captureOutput, false, s.IsRootfull, args...)
219222
if err != nil {
220223
return "", err
221224
}
@@ -232,7 +235,7 @@ func (s *SubSystem) Enter() error {
232235
if err != nil {
233236
return err
234237
}
235-
return dbox.ContainerEnter(s.InternalName)
238+
return dbox.ContainerEnter(s.InternalName, s.IsRootfull)
236239
}
237240

238241
func (s *SubSystem) Remove() error {
@@ -241,7 +244,7 @@ func (s *SubSystem) Remove() error {
241244
return err
242245
}
243246

244-
return dbox.ContainerDelete(s.InternalName)
247+
return dbox.ContainerDelete(s.InternalName, s.IsRootfull)
245248
}
246249

247250
func (s *SubSystem) Reset() error {
@@ -259,7 +262,7 @@ func (s *SubSystem) ExportDesktopEntry(appName string) error {
259262
return err
260263
}
261264

262-
return dbox.ContainerExportDesktopEntry(s.InternalName, appName, fmt.Sprintf("on %s", s.Name))
265+
return dbox.ContainerExportDesktopEntry(s.InternalName, appName, fmt.Sprintf("on %s", s.Name), s.IsRootfull)
263266
}
264267

265268
func (s *SubSystem) ExportDesktopEntries(args ...string) (int, error) {
@@ -327,7 +330,7 @@ func (s *SubSystem) ExportBin(binary string, exportPath string) error {
327330
return err
328331
}
329332

330-
err = dbox.ContainerExportBin(s.InternalName, binary, tmpExportPath)
333+
err = dbox.ContainerExportBin(s.InternalName, binary, tmpExportPath, s.IsRootfull)
331334
if err != nil {
332335
return err
333336
}
@@ -355,7 +358,7 @@ func (s *SubSystem) ExportBin(binary string, exportPath string) error {
355358
return err
356359
}
357360

358-
err = dbox.ContainerExportBin(s.InternalName, binary, exportPath)
361+
err = dbox.ContainerExportBin(s.InternalName, binary, exportPath, s.IsRootfull)
359362
if err != nil {
360363
return err
361364
}
@@ -369,7 +372,7 @@ func (s *SubSystem) UnexportDesktopEntry(appName string) error {
369372
return err
370373
}
371374

372-
return dbox.ContainerUnexportDesktopEntry(s.InternalName, appName)
375+
return dbox.ContainerUnexportDesktopEntry(s.InternalName, appName, s.IsRootfull)
373376
}
374377

375378
func (s *SubSystem) UnexportBin(binary string, exportPath string) error {
@@ -378,5 +381,5 @@ func (s *SubSystem) UnexportBin(binary string, exportPath string) error {
378381
return err
379382
}
380383

381-
return dbox.ContainerUnexportBin(s.InternalName, binary, exportPath)
384+
return dbox.ContainerUnexportBin(s.InternalName, binary, exportPath, s.IsRootfull)
382385
}

0 commit comments

Comments
 (0)