forked from pivotal-cf/om
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
330 lines (276 loc) · 15.1 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package main
import (
"fmt"
"log"
"net/http"
"os"
"regexp"
"strings"
"github.com/pivotal-cf/om/interpolate"
"time"
"github.com/olekukonko/tablewriter"
"github.com/pivotal-cf/om/renderers"
"github.com/pivotal/uilive"
"gopkg.in/yaml.v2"
"github.com/pivotal-cf/jhanda"
"github.com/pivotal-cf/om/api"
"github.com/pivotal-cf/om/commands"
"github.com/pivotal-cf/om/extractor"
"github.com/pivotal-cf/om/formcontent"
"github.com/pivotal-cf/om/network"
"github.com/pivotal-cf/om/presenters"
"github.com/pivotal-cf/om/progress"
_ "github.com/pivotal-cf/om/download_clients"
)
var version = "unknown"
var applySleepDurationString = "10s"
type httpClient interface {
Do(*http.Request) (*http.Response, error)
}
type options struct {
CACert string `yaml:"ca-cert" long:"ca-cert" env:"OM_CA_CERT" description:"OpsManager CA certificate path or value"`
ClientID string `yaml:"client-id" short:"c" long:"client-id" env:"OM_CLIENT_ID" description:"Client ID for the Ops Manager VM (not required for unauthenticated commands)"`
ClientSecret string `yaml:"client-secret" short:"s" long:"client-secret" env:"OM_CLIENT_SECRET" description:"Client Secret for the Ops Manager VM (not required for unauthenticated commands)"`
ConnectTimeout int `yaml:"connect-timeout" short:"o" long:"connect-timeout" env:"OM_CONNECT_TIMEOUT" default:"10" description:"timeout in seconds to make TCP connections"`
DecryptionPassphrase string `yaml:"decryption-passphrase" short:"d" long:"decryption-passphrase" env:"OM_DECRYPTION_PASSPHRASE" description:"Passphrase to decrypt the installation if the Ops Manager VM has been rebooted (optional for most commands)"`
Env string ` short:"e" long:"env" description:"env file with login credentials"`
Help bool ` short:"h" long:"help" default:"false" description:"prints this usage information"`
Password string `yaml:"password" short:"p" long:"password" env:"OM_PASSWORD" description:"admin password for the Ops Manager VM (not required for unauthenticated commands)"`
RequestTimeout int `yaml:"request-timeout" short:"r" long:"request-timeout" env:"OM_REQUEST_TIMEOUT" default:"1800" description:"timeout in seconds for HTTP requests to Ops Manager"`
SkipSSLValidation bool `yaml:"skip-ssl-validation" short:"k" long:"skip-ssl-validation" env:"OM_SKIP_SSL_VALIDATION" default:"false" description:"skip ssl certificate validation during http requests"`
Target string `yaml:"target" short:"t" long:"target" env:"OM_TARGET" description:"location of the Ops Manager VM"`
Trace bool `yaml:"trace" short:"tr" long:"trace" env:"OM_TRACE" description:"prints HTTP requests and response payloads"`
Username string `yaml:"username" short:"u" long:"username" env:"OM_USERNAME" description:"admin username for the Ops Manager VM (not required for unauthenticated commands)"`
VarsEnv string ` env:"OM_VARS_ENV" experimental:"true" description:"load vars from environment variables by specifying a prefix (e.g.: 'MY' to load MY_var=value)"`
Version bool ` short:"v" long:"version" default:"false" description:"prints the om release version"`
}
func main() {
applySleepDuration, _ := time.ParseDuration(applySleepDurationString)
stdout := log.New(os.Stdout, "", 0)
stderr := log.New(os.Stderr, "", 0)
var global options
args, err := jhanda.Parse(&global, os.Args[1:])
if err != nil {
stderr.Fatal(err)
}
err = setEnvFileProperties(&global)
if err != nil {
stderr.Fatal(err)
}
globalFlagsUsage, err := jhanda.PrintUsage(global)
if err != nil {
stderr.Fatal(err)
}
var command string
if len(args) > 0 {
command, args = args[0], args[1:]
}
if global.Version {
command = "version"
}
if global.Help {
command = "help"
}
if command == "" {
command = "help"
}
requestTimeout := time.Duration(global.RequestTimeout) * time.Second
connectTimeout := time.Duration(global.ConnectTimeout) * time.Second
var unauthenticatedClient, authedClient, authedCookieClient, unauthenticatedProgressClient, authedProgressClient httpClient
unauthenticatedClient, err = network.NewUnauthenticatedClient(global.Target, global.SkipSSLValidation, global.CACert, connectTimeout, requestTimeout)
if err != nil {
stderr.Fatal(err)
}
authedClient, err = network.NewOAuthClient(global.Target, global.Username, global.Password, global.ClientID, global.ClientSecret, global.SkipSSLValidation, global.CACert, connectTimeout, requestTimeout)
if err != nil {
stderr.Fatal(err)
}
if global.DecryptionPassphrase != "" {
authedClient = network.NewDecryptClient(authedClient, unauthenticatedClient, global.DecryptionPassphrase, os.Stderr)
}
authedCookieClient, err = network.NewOAuthClient(global.Target, global.Username, global.Password, global.ClientID, global.ClientSecret, global.SkipSSLValidation, "", connectTimeout, requestTimeout)
if err != nil {
stderr.Fatal(err)
}
liveWriter := uilive.New()
liveWriter.Out = os.Stderr
unauthenticatedProgressClient = network.NewProgressClient(unauthenticatedClient, progress.NewBar(), liveWriter)
authedProgressClient = network.NewProgressClient(authedClient, progress.NewBar(), liveWriter)
if global.Trace {
unauthenticatedClient = network.NewTraceClient(unauthenticatedClient, os.Stderr)
unauthenticatedProgressClient = network.NewTraceClient(unauthenticatedProgressClient, os.Stderr)
authedClient = network.NewTraceClient(authedClient, os.Stderr)
authedCookieClient = network.NewTraceClient(authedCookieClient, os.Stderr)
authedProgressClient = network.NewTraceClient(authedProgressClient, os.Stderr)
}
api := api.New(api.ApiInput{
Client: authedClient,
UnauthedClient: unauthenticatedClient,
ProgressClient: authedProgressClient,
UnauthedProgressClient: unauthenticatedProgressClient,
Logger: stderr,
})
logWriter := commands.NewLogWriter(os.Stdout)
tableWriter := tablewriter.NewWriter(os.Stdout)
form := formcontent.NewForm()
metadataExtractor := extractor.MetadataExtractor{}
presenter := presenters.NewPresenter(presenters.NewTablePresenter(tableWriter), presenters.NewJSONPresenter(os.Stdout))
envRendererFactory := renderers.NewFactory(renderers.NewEnvGetter())
commandSet := jhanda.CommandSet{}
commandSet["activate-certificate-authority"] = commands.NewActivateCertificateAuthority(api, stdout)
commandSet["apply-changes"] = commands.NewApplyChanges(api, api, logWriter, stdout, applySleepDuration)
commandSet["assign-multi-stemcell"] = commands.NewAssignMultiStemcell(api, stdout)
commandSet["assign-stemcell"] = commands.NewAssignStemcell(api, stdout)
commandSet["available-products"] = commands.NewAvailableProducts(api, presenter, stdout)
commandSet["bosh-diff"] = commands.NewBoshDiff(api, stdout)
commandSet["bosh-env"] = commands.NewBoshEnvironment(api, stdout, global.Target, envRendererFactory)
commandSet["certificate-authorities"] = commands.NewCertificateAuthorities(api, presenter)
commandSet["certificate-authority"] = commands.NewCertificateAuthority(api, presenter, stdout)
commandSet["config-template"] = commands.NewConfigTemplate(commands.DefaultProvider())
commandSet["configure-authentication"] = commands.NewConfigureAuthentication(os.Environ, api, stdout)
commandSet["configure-director"] = commands.NewConfigureDirector(os.Environ, api, stdout)
commandSet["configure-ldap-authentication"] = commands.NewConfigureLDAPAuthentication(os.Environ, api, stdout)
commandSet["configure-opsman"] = commands.NewConfigureOpsman(os.Environ, api, stderr)
commandSet["configure-product"] = commands.NewConfigureProduct(os.Environ, api, global.Target, stdout)
commandSet["configure-saml-authentication"] = commands.NewConfigureSAMLAuthentication(os.Environ, api, stdout)
commandSet["create-certificate-authority"] = commands.NewCreateCertificateAuthority(api, presenter)
commandSet["create-vm-extension"] = commands.NewCreateVMExtension(os.Environ, api, stdout)
commandSet["credential-references"] = commands.NewCredentialReferences(api, presenter, stdout)
commandSet["credentials"] = commands.NewCredentials(api, presenter, stdout)
commandSet["curl"] = commands.NewCurl(api, stdout, stderr)
commandSet["delete-certificate-authority"] = commands.NewDeleteCertificateAuthority(api, stdout)
commandSet["delete-installation"] = commands.NewDeleteInstallation(api, logWriter, stdout, os.Stdin, applySleepDuration)
commandSet["delete-product"] = commands.NewDeleteProduct(api)
commandSet["delete-ssl-certificate"] = commands.NewDeleteSSLCertificate(api, stdout)
commandSet["delete-unused-products"] = commands.NewDeleteUnusedProducts(api, stdout)
commandSet["deployed-manifest"] = commands.NewDeployedManifest(api, stdout)
commandSet["deployed-products"] = commands.NewDeployedProducts(presenter, api)
commandSet["diagnostic-report"] = commands.NewDiagnosticReport(presenter, api)
commandSet["disable-director-verifiers"] = commands.NewDisableDirectorVerifiers(presenter, api, stdout)
commandSet["disable-product-verifiers"] = commands.NewDisableProductVerifiers(presenter, api, stdout)
commandSet["download-product"] = commands.NewDownloadProduct(os.Environ, stdout, stderr, os.Stderr)
commandSet["errands"] = commands.NewErrands(presenter, api)
commandSet["expiring-certificates"] = commands.NewExpiringCertificates(api, stdout)
commandSet["export-installation"] = commands.NewExportInstallation(api, stderr)
commandSet["generate-certificate"] = commands.NewGenerateCertificate(api, stdout)
commandSet["generate-certificate-authority"] = commands.NewGenerateCertificateAuthority(api, presenter)
commandSet["help"] = commands.NewHelp(os.Stdout, globalFlagsUsage, commandSet)
commandSet["import-installation"] = commands.NewImportInstallation(form, api, global.DecryptionPassphrase, stdout)
commandSet["installation-log"] = commands.NewInstallationLog(api, stdout)
commandSet["installations"] = commands.NewInstallations(api, presenter)
commandSet["interpolate"] = commands.NewInterpolate(os.Environ, stdout, os.Stdin)
commandSet["pending-changes"] = commands.NewPendingChanges(presenter, api)
commandSet["pre-deploy-check"] = commands.NewPreDeployCheck(presenter, api, stdout)
commandSet["product-metadata"] = commands.NewProductMetadata(stdout)
commandSet["regenerate-certificates"] = commands.NewRegenerateCertificates(api, stdout)
commandSet["revert-staged-changes"] = commands.NewRevertStagedChanges(api, stdout)
commandSet["ssl-certificate"] = commands.NewSSLCertificate(api, presenter)
commandSet["stage-product"] = commands.NewStageProduct(api, stdout)
commandSet["staged-config"] = commands.NewStagedConfig(api, stdout)
commandSet["staged-director-config"] = commands.NewStagedDirectorConfig(api, stdout, stderr)
commandSet["staged-manifest"] = commands.NewStagedManifest(api, stdout)
commandSet["staged-products"] = commands.NewStagedProducts(presenter, api)
commandSet["tile-metadata"] = commands.NewDeprecatedProductMetadata(stdout)
commandSet["unstage-product"] = commands.NewUnstageProduct(api, stdout)
commandSet["update-ssl-certificate"] = commands.NewUpdateSSLCertificate(os.Environ, api, stdout)
commandSet["upload-product"] = commands.NewUploadProduct(form, metadataExtractor, api, stdout)
commandSet["upload-stemcell"] = commands.NewUploadStemcell(form, api, stdout)
commandSet["version"] = commands.NewVersion(version, os.Stdout)
err = commandSet.Execute(command, args)
if err != nil {
stderr.Fatal(err)
}
}
func setEnvFileProperties(global *options) error {
if global.Env == "" {
return nil
}
var opts options
_, err := os.Open(global.Env)
if err != nil {
return fmt.Errorf("env file does not exist: %s", err)
}
interpolateOptions := interpolate.Options{
TemplateFile: global.Env,
EnvironFunc: os.Environ,
ExpectAllKeys: false,
}
if global.VarsEnv != "" {
interpolateOptions.VarsEnvs = []string{global.VarsEnv}
}
contents, err := interpolate.Execute(interpolateOptions)
if err != nil {
return err
}
err = yaml.UnmarshalStrict(contents, &opts)
if err != nil {
return fmt.Errorf("could not parse env file: %s", err)
}
if global.ClientID == "" {
global.ClientID = opts.ClientID
}
if global.ClientSecret == "" {
global.ClientSecret = opts.ClientSecret
}
if global.Password == "" {
global.Password = opts.Password
}
if global.ConnectTimeout == 10 && opts.ConnectTimeout != 0 {
global.ConnectTimeout = opts.ConnectTimeout
}
if global.RequestTimeout == 1800 && opts.RequestTimeout != 0 {
global.RequestTimeout = opts.RequestTimeout
}
if global.SkipSSLValidation == false {
global.SkipSSLValidation = opts.SkipSSLValidation
}
if global.Target == "" {
global.Target = opts.Target
}
if global.Trace == false {
global.Trace = opts.Trace
}
if global.Username == "" {
global.Username = opts.Username
}
if global.DecryptionPassphrase == "" {
global.DecryptionPassphrase = opts.DecryptionPassphrase
}
if global.CACert == "" {
global.CACert = opts.CACert
}
err = checkForVars(global)
if err != nil {
return fmt.Errorf("found problem in --env file: %s", err)
}
return nil
}
func checkForVars(opts *options) error {
var errBuffer []string
interpolateRegex := regexp.MustCompile(`\(\(.*\)\)`)
if interpolateRegex.MatchString(opts.DecryptionPassphrase) {
errBuffer = append(errBuffer, "* use OM_DECRYPTION_PASSPHRASE environment variable for the decryption-passphrase value")
}
if interpolateRegex.MatchString(opts.ClientID) {
errBuffer = append(errBuffer, "* use OM_CLIENT_ID environment variable for the client-id value")
}
if interpolateRegex.MatchString(opts.ClientSecret) {
errBuffer = append(errBuffer, "* use OM_CLIENT_SECRET environment variable for the client-secret value")
}
if interpolateRegex.MatchString(opts.Password) {
errBuffer = append(errBuffer, "* use OM_PASSWORD environment variable for the password value")
}
if interpolateRegex.MatchString(opts.Target) {
errBuffer = append(errBuffer, "* use OM_TARGET environment variable for the target value")
}
if interpolateRegex.MatchString(opts.Username) {
errBuffer = append(errBuffer, "* use OM_USERNAME environment variable for the username value")
}
if len(errBuffer) > 0 {
errBuffer = append([]string{"env file contains YAML placeholders. Pleases provide them via interpolation or environment variables."}, errBuffer...)
errBuffer = append(errBuffer, "Or, to enable interpolation of env.yml with variables from env-vars,")
errBuffer = append(errBuffer, "set the OM_VARS_ENV env var and put export the needed vars.")
return fmt.Errorf(strings.Join(errBuffer, "\n"))
}
return nil
}