github.com/pensu/helm@v2.6.1+incompatible/pkg/getter/plugingetter.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 getter
    17  
    18  import (
    19  	"bytes"
    20  	"fmt"
    21  	"os"
    22  	"os/exec"
    23  	"path/filepath"
    24  
    25  	"k8s.io/helm/pkg/helm/environment"
    26  	"k8s.io/helm/pkg/plugin"
    27  )
    28  
    29  // collectPlugins scans for getter plugins.
    30  // This will load plugins according to the environment.
    31  func collectPlugins(settings environment.EnvSettings) (Providers, error) {
    32  	plugins, err := plugin.FindPlugins(settings.PluginDirs())
    33  	if err != nil {
    34  		return nil, err
    35  	}
    36  	var result Providers
    37  	for _, plugin := range plugins {
    38  		for _, downloader := range plugin.Metadata.Downloaders {
    39  			result = append(result, Provider{
    40  				Schemes: downloader.Protocols,
    41  				New: newPluginGetter(
    42  					downloader.Command,
    43  					settings,
    44  					plugin.Metadata.Name,
    45  					plugin.Dir,
    46  				),
    47  			})
    48  		}
    49  	}
    50  	return result, nil
    51  }
    52  
    53  // pluginGetter is a generic type to invoke custom downloaders,
    54  // implemented in plugins.
    55  type pluginGetter struct {
    56  	command                   string
    57  	certFile, keyFile, cAFile string
    58  	settings                  environment.EnvSettings
    59  	name                      string
    60  	base                      string
    61  }
    62  
    63  // Get runs downloader plugin command
    64  func (p *pluginGetter) Get(href string) (*bytes.Buffer, error) {
    65  	argv := []string{p.certFile, p.keyFile, p.cAFile, href}
    66  	prog := exec.Command(filepath.Join(p.base, p.command), argv...)
    67  	plugin.SetupPluginEnv(p.settings, p.name, p.base)
    68  	prog.Env = os.Environ()
    69  	buf := bytes.NewBuffer(nil)
    70  	prog.Stdout = buf
    71  	prog.Stderr = os.Stderr
    72  	if err := prog.Run(); err != nil {
    73  		if eerr, ok := err.(*exec.ExitError); ok {
    74  			os.Stderr.Write(eerr.Stderr)
    75  			return nil, fmt.Errorf("plugin %q exited with error", p.command)
    76  		}
    77  		return nil, err
    78  	}
    79  	return buf, nil
    80  }
    81  
    82  // newPluginGetter constructs a valid plugin getter
    83  func newPluginGetter(command string, settings environment.EnvSettings, name, base string) Constructor {
    84  	return func(URL, CertFile, KeyFile, CAFile string) (Getter, error) {
    85  		result := &pluginGetter{
    86  			command:  command,
    87  			certFile: CertFile,
    88  			keyFile:  KeyFile,
    89  			cAFile:   CAFile,
    90  			settings: settings,
    91  			name:     name,
    92  			base:     base,
    93  		}
    94  		return result, nil
    95  	}
    96  }