forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install_plugin.go
320 lines (275 loc) · 10.3 KB
/
install_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
package plugin
import (
"errors"
"fmt"
"net/rpc"
"os"
"os/exec"
"path/filepath"
"code.cloudfoundry.org/cli/cf/actors/plugininstaller"
"code.cloudfoundry.org/cli/cf/actors/pluginrepo"
"code.cloudfoundry.org/cli/cf/commandregistry"
"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
"code.cloudfoundry.org/cli/cf/configuration/pluginconfig"
"code.cloudfoundry.org/cli/cf/flags"
. "code.cloudfoundry.org/cli/cf/i18n"
"code.cloudfoundry.org/cli/cf/requirements"
"code.cloudfoundry.org/cli/cf/terminal"
"code.cloudfoundry.org/cli/plugin"
"code.cloudfoundry.org/cli/util"
"code.cloudfoundry.org/cli/util/downloader"
"code.cloudfoundry.org/gofileutils/fileutils"
pluginRPCService "code.cloudfoundry.org/cli/plugin/rpc"
)
type PluginInstall struct {
ui terminal.UI
config coreconfig.Reader
pluginConfig pluginconfig.PluginConfiguration
pluginRepo pluginrepo.PluginRepo
checksum util.Sha1Checksum
rpcService *pluginRPCService.CliRpcService
}
func init() {
commandregistry.Register(&PluginInstall{})
}
func (cmd *PluginInstall) MetaData() commandregistry.CommandMetadata {
fs := make(map[string]flags.FlagSet)
fs["r"] = &flags.StringFlag{ShortName: "r", Usage: T("Name of a registered repository where the specified plugin is located")}
fs["f"] = &flags.BoolFlag{ShortName: "f", Usage: T("Force install of plugin without confirmation")}
return commandregistry.CommandMetadata{
Name: "install-plugin",
Description: T("Install CLI plugin"),
Usage: []string{
T(`CF_NAME install-plugin (LOCAL-PATH/TO/PLUGIN | URL | -r REPO_NAME PLUGIN_NAME) [-f]
Prompts for confirmation unless '-f' is provided.`),
},
Examples: []string{
"CF_NAME install-plugin ~/Downloads/plugin-foobar",
"CF_NAME install-plugin https://example.com/plugin-foobar_linux_amd64",
"CF_NAME install-plugin -r My-Repo plugin-echo",
},
Flags: fs,
TotalArgs: 1,
}
}
func (cmd *PluginInstall) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
if len(fc.Args()) != 1 {
cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + commandregistry.Commands.CommandUsage("install-plugin"))
return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 1)
}
reqs := []requirements.Requirement{}
return reqs, nil
}
func (cmd *PluginInstall) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
cmd.ui = deps.UI
cmd.config = deps.Config
cmd.pluginConfig = deps.PluginConfig
cmd.pluginRepo = deps.PluginRepo
cmd.checksum = deps.ChecksumUtil
//reset rpc registration in case there is other running instance,
//each service can only be registered once
server := rpc.NewServer()
rpcService, err := pluginRPCService.NewRpcService(deps.TeePrinter, deps.TeePrinter, deps.Config, deps.RepoLocator, pluginRPCService.NewCommandRunner(), deps.Logger, cmd.ui.Writer(), server)
if err != nil {
cmd.ui.Failed("Error initializing RPC service: " + err.Error())
}
cmd.rpcService = rpcService
return cmd
}
func (cmd *PluginInstall) Execute(c flags.FlagContext) error {
if !cmd.confirmWithUser(
c,
T("**Attention: Plugins are binaries written by potentially untrusted authors. Install and use plugins at your own risk.**\n\nDo you want to install the plugin {{.Plugin}}?",
map[string]interface{}{
"Plugin": c.Args()[0],
}),
) {
return errors.New(T("Plugin installation cancelled"))
}
fileDownloader := downloader.NewDownloader(os.TempDir())
removeTmpFile := func() {
err := fileDownloader.RemoveFile()
if err != nil {
cmd.ui.Say(T("Problem removing downloaded binary in temp directory: ") + err.Error())
}
}
defer removeTmpFile()
deps := &plugininstaller.Context{
Checksummer: cmd.checksum,
GetPluginRepos: cmd.config.PluginRepos,
FileDownloader: fileDownloader,
PluginRepo: cmd.pluginRepo,
RepoName: c.String("r"),
UI: cmd.ui,
}
installer := plugininstaller.NewPluginInstaller(deps)
pluginSourceFilepath := installer.Install(c.Args()[0])
_, pluginExecutableName := filepath.Split(pluginSourceFilepath)
cmd.ui.Say(T(
"Installing plugin {{.PluginPath}}...",
map[string]interface{}{
"PluginPath": pluginExecutableName,
}),
)
pluginDestinationFilepath := filepath.Join(cmd.pluginConfig.GetPluginPath(), pluginExecutableName)
err := cmd.ensurePluginBinaryWithSameFileNameDoesNotAlreadyExist(pluginDestinationFilepath, pluginExecutableName)
if err != nil {
return err
}
pluginMetadata, err := cmd.runBinaryAndObtainPluginMetadata(pluginSourceFilepath)
if err != nil {
return err
}
err = cmd.ensurePluginIsSafeForInstallation(pluginMetadata, pluginDestinationFilepath, pluginSourceFilepath)
if err != nil {
return err
}
err = cmd.installPlugin(pluginMetadata, pluginDestinationFilepath, pluginSourceFilepath)
if err != nil {
return err
}
cmd.ui.Ok()
cmd.ui.Say(T(
"Plugin {{.PluginName}} v{{.Version}} successfully installed.",
map[string]interface{}{
"PluginName": pluginMetadata.Name,
"Version": fmt.Sprintf("%d.%d.%d", pluginMetadata.Version.Major, pluginMetadata.Version.Minor, pluginMetadata.Version.Build),
}),
)
return nil
}
func (cmd *PluginInstall) confirmWithUser(c flags.FlagContext, prompt string) bool {
return c.Bool("f") || cmd.ui.Confirm(prompt)
}
func (cmd *PluginInstall) ensurePluginBinaryWithSameFileNameDoesNotAlreadyExist(pluginDestinationFilepath, pluginExecutableName string) error {
_, err := os.Stat(pluginDestinationFilepath)
if err == nil || os.IsExist(err) {
return errors.New(T(
"The file {{.PluginExecutableName}} already exists under the plugin directory.\n",
map[string]interface{}{
"PluginExecutableName": pluginExecutableName,
}),
)
} else if !os.IsNotExist(err) {
return errors.New(T(
"Unexpected error has occurred:\n{{.Error}}",
map[string]interface{}{
"Error": err.Error(),
}),
)
}
return nil
}
func (cmd *PluginInstall) ensurePluginIsSafeForInstallation(pluginMetadata *plugin.PluginMetadata, pluginDestinationFilepath string, pluginSourceFilepath string) error {
plugins := cmd.pluginConfig.Plugins()
if pluginMetadata.Name == "" {
return errors.New(T(
"Unable to obtain plugin name for executable {{.Executable}}",
map[string]interface{}{
"Executable": pluginSourceFilepath,
}),
)
}
if _, ok := plugins[pluginMetadata.Name]; ok {
return errors.New(T(
"Plugin name {{.PluginName}} is already taken",
map[string]interface{}{
"PluginName": pluginMetadata.Name,
}),
)
}
if pluginMetadata.Commands == nil {
return errors.New(T(
"Error getting command list from plugin {{.FilePath}}",
map[string]interface{}{
"FilePath": pluginSourceFilepath,
}),
)
}
for _, pluginCmd := range pluginMetadata.Commands {
//check for command conflicting core commands/alias
if pluginCmd.Name == "help" || commandregistry.Commands.CommandExists(pluginCmd.Name) {
return errors.New(T(
"Command `{{.Command}}` in the plugin being installed is a native CF command/alias. Rename the `{{.Command}}` command in the plugin being installed in order to enable its installation and use.",
map[string]interface{}{
"Command": pluginCmd.Name,
}),
)
}
//check for alias conflicting core command/alias
if pluginCmd.Alias == "help" || commandregistry.Commands.CommandExists(pluginCmd.Alias) {
return errors.New(T(
"Alias `{{.Command}}` in the plugin being installed is a native CF command/alias. Rename the `{{.Command}}` command in the plugin being installed in order to enable its installation and use.",
map[string]interface{}{
"Command": pluginCmd.Alias,
}),
)
}
for installedPluginName, installedPlugin := range plugins {
for _, installedPluginCmd := range installedPlugin.Commands {
//check for command conflicting other plugin commands/alias
if installedPluginCmd.Name == pluginCmd.Name || installedPluginCmd.Alias == pluginCmd.Name {
return errors.New(T(
"Command `{{.Command}}` is a command/alias in plugin '{{.PluginName}}'. You could try uninstalling plugin '{{.PluginName}}' and then install this plugin in order to invoke the `{{.Command}}` command. However, you should first fully understand the impact of uninstalling the existing '{{.PluginName}}' plugin.",
map[string]interface{}{
"Command": pluginCmd.Name,
"PluginName": installedPluginName,
}),
)
}
//check for alias conflicting other plugin commands/alias
if pluginCmd.Alias != "" && (installedPluginCmd.Name == pluginCmd.Alias || installedPluginCmd.Alias == pluginCmd.Alias) {
return errors.New(T(
"Alias `{{.Command}}` is a command/alias in plugin '{{.PluginName}}'. You could try uninstalling plugin '{{.PluginName}}' and then install this plugin in order to invoke the `{{.Command}}` command. However, you should first fully understand the impact of uninstalling the existing '{{.PluginName}}' plugin.",
map[string]interface{}{
"Command": pluginCmd.Alias,
"PluginName": installedPluginName,
}),
)
}
}
}
}
return nil
}
func (cmd *PluginInstall) installPlugin(pluginMetadata *plugin.PluginMetadata, pluginDestinationFilepath, pluginSourceFilepath string) error {
err := fileutils.CopyPathToPath(pluginSourceFilepath, pluginDestinationFilepath)
if err != nil {
return errors.New(T(
"Could not copy plugin binary: \n{{.Error}}",
map[string]interface{}{
"Error": err.Error(),
}),
)
}
configMetadata := pluginconfig.PluginMetadata{
Location: pluginDestinationFilepath,
Version: pluginMetadata.Version,
Commands: pluginMetadata.Commands,
}
cmd.pluginConfig.SetPlugin(pluginMetadata.Name, configMetadata)
return nil
}
func (cmd *PluginInstall) runBinaryAndObtainPluginMetadata(pluginSourceFilepath string) (*plugin.PluginMetadata, error) {
err := cmd.rpcService.Start()
if err != nil {
return nil, err
}
defer cmd.rpcService.Stop()
err = cmd.runPluginBinary(pluginSourceFilepath, cmd.rpcService.Port())
if err != nil {
return nil, err
}
c := cmd.rpcService.RpcCmd
c.MetadataMutex.RLock()
defer c.MetadataMutex.RUnlock()
return c.PluginMetadata, nil
}
func (cmd *PluginInstall) runPluginBinary(location string, servicePort string) error {
pluginInvocation := exec.Command(location, servicePort, "SendMetadata")
err := pluginInvocation.Run()
if err != nil {
return err
}
return nil
}