-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathmain.go
224 lines (192 loc) · 5.24 KB
/
main.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package main
import (
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"os/signal"
"runtime"
"strings"
"syscall"
"github.com/docker/docker/client"
"github.com/genuinetools/pkg/cli"
"github.com/genuinetools/riddler/parse"
"github.com/genuinetools/riddler/version"
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/sirupsen/logrus"
)
//
const (
specConfig = "config.json"
)
var (
bundle string
dockerHost string
force bool
hooks specs.Hooks
hookflags stringSlice
idroot, idlen uint32
idrootVar, idlenVar int
debug bool
)
// stringSlice is a slice of strings
type stringSlice []string
// implement the flag interface for stringSlice
func (s *stringSlice) String() string {
return fmt.Sprintf("%s", *s)
}
func (s *stringSlice) Set(value string) error {
*s = append(*s, value)
return nil
}
func (s stringSlice) ParseHooks() (hooks specs.Hooks, err error) {
for _, v := range s {
parts := strings.SplitN(v, ":", 2)
if len(parts) <= 1 {
return hooks, fmt.Errorf("parsing %s as hook_name:exec failed", v)
}
cmd := strings.Split(parts[1], " ")
exec, err := exec.LookPath(cmd[0])
if err != nil {
return hooks, fmt.Errorf("looking up exec path for %s failed: %v", cmd[0], err)
}
hook := specs.Hook{
Path: exec,
}
if len(cmd) > 1 {
hook.Args = append(hook.Args, cmd...)
}
switch parts[0] {
case "prestart":
hooks.Prestart = append(hooks.Prestart, hook)
case "poststart":
hooks.Poststart = append(hooks.Poststart, hook)
case "poststop":
hooks.Poststop = append(hooks.Poststop, hook)
default:
return hooks, fmt.Errorf("%s is not a valid hook, try 'prestart', 'poststart', or 'poststop'", parts[0])
}
}
return hooks, nil
}
func main() {
// Create a new cli program.
p := cli.NewProgram()
p.Name = "riddler"
p.Description = "A tool to convert docker inspect to the opencontainers runc spec"
// Set the GitCommit and Version.
p.GitCommit = version.GITCOMMIT
p.Version = version.VERSION
// Setup the global flags.
p.FlagSet = flag.NewFlagSet("global", flag.ExitOnError)
p.FlagSet.StringVar(&dockerHost, "host", "unix:///var/run/docker.sock", "Docker Daemon socket(s) to connect to")
p.FlagSet.StringVar(&bundle, "bundle", "", "Path to the root of the bundle directory")
p.FlagSet.Var(&hookflags, "hook", "Hooks to prefill into spec file. (ex. --hook prestart:netns)")
p.FlagSet.IntVar(&idrootVar, "idroot", 0, "Root UID/GID for user namespaces")
p.FlagSet.IntVar(&idlenVar, "idlen", 0, "Length of UID/GID ID space ranges for user namespaces")
p.FlagSet.BoolVar(&force, "force", false, "force overwrite existing files")
p.FlagSet.BoolVar(&force, "f", false, "force overwrite existing files")
p.FlagSet.BoolVar(&debug, "d", false, "enable debug logging")
// Set the before function.
p.Before = func(ctx context.Context) error {
// Set the log level.
if debug {
logrus.SetLevel(logrus.DebugLevel)
}
idroot = uint32(idrootVar)
idlen = uint32(idlenVar)
if p.FlagSet.NArg() < 1 {
return errors.New("pass the container name or ID")
}
var err error
hooks, err = hookflags.ParseHooks()
return err
}
// Set the main program action.
p.Action = func(ctx context.Context, args []string) error {
// On ^C, or SIGTERM handle exit.
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
signal.Notify(c, syscall.SIGTERM)
go func() {
for sig := range c {
logrus.Infof("Received %s, exiting.", sig.String())
os.Exit(0)
}
}()
defaultHeaders := map[string]string{"User-Agent": "engine-api-cli-1.0"}
cli, err := client.NewClient(dockerHost, "", nil, defaultHeaders)
if err != nil {
panic(err)
}
// get container info
ctr, err := cli.ContainerInspect(ctx, args[0])
if err != nil {
logrus.Fatalf("inspecting container (%s) failed: %v", args[0], err)
}
spec, err := parse.Config(ctr, runtime.GOOS, runtime.GOARCH, defaultCapabilities(), idroot, idlen)
if err != nil {
logrus.Fatalf("Spec config conversion for %s failed: %v", args[0], err)
}
// fill in hooks, if passed through command line
spec.Hooks = &hooks
if err := writeConfig(spec); err != nil {
logrus.Fatal(err)
}
fmt.Printf("%s has been saved.\n", specConfig)
return nil
}
// Run our program.
p.Run()
}
func checkNoFile(name string) error {
_, err := os.Stat(name)
if err == nil {
return fmt.Errorf("file %s exists, remove it", name)
}
if !os.IsNotExist(err) {
return err
}
return nil
}
func writeConfig(spec *specs.Spec) error {
if bundle != "" {
// change current working directory
if err := os.Chdir(bundle); err != nil {
return fmt.Errorf("change working directory to %s failed: %v", bundle, err)
}
}
// make sure we don't already have files, we would not want to overwrite them
if !force {
if err := checkNoFile(specConfig); err != nil {
return err
}
}
data, err := json.MarshalIndent(&spec, "", " ")
if err != nil {
return err
}
return ioutil.WriteFile(specConfig, data, 0666)
}
func defaultCapabilities() []string {
return []string{
"CAP_CHOWN",
"CAP_DAC_OVERRIDE",
"CAP_FSETID",
"CAP_FOWNER",
"CAP_MKNOD",
"CAP_NET_RAW",
"CAP_SETGID",
"CAP_SETUID",
"CAP_SETFCAP",
"CAP_SETPCAP",
"CAP_NET_BIND_SERVICE",
"CAP_SYS_CHROOT",
"CAP_KILL",
"CAP_AUDIT_WRITE",
}
}