-
Notifications
You must be signed in to change notification settings - Fork 18
/
export.go
184 lines (155 loc) · 4.74 KB
/
export.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
package handler
import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/pkg/stdcopy"
"github.com/labstack/echo/v4"
"github.com/docker/volumes-backup-extension/internal"
"github.com/docker/volumes-backup-extension/internal/backend"
"github.com/docker/volumes-backup-extension/internal/log"
)
func (h *Handler) ExportVolume(ctx echo.Context) error {
ctxReq := ctx.Request().Context()
volumeName := ctx.Param("volume")
path := ctx.QueryParam("path")
fileName := ctx.QueryParam("fileName")
if volumeName == "" {
return ctx.String(http.StatusBadRequest, "volume is required")
}
if path == "" {
return ctx.String(http.StatusBadRequest, "path is required")
}
if fileName == "" {
return ctx.String(http.StatusBadRequest, "path is required")
}
log.Infof("volumeName: %s", volumeName)
log.Infof("path: %s", path)
log.Infof("fileName: %s", fileName)
cli, err := h.DockerClient()
if err != nil {
return err
}
defer func() {
h.ProgressCache.Lock()
delete(h.ProgressCache.m, volumeName)
h.ProgressCache.Unlock()
_ = backend.TriggerUIRefresh(ctxReq, cli)
}()
h.ProgressCache.Lock()
h.ProgressCache.m[volumeName] = "export"
h.ProgressCache.Unlock()
if err := backend.TriggerUIRefresh(ctxReq, cli); err != nil {
return err
}
// Stop container(s)
stoppedContainers, err := backend.StopRunningContainersAttachedToVolume(ctxReq, cli, volumeName)
if err != nil {
return err
}
var compressProgram string
tarOpts := "-cvf"
fileExt := filepath.Ext(fileName)
log.Infof("fileExt: %s", fileExt)
switch fileExt {
case ".gz":
compressProgram = "\"pigz -6 -k\"" // pigz (parallel implementation of gzip), use -6 as the default compression level (-1 is fastest, -9 is best), "-k" to not delete the original file after processing
case ".zst":
compressProgram = "zstdmt" // zstdmt is equivalent to zstd -T0 (attempt to detect and use the number of physical CPU cores)
case ".bz2":
compressProgram = "bzip2"
default:
compressProgram = ""
}
// Export
cmd := []string{"tar"}
if compressProgram != "" {
cmd = append(cmd, "-I", compressProgram)
}
cmd = append(cmd,
tarOpts,
"/vackup"+"/"+filepath.Base(fileName), // the .tar.zst file
"-C", // -C is used to not include the parent directory
"/vackup-volume", // the directory where the files to compress are
".")
cmdJoined := strings.Join(cmd, " ")
log.Infof("cmdJoined: %s", cmdJoined)
binds := []string{
volumeName + ":" + "/vackup-volume",
path + ":" + "/vackup",
}
log.Infof("binds: %+v", binds)
// Ensure the image is present before creating the container
reader, err := cli.ImagePull(ctxReq, internal.AlpineTarZstdImage, types.ImagePullOptions{
Platform: "linux/" + runtime.GOARCH,
})
if err != nil {
return err
}
_, err = io.Copy(os.Stdout, reader)
if err != nil {
return err
}
resp, err := cli.ContainerCreate(ctxReq, &container.Config{
Image: internal.AlpineTarZstdImage,
AttachStdout: true,
AttachStderr: true,
Cmd: []string{"/bin/sh", "-c", cmdJoined},
User: "root",
Labels: map[string]string{
"com.docker.desktop.extension": "true",
"com.docker.desktop.extension.name": "Volumes Backup & Share",
"com.docker.compose.project": "docker_volumes-backup-extension-desktop-extension",
"com.volumes-backup-extension.action": "export",
"com.volumes-backup-extension.volume": volumeName,
"com.volumes-backup-extension.path": path,
"com.volumes-backup-extension.fileName": fileName,
},
}, &container.HostConfig{
Binds: binds,
}, nil, nil, "")
if err != nil {
return err
}
if err := cli.ContainerStart(ctxReq, resp.ID, types.ContainerStartOptions{}); err != nil {
return err
}
var exitCode int64
statusCh, errCh := cli.ContainerWait(ctxReq, resp.ID, container.WaitConditionNotRunning)
select {
case err := <-errCh:
if err != nil {
return err
}
case status := <-statusCh:
log.Infof("status: %#+v\n", status)
exitCode = status.StatusCode
}
out, err := cli.ContainerLogs(ctxReq, resp.ID, types.ContainerLogsOptions{ShowStdout: true, ShowStderr: true})
if err != nil {
return err
}
_, err = stdcopy.StdCopy(os.Stdout, os.Stderr, out)
if err != nil {
return err
}
if exitCode != 0 {
return ctx.String(http.StatusInternalServerError, fmt.Sprintf("container exited with status code %d\n", exitCode))
}
err = cli.ContainerRemove(ctxReq, resp.ID, types.ContainerRemoveOptions{})
if err != nil {
return err
}
// Start container(s)
err = backend.StartContainersByName(ctxReq, cli, stoppedContainers)
if err != nil {
return err
}
return ctx.String(http.StatusCreated, "")
}