Skip to content

Commit

Permalink
correctly bootstrap the helm home path
Browse files Browse the repository at this point in the history
  • Loading branch information
JeanMertz committed May 13, 2017
1 parent b8f947d commit a591b23
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 10 deletions.
51 changes: 51 additions & 0 deletions helm/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,26 @@ import (
"fmt"
"os"

"k8s.io/helm/pkg/getter"
"k8s.io/helm/pkg/helm/helmpath"
"k8s.io/helm/pkg/repo"
)

const (
stableRepository = "stable"
stableRepositoryURL = "https://kubernetes-charts.storage.googleapis.com"
)

// Init makes sure the Helm home path exists and the required subfolders.
func Init() error {
if err := ensureDirectories(settings.Home); err != nil {
return err
}

if err := ensureDefaultRepos(settings.Home); err != nil {
return err
}

if err := ensureRepoFileFormat(settings.Home.RepositoryFile()); err != nil {
return err
}
Expand All @@ -27,6 +37,7 @@ func ensureDirectories(home helmpath.Home) error {
home.Repository(),
home.Cache(),
home.Plugins(),
home.Starters(),
}

for _, p := range configDirectories {
Expand All @@ -52,3 +63,43 @@ func ensureRepoFileFormat(file string) error {

return nil
}

func ensureDefaultRepos(home helmpath.Home) error {
repoFile := home.RepositoryFile()
if fi, err := os.Stat(repoFile); err != nil {
f := repo.NewRepoFile()
sr, err := initStableRepo(home.CacheIndex(stableRepository))
if err != nil {
return err
}

f.Add(sr)

if err := f.WriteFile(repoFile, 0644); err != nil {
return err
}
} else if fi.IsDir() {
return fmt.Errorf("%s must be a file, not a directory", repoFile)
}
return nil
}

func initStableRepo(cacheFile string) (*repo.Entry, error) {
c := repo.Entry{
Name: stableRepository,
URL: stableRepositoryURL,
Cache: cacheFile,
}
r, err := repo.NewChartRepository(&c, getter.All(settings))
if err != nil {
return nil, err
}

// In this case, the cacheFile is always absolute. So passing empty string
// is safe.
if err := r.DownloadIndexFile(""); err != nil {
return nil, fmt.Errorf("Looks like %q is not a valid chart repository or cannot be reached: %s", stableRepositoryURL, err.Error())
}

return &c, nil
}
20 changes: 10 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ func main() {
os.Exit(1)
}

if err = helm.Init(); err != nil {
fmt.Fprintf(os.Stderr, "error initialising helm: \n\n%s\n", err)
os.Exit(1)
}

if err = helm.AddRepository("stable", "https://kubernetes-charts.storage.googleapis.com"); err != nil {
fmt.Fprintf(os.Stderr, "error adding repository: \n\n%s\n", err)
os.Exit(1)
}

if cli["--repo"] != nil {
for _, r := range strings.Split(cli["--repo"].(string), ",") {
p := strings.SplitN(r, "=", 2)
Expand All @@ -38,16 +48,6 @@ func main() {
os.Exit(1)
}

if err = helm.Init(); err != nil {
fmt.Fprintf(os.Stderr, "error initialising helm: \n\n%s\n", err)
os.Exit(1)
}

if err = helm.AddRepository("stable", "https://kubernetes-charts.storage.googleapis.com"); err != nil {
fmt.Fprintf(os.Stderr, "error adding repository: \n\n%s\n", err)
os.Exit(1)
}

cc, err := chartsconfig.NewChartsConfiguration(cfg)
if err != nil {
fmt.Fprintf(os.Stderr, "charts config parsing error: \n\n%s\n", err)
Expand Down

0 comments on commit a591b23

Please sign in to comment.