/
main.go
246 lines (220 loc) · 9.25 KB
/
main.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
package pkg
import (
"fmt"
"os"
"os/signal"
"strings"
"sync"
"time"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
type Locations struct {
Src Location
Dst Location
SameRegion bool
SameAZ bool
SameProject bool
}
type Location struct {
AuthURL string
Region string
Domain string
Project string
Username string
Password string
ApplicationCredentialName string
ApplicationCredentialID string
ApplicationCredentialSecret string
Token string
Origin string
}
var (
// RootCmd represents the base command when called without any subcommands
RootCmd = &cobra.Command{
Use: "cyclone",
Short: "Clone OpenStack entities easily",
SilenceUsage: true,
}
cleanupFuncs []func(*sync.WaitGroup)
// unattended mode, assume yes to all questions
yes bool
// unattended mode, assume no to all questions
no bool
)
// Execute adds all child commands to the root command sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
initRootCmdFlags()
cleanupFunc := func() {
var wg = &sync.WaitGroup{}
for _, f := range cleanupFuncs {
wg.Add(1)
go f(wg)
}
wg.Wait()
}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
<-c
log.Printf("Interrupted")
cleanupFunc()
os.Exit(1)
}()
err := RootCmd.Execute()
if err != nil {
log.Printf("Error: %s", err)
}
cleanupFunc()
if err != nil {
os.Exit(1)
}
}
func initRootCmdFlags() {
// debug flag
RootCmd.PersistentFlags().BoolP("debug", "d", false, "print out request and response objects")
RootCmd.PersistentFlags().BoolP("yes", "y", false, "assume \"yes\" to all questions")
RootCmd.PersistentFlags().BoolP("no", "n", false, "assume \"no\" to all questions")
RootCmd.PersistentFlags().StringP("to-auth-url", "", "", "destination auth URL (if not provided, detected automatically from the source auth URL and destination region)")
RootCmd.PersistentFlags().StringP("to-region", "", "", "destination region")
RootCmd.PersistentFlags().StringP("to-domain", "", "", "destination domain")
RootCmd.PersistentFlags().StringP("to-project", "", "", "destination project")
RootCmd.PersistentFlags().StringP("to-username", "", "", "destination username")
RootCmd.PersistentFlags().StringP("to-password", "", "", "destination username password")
RootCmd.PersistentFlags().StringP("to-application-credential-name", "", "", "destination application credential name")
RootCmd.PersistentFlags().StringP("to-application-credential-id", "", "", "destination application credential ID")
RootCmd.PersistentFlags().StringP("to-application-credential-secret", "", "", "destination application credential secret")
RootCmd.PersistentFlags().StringP("timeout-image", "", "24h", "timeout to wait for an image status")
RootCmd.PersistentFlags().StringP("timeout-volume", "", "24h", "timeout to wait for a volume status")
RootCmd.PersistentFlags().StringP("timeout-server", "", "24h", "timeout to wait for a server status")
RootCmd.PersistentFlags().StringP("timeout-snapshot", "", "24h", "timeout to wait for a snapshot status")
RootCmd.PersistentFlags().StringP("timeout-backup", "", "24h", "timeout to wait for a backup status")
RootCmd.PersistentFlags().StringP("timeout-share", "", "24h", "timeout to wait for a share status")
RootCmd.PersistentFlags().StringP("timeout-share-snapshot", "", "24h", "timeout to wait for a share snapshot status")
RootCmd.PersistentFlags().StringP("timeout-share-replica", "", "24h", "timeout to wait for a share replica status")
RootCmd.PersistentFlags().BoolP("image-web-download", "", false, "use Glance web-download image import method")
viper.BindPFlag("debug", RootCmd.PersistentFlags().Lookup("debug"))
viper.BindPFlag("yes", RootCmd.PersistentFlags().Lookup("yes"))
viper.BindPFlag("no", RootCmd.PersistentFlags().Lookup("no"))
viper.BindPFlag("to-auth-url", RootCmd.PersistentFlags().Lookup("to-auth-url"))
viper.BindPFlag("to-region", RootCmd.PersistentFlags().Lookup("to-region"))
viper.BindPFlag("to-domain", RootCmd.PersistentFlags().Lookup("to-domain"))
viper.BindPFlag("to-project", RootCmd.PersistentFlags().Lookup("to-project"))
viper.BindPFlag("to-username", RootCmd.PersistentFlags().Lookup("to-username"))
viper.BindPFlag("to-password", RootCmd.PersistentFlags().Lookup("to-password"))
viper.BindPFlag("to-application-credential-name", RootCmd.PersistentFlags().Lookup("to-application-credential-name"))
viper.BindPFlag("to-application-credential-id", RootCmd.PersistentFlags().Lookup("to-application-credential-id"))
viper.BindPFlag("to-application-credential-secret", RootCmd.PersistentFlags().Lookup("to-application-credential-secret"))
viper.BindPFlag("timeout-image", RootCmd.PersistentFlags().Lookup("timeout-image"))
viper.BindPFlag("timeout-volume", RootCmd.PersistentFlags().Lookup("timeout-volume"))
viper.BindPFlag("timeout-server", RootCmd.PersistentFlags().Lookup("timeout-server"))
viper.BindPFlag("timeout-snapshot", RootCmd.PersistentFlags().Lookup("timeout-snapshot"))
viper.BindPFlag("timeout-backup", RootCmd.PersistentFlags().Lookup("timeout-backup"))
viper.BindPFlag("timeout-share", RootCmd.PersistentFlags().Lookup("timeout-share"))
viper.BindPFlag("timeout-share-snapshot", RootCmd.PersistentFlags().Lookup("timeout-share-snapshot"))
viper.BindPFlag("timeout-share-replica", RootCmd.PersistentFlags().Lookup("timeout-share-replica"))
viper.BindPFlag("image-web-download", RootCmd.PersistentFlags().Lookup("image-web-download"))
}
func parseTimeoutArg(arg string, dst *float64, errors *[]error) {
s := viper.GetString(arg)
v, err := time.ParseDuration(s)
if err != nil {
*errors = append(*errors, fmt.Errorf("failed to parse --%s value: %q", arg, s))
return
}
t := int(v.Seconds())
if t == 0 {
*errors = append(*errors, fmt.Errorf("--%s value cannot be zero: %d", arg, t))
}
if t < 0 {
*errors = append(*errors, fmt.Errorf("--%s value cannot be negative: %d", arg, t))
}
*dst = float64(t)
}
func parseTimeoutArgs() error {
var errors []error
yes = viper.GetBool("yes")
no = viper.GetBool("no")
if yes && no {
errors = append(errors, fmt.Errorf("combining \"yes\" and \"no\" flags is not allowed"))
}
parseTimeoutArg("timeout-image", &waitForImageSec, &errors)
parseTimeoutArg("timeout-volume", &waitForVolumeSec, &errors)
parseTimeoutArg("timeout-server", &waitForServerSec, &errors)
parseTimeoutArg("timeout-snapshot", &waitForSnapshotSec, &errors)
parseTimeoutArg("timeout-backup", &waitForBackupSec, &errors)
parseTimeoutArg("timeout-share", &waitForShareSec, &errors)
parseTimeoutArg("timeout-share-snapshot", &waitForShareSnapshotSec, &errors)
parseTimeoutArg("timeout-share-replica", &waitForShareReplicaSec, &errors)
if len(errors) > 0 {
return fmt.Errorf("%q", errors)
}
return nil
}
func getSrcAndDst(az string) (Locations, error) {
initLogger()
var loc Locations
// source and destination parameters
loc.Src.Origin = "src"
loc.Src.Region = os.Getenv("OS_REGION_NAME")
loc.Src.AuthURL = os.Getenv("OS_AUTH_URL")
loc.Src.Domain = os.Getenv("OS_PROJECT_DOMAIN_NAME")
if loc.Src.Domain == "" {
loc.Src.Domain = os.Getenv("OS_USER_DOMAIN_NAME")
}
loc.Src.Project = os.Getenv("OS_PROJECT_NAME")
loc.Src.Username = os.Getenv("OS_USERNAME")
loc.Src.Password = os.Getenv("OS_PASSWORD")
loc.Src.ApplicationCredentialName = os.Getenv("OS_APPLICATION_CREDENTIAL_NAME")
loc.Src.ApplicationCredentialID = os.Getenv("OS_APPLICATION_CREDENTIAL_ID")
loc.Src.ApplicationCredentialSecret = os.Getenv("OS_APPLICATION_CREDENTIAL_SECRET")
loc.Src.Token = os.Getenv("OS_AUTH_TOKEN")
loc.Dst.Origin = "dst"
loc.Dst.Region = viper.GetString("to-region")
loc.Dst.AuthURL = viper.GetString("to-auth-url")
loc.Dst.Domain = viper.GetString("to-domain")
loc.Dst.Project = viper.GetString("to-project")
loc.Dst.Username = viper.GetString("to-username")
loc.Dst.Password = viper.GetString("to-password")
loc.Dst.ApplicationCredentialName = viper.GetString("to-application-credential-name")
loc.Dst.ApplicationCredentialID = viper.GetString("to-application-credential-id")
loc.Dst.ApplicationCredentialSecret = viper.GetString("to-application-credential-secret")
if loc.Dst.Project == "" {
loc.Dst.Project = loc.Src.Project
}
if loc.Dst.Region == "" {
loc.Dst.Region = loc.Src.Region
}
if loc.Dst.Domain == "" {
loc.Dst.Domain = loc.Src.Domain
}
if loc.Dst.Username == "" {
loc.Dst.Username = loc.Src.Username
}
if loc.Dst.Password == "" {
loc.Dst.Password = loc.Src.Password
}
if loc.Dst.AuthURL == "" {
// try to transform a source auth URL to a destination source URL
s := strings.Replace(loc.Src.AuthURL, loc.Src.Region, loc.Dst.Region, 1)
if strings.Contains(s, loc.Dst.Region) {
loc.Dst.AuthURL = s
log.Printf("Detected %q destination auth URL", loc.Dst.AuthURL)
} else {
return loc, fmt.Errorf("failed to detect destination auth URL, please specify --to-auth-url explicitly")
}
}
loc.SameRegion = false
if loc.Src.Region == loc.Dst.Region {
if loc.Src.Domain == loc.Dst.Domain {
if loc.Src.Project == loc.Dst.Project {
loc.SameProject = true
// share the same token
loc.Dst.Token = loc.Src.Token
}
}
loc.SameRegion = true
}
return loc, nil
}