github.com/umeshredd/helm@v3.0.0-alpha.1+incompatible/pkg/action/pull.go (about)

     1  /*
     2  Copyright The Helm Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package action
    18  
    19  import (
    20  	"fmt"
    21  	"io/ioutil"
    22  	"os"
    23  	"path/filepath"
    24  	"strings"
    25  
    26  	"github.com/pkg/errors"
    27  
    28  	"helm.sh/helm/pkg/chartutil"
    29  	"helm.sh/helm/pkg/cli"
    30  	"helm.sh/helm/pkg/downloader"
    31  	"helm.sh/helm/pkg/getter"
    32  	"helm.sh/helm/pkg/repo"
    33  )
    34  
    35  // Pull is the action for checking a given release's information.
    36  //
    37  // It provides the implementation of 'helm pull'.
    38  type Pull struct {
    39  	ChartPathOptions
    40  
    41  	Settings cli.EnvSettings // TODO: refactor this out of pkg/action
    42  
    43  	Devel       bool
    44  	Untar       bool
    45  	VerifyLater bool
    46  	UntarDir    string
    47  	DestDir     string
    48  }
    49  
    50  // NewPull creates a new Pull object with the given configuration.
    51  func NewPull() *Pull {
    52  	return &Pull{}
    53  }
    54  
    55  // Run executes 'helm pull' against the given release.
    56  func (p *Pull) Run(chartRef string) (string, error) {
    57  	var out strings.Builder
    58  
    59  	c := downloader.ChartDownloader{
    60  		HelmHome: p.Settings.Home,
    61  		Out:      &out,
    62  		Keyring:  p.Keyring,
    63  		Verify:   downloader.VerifyNever,
    64  		Getters:  getter.All(p.Settings),
    65  		Username: p.Username,
    66  		Password: p.Password,
    67  	}
    68  
    69  	if p.Verify {
    70  		c.Verify = downloader.VerifyAlways
    71  	} else if p.VerifyLater {
    72  		c.Verify = downloader.VerifyLater
    73  	}
    74  
    75  	// If untar is set, we fetch to a tempdir, then untar and copy after
    76  	// verification.
    77  	dest := p.DestDir
    78  	if p.Untar {
    79  		var err error
    80  		dest, err = ioutil.TempDir("", "helm-")
    81  		if err != nil {
    82  			return out.String(), errors.Wrap(err, "failed to untar")
    83  		}
    84  		defer os.RemoveAll(dest)
    85  	}
    86  
    87  	if p.RepoURL != "" {
    88  		chartURL, err := repo.FindChartInAuthRepoURL(p.RepoURL, p.Username, p.Password, chartRef, p.Version, p.CertFile, p.KeyFile, p.CaFile, getter.All(p.Settings))
    89  		if err != nil {
    90  			return out.String(), err
    91  		}
    92  		chartRef = chartURL
    93  	}
    94  
    95  	saved, v, err := c.DownloadTo(chartRef, p.Version, dest)
    96  	if err != nil {
    97  		return out.String(), err
    98  	}
    99  
   100  	if p.Verify {
   101  		fmt.Fprintf(&out, "Verification: %v\n", v)
   102  	}
   103  
   104  	// After verification, untar the chart into the requested directory.
   105  	if p.Untar {
   106  		ud := p.UntarDir
   107  		if !filepath.IsAbs(ud) {
   108  			ud = filepath.Join(p.DestDir, ud)
   109  		}
   110  		if fi, err := os.Stat(ud); err != nil {
   111  			if err := os.MkdirAll(ud, 0755); err != nil {
   112  				return out.String(), errors.Wrap(err, "failed to untar (mkdir)")
   113  			}
   114  
   115  		} else if !fi.IsDir() {
   116  			return out.String(), errors.Errorf("failed to untar: %s is not a directory", ud)
   117  		}
   118  
   119  		return out.String(), chartutil.ExpandFile(ud, saved)
   120  	}
   121  	return out.String(), nil
   122  }