github.com/zoumo/helm@v2.5.0+incompatible/pkg/plugin/plugin.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors All rights reserved.
     3  Licensed under the Apache License, Version 2.0 (the "License");
     4  you may not use this file except in compliance with the License.
     5  You may obtain a copy of the License at
     6  
     7  http://www.apache.org/licenses/LICENSE-2.0
     8  
     9  Unless required by applicable law or agreed to in writing, software
    10  distributed under the License is distributed on an "AS IS" BASIS,
    11  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  See the License for the specific language governing permissions and
    13  limitations under the License.
    14  */
    15  
    16  package plugin // import "k8s.io/helm/pkg/plugin"
    17  
    18  import (
    19  	"io/ioutil"
    20  	"os"
    21  	"path/filepath"
    22  	"strings"
    23  
    24  	helm_env "k8s.io/helm/pkg/helm/environment"
    25  	tiller_env "k8s.io/helm/pkg/tiller/environment"
    26  
    27  	"github.com/ghodss/yaml"
    28  )
    29  
    30  const pluginFileName = "plugin.yaml"
    31  
    32  // Downloaders represents the plugins capability if it can retrieve
    33  // charts from special sources
    34  type Downloaders struct {
    35  	// Protocols are the list of schemes from the charts URL.
    36  	Protocols []string `json:"protocols"`
    37  	// Command is the executable path with which the plugin performs
    38  	// the actual download for the corresponding Protocols
    39  	Command string `json:"command"`
    40  }
    41  
    42  // Metadata describes a plugin.
    43  //
    44  // This is the plugin equivalent of a chart.Metadata.
    45  type Metadata struct {
    46  	// Name is the name of the plugin
    47  	Name string `json:"name"`
    48  
    49  	// Version is a SemVer 2 version of the plugin.
    50  	Version string `json:"version"`
    51  
    52  	// Usage is the single-line usage text shown in help
    53  	Usage string `json:"usage"`
    54  
    55  	// Description is a long description shown in places like `helm help`
    56  	Description string `json:"description"`
    57  
    58  	// Command is the command, as a single string.
    59  	//
    60  	// The command will be passed through environment expansion, so env vars can
    61  	// be present in this command. Unless IgnoreFlags is set, this will
    62  	// also merge the flags passed from Helm.
    63  	//
    64  	// Note that command is not executed in a shell. To do so, we suggest
    65  	// pointing the command to a shell script.
    66  	Command string `json:"command"`
    67  
    68  	// IgnoreFlags ignores any flags passed in from Helm
    69  	//
    70  	// For example, if the plugin is invoked as `helm --debug myplugin`, if this
    71  	// is false, `--debug` will be appended to `--command`. If this is true,
    72  	// the `--debug` flag will be discarded.
    73  	IgnoreFlags bool `json:"ignoreFlags"`
    74  
    75  	// UseTunnel indicates that this command needs a tunnel.
    76  	// Setting this will cause a number of side effects, such as the
    77  	// automatic setting of HELM_HOST.
    78  	UseTunnel bool `json:"useTunnel"`
    79  
    80  	// Hooks are commands that will run on events.
    81  	Hooks Hooks
    82  
    83  	// Downloaders field is used if the plugin supply downloader mechanism
    84  	// for special protocols.
    85  	Downloaders []Downloaders `json:"downloaders"`
    86  }
    87  
    88  // Plugin represents a plugin.
    89  type Plugin struct {
    90  	// Metadata is a parsed representation of a plugin.yaml
    91  	Metadata *Metadata
    92  	// Dir is the string path to the directory that holds the plugin.
    93  	Dir string
    94  }
    95  
    96  // PrepareCommand takes a Plugin.Command and prepares it for execution.
    97  //
    98  // It merges extraArgs into any arguments supplied in the plugin. It
    99  // returns the name of the command and an args array.
   100  //
   101  // The result is suitable to pass to exec.Command.
   102  func (p *Plugin) PrepareCommand(extraArgs []string) (string, []string) {
   103  	parts := strings.Split(os.ExpandEnv(p.Metadata.Command), " ")
   104  	main := parts[0]
   105  	baseArgs := []string{}
   106  	if len(parts) > 1 {
   107  		baseArgs = parts[1:]
   108  	}
   109  	if !p.Metadata.IgnoreFlags {
   110  		baseArgs = append(baseArgs, extraArgs...)
   111  	}
   112  	return main, baseArgs
   113  }
   114  
   115  // LoadDir loads a plugin from the given directory.
   116  func LoadDir(dirname string) (*Plugin, error) {
   117  	data, err := ioutil.ReadFile(filepath.Join(dirname, pluginFileName))
   118  	if err != nil {
   119  		return nil, err
   120  	}
   121  
   122  	plug := &Plugin{Dir: dirname}
   123  	if err := yaml.Unmarshal(data, &plug.Metadata); err != nil {
   124  		return nil, err
   125  	}
   126  	return plug, nil
   127  }
   128  
   129  // LoadAll loads all plugins found beneath the base directory.
   130  //
   131  // This scans only one directory level.
   132  func LoadAll(basedir string) ([]*Plugin, error) {
   133  	plugins := []*Plugin{}
   134  	// We want basedir/*/plugin.yaml
   135  	scanpath := filepath.Join(basedir, "*", pluginFileName)
   136  	matches, err := filepath.Glob(scanpath)
   137  	if err != nil {
   138  		return plugins, err
   139  	}
   140  
   141  	if matches == nil {
   142  		return plugins, nil
   143  	}
   144  
   145  	for _, yaml := range matches {
   146  		dir := filepath.Dir(yaml)
   147  		p, err := LoadDir(dir)
   148  		if err != nil {
   149  			return plugins, err
   150  		}
   151  		plugins = append(plugins, p)
   152  	}
   153  	return plugins, nil
   154  }
   155  
   156  // FindPlugins returns a list of YAML files that describe plugins.
   157  func FindPlugins(plugdirs string) ([]*Plugin, error) {
   158  	found := []*Plugin{}
   159  	// Let's get all UNIXy and allow path separators
   160  	for _, p := range filepath.SplitList(plugdirs) {
   161  		matches, err := LoadAll(p)
   162  		if err != nil {
   163  			return matches, err
   164  		}
   165  		found = append(found, matches...)
   166  	}
   167  	return found, nil
   168  }
   169  
   170  // SetupPluginEnv prepares os.Env for plugins. It operates on os.Env because
   171  // the plugin subsystem itself needs access to the environment variables
   172  // created here.
   173  func SetupPluginEnv(settings helm_env.EnvSettings,
   174  	shortName, base string) {
   175  	for key, val := range map[string]string{
   176  		"HELM_PLUGIN_NAME": shortName,
   177  		"HELM_PLUGIN_DIR":  base,
   178  		"HELM_BIN":         os.Args[0],
   179  
   180  		// Set vars that may not have been set, and save client the
   181  		// trouble of re-parsing.
   182  		helm_env.PluginEnvVar: settings.PluginDirs(),
   183  		helm_env.HomeEnvVar:   settings.Home.String(),
   184  
   185  		// Set vars that convey common information.
   186  		"HELM_PATH_REPOSITORY":       settings.Home.Repository(),
   187  		"HELM_PATH_REPOSITORY_FILE":  settings.Home.RepositoryFile(),
   188  		"HELM_PATH_CACHE":            settings.Home.Cache(),
   189  		"HELM_PATH_LOCAL_REPOSITORY": settings.Home.LocalRepository(),
   190  		"HELM_PATH_STARTER":          settings.Home.Starters(),
   191  
   192  		"TILLER_HOST":                    settings.TillerHost,
   193  		tiller_env.TillerNamespaceEnvVar: settings.TillerNamespace,
   194  	} {
   195  		os.Setenv(key, val)
   196  	}
   197  
   198  	if settings.Debug {
   199  		os.Setenv("HELM_DEBUG", "1")
   200  	}
   201  }