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