This repository has been archived by the owner on Oct 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathruncreate.go
77 lines (70 loc) · 1.68 KB
/
runcreate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"os"
"path/filepath"
"strings"
"github.com/rootless-containers/runrootless/bundle"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
var runCommand = cli.Command{
Name: "run",
Usage: "create and run a container",
Flags: runcRunFlags(),
Action: runCreate,
}
var createCommand = cli.Command{
Name: "create",
Usage: "create a container",
Flags: runcCreateFlags(),
Action: runCreate,
}
func runCreate(context *cli.Context) error {
var err error
bundleDir := context.String("bundle")
if bundleDir == "" {
bundleDir, err = os.Getwd()
if err != nil {
return err
}
}
newBundleDir := filepath.Join(bundleDir, "runrootless")
logrus.Debugf("bundle: %s -> %s", bundleDir, newBundleDir)
if err := bundle.Transform(newBundleDir, bundleDir); err != nil {
return err
}
redirectToRunc(transformRunCreate(newBundleDir)...)
return nil
}
// transformRunCreate transforms os.Args for the new bundle.
// e.g. "runc --root /foo run --bundle /bar baz" -> {"--root", "/foo", "run", "baz", "--bundle", newBundle}
func transformRunCreate(newBundle string) []string {
return _transformRunCreate(os.Args, newBundle)
}
func _transformRunCreate(osArgs []string, newBundle string) []string {
var ss []string
runCreate := false
skipNext := false
for _, s := range osArgs[1:] {
if skipNext {
skipNext = false
continue
}
if s == "run" || s == "create" {
runCreate = true
}
if runCreate {
if s == "-b" || s == "--bundle" {
skipNext = true
continue
} else if strings.HasPrefix(s, "-b=") || strings.HasPrefix(s, "--bundle=") {
continue
}
}
ss = append(ss, s)
}
if runCreate {
ss = append(ss, "--bundle", newBundle)
}
return ss
}