forked from rancher/cli
/
export.go
170 lines (148 loc) · 3.44 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
package cmd
import (
"archive/tar"
"bytes"
"io"
"io/ioutil"
"os"
"path/filepath"
"github.com/Sirupsen/logrus"
"github.com/rancher/go-rancher/v2"
"github.com/urfave/cli"
)
func ExportCommand() cli.Command {
return cli.Command{
Name: "export",
Usage: "Export configuration yml for a stack as a tar archive or to local files",
Description: `
Exports the docker-compose.yml and rancher-compose.yml for the specified stack as a tar archive.
Example:
$ rancher export mystack
$ rancher export -f files.tar mystack
# Export the entire environment, including system stacks
$ rancher export --system mystack
`,
ArgsUsage: "[STACKNAME STACKID...]",
Action: exportService,
Flags: []cli.Flag{
cli.StringFlag{
Name: "file,f",
Usage: "Write to a file, instead of local files, use - to write to STDOUT",
},
cli.BoolFlag{
Name: "system,s",
Usage: "If exporting the entire environment, include system",
},
},
}
}
func getOutput(ctx *cli.Context) (io.WriteCloser, error) {
output := ctx.String("file")
if output == "" {
return nil, nil
} else if output == "-" {
return os.Stdout, nil
}
return os.Create(output)
}
func getStackNames(ctx *cli.Context, c *client.RancherClient) ([]string, error) {
stacks, err := c.Stack.List(defaultListOpts(ctx))
if err != nil {
return nil, err
}
result := []string{}
for _, stack := range stacks.Data {
result = append(result, stack.Name)
}
return result, nil
}
func exportService(ctx *cli.Context) error {
var err error
c, err := GetClient(ctx)
if err != nil {
return err
}
names := ctx.Args()
if len(names) == 0 {
names, err = getStackNames(ctx, c)
if err != nil {
return err
}
}
var archive *tar.Writer
output, err := getOutput(ctx)
if err != nil {
return err
}
if output != nil {
defer output.Close()
archive = tar.NewWriter(output)
defer archive.Close()
}
for _, name := range names {
resource, err := Lookup(c, name, "stack")
if err != nil {
return err
}
stack, err := c.Stack.ById(resource.Id)
if err != nil {
return err
}
if _, ok := stack.Actions["exportconfig"]; !ok {
continue
}
config, err := c.Stack.ActionExportconfig(stack, nil)
if err != nil {
return err
}
if err := addToTar(archive, stack.Name, "docker-compose.yml", config.DockerComposeConfig); err != nil {
return err
}
if err := addToTar(archive, stack.Name, "rancher-compose.yml", config.RancherComposeConfig); err != nil {
return err
}
if len(config.Actions) > 0 {
if err := addToTar(archive, stack.Name, "answers", marshalAnswers(config.Actions)); err != nil {
return err
}
}
}
return nil
}
func marshalAnswers(answers map[string]string) string {
buf := &bytes.Buffer{}
for k, v := range answers {
buf.WriteString(k)
buf.WriteString("=")
buf.WriteString(v)
buf.WriteString("\n")
}
return buf.String()
}
func addToTar(archive *tar.Writer, stackName, name string, stringContent string) error {
if len(stringContent) == 0 {
return nil
}
f := filepath.Join(stackName, name)
if archive == nil {
err := os.MkdirAll(stackName, 0755)
if err != nil {
return err
}
logrus.Infof("Creating %s", f)
return ioutil.WriteFile(f, []byte(stringContent), 0600)
}
content := []byte(stringContent)
err := archive.WriteHeader(&tar.Header{
Name: f,
Size: int64(len(content)),
Mode: 0644,
Uname: "root",
Gname: "root",
})
if err != nil {
return err
}
_, err = archive.Write(content)
return err
}