github.com/darkowlzz/helm@v2.5.1-0.20171213183701-6707fe0468d4+incompatible/pkg/plugin/installer/installer.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 installer 17 18 import ( 19 "errors" 20 "fmt" 21 "os" 22 "path" 23 "path/filepath" 24 25 "k8s.io/helm/pkg/helm/helmpath" 26 "strings" 27 ) 28 29 // ErrMissingMetadata indicates that plugin.yaml is missing. 30 var ErrMissingMetadata = errors.New("plugin metadata (plugin.yaml) missing") 31 32 // Debug enables verbose output. 33 var Debug bool 34 35 // Installer provides an interface for installing helm client plugins. 36 type Installer interface { 37 // Install adds a plugin to $HELM_HOME. 38 Install() error 39 // Path is the directory of the installed plugin. 40 Path() string 41 // Update updates a plugin to $HELM_HOME. 42 Update() error 43 } 44 45 // Install installs a plugin to $HELM_HOME. 46 func Install(i Installer) error { 47 if _, pathErr := os.Stat(path.Dir(i.Path())); os.IsNotExist(pathErr) { 48 return errors.New(`plugin home "$HELM_HOME/plugins" does not exist`) 49 } 50 51 if _, pathErr := os.Stat(i.Path()); !os.IsNotExist(pathErr) { 52 return errors.New("plugin already exists") 53 } 54 55 return i.Install() 56 } 57 58 // Update updates a plugin in $HELM_HOME. 59 func Update(i Installer) error { 60 if _, pathErr := os.Stat(i.Path()); os.IsNotExist(pathErr) { 61 return errors.New("plugin does not exist") 62 } 63 64 return i.Update() 65 } 66 67 // NewForSource determines the correct Installer for the given source. 68 func NewForSource(source, version string, home helmpath.Home) (Installer, error) { 69 // Check if source is a local directory 70 if isLocalReference(source) { 71 return NewLocalInstaller(source, home) 72 } else if isRemoteHTTPArchive(source) { 73 return NewHTTPInstaller(source, home) 74 } 75 return NewVCSInstaller(source, version, home) 76 } 77 78 // FindSource determines the correct Installer for the given source. 79 func FindSource(location string, home helmpath.Home) (Installer, error) { 80 installer, err := existingVCSRepo(location, home) 81 if err != nil && err.Error() == "Cannot detect VCS" { 82 return installer, errors.New("cannot get information about plugin source") 83 } 84 return installer, err 85 } 86 87 // isLocalReference checks if the source exists on the filesystem. 88 func isLocalReference(source string) bool { 89 _, err := os.Stat(source) 90 return err == nil 91 } 92 93 // isRemoteHTTPArchive checks if the source is a http/https url and is an archive 94 func isRemoteHTTPArchive(source string) bool { 95 if strings.HasPrefix(source, "http://") || strings.HasPrefix(source, "https://") { 96 for suffix := range Extractors { 97 if strings.HasSuffix(source, suffix) { 98 return true 99 } 100 } 101 } 102 return false 103 } 104 105 // isPlugin checks if the directory contains a plugin.yaml file. 106 func isPlugin(dirname string) bool { 107 _, err := os.Stat(filepath.Join(dirname, "plugin.yaml")) 108 return err == nil 109 } 110 111 func debug(format string, args ...interface{}) { 112 if Debug { 113 format = fmt.Sprintf("[debug] %s\n", format) 114 fmt.Printf(format, args...) 115 } 116 }