-
Notifications
You must be signed in to change notification settings - Fork 111
/
bootstrap.go
200 lines (171 loc) · 4.82 KB
/
bootstrap.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
package main
import (
"bufio"
"context"
"fmt"
"io"
"os"
"path"
"path/filepath"
"strconv"
"github.com/chef/automate/components/automate-deployment/pkg/bootstrapbundle"
"github.com/chef/automate/lib/secrets"
"github.com/chef/automate/lib/user"
"github.com/pkg/errors"
api "github.com/chef/automate/api/interservice/deployment"
"github.com/chef/automate/components/automate-cli/pkg/status"
"github.com/chef/automate/components/automate-deployment/pkg/client"
"github.com/spf13/cobra"
)
var bootstrapBundleCmdFlags = struct {
overwriteFile bool
rootDir string
}{}
func newBootstrapBundleCmd() *cobra.Command {
bootstrapCmd := &cobra.Command{
Use: "bootstrap COMMAND",
Hidden: true,
}
bundleCmd := &cobra.Command{
Use: "bundle COMMAND",
Hidden: true,
}
createCmd := &cobra.Command{
Use: "create [/path/to/bundle.abb]",
Args: cobra.RangeArgs(0, 1),
RunE: runBootstrapBundleCreate,
Hidden: true,
}
createCmd.PersistentFlags().BoolVarP(
&bootstrapBundleCmdFlags.overwriteFile,
"overwrite",
"o",
false,
"Overwrite existing bootstrap bundle file if one exists",
)
unpackCmd := &cobra.Command{
Use: "unpack /path/to/bundle.tar",
Args: cobra.ExactArgs(1),
RunE: runBootstrapBundleUnpack,
Hidden: true,
}
unpackCmd.PersistentFlags().StringVar(
&bootstrapBundleCmdFlags.rootDir,
"root-dir",
"",
"Set the directory where the bundle will be unpacked",
)
bootstrapCmd.AddCommand(bundleCmd)
bundleCmd.AddCommand(createCmd)
bundleCmd.AddCommand(unpackCmd)
return bootstrapCmd
}
func runBootstrapBundleCreate(cmd *cobra.Command, args []string) error {
connection, err := client.Connection(client.DefaultClientTimeout)
if err != nil {
return status.Wrap(err, status.APIUnreachableError, "Failed to create a connection")
}
// TODO: add timeout
stream, err := connection.BootstrapBundle(context.Background(), &api.BootstrapBundleRequest{})
if err != nil {
return status.WithRecovery(
status.Wrap(err, status.DeploymentServiceCallError, "Request to download bootstrap bundle failed"),
recoveryMsg,
)
}
outfile := ""
if len(args) > 0 && args[0] != "" {
outfile, err = filepath.Abs(args[0])
if err != nil {
return status.Annotate(err, status.FileAccessError)
}
} else {
cwd, err := os.Getwd()
if err != nil {
return status.Annotate(err, status.FileAccessError)
}
outfile = filepath.Join(cwd, "bootstrap-bundle.abb")
}
if _, err := os.Stat(outfile); err == nil {
if !bootstrapBundleCmdFlags.overwriteFile {
ok, err := writer.Confirm(fmt.Sprintf("%s file already exists. Do you wish to overwrite it?", outfile))
if err != nil {
return status.Annotate(err, status.FileAccessError)
}
if !ok {
return status.New(status.FileAccessError, "Bootstrap bundle cannot be overwritten")
}
}
}
downloadedBundleFile, err := os.OpenFile(outfile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return status.Annotate(err, status.FileAccessError)
}
defer downloadedBundleFile.Close() // nolint: errcheck
w := bufio.NewWriter(downloadedBundleFile)
for {
resp, err := stream.Recv()
if err == io.EOF {
break
}
if err != nil {
return status.WithRecovery(
status.Wrap(err, status.DeploymentServiceCallError, "Request to download bootstrap bundle failed"),
recoveryMsg,
)
}
data := resp.GetData()
if len(data) <= 0 {
continue
}
if _, err := w.Write(data); err != nil {
return status.Annotate(err, status.FileAccessError)
}
}
err = w.Flush()
if err != nil {
return status.Annotate(err, status.FileAccessError)
}
writer.Printf("Bootstrap bundle written to: %s\n", outfile)
return nil
}
func runBootstrapBundleUnpack(cmd *cobra.Command, args []string) error {
u, err := user.Lookup(secrets.DefaultDiskStoreDataOwner)
if err != nil {
return errors.Wrap(err, "user lookup")
}
uid, err := strconv.Atoi(u.Uid)
if err != nil {
return errors.Wrap(err, "converting uid to integer")
}
gid, err := strconv.Atoi(u.Gid)
if err != nil {
return errors.Wrap(err, "converting gid to integer")
}
var secretStorePath string
if bootstrapBundleCmdFlags.rootDir == "" {
secretStorePath = secrets.DefaultDiskStoreDataDir
} else {
secretStorePath = path.Join(bootstrapBundleCmdFlags.rootDir, "deployment-service/data/shared")
}
secretStore := secrets.NewDiskStore(
secretStorePath, uid, gid)
if err := secretStore.Initialize(); err != nil {
return errors.Wrap(err, "could not initialize secret store")
}
b := bootstrapbundle.NewCreator(
secretStore,
bootstrapbundle.WithRootDir(bootstrapBundleCmdFlags.rootDir))
f, err := os.Open(args[0])
if err != nil {
return status.Wrapf(err, status.FileAccessError, "could not open %q", args[0])
}
defer f.Close() // nolint: errcheck
if err := b.Unpack(f); err != nil {
return err
}
return nil
}
func init() {
RootCmd.AddCommand(newBootstrapBundleCmd())
}