This repository has been archived by the owner on Jan 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathcommands.go
265 lines (233 loc) · 7 KB
/
commands.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
package main
import (
"bufio"
"encoding/json"
"fmt"
"io"
"os"
"path"
"strings"
"github.com/codegangsta/cli"
"github.com/olekukonko/tablewriter"
)
func volumeList(ctx *cli.Context) {
docker := getDockerClient(ctx)
volumes := setup(docker, ctx.GlobalString("docker-root"))
if ctx.Bool("quiet") {
var out []string
for _, vol := range volumes.s {
id := vol.ID
out = append(out, id)
}
fmt.Fprintln(os.Stdout, strings.Join(out, "\n"))
return
}
var items [][]string
for _, vol := range volumes.s {
id := vol.ID
if len(id) > 12 {
id = id[:12]
}
out := []string{id, strings.Join(vol.Names, ", "), vol.HostPath}
items = append(items, out)
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"ID", "Names", "Path"})
table.SetBorder(false)
table.AppendBulk(items)
table.Render()
}
func volumeInspect(ctx *cli.Context) {
if len(ctx.Args()) != 1 {
fmt.Fprintln(os.Stderr, "Malformed argument. Please supply 1 and only 1 argument")
os.Exit(1)
}
docker := getDockerClient(ctx)
volumes := setup(docker, ctx.GlobalString("docker-root"))
v := volumes.Find(ctx.Args()[0])
vJson, err := json.MarshalIndent(v, "", " ")
if err != nil {
fmt.Fprintln(os.Stderr, "error marshalling volume data: %v", err)
os.Exit(1)
}
fmt.Println(string(vJson))
}
func volumeRm(ctx *cli.Context) {
if len(ctx.Args()) == 0 {
fmt.Fprintln(os.Stderr, "Malformed argument. Must supply at least 1 argument")
os.Exit(1)
}
docker := getDockerClient(ctx)
volumes := setup(docker, ctx.GlobalString("docker-root"))
for _, name := range ctx.Args() {
v := volumes.Find(name)
if v == nil {
fmt.Fprintln(os.Stderr, "Could not find volume: ", name)
continue
}
if !volumes.CanRemove(v) {
fmt.Fprintln(os.Stderr, "Volume is in use, cannot remove: ", name)
continue
}
var containerConfig map[string]interface{}
if dockerApiVersion.LessThan("1.19") {
hostMountPath := strings.TrimSuffix(v.HostPath, path.Base(v.HostPath))
hostConfPath := strings.TrimSuffix(hostMountPath, "/vfs/dir/") + "/volumes"
bindSpec := hostMountPath + ":" + "/.dockervolume"
bindSpec2 := hostConfPath + ":" + "/.dockervolume2"
containerConfig = map[string]interface{}{
"Image": "busybox:latest",
"Entrypoint": []string{"/bin/sh", "-c"},
"Cmd": []string{"rm -rf /.dockervolume/" + path.Base(v.HostPath) + ("&& rm -rf /.dockervolume2/" + path.Base(v.HostPath))},
"HostConfig": map[string]interface{}{
"Binds": []string{bindSpec, bindSpec2},
},
}
} else {
hostMountPath := strings.TrimSuffix(v.HostPath, path.Base(v.HostPath))
hostMountPath = strings.TrimSuffix(v.HostPath, path.Base(v.HostPath))
bindSpec := hostMountPath + ":" + "/.dockervolume"
containerConfig = map[string]interface{}{
"Image": "busybox:latest",
"Entrypoint": []string{"/bin/sh", "-c"},
"Cmd": []string{"rm -rf /.dockervolume/" + path.Base(v.HostPath)},
"HostConfig": map[string]interface{}{
"Binds": []string{bindSpec},
},
}
}
containerId, err := docker.RunContainer(containerConfig)
if err != nil {
docker.RemoveContainer(containerId, true, true)
fmt.Fprintln(os.Stderr, "Could not remove volume: ", v.HostPath)
continue
}
defer docker.RemoveContainer(containerId, true, true)
docker.ContainerWait(containerId)
c, err := docker.FetchContainer(containerId)
if err != nil {
fmt.Fprintln(os.Stderr, "Error getting removeal state")
os.Exit(1)
}
if c.State.ExitCode != 0 {
fmt.Fprintln(os.Stderr, "Could not remove volume ", v.HostPath)
fmt.Println(os.Stderr, c.State.Error)
}
fmt.Println("Successfully removed volume: ", name)
}
}
func volumeExport(ctx *cli.Context) {
if len(ctx.Args()) != 1 {
fmt.Fprintln(os.Stderr, "Malformed argument. Please supply 1 and only 1 argument")
os.Exit(1)
}
docker := getDockerClient(ctx)
volumes := setup(docker, ctx.GlobalString("docker-root"))
name := ctx.Args()[0]
v := volumes.Find(name)
if v == nil {
fmt.Fprintln(os.Stderr, "Could not find volume: ", name)
os.Exit(1)
}
pause := ctx.Bool("pause")
unpause := func() {
if pause {
for _, c := range v.Containers {
docker.ContainerUnpause(c)
}
}
}
if pause {
pauseContainers(docker, v.Containers)
defer unpause()
}
arch, err := copyForExport(docker, v)
if err != nil {
unpause()
fmt.Fprintln(os.Stderr, "Could not create export archive: ", err)
os.Exit(1)
}
io.Copy(os.Stdout, arch)
}
func volumeImport(ctx *cli.Context) {
if len(ctx.Args()) < 1 {
fmt.Fprintln(os.Stderr, "Missing container")
}
docker := getDockerClient(ctx)
buildContext := bufio.NewReader(os.Stdin)
importToName := ctx.Args()[0]
container, err := docker.FetchContainer(importToName)
if err != nil {
fmt.Fprintln(os.Stderr, "Could not find container to import to:", importToName)
os.Exit(1)
}
imgId, err := buildImportImage(docker, buildContext, importToName)
if err != nil {
fmt.Fprintln(os.Stderr, "Could not create import: %s", err)
}
defer docker.RemoveImage(imgId, true, false)
var copyToVolDir string
if len(ctx.Args()) > 1 {
// The user asked for the volume to be put in a sepcific dir
// Let's pull that container and see if there is a volume at that location
vols, _ := container.GetVolumes()
for path, vol := range vols {
if path == ctx.Args()[1] {
copyToVolDir = vol.HostPath
break
}
}
// exit here since we don't know what to do since we couldn't find a volume
// matching the one passed in
if copyToVolDir == "" {
docker.RemoveImage(imgId, true, false)
fmt.Fprintln(os.Stderr, "Did not find a volume matching the path: ", ctx.Args()[1])
os.Exit(1)
}
}
if copyToVolDir == "" {
// Need to get the volume config so we know what volume to restore to
// We could untar the archive from the build context manually, but if it is a
// large volume, this would not be ideal, especially since now it is already
// baked into an image
volPath, err := extractVolConfigJson(imgId, docker)
if err != nil {
docker.RemoveImage(imgId, true, false)
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
vols, err := container.GetVolumes()
if err != nil {
docker.RemoveImage(imgId, true, false)
fmt.Fprintln(os.Stderr, "Could not get volume listing for container ", importToName, ": ", err)
}
for _, v := range vols {
if volPath == v.VolPath {
copyToVolDir = v.HostPath
break
}
}
if copyToVolDir == "" {
docker.RemoveImage(imgId, true, false)
fmt.Fprintln(os.Stderr, "Did not find a volume matching the path: ", volPath)
os.Exit(1)
}
}
bindSpec := fmt.Sprintf("%s:/.dockervolume", copyToVolDir)
containerConfig := map[string]interface{}{
"Image": imgId,
"HostConfig": map[string]interface{}{
"Binds": []string{bindSpec},
},
}
id, err := docker.RunContainer(containerConfig)
if err != nil {
docker.RemoveImage(imgId, true, false)
docker.RemoveContainer(id, true, true)
fmt.Fprintln(os.Stderr, "Could not import data: ", err)
os.Exit(1)
}
docker.ContainerWait(id)
docker.RemoveImage(imgId, true, false)
docker.RemoveContainer(id, true, true)
}