forked from Azure/acs-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
root.go
118 lines (100 loc) · 4.23 KB
/
root.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
package cmd
import (
"os"
"github.com/Azure/acs-engine/pkg/armhelpers"
"github.com/Azure/go-autorest/autorest/azure"
log "github.com/Sirupsen/logrus"
"github.com/satori/go.uuid"
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"
)
const (
rootName = "acs-engine"
rootShortDescription = "ACS-Engine deploys and manages container orchestrators in Azure"
rootLongDescription = "ACS-Engine deploys and manages Kubernetes, Swarm Mode, and DC/OS clusters in Azure"
)
var (
debug bool
)
// NewRootCmd returns the root command for ACS-Engine.
func NewRootCmd() *cobra.Command {
rootCmd := &cobra.Command{
Use: rootName,
Short: rootShortDescription,
Long: rootLongDescription,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if debug {
log.SetLevel(log.DebugLevel)
}
},
}
p := rootCmd.PersistentFlags()
p.BoolVar(&debug, "debug", false, "enable verbose debug logs")
rootCmd.AddCommand(newVersionCmd())
rootCmd.AddCommand(newGenerateCmd())
rootCmd.AddCommand(newDeployCmd())
if val := os.Getenv("ACSENGINE_EXPERIMENTAL_FEATURES"); val == "1" {
rootCmd.AddCommand(newUpgradeCmd())
}
return rootCmd
}
type authArgs struct {
RawAzureEnvironment string
rawSubscriptionID string
SubscriptionID uuid.UUID
AuthMethod string
rawClientID string
ClientID uuid.UUID
ClientSecret string
CertificatePath string
PrivateKeyPath string
language string
}
func addAuthFlags(authArgs *authArgs, f *flag.FlagSet) {
f.StringVar(&authArgs.RawAzureEnvironment, "azure-env", "AzurePublicCloud", "the target Azure cloud")
f.StringVar(&authArgs.rawSubscriptionID, "subscription-id", "", "azure subscription id")
f.StringVar(&authArgs.AuthMethod, "auth-method", "device", "auth method (default:`device`, `client_secret`, `client_certificate`)")
f.StringVar(&authArgs.rawClientID, "client-id", "", "client id (used with --auth-method=[client_secret|client_certificate])")
f.StringVar(&authArgs.ClientSecret, "client-secret", "", "client secret (used with --auth-mode=client_secret)")
f.StringVar(&authArgs.CertificatePath, "certificate-path", "", "path to client certificate (used with --auth-method=client_certificate)")
f.StringVar(&authArgs.PrivateKeyPath, "private-key-path", "", "path to private key (used with --auth-method=client_certificate)")
f.StringVar(&authArgs.language, "language", "en-us", "language to return error messages in")
}
func (authArgs *authArgs) getClient() (*armhelpers.AzureClient, error) {
authArgs.ClientID, _ = uuid.FromString(authArgs.rawClientID)
authArgs.SubscriptionID, _ = uuid.FromString(authArgs.rawSubscriptionID)
if authArgs.AuthMethod == "client_secret" {
if authArgs.ClientID.String() == "" || authArgs.ClientSecret == "" {
log.Fatal(`--client-id and --client-secret must be specified when --auth-method="client_secret"`)
}
// try parse the UUID
} else if authArgs.AuthMethod == "client_certificate" {
if authArgs.ClientID.String() == "" || authArgs.CertificatePath == "" || authArgs.PrivateKeyPath == "" {
log.Fatal(`--client-id and --certificate-path, and --private-key-path must be specified when --auth-method="client_certificate"`)
}
}
if authArgs.SubscriptionID.String() == "" {
log.Fatal("--subscription-id is required (and must be a valid UUID)")
}
env, err := azure.EnvironmentFromName(authArgs.RawAzureEnvironment)
if err != nil {
log.Fatal("failed to parse --azure-env as a valid target Azure cloud environment")
}
var client *armhelpers.AzureClient
switch authArgs.AuthMethod {
case "device":
client, err = armhelpers.NewAzureClientWithDeviceAuth(env, authArgs.SubscriptionID.String())
case "client_secret":
client, err = armhelpers.NewAzureClientWithClientSecret(env, authArgs.SubscriptionID.String(), authArgs.ClientID.String(), authArgs.ClientSecret)
case "client_certificate":
client, err = armhelpers.NewAzureClientWithClientCertificateFile(env, authArgs.SubscriptionID.String(), authArgs.ClientID.String(), authArgs.CertificatePath, authArgs.PrivateKeyPath)
default:
log.Fatalf("--auth-method: ERROR: method unsupported. method=%q.", authArgs.AuthMethod)
return nil, nil // unreachable
}
if err != nil {
return nil, err
}
client.AddAcceptLanguages([]string{authArgs.language})
return client, nil
}