This repository has been archived by the owner on Mar 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathplugin.go
229 lines (194 loc) · 7.78 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
/*
* Copyright 2017-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
*
* 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 (
"crypto/tls"
"fmt"
"hash"
"net/http"
"os"
"os/exec"
"io"
"code.cloudfoundry.org/cli/plugin"
"github.com/pivotal-cf/spring-cloud-dataflow-for-pcf-cli-plugin/cfutil"
"github.com/pivotal-cf/spring-cloud-dataflow-for-pcf-cli-plugin/cli"
"github.com/pivotal-cf/spring-cloud-dataflow-for-pcf-cli-plugin/dataflow"
"github.com/pivotal-cf/spring-cloud-dataflow-for-pcf-cli-plugin/download"
"github.com/pivotal-cf/spring-cloud-dataflow-for-pcf-cli-plugin/download/cache"
"github.com/pivotal-cf/spring-cloud-dataflow-for-pcf-cli-plugin/format"
"github.com/pivotal-cf/spring-cloud-dataflow-for-pcf-cli-plugin/httpclient"
"github.com/pivotal-cf/spring-cloud-dataflow-for-pcf-cli-plugin/java"
"github.com/pivotal-cf/spring-cloud-dataflow-for-pcf-cli-plugin/pluginutil"
"github.com/pivotal-cf/spring-cloud-dataflow-for-pcf-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) {
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},
Proxy: http.ProxyFromEnvironment,
}
client := &http.Client{
Transport: tr,
CheckRedirect: func(*http.Request, []*http.Request) error {
return http.ErrUseLastResponse // avoid following redirects
},
}
authClient := httpclient.NewAuthenticatedClient(client)
argsConsumer := cli.NewArgConsumer(args, diagnoseWithHelp)
switch args[0] {
case "dataflow-shell":
dataflowSIName := getDataflowServerInstanceName(argsConsumer)
runAction(argsConsumer, cliConnection, fmt.Sprintf("Attaching shell to dataflow service %s", format.Bold(format.Cyan(dataflowSIName))), func(progressWriter io.Writer) (string, error) {
argsConsumer.CheckAllConsumed()
accessToken, err := cfutil.GetToken(cliConnection)
if err != nil {
return "", err
}
dataflowServer, err := serviceutil.ServiceInstanceURL(cliConnection, dataflowSIName, accessToken, authClient)
if err != nil {
return "", err
}
return "", downloadAndRunShell("dataflow", func() (string, string, hash.Hash, error) {
return dataflow.DataflowShellDownloadUrl(dataflowServer, authClient, accessToken)
}, func(fileName string) *exec.Cmd {
return dataflow.DataflowShellCommand(fileName, dataflowServer, skipSslValidation)
}, progressWriter)
})
// case "skipper-shell":
// skipperSIName := getSkipperServerInstanceName(argsConsumer)
// runAction(argsConsumer, cliConnection, fmt.Sprintf("Attaching Skipper shell to Skipper service %s", format.Bold(format.Cyan(skipperSIName))), func(progressWriter io.Writer) (string, error) {
// argsConsumer.CheckAllConsumed()
// accessToken, err := cfutil.GetToken(cliConnection)
// if err != nil {
// return "", err
// }
// skipperServer, err := serviceutil.ServiceInstanceURL(cliConnection, skipperSIName, accessToken, authClient)
// if err != nil {
// return "", err
// }
// return "", downloadAndRunShell("Skipper", func() (string, string, hash.Hash, error) {
// return skipper.SkipperShellDownloadUrl(skipperServer, authClient, accessToken)
// }, func(fileName string) *exec.Cmd {
// return skipper.SkipperShellCommand(fileName, skipperServer, skipSslValidation)
// }, progressWriter)
// })
default:
os.Exit(0) // Ignore CLI-MESSAGE-UNINSTALL etc.
}
}
type urlResolver func() (string, string, hash.Hash, error)
type shellCommandFactory func(fileName string) *exec.Cmd
func downloadAndRunShell(shellType string, shellDownloadUrl urlResolver, shellCommand shellCommandFactory, progressWriter io.Writer) error {
url, checksum, hashFunc, err := shellDownloadUrl()
if err != nil {
return err
}
downloadCache, err := cache.NewCache(progressWriter)
if err != nil {
return err
}
httpHelper := download.NewHttpHelper()
downloader, err := download.NewDownloader(downloadCache, httpHelper, progressWriter)
if err != nil {
return err
}
filePath, err := downloader.DownloadFile(url, checksum, hashFunc)
if err != nil {
return err
}
fmt.Fprintf(progressWriter, "Launching %s shell JAR\n", shellType)
err = java.RunShell(shellCommand(filePath))
if err != nil {
fmt.Fprintf(progressWriter, "Launching %s shell JAR failed. Checking Java installation\n", shellType)
checkErr := java.Check(progressWriter, err)
if checkErr != nil {
return fmt.Errorf("Java is needed. Please install a JRE or JDK and try again. Details: %s", checkErr.Error())
}
fmt.Fprintf(progressWriter, "Java installation appears to be ok. Any error messages above may indicate why launching %s shell JAR failed.\n", shellType)
}
return err
}
func getDataflowServerInstanceName(ac *cli.ArgConsumer) string {
return ac.Consume(1, "dataflow server service instance name")
}
func getSkipperServerInstanceName(ac *cli.ArgConsumer) string {
return ac.Consume(1, "Skipper server service instance name")
}
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-dataflow-for-pcf-cli-plugin",
Version: pluginutil.ParsePluginVersion(pluginVersion, failInstallation),
MinCliVersion: plugin.VersionType{
Major: 6,
Minor: 7,
Build: 0,
},
Commands: []plugin.Command{
{
Name: "dataflow-shell",
HelpText: "Open a dataflow shell to a Spring Cloud Dataflow for PCF dataflow server",
Alias: "dfsh",
UsageDetails: plugin.Usage{
Usage: " cf dataflow-shell DATAFLOW_SERVER_SERVICE_INSTANCE_NAME",
},
},
// {
// Name: "skipper-shell",
// HelpText: "Open a Skipper shell to a Spring Cloud Dataflow for PCF Skipper server",
// Alias: "sksh",
// UsageDetails: plugin.Usage{
// Usage: " cf skipper-shell SKIPPER_SERVER_SERVICE_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))
}
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)
})
}