-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
310 lines (279 loc) · 10.2 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
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
// Copyright (c) 2016 Pulcy.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"bufio"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/kardianos/osext"
"github.com/op/go-logging"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
clusterpkg "github.com/pulcy/quark/cluster"
"github.com/pulcy/quark/providers"
"github.com/pulcy/quark/providers/cloudflare"
"github.com/pulcy/quark/providers/digitalocean"
"github.com/pulcy/quark/providers/scaleway"
"github.com/pulcy/quark/providers/vagrant"
"github.com/pulcy/quark/providers/vultr"
)
var (
projectVersion = "dev"
projectBuild = "dev"
domain = "pulcy.com"
)
const (
projectName = "quark"
defaultLogLevel = "info"
)
var (
cmdMain = &cobra.Command{
Use: projectName,
Run: showUsage,
PersistentPreRun: loadDefaults,
}
provider string
digitalOceanToken string
cloudflareApiKey string
cloudflareEmail string
scalewayCfg scaleway.ScalewayProviderConfig
vagrantFolder string
vultrApiKey string
logLevel string
cluster string
vaultCfg providers.VaultProviderConfig
log = logging.MustGetLogger(projectName)
)
func init() {
logging.SetFormatter(logging.MustStringFormatter("[%{level:-5s}] %{message}"))
scalewayCfg = scaleway.NewConfig()
cmdMain.PersistentFlags().StringVar(&logLevel, "log-level", defaultLogLevel, "Log level (debug|info|warning|error)")
cmdMain.PersistentFlags().StringVarP(&provider, "provider", "p", "", "Provider used for creating clusters [digitalocean|scaleway|vagrant|vultr]")
cmdMain.PersistentFlags().StringVarP(&cluster, "cluster", "c", "", "Path of the cluster template [<profile>@]path")
// Digital ocean settings
cmdMain.PersistentFlags().StringVarP(&digitalOceanToken, "digitalocean-token", "t", "", "Digital Ocean token")
// Cloudflare settings
cmdMain.PersistentFlags().StringVarP(&cloudflareApiKey, "cloudflare-apikey", "k", "", "Cloudflare API key")
cmdMain.PersistentFlags().StringVarP(&cloudflareEmail, "cloudflare-email", "e", "", "Cloudflare email address")
// Scaleway settings
cmdMain.PersistentFlags().StringVarP(&scalewayCfg.Organization, "scaleway-organization", "", scalewayCfg.Organization, "Scaleway organization ID")
cmdMain.PersistentFlags().StringVarP(&scalewayCfg.Token, "scaleway-token", "", scalewayCfg.Token, "Scaleway token")
cmdMain.PersistentFlags().StringVarP(&scalewayCfg.Region, "scaleway-region", "", scalewayCfg.Region, "Scaleway region")
cmdMain.PersistentFlags().BoolVar(&scalewayCfg.ReserveLoadBalancerIP, "scaleway-reserve-ip", scalewayCfg.ReserveLoadBalancerIP, "Use reserved IPv4 addresses for load-balancer instances")
cmdMain.PersistentFlags().BoolVar(&scalewayCfg.EnableIPV6, "scaleway-ipv6", scalewayCfg.EnableIPV6, "Enabled IPv6 on all instances")
cmdMain.PersistentFlags().BoolVar(&scalewayCfg.NoIPv4, "scaleway-no-ipv4", scalewayCfg.NoIPv4, "Do not add IPv4 addresses to new instances")
// Vagrant settings
cmdMain.PersistentFlags().StringVarP(&vagrantFolder, "vagrant-folder", "f", defaultVagrantFolder(), "Directory containing vagrant files")
// Vultr settings
cmdMain.PersistentFlags().StringVarP(&vultrApiKey, "vultr-apikey", "", "", "Vultr API key")
// Vault settings
vaultCfg.VaultCAPath = os.Getenv("VAULT_CAPATH")
cmdMain.PersistentFlags().StringVar(&vaultCfg.VaultAddr, "vault-addr", defaultVaultAddr(), "URL of the vault (defaults to VAULT_ADDR environment variable)")
cmdMain.PersistentFlags().StringVar(&vaultCfg.VaultCACert, "vault-cacert", defaultVaultCACert(), "Path to a PEM-encoded CA cert file to use to verify the Vault server SSL certificate")
cmdMain.PersistentFlags().StringVar(&vaultCfg.VaultCAPath, "vault-capath", vaultCfg.VaultCAPath, "Path to a directory of PEM-encoded CA cert files to verify the Vault server SSL certificate")
cmdMain.PersistentFlags().StringVar(&vaultCfg.VaultCAKey, "vault-ca-key", defaultVaultCAKey(), "Path to a PEM-encoded CA key file to use to verify the Vault server SSL certificate")
cmdMain.PersistentFlags().StringVar(&vaultCfg.VaultCAKeyCommand, "vault-ca-key-command", defaultVaultCAKeyCommand(), "Shell command that outputs a PEM-encoded CA key to use to as the Vault server SSL certificate key")
cmdMain.PersistentFlags().StringVarP(&vaultCfg.GithubToken, "github-token", "G", defaultGithubToken(), "Personal github token for administrator logins")
}
func main() {
cmdMain.Execute()
}
func showUsage(cmd *cobra.Command, args []string) {
cmd.Usage()
}
func loadDefaults(cmd *cobra.Command, args []string) {
if digitalOceanToken == "" {
digitalOceanToken = os.Getenv("DIGITALOCEAN_TOKEN")
}
if cloudflareApiKey == "" {
cloudflareApiKey = os.Getenv("CLOUDFLARE_APIKEY")
}
if cloudflareEmail == "" {
cloudflareEmail = os.Getenv("CLOUDFLARE_EMAIL")
}
if vultrApiKey == "" {
vultrApiKey = os.Getenv("VULTR_APIKEY")
}
// Set loglevel
level, err := logging.LogLevel(logLevel)
if err != nil {
Exitf("Invalid log-level '%s': %#v", logLevel, err)
}
logging.SetLevel(level, projectName)
}
func newProvider() providers.CloudProvider {
switch provider {
case "digitalocean":
if digitalOceanToken == "" {
Exitf("Please specify a token\n")
}
return digitalocean.NewProvider(log, digitalOceanToken)
case "scaleway":
if scalewayCfg.Organization == "" || scalewayCfg.Token == "" {
rc, err := scaleway.ReadRC()
if err != nil {
Exitf("Cannot read .scwrc: %#v\n", err)
}
scalewayCfg.Organization = rc.Organization
scalewayCfg.Token = rc.Token
}
if scalewayCfg.Organization == "" {
Exitf("Please specify a scaleway-organization\n")
}
if scalewayCfg.Token == "" {
Exitf("Please specify a scaleway-token\n")
}
provider, err := scaleway.NewProvider(log, scalewayCfg)
if err != nil {
Exitf("NewProvider failed: %#v\n", err)
}
return provider
case "vagrant":
if vagrantFolder == "" {
Exitf("Please specify a vagrant-folder\n")
}
return vagrant.NewProvider(log, vagrantFolder)
case "vultr":
if vultrApiKey == "" {
Exitf("Please specify a vultr-apikey\n")
}
return vultr.NewProvider(log, vultrApiKey)
default:
Exitf("Unknown provider '%s'\n", provider)
return nil
}
}
func newDnsProvider() providers.DnsProvider {
if cloudflareApiKey == "" {
Exitf("Please specify a cloudflare-apikey\n")
}
if cloudflareEmail == "" {
Exitf("Please specify a cloudflare-email\n")
}
return cloudflare.NewProvider(log, cloudflareApiKey, cloudflareEmail)
}
func newVaultProvider() providers.VaultProvider {
provider, err := providers.NewVaultProvider(log, vaultCfg)
if err != nil {
Exitf("Failed to created vault provider: %#v\n", err)
}
return provider
}
func confirm(question string) error {
for {
fmt.Printf("%s [yes|no]", question)
bufStdin := bufio.NewReader(os.Stdin)
line, _, err := bufStdin.ReadLine()
if err != nil {
return err
}
if string(line) == "yes" || string(line) == "y" {
return nil
}
fmt.Println("Please enter 'yes' to confirm.")
}
}
func Exitf(format string, args ...interface{}) {
fmt.Printf(format, args...)
os.Exit(1)
}
func Infof(format string, args ...interface{}) {
fmt.Printf(format, args...)
}
// clusterInfoFromArgs fills the given cluster info from a command line argument
func clusterInfoFromArgs(info *providers.ClusterInfo, args []string) {
if len(args) == 1 && info.Name == "" {
parts := strings.SplitN(args[0], ".", 2)
info.Name = parts[0]
info.Domain = parts[1]
}
}
// clusterInstanceInfoFromArgs fills the given cluster info from a command line argument
func clusterInstanceInfoFromArgs(info *providers.ClusterInstanceInfo, args []string) {
if len(args) == 1 && info.Prefix == "" && info.Name == "" {
parts := strings.SplitN(args[0], ".", 3)
info.Prefix = parts[0]
info.Name = parts[1]
info.Domain = parts[2]
}
}
// loadArgumentsFromCluster and uses its data to update the given flagset.
func loadArgumentsFromCluster(flagSet *pflag.FlagSet, requireProfile bool) {
if cluster == "" {
return
}
profile := ""
parts := strings.Split(cluster, "@")
if len(parts) == 2 {
profile = parts[0]
cluster = parts[1]
} else if requireProfile {
Exitf("No cluster profile specified (-c profile@cluster)")
}
clustersPath := os.Getenv("PULCY_CLUSTERS")
if clustersPath == "" {
clustersPath = "config/clusters"
}
path, err := resolvePath(cluster, clustersPath, ".hcl")
if err != nil {
Exitf("Cannot resolve cluster path: %#v", err)
}
c, err := clusterpkg.ParseClusterFromFile(path)
if err != nil {
Exitf("Cannot load cluster from path '%s': %#v", clustersPath, err)
}
values, err := c.ResolveProfile(profile)
if err != nil {
Exitf("Cannot resolve profile '%s' in cluster path '%s': %#v", profile, clustersPath, err)
}
flagSet.VisitAll(func(flag *pflag.Flag) {
if !flag.Changed {
value, ok := values[flag.Name]
if ok {
err := flagSet.Set(flag.Name, fmt.Sprintf("%v", value))
if err != nil {
Exitf("Error in option '%s': %#v\n", flag.Name, err)
}
log.Debugf("--%s=%v", flag.Name, value)
}
}
})
}
// resolvePath tries to resolve a given path.
// 1) Try as real path
// 2) Try as filename relative to my process with given relative folder & extension
func resolvePath(path, altFolder, extension string) (string, error) {
if _, err := os.Stat(path); os.IsNotExist(err) {
// path not found, try locating it by name in a different folder
var folder string
if filepath.IsAbs(altFolder) {
folder = altFolder
} else {
// altFolder is relative, assume it is relative to our executable
exeFolder, err := osext.ExecutableFolder()
if err != nil {
return "", maskAny(err)
}
folder = filepath.Join(exeFolder, altFolder)
}
path = filepath.Join(folder, path) + extension
if _, err := os.Stat(path); os.IsNotExist(err) {
// Try without extensions
path = filepath.Join(folder, path)
}
}
return path, nil
}