Ejemplo n.º 1
0
// Resolve takes a chart and a location and checks whether the chart's dependencies are satisfied.
//
// The `installdir` is the location where installed charts are located. Typically
// this is in $HELM_HOME/workspace/charts.
//
// This returns a list of unsatisfied dependencies (NOT an error condition).
//
// It returns an error only if it cannot perform the task of resolving dependencies.
// Failed dependencies to not constitute an error.
func Resolve(cf *chart.Chartfile, installdir string) ([]*chart.Dependency, error) {
	if len(cf.Dependencies) == 0 {
		log.Debug("No dependencies to check. :achievement-unlocked:")
		return []*chart.Dependency{}, nil
	}

	cache, err := dependencyCache(installdir)
	if err != nil {
		log.Debug("Failed to build dependency cache: %s", err)
		return []*chart.Dependency{}, err
	}

	res := []*chart.Dependency{}

	// TODO: This could be made more efficient.
	for _, check := range cf.Dependencies {
		resolved := false
		for n, chart := range cache {
			log.Debug("Checking if %s (%s) %s meets %s %s", chart.Name, n, chart.Version, check.Name, check.Version)
			if chart.Name == check.Name && check.VersionOK(chart.Version) {
				log.Debug("✔︎")
				resolved = true
				break
			}
		}
		if !resolved {
			log.Debug("No matches found for %s %s", check.Name, check.Version)
			res = append(res, check)
		}
	}
	return res, nil
}
Ejemplo n.º 2
0
// Check by chart directory name whether a chart is fetched into the workspace.
//
// This does NOT check the Chart.yaml file.
func chartFetched(chartName, home string) bool {
	p := helm.WorkspaceChartDirectory(home, chartName, Chartfile)
	log.Debug("Looking for %q", p)
	if fi, err := os.Stat(p); err != nil || fi.IsDir() {
		log.Debug("No chart: %s", err)
		return false
	}
	return true
}
Ejemplo n.º 3
0
// Check by chart directory name whether a chart is fetched into the workspace.
//
// This does NOT check the Chart.yaml file.
func chartFetched(chartName, home string) bool {
	p := filepath.Join(home, WorkspaceChartPath, chartName, "Chart.yaml")
	log.Debug("Looking for %q", p)
	if fi, err := os.Stat(p); err != nil || fi.IsDir() {
		log.Debug("No chart: %s", err)
		return false
	}
	return true
}
Ejemplo n.º 4
0
func fetch(chartName, lname, homedir, chartpath string) {
	src := helm.CacheDirectory(homedir, chartpath, chartName)
	dest := helm.WorkspaceChartDirectory(homedir, lname)

	fi, err := os.Stat(src)
	if err != nil {
		log.Warn("Oops. Looks like there was an issue finding the chart, %s, n %s. Running `helm update` to ensure you have the latest version of all Charts from Github...", lname, src)
		Update(homedir)
		fi, err = os.Stat(src)
		if err != nil {
			log.Die("Chart %s not found in %s", lname, src)
		}
		log.Info("Good news! Looks like that did the trick. Onwards and upwards!")
	}

	if !fi.IsDir() {
		log.Die("Malformed chart %s: Chart must be in a directory.", chartName)
	}

	if err := os.MkdirAll(dest, 0755); err != nil {
		log.Die("Could not create %q: %s", dest, err)
	}

	log.Debug("Fetching %s to %s", src, dest)
	if err := helm.CopyDir(src, dest); err != nil {
		log.Die("Failed copying %s to %s", src, dest)
	}

	if err := updateChartfile(src, dest, lname); err != nil {
		log.Die("Failed to update Chart.yaml: %s", err)
	}
}
Ejemplo n.º 5
0
// ensureHome ensures that a HELM_HOME exists.
func ensureHome(home string) {

	must := []string{home, filepath.Join(home, CachePath), filepath.Join(home, WorkspacePath), filepath.Join(home, CacheChartPath)}

	for _, p := range must {
		if fi, err := os.Stat(p); err != nil {
			log.Debug("Creating %s", p)
			if err := os.MkdirAll(p, 0755); err != nil {
				log.Die("Could not create %q: %s", p, err)
			}
		} else if !fi.IsDir() {
			log.Die("%s must be a directory.", home)
		}
	}

	refi := filepath.Join(home, Configfile)
	if _, err := os.Stat(refi); err != nil {
		log.Info("Creating %s", refi)
		// Attempt to create a Repos.yaml
		if err := ioutil.WriteFile(refi, []byte(config.DefaultConfigfile), 0755); err != nil {
			log.Die("Could not create %s: %s", refi, err)
		}
	}

	if err := os.Chdir(home); err != nil {
		log.Die("Could not change to directory %q: %s", home, err)
	}
}
Ejemplo n.º 6
0
// gitUpdate updates a Git repo.
func gitUpdate(git *vcs.GitRepo) error {
	if err := git.Update(); err != nil {
		return err
	}

	log.Debug("Updated %s from %s", git.LocalPath(), git.Remote())
	return nil
}
Ejemplo n.º 7
0
func marshalAndCreate(o interface{}, ns string, client kubectl.Runner) ([]byte, error) {
	var b bytes.Buffer
	if err := codec.JSON.Encode(&b).One(o); err != nil {
		return nil, err
	}
	data := b.Bytes()
	log.Debug("File: %s", string(data))
	return client.Create(data, ns)
}
Ejemplo n.º 8
0
// Copy a directory and its subdirectories.
func copyDir(src, dst string) error {

	var failure error

	walker := func(fname string, fi os.FileInfo, e error) error {
		if e != nil {
			log.Warn("Encounter error walking %q: %s", fname, e)
			failure = e
			return nil
		}

		log.Debug("Copying %s", fname)
		rf, err := filepath.Rel(src, fname)
		if err != nil {
			log.Warn("Could not find relative path: %s", err)
			return nil
		}
		df := filepath.Join(dst, rf)

		// Handle directories by creating mirrors.
		if fi.IsDir() {
			if err := os.MkdirAll(df, fi.Mode()); err != nil {
				log.Warn("Could not create %q: %s", df, err)
				failure = err
			}
			return nil
		}

		// Otherwise, copy files.
		in, err := os.Open(fname)
		if err != nil {
			log.Warn("Skipping file %s: %s", fname, err)
			return nil
		}
		out, err := os.Create(df)
		if err != nil {
			in.Close()
			log.Warn("Skipping file copy %s: %s", fname, err)
			return nil
		}
		if _, err = io.Copy(out, in); err != nil {
			log.Warn("Copy from %s to %s failed: %s", fname, df, err)
		}

		if err := out.Close(); err != nil {
			log.Warn("Failed to close %q: %s", df, err)
		}
		if err := in.Close(); err != nil {
			log.Warn("Failed to close reader %q: %s", fname, err)
		}

		return nil
	}
	filepath.Walk(src, walker)
	return failure
}
Ejemplo n.º 9
0
func (r *Repos) deleteRepo(name string) error {
	rpath := filepath.Join(r.Dir, name)
	if fi, err := os.Stat(rpath); err != nil || !fi.IsDir() {
		log.Info("Deleted nothing. No repo named %s", name)
		return nil
	}

	log.Debug("Deleting %s", rpath)
	return os.RemoveAll(rpath)
}
Ejemplo n.º 10
0
// renderTemplate renders a template and values into an output stream.
//
// tpl should be a string template.
func renderTemplate(out io.Writer, tpl string, vals interface{}) error {
	t, err := template.New("helmTpl").Funcs(sprig.TxtFuncMap()).Parse(tpl)
	if err != nil {
		return err
	}

	log.Debug("Vals: %#v", vals)

	if err := t.ExecuteTemplate(out, "helmTpl", vals); err != nil {
		return err
	}
	return nil
}
Ejemplo n.º 11
0
func kubectlDelete(name, ktype, ns string) error {
	log.Debug("Deleting %s (%s)", name, ktype)
	a := []string{"delete", ktype, name}
	if ns != "" {
		a = append([]string{fmt.Sprintf("--namespace=%q", ns)}, a...)
	}

	cmd := exec.Command("kubectl", a...)

	if d, err := cmd.CombinedOutput(); err != nil {
		return fmt.Errorf("%s: %s", string(d), err)
	}
	return nil
}
Ejemplo n.º 12
0
func repoChartDiff(rpath, initialVersion string) (string, error) {
	// build git diff-tree command
	cmd := exec.Command("git", "-C", rpath, "diff-tree", "--name-status", fmt.Sprintf("%s..HEAD", initialVersion))

	log.Debug("git diff cmd: %s", cmd.Args)

	out, err := cmd.CombinedOutput()
	if err != nil {
		return "", err
	}

	// cleanup any trailing whitespace
	return strings.TrimSpace(string(out)), nil
}
Ejemplo n.º 13
0
// ParseDir parses all of the manifests inside of a chart directory.
//
// The directory should be the Chart directory (contains Chart.yaml and manifests/)
//
// This will return an error if the directory does not exist, or if there is an
// error parsing or decoding any yaml files.
func ParseDir(chartDir string) ([]*Manifest, error) {
	dir := filepath.Join(chartDir, "manifests")
	files := []*Manifest{}

	if _, err := os.Stat(dir); err != nil {
		return files, err
	}

	// add manifest files
	walker := func(fname string, fi os.FileInfo, e error) error {
		log.Debug("Parsing %s", fname)
		// Chauncey was right.
		if e != nil {
			return e
		}

		if fi.IsDir() {
			return nil
		}

		if filepath.Ext(fname) != ".yaml" {
			log.Debug("Skipping %s. Not a YAML file.", fname)
			return nil
		}

		m, err := Parse(fname)
		if err != nil {
			return err
		}

		files = append(files, m...)

		return nil
	}

	return files, filepath.Walk(dir, walker)
}
Ejemplo n.º 14
0
func fetch(chartName, lname, homedir, chartpath string) {
	src := filepath.Join(homedir, CachePath, chartpath, chartName)
	dest := filepath.Join(homedir, WorkspaceChartPath, lname)

	if fi, err := os.Stat(src); err != nil {
		log.Die("Chart %s not found in %s", lname, src)
	} else if !fi.IsDir() {
		log.Die("Malformed chart %s: Chart must be in a directory.", chartName)
	}

	if err := os.MkdirAll(dest, 0755); err != nil {
		log.Die("Could not create %q: %s", dest, err)
	}

	log.Debug("Fetching %s to %s", src, dest)
	if err := copyDir(src, dest); err != nil {
		log.Die("Failed copying %s to %s", src, dest)
	}
}
Ejemplo n.º 15
0
// PrintREADME prints the README file (if it exists) to the console.
func PrintREADME(chart, home string) {
	p := filepath.Join(home, WorkspaceChartPath, chart, "README.*")
	files, err := filepath.Glob(p)
	if err != nil || len(files) == 0 {
		// No README. Skip.
		log.Debug("No readme in %s", p)
		return
	}

	f, err := os.Open(files[0])
	if err != nil {
		log.Warn("Could not read README: %s", err)
		return
	}
	log.Msg(strings.Repeat("=", 40))
	io.Copy(log.Stdout, f)
	log.Msg(strings.Repeat("=", 40))
	f.Close()

}
Ejemplo n.º 16
0
// uploadManifests sends manifests to Kubectl in a particular order.
func uploadManifests(c *chart.Chart, namespace string, client kubectl.Runner) error {

	// Install known kinds in a predictable order.
	for _, k := range InstallOrder {
		for _, m := range c.Kind[k] {
			o := m.VersionedObject
			o.AddAnnotations(map[string]string{
				chart.AnnFile:         m.Source,
				chart.AnnChartVersion: c.Chartfile.Version,
				chart.AnnChartDesc:    c.Chartfile.Description,
				chart.AnnChartName:    c.Chartfile.Name,
			})
			var data []byte
			var err error
			if data, err = o.JSON(); err != nil {
				return err
			}
			log.Debug("File: %s", string(data))
			out, err := client.Create(data, namespace)
			log.Msg(string(out))
			if err != nil {
				return err
			}
		}
	}

	// Install unknown kinds afterward. Order here is not predictable.
	for _, k := range c.UnknownKinds(InstallOrder) {
		for _, o := range c.Kind[k] {
			o.VersionedObject.AddAnnotations(map[string]string{chart.AnnFile: o.Source})
			out, err := marshalAndCreate(o.VersionedObject, namespace, client)
			log.Msg(string(out))
			if err != nil {
				return err
			}
		}
	}

	return nil
}
Ejemplo n.º 17
0
// checkManifests gets any installed manifests within a chart
func checkManifests(chartPath string) ([]string, error) {
	var foundManifests []string

	manifests, err := manifest.Files(chartPath)
	if err != nil {
		return nil, err
	}

	for _, m := range manifests {
		out := kubeGet(m)

		if strings.Contains(out, "unable to connect") {
			return nil, fmt.Errorf(out)
		}
		if !strings.Contains(out, "not found") {
			foundManifests = append(foundManifests, m)
		}
	}

	log.Debug("Found %d installed manifests", len(foundManifests))

	return foundManifests, nil
}
Ejemplo n.º 18
0
// kubectlCreate calls `kubectl create` and sends the data via Stdin.
//
// If dryRun is set to true, then we just output the command that was
// going to be run to os.Stdout and return nil.
func kubectlCreate(data []byte, ns string, dryRun bool) error {
	a := []string{"create", "-f", "-"}

	if ns != "" {
		a = append([]string{"--namespace=" + ns}, a...)
	}

	if dryRun {
		cmd := "kubectl"
		for _, arg := range a {
			cmd = fmt.Sprintf("%s %s", cmd, arg)
		}
		cmd = fmt.Sprintf("%s < %s", cmd, data)
		log.Info(cmd)
		return nil
	}

	c := exec.Command("kubectl", a...)
	in, err := c.StdinPipe()
	if err != nil {
		return err
	}

	c.Stdout = os.Stdout
	c.Stderr = os.Stderr

	if err := c.Start(); err != nil {
		return err
	}

	log.Debug("File: %s", string(data))
	in.Write(data)
	in.Close()

	return c.Wait()
}
Ejemplo n.º 19
0
import (
	"fmt"
	"os"
	"os/exec"
	"strings"

	"github.com/helm/helm/log"
	"github.com/helm/helm/manifest"
	helm "github.com/helm/helm/util"
)

// kubeGetter wraps the kubectl command, override in tests
type kubeGetter func(string) string

var kubeGet kubeGetter = func(m string) string {
	log.Debug("Getting manifests from %s", m)

	a := []string{"get", "-f", m}
	out, _ := exec.Command("kubectl", a...).CombinedOutput()
	return string(out)
}

// Remove removes a chart from the workdir.
//
// - chart is the source
// - homedir is the home directory for the user
// - force will remove installed charts from workspace
func Remove(chart, homedir string, force bool) {
	chartPath := helm.WorkspaceChartDirectory(homedir, chart)
	if _, err := os.Stat(chartPath); err != nil {
		log.Err("Chart not found. %s", err)
Ejemplo n.º 20
0
// Walk walks a chart directory and executes generators as it finds them.
//
// Returns the number of generators executed.
//
// Walking will error out whenever a generator cannot be completely executed.
// This includes cases such as not finding the generator referenced, and
// cases where the generator itself exits with a non-zero exit code.
func Walk(dir string, exclude []string) (int, error) {

	excludes := make(map[string]bool, len(exclude))
	for i := 0; i < len(exclude); i++ {
		excludes[filepath.Join(dir, exclude[i])] = true
	}

	count := 0
	err := filepath.Walk(dir, func(path string, fi os.FileInfo, err error) error {

		// dive-bomb if we hit an error.
		if err != nil {
			return err
		}

		// Exclude anything explicitly excluded.
		if excludes[path] == true {
			if fi.IsDir() {
				return filepath.SkipDir
			}
			return nil
		}

		// Skip directory entries. If the directory prefix is . or _, skip the
		// contents of the directory as well.
		if fi.IsDir() {
			return skip(path)
		}

		f, err := os.Open(path)
		if err != nil {
			return err
		}
		defer f.Close()

		line, err := readGenerator(f)
		if err != nil {
			return err
		}
		if line == "" {
			return nil
		}
		// Run the generator.
		os.Setenv("HELM_GENERATE_COMMAND", line)
		os.Setenv("HELM_GENERATE_FILE", path)
		os.Setenv("HELM_GENERATE_DIR", dir)
		line = os.ExpandEnv(line)
		os.Setenv("HELM_GENERATE_COMMAND_EXPANDED", line)
		log.Debug("File: %s, Command: %s", path, line)
		count++

		// Execute the command in the file's directory to make relative
		// paths usable.
		origin, err := os.Getwd()
		if err != nil {
			log.Warn("Could not get PWD: %s", err)
		} else if err := os.Chdir(dir); err != nil {
			log.Warn("Could not change directory to %s: %s", dir, err)
		} else {
			origin = dir
			defer func() {
				if e := os.Chdir(origin); e != nil {
					log.Warn("Could not return to %s: %s", origin, e)
				}
			}()
		}
		err = execute(line)
		if err != nil {
			return fmt.Errorf("failed to execute %s (%s): %s", line, path, err)
		}
		return nil
	})

	return count, err
}