forked from docker-archive/communitytools-image2docker-linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
284 lines (256 loc) · 5.84 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package main
import (
"context"
"errors"
"fmt"
"github.com/docker/v2c/api"
"github.com/docker/v2c/system"
"github.com/docker/v2c/workflow"
"github.com/urfave/cli"
"os"
"os/signal"
"path/filepath"
)
const name string = `v2c`
const description string = `Lift and shift the contents of a virtual machine image
into build materials for a Docker image.`
const version string = `v0.1.0`
var (
errAtLeastOne = errors.New(`expected at least one argument`)
errExactlyOne = errors.New(`expected exactly one argument`)
errExactlyNone = errors.New(`no arguments are expected`)
)
func main() {
newApp().Run(os.Args)
}
func newApp() *cli.App {
app := cli.NewApp()
app.Name = name
app.Usage = description
app.Version = version
app.Commands = []cli.Command{
{
Name: `build`,
Usage: `transform a virtual disk into a container`,
Category: `Transform`,
Flags: []cli.Flag{
cli.StringFlag{
Name: `tag, t`,
Usage: "Tag the resulting image with `REPOSITORY[:TAG]`",
},
cli.BoolFlag{
Name: `no-cleanup, n`,
Usage: `Do no delete unpacked disk`,
},
},
Action: buildHandler,
},
{
Name: `local-build`,
Usage: `transform the host root partition into a container image`,
Category: `Transform`,
Flags: []cli.Flag{
cli.StringFlag{
Name: `tag, t`,
Usage: "Tag the resulting image with `REPOSITORY[:TAG]`",
},
},
Action: localBuildHandler,
},
{
Name: `image`,
Usage: `options for working with transformed images`,
Category: `Transform`,
Subcommands: []cli.Command{
{
Name: `rm`,
Usage: `remove a transformed image`,
Flags: []cli.Flag{
cli.BoolFlag{
Name: `force, f`,
Usage: `Remove any running containers that may be using IMAGE`,
},
cli.BoolFlag{
Name: `no-prune`,
Usage: `Do no delete untagged parents`,
},
},
Action: removeImageHandler,
},
{
Name: `list`,
Usage: `list the transformed images`,
Action: listImageHandler,
},
{
Name: `export`,
Usage: `export a transformed image`,
Flags: []cli.Flag{
cli.StringFlag{
Name: `output, o`,
Usage: "Write to a `file` instad of STDOUT",
},
},
Action: exportImageHandler,
},
},
},
{
Name: `detective`,
Usage: `options for working with detectives`,
Category: `Component`,
Subcommands: []cli.Command{
{
Name: `install`,
Usage: `install a detective`,
Action: installDetectiveHandler,
},
{
Name: `list`,
Usage: `list the installed detectives`,
Action: listDetectiveHandler,
},
},
},
{
Name: `provisioner`,
Usage: `options for working with provisioners`,
Category: `Component`,
Subcommands: []cli.Command{
{
Name: `install`,
Usage: `install a provisioner`,
Action: installProvisionerHandler,
},
{
Name: `list`,
Usage: `list the installed provisioners`,
Action: listProvisionerHandler,
},
},
},
}
return app
}
func buildHandler(c *cli.Context) error {
if c.NArg() != 1 {
return errExactlyOne
}
fmt.Println("Running image transformation.")
abs, err := filepath.Abs(c.Args().Get(0))
if err != nil {
return err
}
_, err = os.Stat(abs)
if err != nil {
return err
}
var (
ctx context.Context
cancel context.CancelFunc
)
ctx, cancel = context.WithCancel(context.Background())
go func(cancel context.CancelFunc) {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
cancel()
}(cancel)
if c.Bool(`no-cleanup`) {
fmt.Println(`Unpacked input will not be cleaned up upon completion.`)
}
_, err = workflow.Build(ctx, abs, c.Bool(`no-cleanup`))
return err
}
func localBuildHandler(c *cli.Context) error {
if c.NArg() != 1 {
return errExactlyOne
}
fmt.Println("Running local transformation.")
abs, err := filepath.Abs(c.Args().Get(0))
if err != nil {
return err
}
_, err = os.Stat(abs)
if err != nil {
return err
}
var (
ctx context.Context
cancel context.CancelFunc
)
ctx, cancel = context.WithCancel(context.Background())
go func(cancel context.CancelFunc) {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
cancel()
}(cancel)
_, err = workflow.BuildLocal(ctx, abs)
return err
}
// management handlers
func installDetectiveHandler(c *cli.Context) error {
if c.NArg() != 1 {
return errExactlyOne
}
fmt.Println("Installing a detective")
return fmt.Errorf(`Not yet implemented`)
}
func installProvisionerHandler(c *cli.Context) error {
if c.NArg() != 1 {
return errExactlyOne
}
fmt.Println("Installing provisioners")
return fmt.Errorf(`Not yet implemented`)
}
func removeImageHandler(c *cli.Context) error {
if c.NArg() < 1 {
return errAtLeastOne
}
gone, ie := system.RemoveProducts(c.Args(), c.Bool(`force`), !c.Bool(`no-prune`))
return renderRemoved(os.Stdout, gone, ie)
}
func exportImageHandler(c *cli.Context) error {
if c.NArg() != 1 {
return errExactlyOne
}
fmt.Println("Exporting an image")
return fmt.Errorf(`Not yet implemented`)
}
// list handlers
func listImageHandler(c *cli.Context) error {
if c.Args().Present() {
return errExactlyNone
}
products, err := system.ListProducedImages()
if err != nil {
return err
}
return renderTabbed(`imageList`, os.Stdout, struct {
Products []api.Product
}{
Products: products,
},
)
}
func listProvisionerHandler(c *cli.Context) error {
if c.NArg() > 0 {
return errExactlyNone
}
components, err := system.DetectComponents()
if err != nil {
return err
}
return renderTabbed(`provisionerList`, os.Stdout, components)
}
func listDetectiveHandler(c *cli.Context) error {
if c.NArg() > 0 {
return errExactlyNone
}
components, err := system.DetectComponents()
if err != nil {
return err
}
return renderTabbed(`detectiveList`, os.Stdout, components)
}