Skip to content

Commit

Permalink
feat(helm): add plugin management commands
Browse files Browse the repository at this point in the history
Add plugin management subcommands for installing and removing plugins
to `$HELM_HOST/plugins`.

Install accepts a vcs url or a local directory.

```
$ helm plugin install http://github.com/adamreese/helm-env
Installed plugin: env

$ helm plugin list
NAME    	VERSION	DESCRIPTION
env     	0.1.0  	Print out the helm environment.

$ helm plugin remove env
Removed plugin: env
```

closes helm#1977
  • Loading branch information
adamreese committed Apr 6, 2017
1 parent 9e04adc commit 0c9c644
Show file tree
Hide file tree
Showing 21 changed files with 955 additions and 10 deletions.
10 changes: 7 additions & 3 deletions cmd/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,16 @@ func newRootCmd(out io.Writer) *cobra.Command {
addFlagsTLS(newStatusCmd(nil, out)),
addFlagsTLS(newUpgradeCmd(nil, out)),

addFlagsTLS(newReleaseTestCmd(nil, out)),
addFlagsTLS(newResetCmd(nil, out)),
addFlagsTLS(newVersionCmd(nil, out)),
newCompletionCmd(out),
newHomeCmd(out),
newInitCmd(out),
addFlagsTLS(newResetCmd(nil, out)),
addFlagsTLS(newVersionCmd(nil, out)),
addFlagsTLS(newReleaseTestCmd(nil, out)),
newResetCmd(nil, out),
newVersionCmd(nil, out),
newReleaseTestCmd(nil, out),
newPluginCmd(out),

// Hidden documentation generator command: 'helm docs'
newDocsCmd(out),
Expand Down
13 changes: 8 additions & 5 deletions cmd/helm/plugins.go → cmd/helm/load_plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ import (

const pluginEnvVar = "HELM_PLUGIN"

func pluginDirs(home helmpath.Home) string {
if dirs := os.Getenv(pluginEnvVar); dirs != "" {
return dirs
}
return home.Plugins()
}

// loadPlugins loads plugins into the command list.
//
// This follows a different pattern than the other commands because it has
Expand All @@ -43,11 +50,7 @@ func loadPlugins(baseCmd *cobra.Command, home helmpath.Home, out io.Writer) {
return
}

plugdirs := os.Getenv(pluginEnvVar)
if plugdirs == "" {
plugdirs = home.Plugins()
}

plugdirs := pluginDirs(home)
found, err := findPlugins(plugdirs)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to load plugins: %s", err)
Expand Down
72 changes: 72 additions & 0 deletions cmd/helm/plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
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 (
"fmt"
"io"
"os"
"os/exec"

"k8s.io/helm/pkg/helm/helmpath"
"k8s.io/helm/pkg/plugin"

"github.com/spf13/cobra"
)

const pluginHelp = `
Manage client-side Helm plugins.
`

func newPluginCmd(out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "plugin",
Short: "add, list, remove, or update Helm plugins",
Long: pluginHelp,
}
cmd.AddCommand(
newPluginInstallCmd(out),
newPluginListCmd(out),
newPluginRemoveCmd(out),
)
return cmd
}

// runHook will execute a plugin hook.
func runHook(p *plugin.Plugin, event string, home helmpath.Home) error {
hook := p.Metadata.Hooks.Get(event)
if hook == "" {
return nil
}

prog := exec.Command("sh", "-c", hook)
// TODO make this work on windows
// I think its ... ¯\_(ツ)_/¯
// prog := exec.Command("cmd", "/C", p.Metadata.Hooks.Install())

debug("running %s hook: %s", event, prog)

setupEnv(p.Metadata.Name, p.Dir, home.Plugins(), home)
prog.Stdout, prog.Stderr = os.Stdout, os.Stderr
if err := prog.Run(); err != nil {
if eerr, ok := err.(*exec.ExitError); ok {
os.Stderr.Write(eerr.Stderr)
return fmt.Errorf("plugin %q exited with error", p.Metadata.Name)
}
return err
}
return nil
}
84 changes: 84 additions & 0 deletions cmd/helm/plugin_install.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
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 (
"fmt"
"io"

"k8s.io/helm/pkg/helm/helmpath"
"k8s.io/helm/pkg/plugin"
"k8s.io/helm/pkg/plugin/installer"

"github.com/spf13/cobra"
)

type pluginInstallCmd struct {
source string
version string
home helmpath.Home
out io.Writer
}

func newPluginInstallCmd(out io.Writer) *cobra.Command {
pcmd := &pluginInstallCmd{out: out}
cmd := &cobra.Command{
Use: "install [PLUGIN]",
Short: "install one or more Helm plugins",
PreRunE: func(cmd *cobra.Command, args []string) error {
return pcmd.complete(args)
},
RunE: func(cmd *cobra.Command, args []string) error {
return pcmd.run()
},
}
cmd.Flags().StringVar(&pcmd.version, "version", "", "specify the exact plugin version to install. If this is not specified, the latest version is installed")
return cmd
}

func (pcmd *pluginInstallCmd) complete(args []string) error {
if err := checkArgsLength(len(args), "plugin"); err != nil {
return err
}
pcmd.source = args[0]
pcmd.home = helmpath.Home(homePath())
return nil
}

func (pcmd *pluginInstallCmd) run() error {
installer.Debug = flagDebug

i, err := installer.NewForSource(pcmd.source, pcmd.version, pcmd.home)
if err != nil {
return err
}
if err := installer.Install(i); err != nil {
return err
}

debug("loading plugin from %s", i.Path())
p, err := plugin.LoadDir(i.Path())
if err != nil {
return err
}

if err := runHook(p, plugin.Install, pcmd.home); err != nil {
return err
}

fmt.Fprintf(pcmd.out, "Installed plugin: %s\n", p.Metadata.Name)
return nil
}
63 changes: 63 additions & 0 deletions cmd/helm/plugin_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
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 (
"fmt"
"io"

"k8s.io/helm/pkg/helm/helmpath"

"github.com/gosuri/uitable"
"github.com/spf13/cobra"
)

type pluginListCmd struct {
home helmpath.Home
out io.Writer
}

func newPluginListCmd(out io.Writer) *cobra.Command {
pcmd := &pluginListCmd{out: out}
cmd := &cobra.Command{
Use: "list",
Short: "list installed Helm plugins",
RunE: func(cmd *cobra.Command, args []string) error {
pcmd.home = helmpath.Home(homePath())
return pcmd.run()
},
}
return cmd
}

func (pcmd *pluginListCmd) run() error {
plugdirs := pluginDirs(pcmd.home)

debug("pluginDirs: %s", plugdirs)

plugins, err := findPlugins(plugdirs)
if err != nil {
return err
}

table := uitable.New()
table.AddRow("NAME", "VERSION", "DESCRIPTION")
for _, p := range plugins {
table.AddRow(p.Metadata.Name, p.Metadata.Version, p.Metadata.Description)
}
fmt.Fprintln(pcmd.out, table)
return nil
}
92 changes: 92 additions & 0 deletions cmd/helm/plugin_remove.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
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 (
"fmt"
"io"
"os"

"k8s.io/helm/pkg/helm/helmpath"
"k8s.io/helm/pkg/plugin"

"github.com/spf13/cobra"
)

type pluginRemoveCmd struct {
names []string
home helmpath.Home
out io.Writer
}

func newPluginRemoveCmd(out io.Writer) *cobra.Command {
pcmd := &pluginRemoveCmd{out: out}
cmd := &cobra.Command{
Use: "remove [PLUGIN]",
Short: "remove a helm plugin",
PreRunE: func(cmd *cobra.Command, args []string) error {
return pcmd.complete(args)
},
RunE: func(cmd *cobra.Command, args []string) error {
return pcmd.run()
},
}
return cmd
}

func (pcmd *pluginRemoveCmd) complete(args []string) error {
if err := checkArgsLength(len(args), "plugin"); err != nil {
return err
}
pcmd.names = args
pcmd.home = helmpath.Home(homePath())
return nil
}

func (pcmd *pluginRemoveCmd) run() error {
plugdirs := pluginDirs(pcmd.home)
debug("loading installed plugins from %s", plugdirs)
plugins, err := findPlugins(plugdirs)
if err != nil {
return err
}

for _, name := range pcmd.names {
if found := findPlugin(plugins, name); found != nil {
if err := removePlugin(found, pcmd.home); err != nil {
return err
}
fmt.Fprintf(pcmd.out, "Removed plugin: %s\n", name)
}
}
return nil
}

func removePlugin(p *plugin.Plugin, home helmpath.Home) error {
if err := os.Remove(p.Dir); err != nil {
return err
}
return runHook(p, plugin.Delete, home)
}

func findPlugin(plugins []*plugin.Plugin, name string) *plugin.Plugin {
for _, p := range plugins {
if p.Metadata.Name == name {
return p
}
}
return nil
}
File renamed without changes.
8 changes: 8 additions & 0 deletions cmd/helm/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package main

import (
"fmt"
"io"
"text/template"
"time"
Expand Down Expand Up @@ -72,3 +73,10 @@ func tpl(t string, vals map[string]interface{}, out io.Writer) error {
}
return tt.Execute(out, vals)
}

func debug(format string, args ...interface{}) {
if flagDebug {
format = fmt.Sprintf("[debug] %s\n", format)
fmt.Printf(format, args...)
}
}
Loading

0 comments on commit 0c9c644

Please sign in to comment.