/
backup.go
141 lines (119 loc) · 4.93 KB
/
backup.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
// Copyright (c) 2022 EPAM Systems, Inc.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package cmd
import (
"errors"
"io"
"strings"
"github.com/spf13/cobra"
"github.com/agilestacks/hub/cmd/hub/compose"
"github.com/agilestacks/hub/cmd/hub/lifecycle"
"github.com/agilestacks/hub/cmd/hub/util"
)
var (
backupBundleInJson bool
backupAllowPartial bool
backupRenameComponents string
backupEraseComponents string
backupIncludeOnlyComponents string
)
var backupCmd = &cobra.Command{
Use: "backup <create | unbundle> ...",
Short: "Create and manage backups",
Long: `Create backup of stack components; transform backup bundle into parameters manifest.`,
}
var backupCreateCmd = &cobra.Command{
Use: "create hub.yaml.elaborate -s hub.yaml.state[,s3://bucket/hub.yaml.state] -o bundle.yaml",
Short: "Create backup bundle",
Long: `Create backup of stack component(s).
Each stack component that supports 'backup' verb is invoked.
Bundle can be saved into multiple files and also sent to S3.`,
Annotations: map[string]string{
"usage-metering": "tags",
},
RunE: func(cmd *cobra.Command, args []string) error {
pipe := cmdContextPipe(cmd)
if pipe != nil {
defer pipe.Close()
}
return backupCreate(args, pipe)
},
}
var backupUnbundleCmd = &cobra.Command{
Use: "unbundle bundle.yaml [bundle2.yaml ...] -o restore-params.yaml[,s3://bucket/params.yaml]",
Short: "Create parameters for restore from bundle",
Long: `Transform backup bundle(s) into parameters manifest.
Multiple bundles will be merged with priority determined by file order on command-line (from left to right).
Components can be renamed, included and excluded.
Parameters can be saved into multiple files and also sent to S3.`,
RunE: func(cmd *cobra.Command, args []string) error {
return backupUnbundle(args)
},
}
func backupCreate(args []string, pipe io.WriteCloser) error {
if len(args) != 1 {
return errors.New("Backup Create command has only one argument - path to Stack Elaborate file")
}
manifests := util.SplitPaths(args[0])
stateManifests := util.SplitPaths(stateManifestExplicit)
bundleFiles := util.SplitPaths(outputFiles)
components := util.SplitPaths(componentName)
setOsEnvForNestedCli(manifests, stateManifests, componentsBaseDir)
request := &lifecycle.Request{
Verb: "backup",
DryRun: dryRun,
ManifestFilenames: manifests,
StateFilenames: stateManifests,
Components: components,
OsEnvironmentMode: osEnvironmentMode,
ComponentsBaseDir: componentsBaseDir,
Environment: hubEnvironment,
StackInstance: hubStackInstance,
Application: hubApplication,
}
lifecycle.BackupCreate(request, bundleFiles, backupBundleInJson, backupAllowPartial, pipe)
return nil
}
func backupUnbundle(args []string) error {
if len(args) == 0 {
return errors.New("Backup Unbunle command has one or more arguments - path(s) to Backup Bundle file(s)")
}
bundles := make([][]string, 0, len(args))
for _, files := range args {
bundles = append(bundles, strings.Split(files, ","))
}
parametersFiles := util.SplitPaths(outputFiles)
rename := util.SplitPaths(backupRenameComponents)
erase := util.SplitPaths(backupEraseComponents)
includeOnly := util.SplitPaths(backupIncludeOnlyComponents)
compose.BackupUnbundle(bundles, parametersFiles,
rename, erase, includeOnly)
return nil
}
func init() {
backupCreateCmd.Flags().StringVarP(&stateManifestExplicit, "state", "s", "",
"Path to state file(s), for example hub.yaml.state,s3://bucket/hub.yaml.state")
backupCreateCmd.Flags().StringVarP(&outputFiles, "output", "o", "",
"Bundle output file(s), for example bundle.yaml,s3://bucket/bundle.yaml (default to stdout)")
backupCreateCmd.Flags().BoolVarP(&backupBundleInJson, "json", "", false,
"JSON output")
backupCreateCmd.Flags().StringVarP(&componentName, "components", "c", "",
"A list of components to backup (in order, separated by comma)")
backupCreateCmd.Flags().BoolVarP(&backupAllowPartial, "allow-partial", "", false,
"Allow partial backups to succeed")
initCommonLifecycleFlags(backupCreateCmd, "backup")
backupUnbundleCmd.Flags().StringVarP(&outputFiles, "output", "o", "",
"Parameters output file(s), optionally write to S3 (default to stdout)")
backupUnbundleCmd.Flags().StringVarP(&backupRenameComponents, "rename", "r", "",
"Components to rename, for example -r pg1:postgresql,pg2:postgresql-rds")
backupUnbundleCmd.Flags().StringVarP(&backupEraseComponents, "erase", "e", "",
"Components to omit from parameters file, for example -e etcd,vault")
backupUnbundleCmd.Flags().StringVarP(&backupIncludeOnlyComponents, "include-only", "i", "",
"Include only specified components, for example -i postgresql,postgresql-rds")
backupCmd.AddCommand(backupCreateCmd)
backupCmd.AddCommand(backupUnbundleCmd)
RootCmd.AddCommand(backupCmd)
}