github.com/sgoings/helm@v2.0.0-alpha.2.0.20170406211108-734e92851ac3+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 "github.com/Masterminds/vcs" 25 "github.com/ghodss/yaml" 26 ) 27 28 // PluginFileName is the name of a plugin file. 29 const PluginFileName = "plugin.yaml" 30 31 // Metadata describes a plugin. 32 // 33 // This is the plugin equivalent of a chart.Metadata. 34 type Metadata struct { 35 // Name is the name of the plugin 36 Name string `json:"name"` 37 38 // Version is a SemVer 2 version of the plugin. 39 Version string `json:"version"` 40 41 // Usage is the single-line usage text shown in help 42 Usage string `json:"usage"` 43 44 // Description is a long description shown in places like `helm help` 45 Description string `json:"description"` 46 47 // Command is the command, as a single string. 48 // 49 // The command will be passed through environment expansion, so env vars can 50 // be present in this command. Unless IgnoreFlags is set, this will 51 // also merge the flags passed from Helm. 52 // 53 // Note that command is not executed in a shell. To do so, we suggest 54 // pointing the command to a shell script. 55 Command string `json:"command"` 56 57 // IgnoreFlags ignores any flags passed in from Helm 58 // 59 // For example, if the plugin is invoked as `helm --debug myplugin`, if this 60 // is false, `--debug` will be appended to `--command`. If this is true, 61 // the `--debug` flag will be discarded. 62 IgnoreFlags bool `json:"ignoreFlags"` 63 64 // UseTunnel indicates that this command needs a tunnel. 65 // Setting this will cause a number of side effects, such as the 66 // automatic setting of HELM_HOST. 67 UseTunnel bool `json:"useTunnel"` 68 69 // Hooks are commands that will run on events. 70 Hooks Hooks 71 } 72 73 // Plugin represents a plugin. 74 type Plugin struct { 75 // Metadata is a parsed representation of a plugin.yaml 76 Metadata *Metadata 77 // Dir is the string path to the directory that holds the plugin. 78 Dir string 79 // Remote is the remote repo location. 80 Remote string 81 } 82 83 func detectSource(dirname string) (string, error) { 84 if repo, err := vcs.NewRepo("", dirname); err == nil { 85 if repo.CheckLocal() { 86 return repo.Remote(), nil 87 } 88 } 89 return os.Readlink(dirname) 90 } 91 92 // PrepareCommand takes a Plugin.Command and prepares it for execution. 93 // 94 // It merges extraArgs into any arguments supplied in the plugin. It 95 // returns the name of the command and an args array. 96 // 97 // The result is suitable to pass to exec.Command. 98 func (p *Plugin) PrepareCommand(extraArgs []string) (string, []string) { 99 parts := strings.Split(os.ExpandEnv(p.Metadata.Command), " ") 100 main := parts[0] 101 baseArgs := []string{} 102 if len(parts) > 1 { 103 baseArgs = parts[1:] 104 } 105 if !p.Metadata.IgnoreFlags { 106 baseArgs = append(baseArgs, extraArgs...) 107 } 108 return main, baseArgs 109 } 110 111 // LoadDir loads a plugin from the given directory. 112 func LoadDir(dirname string) (*Plugin, error) { 113 data, err := ioutil.ReadFile(filepath.Join(dirname, PluginFileName)) 114 if err != nil { 115 return nil, err 116 } 117 118 plug := &Plugin{Dir: dirname} 119 if src, err := detectSource(dirname); err == nil { 120 plug.Remote = src 121 } 122 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 }