mirror of https://github.com/helm/helm
Refactor helm init command for reuse, allowing other programs to initialize local helm home directory without shelling out to the helm binary
Signed-off-by: Patrick Decat <pdecat@gmail.com>pull/5189/head
parent
c94c00915f
commit
7da53d6e70
@ -0,0 +1,166 @@
|
|||||||
|
/*
|
||||||
|
Copyright The Helm Authors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package installer // import "k8s.io/helm/cmd/helm/installer"
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"k8s.io/helm/pkg/getter"
|
||||||
|
helm_env "k8s.io/helm/pkg/helm/environment"
|
||||||
|
"k8s.io/helm/pkg/helm/helmpath"
|
||||||
|
"k8s.io/helm/pkg/repo"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
stableRepository = "stable"
|
||||||
|
|
||||||
|
// LocalRepository is the standard name of the local repository
|
||||||
|
LocalRepository = "local"
|
||||||
|
|
||||||
|
// LocalRepositoryIndexFile is the standard name of the local repository index file
|
||||||
|
LocalRepositoryIndexFile = "index.yaml"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Initialize initializes local config
|
||||||
|
//
|
||||||
|
// Returns an error if the command failed.
|
||||||
|
func Initialize(home helmpath.Home, out io.Writer, skipRefresh bool, settings helm_env.EnvSettings, stableRepositoryURL, localRepositoryURL string) error {
|
||||||
|
if err := ensureDirectories(home, out); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := ensureDefaultRepos(home, out, skipRefresh, settings, stableRepositoryURL, localRepositoryURL); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := ensureRepoFileFormat(home.RepositoryFile(), out); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ensureDirectories checks to see if $HELM_HOME exists.
|
||||||
|
//
|
||||||
|
// If $HELM_HOME does not exist, this function will create it.
|
||||||
|
func ensureDirectories(home helmpath.Home, out io.Writer) error {
|
||||||
|
configDirectories := []string{
|
||||||
|
home.String(),
|
||||||
|
home.Repository(),
|
||||||
|
home.Cache(),
|
||||||
|
home.LocalRepository(),
|
||||||
|
home.Plugins(),
|
||||||
|
home.Starters(),
|
||||||
|
home.Archive(),
|
||||||
|
}
|
||||||
|
for _, p := range configDirectories {
|
||||||
|
if fi, err := os.Stat(p); err != nil {
|
||||||
|
fmt.Fprintf(out, "Creating %s \n", p)
|
||||||
|
if err := os.MkdirAll(p, 0755); err != nil {
|
||||||
|
return fmt.Errorf("Could not create %s: %s", p, err)
|
||||||
|
}
|
||||||
|
} else if !fi.IsDir() {
|
||||||
|
return fmt.Errorf("%s must be a directory", p)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func ensureDefaultRepos(home helmpath.Home, out io.Writer, skipRefresh bool, settings helm_env.EnvSettings, stableRepositoryURL, localRepositoryURL string) error {
|
||||||
|
repoFile := home.RepositoryFile()
|
||||||
|
if fi, err := os.Stat(repoFile); err != nil {
|
||||||
|
fmt.Fprintf(out, "Creating %s \n", repoFile)
|
||||||
|
f := repo.NewRepoFile()
|
||||||
|
sr, err := initStableRepo(home.CacheIndex(stableRepository), home, out, skipRefresh, settings, stableRepositoryURL)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
lr, err := initLocalRepo(home.LocalRepository(LocalRepositoryIndexFile), home.CacheIndex("local"), home, out, settings, localRepositoryURL)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
f.Add(sr)
|
||||||
|
f.Add(lr)
|
||||||
|
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, home helmpath.Home, out io.Writer, skipRefresh bool, settings helm_env.EnvSettings, stableRepositoryURL string) (*repo.Entry, error) {
|
||||||
|
fmt.Fprintf(out, "Adding %s repo with URL: %s \n", stableRepository, stableRepositoryURL)
|
||||||
|
c := repo.Entry{
|
||||||
|
Name: stableRepository,
|
||||||
|
URL: stableRepositoryURL,
|
||||||
|
Cache: cacheFile,
|
||||||
|
}
|
||||||
|
r, err := repo.NewChartRepository(&c, getter.All(settings))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if skipRefresh {
|
||||||
|
return &c, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
|
||||||
|
func initLocalRepo(indexFile, cacheFile string, home helmpath.Home, out io.Writer, settings helm_env.EnvSettings, localRepositoryURL string) (*repo.Entry, error) {
|
||||||
|
if fi, err := os.Stat(indexFile); err != nil {
|
||||||
|
fmt.Fprintf(out, "Adding %s repo with URL: %s \n", LocalRepository, localRepositoryURL)
|
||||||
|
i := repo.NewIndexFile()
|
||||||
|
if err := i.WriteFile(indexFile, 0644); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
//TODO: take this out and replace with helm update functionality
|
||||||
|
if err := createLink(indexFile, cacheFile, home); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
} else if fi.IsDir() {
|
||||||
|
return nil, fmt.Errorf("%s must be a file, not a directory", indexFile)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &repo.Entry{
|
||||||
|
Name: LocalRepository,
|
||||||
|
URL: localRepositoryURL,
|
||||||
|
Cache: cacheFile,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func ensureRepoFileFormat(file string, out io.Writer) error {
|
||||||
|
r, err := repo.LoadRepositoriesFile(file)
|
||||||
|
if err == repo.ErrRepoOutOfDate {
|
||||||
|
fmt.Fprintln(out, "Updating repository file format...")
|
||||||
|
if err := r.WriteFile(file, 0644); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
@ -0,0 +1,120 @@
|
|||||||
|
/*
|
||||||
|
Copyright The Helm Authors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package installer // import "k8s.io/helm/cmd/helm/installer"
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
helm_env "k8s.io/helm/pkg/helm/environment"
|
||||||
|
"k8s.io/helm/pkg/helm/helmpath"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestInitialize(t *testing.T) {
|
||||||
|
home, err := ioutil.TempDir("", "helm_home")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer os.RemoveAll(home)
|
||||||
|
|
||||||
|
b := bytes.NewBuffer(nil)
|
||||||
|
hh := helmpath.Home(home)
|
||||||
|
|
||||||
|
settings := helm_env.EnvSettings{
|
||||||
|
Home: hh,
|
||||||
|
}
|
||||||
|
stableRepositoryURL := "https://kubernetes-charts.storage.googleapis.com"
|
||||||
|
localRepositoryURL := "http://127.0.0.1:8879/charts"
|
||||||
|
|
||||||
|
if err := Initialize(hh, b, false, settings, stableRepositoryURL, localRepositoryURL); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
expectedDirs := []string{hh.String(), hh.Repository(), hh.Cache(), hh.LocalRepository()}
|
||||||
|
for _, dir := range expectedDirs {
|
||||||
|
if fi, err := os.Stat(dir); err != nil {
|
||||||
|
t.Errorf("%s", err)
|
||||||
|
} else if !fi.IsDir() {
|
||||||
|
t.Errorf("%s is not a directory", fi)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if fi, err := os.Stat(hh.RepositoryFile()); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
} else if fi.IsDir() {
|
||||||
|
t.Errorf("%s should not be a directory", fi)
|
||||||
|
}
|
||||||
|
|
||||||
|
if fi, err := os.Stat(hh.LocalRepository(LocalRepositoryIndexFile)); err != nil {
|
||||||
|
t.Errorf("%s", err)
|
||||||
|
} else if fi.IsDir() {
|
||||||
|
t.Errorf("%s should not be a directory", fi)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEnsureHome(t *testing.T) {
|
||||||
|
home, err := ioutil.TempDir("", "helm_home")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer os.RemoveAll(home)
|
||||||
|
|
||||||
|
b := bytes.NewBuffer(nil)
|
||||||
|
hh := helmpath.Home(home)
|
||||||
|
|
||||||
|
settings := helm_env.EnvSettings{
|
||||||
|
Home: hh,
|
||||||
|
}
|
||||||
|
stableRepositoryURL := "https://kubernetes-charts.storage.googleapis.com"
|
||||||
|
localRepositoryURL := "http://127.0.0.1:8879/charts"
|
||||||
|
|
||||||
|
if err := ensureDirectories(hh, b); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
if err := ensureDefaultRepos(hh, b, false, settings, stableRepositoryURL, localRepositoryURL); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
if err := ensureDefaultRepos(hh, b, true, settings, stableRepositoryURL, localRepositoryURL); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
if err := ensureRepoFileFormat(hh.RepositoryFile(), b); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
expectedDirs := []string{hh.String(), hh.Repository(), hh.Cache(), hh.LocalRepository()}
|
||||||
|
for _, dir := range expectedDirs {
|
||||||
|
if fi, err := os.Stat(dir); err != nil {
|
||||||
|
t.Errorf("%s", err)
|
||||||
|
} else if !fi.IsDir() {
|
||||||
|
t.Errorf("%s is not a directory", fi)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if fi, err := os.Stat(hh.RepositoryFile()); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
} else if fi.IsDir() {
|
||||||
|
t.Errorf("%s should not be a directory", fi)
|
||||||
|
}
|
||||||
|
|
||||||
|
if fi, err := os.Stat(hh.LocalRepository(LocalRepositoryIndexFile)); err != nil {
|
||||||
|
t.Errorf("%s", err)
|
||||||
|
} else if fi.IsDir() {
|
||||||
|
t.Errorf("%s should not be a directory", fi)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue