github.com/zsuzhengdu/helm@v3.0.0-beta.3+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  		Out:     &out,
    61  		Keyring: p.Keyring,
    62  		Verify:  downloader.VerifyNever,
    63  		Getters: getter.All(p.Settings),
    64  		Options: []getter.Option{
    65  			getter.WithBasicAuth(p.Username, p.Password),
    66  		},
    67  		RepositoryConfig: p.Settings.RepositoryConfig,
    68  		RepositoryCache:  p.Settings.RepositoryCache,
    69  	}
    70  
    71  	if p.Verify {
    72  		c.Verify = downloader.VerifyAlways
    73  	} else if p.VerifyLater {
    74  		c.Verify = downloader.VerifyLater
    75  	}
    76  
    77  	// If untar is set, we fetch to a tempdir, then untar and copy after
    78  	// verification.
    79  	dest := p.DestDir
    80  	if p.Untar {
    81  		var err error
    82  		dest, err = ioutil.TempDir("", "helm-")
    83  		if err != nil {
    84  			return out.String(), errors.Wrap(err, "failed to untar")
    85  		}
    86  		defer os.RemoveAll(dest)
    87  	}
    88  
    89  	if p.RepoURL != "" {
    90  		chartURL, err := repo.FindChartInAuthRepoURL(p.RepoURL, p.Username, p.Password, chartRef, p.Version, p.CertFile, p.KeyFile, p.CaFile, getter.All(p.Settings))
    91  		if err != nil {
    92  			return out.String(), err
    93  		}
    94  		chartRef = chartURL
    95  	}
    96  
    97  	saved, v, err := c.DownloadTo(chartRef, p.Version, dest)
    98  	if err != nil {
    99  		return out.String(), err
   100  	}
   101  
   102  	if p.Verify {
   103  		fmt.Fprintf(&out, "Verification: %v\n", v)
   104  	}
   105  
   106  	// After verification, untar the chart into the requested directory.
   107  	if p.Untar {
   108  		ud := p.UntarDir
   109  		if !filepath.IsAbs(ud) {
   110  			ud = filepath.Join(p.DestDir, ud)
   111  		}
   112  		if fi, err := os.Stat(ud); err != nil {
   113  			if err := os.MkdirAll(ud, 0755); err != nil {
   114  				return out.String(), errors.Wrap(err, "failed to untar (mkdir)")
   115  			}
   116  
   117  		} else if !fi.IsDir() {
   118  			return out.String(), errors.Errorf("failed to untar: %s is not a directory", ud)
   119  		}
   120  
   121  		return out.String(), chartutil.ExpandFile(ud, saved)
   122  	}
   123  	return out.String(), nil
   124  }