-
Notifications
You must be signed in to change notification settings - Fork 17
/
plugin.go
405 lines (356 loc) · 16.7 KB
/
plugin.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
* Copyright 2016-Present the original author or authors.
*
* 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
*
* https://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 (
"crypto/tls"
"fmt"
"net/http"
"os"
"io"
"code.cloudfoundry.org/cli/plugin"
"github.com/pivotal-cf/spring-cloud-services-cli-plugin/cli"
"github.com/pivotal-cf/spring-cloud-services-cli-plugin/config"
"github.com/pivotal-cf/spring-cloud-services-cli-plugin/eureka"
"github.com/pivotal-cf/spring-cloud-services-cli-plugin/format"
"github.com/pivotal-cf/spring-cloud-services-cli-plugin/httpclient"
"github.com/pivotal-cf/spring-cloud-services-cli-plugin/instance"
"github.com/pivotal-cf/spring-cloud-services-cli-plugin/pluginutil"
"github.com/pivotal-cf/spring-cloud-services-cli-plugin/serviceutil"
)
// Plugin version. Substitute "<major>.<minor>.<build>" at build time, e.g. using -ldflags='-X main.pluginVersion=1.2.3'
var pluginVersion = "invalid version - plugin was not built correctly"
// Plugin is a struct implementing the Plugin interface, defined by the core CLI, which can
// be found in "code.cloudfoundry.org/cli/plugin/plugin.go".
type Plugin struct{}
func (c *Plugin) Run(cliConnection plugin.CliConnection, args []string) {
var cfInstanceIndex *int = nil
var fileToEncrypt string
var positionalArgs []string
var err error
if args[0] == "config-server-encrypt-value" {
// Enable encryption of a value starting with "-".
fileToEncrypt, positionalArgs, err = cli.ParseStringFlags(args)
} else {
cfInstanceIndex, positionalArgs, err = cli.ParseFlags(args)
}
if err != nil {
format.Diagnose(string(err.Error()), os.Stderr, func() {
os.Exit(1)
})
}
skipSslValidation, err := cliConnection.IsSSLDisabled()
if err != nil {
format.Diagnose(string(err.Error()), os.Stderr, func() {
os.Exit(1)
})
}
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: skipSslValidation},
}
client := &http.Client{Transport: tr}
authClient := httpclient.NewAuthenticatedClient(client)
serviceInstanceUrlResolver := serviceutil.NewServiceInstanceUrlResolver(cliConnection, authClient)
operationRunner := instance.NewAuthenticatedOperationRunner(cliConnection, serviceInstanceUrlResolver)
encrypter := config.NewEncrypter(cliConnection, authClient, serviceInstanceUrlResolver)
argsConsumer := cli.NewArgConsumer(positionalArgs, diagnoseWithHelp)
switch args[0] {
case "config-server-encrypt-value":
configServerInstanceName := getConfigServerInstanceName(argsConsumer)
plainText := getPlainText(argsConsumer)
if (plainText == "" && fileToEncrypt == "") || (plainText != "" && fileToEncrypt != "") {
diagnoseWithHelp(fmt.Sprintf("Provide either VALUE_TO_ENCRYPT or the --file-to-encrypt flag, but not both."), "config-server-encrypt-value")
}
runActionQuietly(argsConsumer, cliConnection, func() (string, error) {
if fileToEncrypt != "" {
return encrypter.EncryptFile(configServerInstanceName, fileToEncrypt)
} else {
return encrypter.EncryptString(configServerInstanceName, plainText)
}
})
case "config-server-sync-mirrors":
configServerInstanceName := getConfigServerInstanceName(argsConsumer)
runActionQuietly(argsConsumer, cliConnection, func() (string, error) {
refresher := config.NewRefresher(cliConnection, authClient, serviceInstanceUrlResolver)
return "Successfully refreshed mirrors", refresher.Refresh(configServerInstanceName)
})
case "config-server-add-credhub-secret":
configServerInstanceName := getConfigServerInstanceName(argsConsumer)
configServerCredHubPath := getConfigServerCredHubPath(argsConsumer)
configServerCredHubSecret := getConfigServerCredHubSecret(argsConsumer)
runActionQuietly(argsConsumer, cliConnection, func() (string, error) {
credHubSecret := config.NewCredHubSecret(cliConnection, authClient, serviceInstanceUrlResolver)
return "Successfully added secret", credHubSecret.Add(configServerInstanceName, configServerCredHubPath, configServerCredHubSecret)
})
case "config-server-remove-credhub-secret":
configServerInstanceName := getConfigServerInstanceName(argsConsumer)
configServerCredHubPath := getConfigServerCredHubPath(argsConsumer)
runActionQuietly(argsConsumer, cliConnection, func() (string, error) {
credHubSecret := config.NewCredHubSecret(cliConnection, authClient, serviceInstanceUrlResolver)
return "Successfully removed secret", credHubSecret.Remove(configServerInstanceName, configServerCredHubPath)
})
case "spring-cloud-service-stop":
serviceInstanceName := getServiceInstanceName(argsConsumer)
runAction(argsConsumer, cliConnection, fmt.Sprintf("Stopping service instance %s", format.Bold(format.Cyan(serviceInstanceName))), func(progressWriter io.Writer) (string, error) {
return operationRunner.RunOperation(serviceInstanceName, instance.NewStopOperation(authClient))
})
case "spring-cloud-service-start":
serviceInstanceName := getServiceInstanceName(argsConsumer)
runAction(argsConsumer, cliConnection, fmt.Sprintf("Starting service instance %s", format.Bold(format.Cyan(serviceInstanceName))), func(progressWriter io.Writer) (string, error) {
return operationRunner.RunOperation(serviceInstanceName, instance.NewStartOperation(authClient))
})
case "spring-cloud-service-restart":
serviceInstanceName := getServiceInstanceName(argsConsumer)
runAction(argsConsumer, cliConnection, fmt.Sprintf("Restarting service instance %s", format.Bold(format.Cyan(serviceInstanceName))), func(progressWriter io.Writer) (string, error) {
return operationRunner.RunOperation(serviceInstanceName, instance.NewRestartOperation(authClient))
})
case "spring-cloud-service-restage":
serviceInstanceName := getServiceInstanceName(argsConsumer)
runAction(argsConsumer, cliConnection, fmt.Sprintf("Restaging service instance %s", format.Bold(format.Cyan(serviceInstanceName))), func(progressWriter io.Writer) (string, error) {
return operationRunner.RunOperation(serviceInstanceName, instance.NewRestageOperation(authClient))
})
case "spring-cloud-service-view":
serviceInstanceName := getServiceInstanceName(argsConsumer)
runAction(argsConsumer, cliConnection, fmt.Sprintf("Viewing service instance %s", format.Bold(format.Cyan(serviceInstanceName))), func(progressWriter io.Writer) (string, error) {
return operationRunner.RunOperation(serviceInstanceName, instance.NewViewOperation(authClient))
})
case "spring-cloud-service-configuration":
serviceInstanceName := getServiceInstanceName(argsConsumer)
runActionQuietly(argsConsumer, cliConnection, func() (string, error) {
return operationRunner.RunOperation(serviceInstanceName, instance.NewParametersOperation(authClient))
})
case "service-registry-enable":
serviceRegistryInstanceName := getServiceRegistryInstanceName(argsConsumer)
cfApplicationName := getCfApplicationName(argsConsumer)
runAction(argsConsumer, cliConnection, fmt.Sprintf("Enabling application %s in service registry %s", format.Bold(format.Cyan(cfApplicationName)), format.Bold(format.Cyan(serviceRegistryInstanceName))), func(progressWriter io.Writer) (string, error) {
return eureka.OperateOnApplication(cliConnection, serviceRegistryInstanceName, cfApplicationName, authClient, cfInstanceIndex, progressWriter, serviceInstanceUrlResolver, eureka.Enable)
})
case "service-registry-deregister":
serviceRegistryInstanceName := getServiceRegistryInstanceName(argsConsumer)
cfApplicationName := getCfApplicationName(argsConsumer)
runAction(argsConsumer, cliConnection, fmt.Sprintf("Deregistering application %s from service registry %s", format.Bold(format.Cyan(cfApplicationName)), format.Bold(format.Cyan(serviceRegistryInstanceName))), func(progressWriter io.Writer) (string, error) {
return eureka.OperateOnApplication(cliConnection, serviceRegistryInstanceName, cfApplicationName, authClient, cfInstanceIndex, progressWriter, serviceInstanceUrlResolver, eureka.Deregister)
})
case "service-registry-disable":
serviceRegistryInstanceName := getServiceRegistryInstanceName(argsConsumer)
cfApplicationName := getCfApplicationName(argsConsumer)
runAction(argsConsumer, cliConnection, fmt.Sprintf("Disabling application %s in service registry %s", format.Bold(format.Cyan(cfApplicationName)), format.Bold(format.Cyan(serviceRegistryInstanceName))), func(progressWriter io.Writer) (string, error) {
return eureka.OperateOnApplication(cliConnection, serviceRegistryInstanceName, cfApplicationName, authClient, cfInstanceIndex, progressWriter, serviceInstanceUrlResolver, eureka.Disable)
})
case "service-registry-info":
serviceRegistryInstanceName := getServiceRegistryInstanceName(argsConsumer)
runAction(argsConsumer, cliConnection, fmt.Sprintf("Getting information for service registry %s", format.Bold(format.Cyan(serviceRegistryInstanceName))), func(progressWriter io.Writer) (string, error) {
return eureka.Info(cliConnection, client, serviceRegistryInstanceName, serviceInstanceUrlResolver)
})
case "service-registry-list":
serviceRegistryInstanceName := getServiceRegistryInstanceName(argsConsumer)
runAction(argsConsumer, cliConnection, fmt.Sprintf("Listing service registry %s", format.Bold(format.Cyan(serviceRegistryInstanceName))), func(progressWriter io.Writer) (string, error) {
return eureka.List(cliConnection, serviceRegistryInstanceName, authClient, serviceInstanceUrlResolver)
})
default:
os.Exit(0) // Ignore CLI-MESSAGE-UNINSTALL etc.
}
}
func getCfApplicationName(ac *cli.ArgConsumer) string {
return ac.Consume(2, "cf application name")
}
func getConfigServerInstanceName(ac *cli.ArgConsumer) string {
return ac.Consume(1, "configuration server instance name")
}
func getConfigServerCredHubPath(ac *cli.ArgConsumer) string {
return ac.Consume(2, "configuration server credhub path")
}
func getConfigServerCredHubSecret(ac *cli.ArgConsumer) string {
return ac.Consume(3, "configuration server credhub secret")
}
func getServiceInstanceName(ac *cli.ArgConsumer) string {
return ac.Consume(1, "service instance name")
}
func getPlainText(ac *cli.ArgConsumer) string {
return ac.ConsumeOptional(2, "string to encrypt")
}
func getServiceRegistryInstanceName(ac *cli.ArgConsumer) string {
return ac.Consume(1, "service registry instance name")
}
func runAction(argsConsumer *cli.ArgConsumer, cliConnection plugin.CliConnection, message string, action func(progressWriter io.Writer) (string, error)) {
argsConsumer.CheckAllConsumed()
format.RunAction(cliConnection, message, action, os.Stdout, func() {
os.Exit(1)
})
}
func runActionQuietly(argsConsumer *cli.ArgConsumer, cliConnection plugin.CliConnection, action func() (string, error)) {
argsConsumer.CheckAllConsumed()
format.RunActionQuietly(cliConnection, action, os.Stdout, func() {
os.Exit(1)
})
}
func diagnoseWithHelp(message string, command string) {
fmt.Printf("%s See 'cf help %s'.\n", message, command)
os.Exit(1)
}
func failInstallation(format string, inserts ...interface{}) {
// There is currently no way to emit the message to the command line during plugin installation. Standard output and error are swallowed.
fmt.Printf(format, inserts...)
fmt.Println("")
// Fail the installation
os.Exit(64)
}
func (c *Plugin) GetMetadata() plugin.PluginMetadata {
return plugin.PluginMetadata{
Name: "spring-cloud-services",
Version: pluginutil.ParsePluginVersion(pluginVersion, failInstallation),
MinCliVersion: plugin.VersionType{
Major: 6,
Minor: 7,
Build: 0,
},
Commands: []plugin.Command{
{
Name: "config-server-encrypt-value",
HelpText: "Encrypt a string using a Spring Cloud Services configuration server",
Alias: "csev",
UsageDetails: plugin.Usage{
Usage: ` cf config-server-encrypt-value CONFIG_SERVER_INSTANCE_NAME [VALUE_TO_ENCRYPT]
NOTE: Either VALUE_TO_ENCRYPT or --file-to-encrypt flag is required, but not both.`,
Options: map[string]string{"-f/--file-to-encrypt": cli.FileNameUsage},
},
},
{
Name: "config-server-sync-mirrors",
HelpText: "Synchronize Git mirrors associated with given Spring Cloud Services configuration server",
Alias: "cssm",
UsageDetails: plugin.Usage{
Usage: ` cf config-server-sync-mirrors CONFIG_SERVER_INSTANCE_NAME`,
},
},
{
Name: "config-server-add-credhub-secret",
HelpText: "Add secret in JSON format to the given path",
Alias: "cs-add",
UsageDetails: plugin.Usage{
Usage: ` cf config-server-add-credhub-secret CONFIG_SERVER_INSTANCE_NAME CREDHUB_PATH JSON_SECRET`,
},
},
{
Name: "config-server-remove-credhub-secret",
HelpText: "Remove secrets from the given path",
Alias: "cs-remove",
UsageDetails: plugin.Usage{
Usage: ` cf config-server-remove-credhub-secret CONFIG_SERVER_INSTANCE_NAME CREDHUB_PATH`,
},
},
{
Name: "spring-cloud-service-stop",
HelpText: "Stop a Spring Cloud Services service instance",
Alias: "scs-stop",
UsageDetails: plugin.Usage{
Usage: " cf scs-stop SERVICE_INSTANCE_NAME",
},
},
{
Name: "spring-cloud-service-start",
HelpText: "Start a Spring Cloud Services service instance",
Alias: "scs-start",
UsageDetails: plugin.Usage{
Usage: " cf scs-start SERVICE_INSTANCE_NAME",
},
},
{
Name: "spring-cloud-service-restart",
HelpText: "Restart a Spring Cloud Services service instance",
Alias: "scs-restart",
UsageDetails: plugin.Usage{
Usage: " cf scs-restart SERVICE_INSTANCE_NAME",
},
},
{
Name: "spring-cloud-service-restage",
HelpText: "Restage a Spring Cloud Services service instance",
Alias: "scs-restage",
UsageDetails: plugin.Usage{
Usage: " cf scs-restage SERVICE_INSTANCE_NAME",
},
},
{
Name: "spring-cloud-service-view",
HelpText: "Display health and status for a Spring Cloud Services service instance",
Alias: "scs-view",
UsageDetails: plugin.Usage{
Usage: " cf scs-view SERVICE_INSTANCE_NAME",
},
},
{
Name: "spring-cloud-service-configuration",
HelpText: "Display configuration parameters for a Spring Cloud Services service instance",
Alias: "scs-config",
UsageDetails: plugin.Usage{
Usage: " cf scs-config SERVICE_INSTANCE_NAME",
},
},
{
Name: "service-registry-deregister",
HelpText: "Deregister an application registered with a Spring Cloud Services service registry",
Alias: "srdr",
UsageDetails: plugin.Usage{
Usage: " cf service-registry-deregister SERVICE_REGISTRY_INSTANCE_NAME CF_APPLICATION_NAME",
Options: map[string]string{"-i/--cf-instance-index": cli.CfInstanceIndexUsage},
},
},
{
Name: "service-registry-disable",
HelpText: "Disable an application registered with a Spring Cloud Services service registry so that it is unavailable for traffic",
Alias: "srda",
UsageDetails: plugin.Usage{
Usage: " cf service-registry-disable SERVICE_REGISTRY_INSTANCE_NAME CF_APPLICATION_NAME",
Options: map[string]string{"-i/--cf-instance-index": cli.CfInstanceIndexUsage},
},
},
{
Name: "service-registry-enable",
HelpText: "Enable an application registered with a Spring Cloud Services service registry so that it is available for traffic",
Alias: "sren",
UsageDetails: plugin.Usage{
Usage: " cf service-registry-enable SERVICE_REGISTRY_INSTANCE_NAME CF_APPLICATION_NAME",
Options: map[string]string{"-i/--cf-instance-index": cli.CfInstanceIndexUsage},
},
},
{
Name: "service-registry-info",
HelpText: "Display Spring Cloud Services service registry instance information",
Alias: "sri",
UsageDetails: plugin.Usage{
Usage: " cf service-registry-info SERVICE_REGISTRY_INSTANCE_NAME",
},
},
{
Name: "service-registry-list",
HelpText: "Display all applications registered with a Spring Cloud Services service registry",
Alias: "srl",
UsageDetails: plugin.Usage{
Usage: " cf service-registry-list SERVICE_REGISTRY_INSTANCE_NAME",
},
},
},
}
}
func main() {
if len(os.Args) == 1 {
fmt.Println("This program is a plugin which expects to be installed into the cf CLI. It is not intended to be run stand-alone.")
pv := pluginutil.ParsePluginVersion(pluginVersion, failInstallation)
fmt.Printf("Plugin version: %d.%d.%d\n", pv.Major, pv.Minor, pv.Build)
os.Exit(0)
}
plugin.Start(new(Plugin))
}