github.com/amtisyAts/helm@v2.17.0+incompatible/cmd/helm/installer/init.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 installer // import "k8s.io/helm/cmd/helm/installer"
    18  
    19  import (
    20  	"fmt"
    21  	"io"
    22  	"os"
    23  
    24  	"k8s.io/helm/pkg/getter"
    25  	helm_env "k8s.io/helm/pkg/helm/environment"
    26  	"k8s.io/helm/pkg/helm/helmpath"
    27  	"k8s.io/helm/pkg/repo"
    28  )
    29  
    30  const (
    31  	stableRepository = "stable"
    32  
    33  	// LocalRepository is the standard name of the local repository
    34  	LocalRepository = "local"
    35  
    36  	// LocalRepositoryIndexFile is the standard name of the local repository index file
    37  	LocalRepositoryIndexFile = "index.yaml"
    38  )
    39  
    40  // Initialize initializes local config
    41  //
    42  // Returns an error if the command failed.
    43  func Initialize(home helmpath.Home, out io.Writer, skipRefresh bool, settings helm_env.EnvSettings, stableRepositoryURL, localRepositoryURL string) error {
    44  	if err := ensureDirectories(home, out); err != nil {
    45  		return err
    46  	}
    47  	if err := ensureDefaultRepos(home, out, skipRefresh, settings, stableRepositoryURL, localRepositoryURL); err != nil {
    48  		return err
    49  	}
    50  
    51  	return ensureRepoFileFormat(home.RepositoryFile(), out)
    52  }
    53  
    54  // InitializeWithoutRepos initializes local config without adding repos
    55  //
    56  // Returns an error if the command failed.
    57  func InitializeWithoutRepos(home helmpath.Home, out io.Writer) error {
    58  	if err := ensureDirectories(home, out); err != nil {
    59  		return err
    60  	}
    61  
    62  	// Adding an empty repositories file
    63  	repoFile := home.RepositoryFile()
    64  	if fi, err := os.Stat(repoFile); err != nil {
    65  		fmt.Fprintf(out, "Creating %s \n", repoFile)
    66  		f := repo.NewRepoFile()
    67  		if err := f.WriteFile(repoFile, 0644); err != nil {
    68  			return err
    69  		}
    70  	} else if fi.IsDir() {
    71  		return fmt.Errorf("%s must be a file, not a directory", repoFile)
    72  	}
    73  
    74  	return ensureRepoFileFormat(home.RepositoryFile(), out)
    75  }
    76  
    77  // ensureDirectories checks to see if $HELM_HOME exists.
    78  //
    79  // If $HELM_HOME does not exist, this function will create it.
    80  func ensureDirectories(home helmpath.Home, out io.Writer) error {
    81  	configDirectories := []string{
    82  		home.String(),
    83  		home.Repository(),
    84  		home.Cache(),
    85  		home.LocalRepository(),
    86  		home.Plugins(),
    87  		home.Starters(),
    88  		home.Archive(),
    89  	}
    90  	for _, p := range configDirectories {
    91  		if fi, err := os.Stat(p); err != nil {
    92  			fmt.Fprintf(out, "Creating %s \n", p)
    93  			if err := os.MkdirAll(p, 0755); err != nil {
    94  				return fmt.Errorf("Could not create %s: %s", p, err)
    95  			}
    96  		} else if !fi.IsDir() {
    97  			return fmt.Errorf("%s must be a directory", p)
    98  		}
    99  	}
   100  
   101  	return nil
   102  }
   103  
   104  func ensureDefaultRepos(home helmpath.Home, out io.Writer, skipRefresh bool, settings helm_env.EnvSettings, stableRepositoryURL, localRepositoryURL string) error {
   105  	repoFile := home.RepositoryFile()
   106  	if fi, err := os.Stat(repoFile); err != nil {
   107  		fmt.Fprintf(out, "Creating %s \n", repoFile)
   108  		f := repo.NewRepoFile()
   109  		sr, err := initStableRepo(home.CacheIndex(stableRepository), home, out, skipRefresh, settings, stableRepositoryURL)
   110  		if err != nil {
   111  			return err
   112  		}
   113  		lr, err := initLocalRepo(home.LocalRepository(LocalRepositoryIndexFile), home.CacheIndex("local"), home, out, settings, localRepositoryURL)
   114  		if err != nil {
   115  			return err
   116  		}
   117  		f.Add(sr)
   118  		f.Add(lr)
   119  		if err := f.WriteFile(repoFile, 0644); err != nil {
   120  			return err
   121  		}
   122  	} else if fi.IsDir() {
   123  		return fmt.Errorf("%s must be a file, not a directory", repoFile)
   124  	}
   125  	return nil
   126  }
   127  
   128  func initStableRepo(cacheFile string, home helmpath.Home, out io.Writer, skipRefresh bool, settings helm_env.EnvSettings, stableRepositoryURL string) (*repo.Entry, error) {
   129  	fmt.Fprintf(out, "Adding %s repo with URL: %s \n", stableRepository, stableRepositoryURL)
   130  	c := repo.Entry{
   131  		Name:  stableRepository,
   132  		URL:   stableRepositoryURL,
   133  		Cache: cacheFile,
   134  	}
   135  	r, err := repo.NewChartRepository(&c, getter.All(settings))
   136  	if err != nil {
   137  		return nil, err
   138  	}
   139  
   140  	if skipRefresh {
   141  		return &c, nil
   142  	}
   143  
   144  	// In this case, the cacheFile is always absolute. So passing empty string
   145  	// is safe.
   146  	if err := r.DownloadIndexFile(""); err != nil {
   147  		return nil, fmt.Errorf("Looks like %q is not a valid chart repository or cannot be reached: %s", stableRepositoryURL, err.Error())
   148  	}
   149  
   150  	return &c, nil
   151  }
   152  
   153  func initLocalRepo(indexFile, cacheFile string, home helmpath.Home, out io.Writer, settings helm_env.EnvSettings, localRepositoryURL string) (*repo.Entry, error) {
   154  	if fi, err := os.Stat(indexFile); err != nil {
   155  		fmt.Fprintf(out, "Adding %s repo with URL: %s \n", LocalRepository, localRepositoryURL)
   156  		i := repo.NewIndexFile()
   157  		if err := i.WriteFile(indexFile, 0644); err != nil {
   158  			return nil, err
   159  		}
   160  
   161  		//TODO: take this out and replace with helm update functionality
   162  		if err := createLink(indexFile, cacheFile, home); err != nil {
   163  			return nil, err
   164  		}
   165  	} else if fi.IsDir() {
   166  		return nil, fmt.Errorf("%s must be a file, not a directory", indexFile)
   167  	}
   168  
   169  	return &repo.Entry{
   170  		Name:  LocalRepository,
   171  		URL:   localRepositoryURL,
   172  		Cache: cacheFile,
   173  	}, nil
   174  }
   175  
   176  func ensureRepoFileFormat(file string, out io.Writer) error {
   177  	r, err := repo.LoadRepositoriesFile(file)
   178  	if err == repo.ErrRepoOutOfDate {
   179  		fmt.Fprintln(out, "Updating repository file format...")
   180  		if err := r.WriteFile(file, 0644); err != nil {
   181  			return err
   182  		}
   183  	}
   184  
   185  	return nil
   186  }