-
Notifications
You must be signed in to change notification settings - Fork 111
/
airgap.go
339 lines (280 loc) · 8.84 KB
/
airgap.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package main
import (
"fmt"
"io/ioutil"
"os"
"time"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/chef/automate/components/automate-cli/pkg/status"
"github.com/chef/automate/components/automate-deployment/pkg/airgap"
"github.com/chef/automate/components/automate-deployment/pkg/habpkg"
"github.com/chef/automate/components/automate-deployment/pkg/manifest/parser"
)
var airgapCmdFlags = airgapFlags{}
type airgapFlags struct {
manifestPath string
workspacePath string
channel string
overrideOrigin string
hartifactsPath string
version string
verbose bool
hartsOnly bool
retries int
retryDelay int
versionsPath string
}
func (f airgapFlags) validateArgs() error {
if f.manifestPath != "" && f.version != "" {
return status.New(status.AirgapCreateInstallBundleError, "You cannot provide both a manifest.json and a version")
}
if f.hartifactsPath != "" && f.overrideOrigin == "" {
return status.New(status.InvalidCommandArgsError, "You must provide --override-origin with --hartifacts")
}
if f.hartifactsPath == "" && f.overrideOrigin != "" {
return status.New(status.InvalidCommandArgsError, "Must must provide --hartifacts with --override-origin")
}
return nil
}
const successString = `Your Automate Install Bundle has been written to %s. To install the
bundle on an Internet-disconnected server, copy the bundle and the chef-automate tool
to the server and run
chef-automate deploy --airgap-bundle </path/to/bundle>
`
func newAirgapCmd() *cobra.Command {
var airgapCmd = &cobra.Command{
Use: "airgap COMMAND",
}
var bundleCmd = &cobra.Command{
Use: "bundle COMMAND",
}
var bundleCreateCmd = &cobra.Command{
Use: "create [/path/to/bundle.aib]",
Annotations: map[string]string{
NoCheckVersionAnnotation: NoCheckVersionAnnotation,
NoRequireRootAnnotation: NoRequireRootAnnotation,
},
RunE: runAirgapCreateInstallBundle,
Args: cobra.RangeArgs(0, 1),
}
bundleCreateCmd.PersistentFlags().StringVarP(
&airgapCmdFlags.channel, "channel", "c", "",
"Release channel to pull packages from",
)
bundleCreateCmd.PersistentFlags().StringVarP(
&airgapCmdFlags.manifestPath, "manifest", "m", "",
"Path to a release manifest.json",
)
bundleCreateCmd.PersistentFlags().StringVarP(
&airgapCmdFlags.workspacePath, "workspace", "w", "",
"Path to workspace storage location where temporary data will be stored",
)
bundleCreateCmd.PersistentFlags().StringVar(
&airgapCmdFlags.hartifactsPath, "hartifacts", "",
"Path to habitat package artifacts",
)
bundleCreateCmd.PersistentFlags().StringVar(
&airgapCmdFlags.overrideOrigin, "override-origin", "",
"Origin of habitat package creator",
)
bundleCreateCmd.PersistentFlags().IntVarP(
&airgapCmdFlags.retries, "retries", "r", 2,
"Number of times to retry failed hab package downloads",
)
bundleCreateCmd.PersistentFlags().IntVar(
&airgapCmdFlags.retryDelay, "retry-delay", -1,
"Number of seconds to wait between retries (exponential backoff is used if not provided)",
)
bundleCreateCmd.PersistentFlags().StringVar(
&airgapCmdFlags.version, "version", "",
"Chef Automate version to create an airgap bundle for",
)
bundleCreateCmd.PersistentFlags().StringVar(
&airgapCmdFlags.versionsPath, "versions-file", "",
"Path to versions.json",
)
if !isDevMode() {
for _, flagName := range []string{
"override-origin",
"hartifacts",
"versions-file",
} {
err := bundleCreateCmd.PersistentFlags().MarkHidden(flagName)
if err != nil {
fmt.Printf("failed configuring cobra: %s\n", err.Error())
panic(":(")
}
}
}
var bundleUnpackCmd = &cobra.Command{
Use: "unpack /path/to/bundle.aib",
Annotations: map[string]string{
NoCheckVersionAnnotation: NoCheckVersionAnnotation,
NoRequireRootAnnotation: NoRequireRootAnnotation,
},
RunE: runAirgapUnpackInstallBundle,
Args: cobra.ExactArgs(1),
Hidden: true,
}
bundleUnpackCmd.PersistentFlags().BoolVar(
&airgapCmdFlags.hartsOnly, "harts-only", false, "Only unpack hartifacts and their keys",
)
var bundleInfoCmd = &cobra.Command{
Use: "info /path/to/bundle.aib",
Annotations: map[string]string{
NoCheckVersionAnnotation: NoCheckVersionAnnotation,
NoRequireRootAnnotation: NoRequireRootAnnotation,
},
RunE: runAirgapInfoInstallBundle,
Args: cobra.ExactArgs(1),
}
bundleInfoCmd.PersistentFlags().BoolVar(
&airgapCmdFlags.verbose, "verbose", false, "Output full AIB metadata",
)
if isDevMode() {
bundleUnpackCmd.Hidden = false
}
airgapCmd.AddCommand(bundleCmd)
bundleCmd.AddCommand(bundleCreateCmd)
bundleCmd.AddCommand(bundleInfoCmd)
bundleCmd.AddCommand(bundleUnpackCmd)
return airgapCmd
}
type installBundlePath struct {
InstallBundlePath string `json:"install_bundle_path"`
}
type installBundleCreateProgress struct{}
func (installBundleCreateProgress) Downloading(name string, try int) {
if try > 1 {
writer.Bodyf("Downloading %s (try %d)", name, try)
} else {
writer.Bodyf("Downloading %s", name)
}
writer.StartSpinner()
}
func (installBundleCreateProgress) DownloadComplete(name string, wasCached bool) {
writer.StopSpinner()
if wasCached {
writer.Bodyf("%s found in cache", name)
} else {
writer.Bodyf("Downloaded %s", name)
}
}
func (installBundleCreateProgress) RetriableDownloadError(name string, info string, delay time.Duration) {
writer.StopSpinner()
writer.Bodyf("Downloading %s failed (will retry in %s): %s", name, delay, info)
}
func runAirgapCreateInstallBundle(cmd *cobra.Command, args []string) error {
if err := airgapCmdFlags.validateArgs(); err != nil {
return err
}
opts := []airgap.InstallBundleCreatorOpt{}
if airgapCmdFlags.channel != "" {
opts = append(
opts,
airgap.WithInstallBundleChannel(airgapCmdFlags.channel),
)
}
if airgapCmdFlags.version != "" {
opts = append(
opts,
airgap.WithInstallBundleVersion(airgapCmdFlags.version),
)
}
if airgapCmdFlags.manifestPath != "" {
opts = append(
opts,
airgap.WithInstallBundleManifestFile(airgapCmdFlags.manifestPath),
)
}
if airgapCmdFlags.hartifactsPath != "" && airgapCmdFlags.overrideOrigin != "" {
opts = append(
opts,
airgap.WithInstallBundleHartifactsPath(
airgapCmdFlags.hartifactsPath,
airgapCmdFlags.overrideOrigin,
),
)
}
if airgapCmdFlags.versionsPath != "" {
opts = append(opts, airgap.WithInstallBundleVersionsPath(airgapCmdFlags.versionsPath))
}
if len(args) > 0 && args[0] != "" {
opts = append(opts, airgap.WithInstallBundleOutputPath(args[0]))
}
if airgapCmdFlags.workspacePath == "" {
var err error
airgapCmdFlags.workspacePath, err = ioutil.TempDir("", "aib-workspace")
if err != nil {
return status.Wrap(
err,
status.FileAccessError,
"Creating temporary workspace directory failed",
)
}
logrus.WithField(
"dir", airgapCmdFlags.workspacePath,
).Debug("Using temporary workspace")
defer func() {
_ = os.RemoveAll(airgapCmdFlags.workspacePath)
}()
}
opts = append(
opts,
airgap.WithInstallBundleWorkspacePath(airgapCmdFlags.workspacePath),
airgap.WithInstallBundleRetries(airgapCmdFlags.retries),
airgap.WithInstallBundleRetryDelay(airgapCmdFlags.retryDelay),
)
writer.Title("Creating Airgap Installation Bundle...")
creator := airgap.NewInstallBundleCreator(opts...)
outputFile, err := creator.Create(installBundleCreateProgress{})
if err != nil {
writer.StopSpinner()
return status.Annotate(err, status.AirgapCreateInstallBundleError)
}
writer.Successf(successString, outputFile)
status.GlobalResult = installBundlePath{
InstallBundlePath: outputFile,
}
return nil
}
func runAirgapUnpackInstallBundle(cmd *cobra.Command, args []string) error {
_, err := airgap.Unpack(args[0], airgap.WithUnpackHartsOnly(airgapCmdFlags.hartsOnly))
if err != nil {
return status.Annotate(err, status.AirgapUnpackInstallBundleError)
}
return nil
}
func runAirgapInfoInstallBundle(cmd *cobra.Command, args []string) error {
metadata, manifestBytes, err := airgap.GetMetadata(args[0])
if err != nil {
return status.Annotate(err, status.AirgapUnpackInstallBundleError)
}
manifest, err := parser.ManifestFromBytes(manifestBytes)
if err != nil {
return status.Annotate(err, status.AirgapUnpackInstallBundleError)
}
writer.Printf(" Version: %s\n", manifest.Build)
writer.Printf("Build SHA: %s\n", manifest.BuildSHA)
writer.Printf(" Packages:\n")
for _, pkg := range manifest.Packages {
writer.Printf(" %s\n", habpkg.Ident(&pkg))
}
if airgapCmdFlags.verbose {
writer.Printf("\nHab binary path: %s\n", metadata.HabBinPath)
writer.Printf(" Manifest path: %s\n", metadata.HabBinPath)
writer.Printf(" Hartifacts:\n")
for _, p := range metadata.HartifactPaths {
writer.Printf(" %s\n", p)
}
writer.Printf(" Origin Keys:\n")
for _, p := range metadata.OriginKeyPaths {
writer.Printf(" %s\n", p)
}
}
return nil
}
func init() {
RootCmd.AddCommand(newAirgapCmd())
}