github.com/wangchanggan/helm@v0.0.0-20211020154240-11b1b7d5406d/cmd/helm/fetch.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 main 18 19 import ( 20 "fmt" 21 "io" 22 "io/ioutil" 23 "os" 24 "path/filepath" 25 26 "github.com/spf13/cobra" 27 "k8s.io/helm/pkg/chartutil" 28 "k8s.io/helm/pkg/downloader" 29 "k8s.io/helm/pkg/getter" 30 "k8s.io/helm/pkg/repo" 31 ) 32 33 const fetchDesc = ` 34 Retrieve a package from a package repository, and download it locally. 35 36 This is useful for fetching packages to inspect, modify, or repackage. It can 37 also be used to perform cryptographic verification of a chart without installing 38 the chart. 39 40 There are options for unpacking the chart after download. This will create a 41 directory for the chart and uncompress into that directory. 42 43 If the --verify flag is specified, the requested chart MUST have a provenance 44 file, and MUST pass the verification process. Failure in any part of this will 45 result in an error, and the chart will not be saved locally. 46 ` 47 48 type fetchCmd struct { 49 untar bool 50 untardir string 51 chartRef string 52 destdir string 53 version string 54 repoURL string 55 username string 56 password string 57 58 verify bool 59 verifyLater bool 60 keyring string 61 62 certFile string 63 keyFile string 64 caFile string 65 66 devel bool 67 68 out io.Writer 69 } 70 71 func newFetchCmd(out io.Writer) *cobra.Command { 72 fch := &fetchCmd{out: out} 73 74 cmd := &cobra.Command{ 75 Use: "fetch [flags] [chart URL | repo/chartname] [...]", 76 Short: "Download a chart from a repository and (optionally) unpack it in local directory", 77 Long: fetchDesc, 78 RunE: func(cmd *cobra.Command, args []string) error { 79 if len(args) == 0 { 80 return fmt.Errorf("need at least one argument, url or repo/name of the chart") 81 } 82 83 if fch.version == "" && fch.devel { 84 debug("setting version to >0.0.0-0") 85 fch.version = ">0.0.0-0" 86 } 87 88 for i := 0; i < len(args); i++ { 89 fch.chartRef = args[i] 90 if err := fch.run(); err != nil { 91 return err 92 } 93 } 94 return nil 95 }, 96 } 97 98 f := cmd.Flags() 99 f.BoolVar(&fch.untar, "untar", false, "If set to true, will untar the chart after downloading it") 100 f.StringVar(&fch.untardir, "untardir", ".", "If untar is specified, this flag specifies the name of the directory into which the chart is expanded") 101 f.BoolVar(&fch.verify, "verify", false, "Verify the package against its signature") 102 f.BoolVar(&fch.verifyLater, "prov", false, "Fetch the provenance file, but don't perform verification") 103 f.StringVar(&fch.version, "version", "", "Specific version of a chart. Without this, the latest version is fetched") 104 f.StringVar(&fch.keyring, "keyring", defaultKeyring(), "Keyring containing public keys") 105 f.StringVarP(&fch.destdir, "destination", "d", ".", "Location to write the chart. If this and tardir are specified, tardir is appended to this") 106 f.StringVar(&fch.repoURL, "repo", "", "Chart repository url where to locate the requested chart") 107 f.StringVar(&fch.certFile, "cert-file", "", "Identify HTTPS client using this SSL certificate file") 108 f.StringVar(&fch.keyFile, "key-file", "", "Identify HTTPS client using this SSL key file") 109 f.StringVar(&fch.caFile, "ca-file", "", "Verify certificates of HTTPS-enabled servers using this CA bundle") 110 f.BoolVar(&fch.devel, "devel", false, "Use development versions, too. Equivalent to version '>0.0.0-0'. If --version is set, this is ignored.") 111 f.StringVar(&fch.username, "username", "", "Chart repository username") 112 f.StringVar(&fch.password, "password", "", "Chart repository password") 113 114 return cmd 115 } 116 117 func (f *fetchCmd) run() error { 118 c := downloader.ChartDownloader{ 119 HelmHome: settings.Home, 120 Out: f.out, 121 Keyring: f.keyring, 122 Verify: downloader.VerifyNever, 123 Getters: getter.All(settings), 124 Username: f.username, 125 Password: f.password, 126 } 127 128 if f.verify { 129 c.Verify = downloader.VerifyAlways 130 } else if f.verifyLater { 131 c.Verify = downloader.VerifyLater 132 } 133 134 // If untar is set, we fetch to a tempdir, then untar and copy after 135 // verification. 136 dest := f.destdir 137 if f.untar { 138 var err error 139 dest, err = ioutil.TempDir("", "helm-") 140 if err != nil { 141 return fmt.Errorf("Failed to untar: %s", err) 142 } 143 defer os.RemoveAll(dest) 144 } 145 146 if f.repoURL != "" { 147 chartURL, err := repo.FindChartInAuthRepoURL(f.repoURL, f.username, f.password, f.chartRef, f.version, f.certFile, f.keyFile, f.caFile, getter.All(settings)) 148 if err != nil { 149 return err 150 } 151 f.chartRef = chartURL 152 } 153 154 saved, v, err := c.DownloadTo(f.chartRef, f.version, dest) 155 if err != nil { 156 return err 157 } 158 159 if f.verify { 160 fmt.Fprintf(f.out, "Verification: %v\n", v) 161 } 162 163 // After verification, untar the chart into the requested directory. 164 if f.untar { 165 ud := f.untardir 166 if !filepath.IsAbs(ud) { 167 ud = filepath.Join(f.destdir, ud) 168 } 169 // Let udCheck to check conflict file/dir without replacing ud when untarDir is the current directory(.). 170 udCheck := ud 171 if udCheck == "." { 172 _, udCheck = filepath.Split(f.chartRef) 173 } else { 174 _, chartName := filepath.Split(f.chartRef) 175 udCheck = filepath.Join(udCheck, chartName) 176 } 177 if _, err := os.Stat(udCheck); err != nil { 178 if err := os.MkdirAll(udCheck, 0755); err != nil { 179 return fmt.Errorf("Failed to untar (mkdir): %s", err) 180 } 181 } else { 182 return fmt.Errorf("Failed to untar: a file or directory with the name %s already exists", udCheck) 183 } 184 185 return chartutil.ExpandFile(ud, saved) 186 } 187 return nil 188 } 189 190 // defaultKeyring returns the expanded path to the default keyring. 191 func defaultKeyring() string { 192 return os.ExpandEnv("$HOME/.gnupg/pubring.gpg") 193 }