Merge pull request #38 from michelleN/helm-serve

feat(helm): add local chart repository workflow spike
pull/613/head
Michelle Noorali 9 years ago
commit eca7064d07

@ -0,0 +1,46 @@
package main
import (
"fmt"
"io"
"net/http"
"os"
"github.com/spf13/cobra"
)
func init() {
RootCommand.AddCommand(fetchCmd)
}
var fetchCmd = &cobra.Command{
Use: "fetch",
Short: "Download a chart from a repository and unpack it in local directory.",
Long: "",
RunE: fetch,
}
func fetch(cmd *cobra.Command, args []string) error {
// parse args
// get download url
// call download url
out, err := os.Create("nginx-2.0.0.tgz")
if err != nil {
return err
}
defer out.Close()
resp, err := http.Get("http://localhost:8879/charts/nginx-2.0.0.tgz")
fmt.Println("after req")
// unpack file
if err != nil {
return err
}
defer resp.Body.Close()
_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}
return nil
}

@ -19,6 +19,8 @@ Kubernetes Cluster and sets up local configuration in $HELM_HOME (default: ~/.he
const repositoriesPath = ".repositories"
const cachePath = "cache"
const localPath = "local"
const localCacheFilePath = localPath + "/cache.yaml"
var defaultRepo = map[string]string{"default-name": "default-url"}
var tillerImg string
@ -79,7 +81,7 @@ func buildKubectlRunner(kubectlPath string) kubectl.Runner {
//
// If $HELM_HOME does not exist, this function will create it.
func ensureHome(home string) error {
configDirectories := []string{home, cacheDirectory(home)}
configDirectories := []string{home, cacheDirectory(home), localDirectory(home)}
for _, p := range configDirectories {
if fi, err := os.Stat(p); err != nil {
@ -95,12 +97,26 @@ func ensureHome(home string) error {
repoPath := repositoriesFile(home)
if fi, err := os.Stat(repoPath); err != nil {
fmt.Printf("Creating %s \n", repoPath)
if err := ioutil.WriteFile(repoPath, []byte("test-charts: https://www.googleapis.com/storage/v1/b/test-charts/o\n"), 0644); err != nil {
if err := ioutil.WriteFile(repoPath, []byte("local: localhost:8879/charts\n"), 0644); err != nil {
return err
}
} else if fi.IsDir() {
return fmt.Errorf("%s must be a file, not a directory", repoPath)
}
localCacheFile := localDirCacheFile(home)
if fi, err := os.Stat(localCacheFile); err != nil {
fmt.Printf("Creating %s \n", localCacheFile)
_, err := os.Create(localCacheFile)
if err != nil {
return err
}
//TODO: take this out and replace with helm update functionality
os.Symlink(localCacheFile, cacheDirectory(home)+"/local-cache.yaml")
} else if fi.IsDir() {
return fmt.Errorf("%s must be a file, not a directory", repoPath)
}
return nil
}
@ -111,3 +127,11 @@ func cacheDirectory(home string) string {
func repositoriesFile(home string) string {
return filepath.Join(home, repositoriesPath)
}
func localDirectory(home string) string {
return filepath.Join(home, localPath)
}
func localDirCacheFile(home string) string {
return filepath.Join(home, localCacheFilePath)
}

@ -12,7 +12,7 @@ func TestEnsureHome(t *testing.T) {
t.Errorf("%s", err)
}
dirs := []string{home, cacheDirectory(home)}
dirs := []string{home, cacheDirectory(home), localDirectory(home)}
for _, dir := range dirs {
if fi, err := os.Stat(dir); err != nil {
t.Errorf("%s", err)

@ -6,6 +6,7 @@ import (
"path/filepath"
"github.com/deis/tiller/pkg/chart"
"github.com/deis/tiller/pkg/repo"
"github.com/spf13/cobra"
)
@ -20,7 +21,10 @@ Chart.yaml file, and (if found) build the current directory into a chart.
Versioned chart archives are used by Helm package repositories.
`
var save bool
func init() {
packageCmd.Flags().BoolVar(&save, "save", true, "save packaged chart to local chart repository")
RootCommand.AddCommand(packageCmd)
}
@ -50,6 +54,13 @@ func runPackage(cmd *cobra.Command, args []string) error {
return err
}
// Save to $HELM_HOME/local directory.
if save {
if err := repo.AddChartToLocalRepo(ch, localDirectory(os.ExpandEnv(helmHome))); err != nil {
return err
}
}
// Save to the current working directory.
cwd, err := os.Getwd()
if err != nil {
@ -57,7 +68,7 @@ func runPackage(cmd *cobra.Command, args []string) error {
}
name, err := chart.Save(ch, cwd)
if err == nil {
cmd.Printf("Saved %s", name)
cmd.Printf("Saved %s to current directory\n", name)
}
return err
}

@ -0,0 +1,61 @@
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/deis/tiller/pkg/repo"
"github.com/spf13/cobra"
)
func init() {
RootCommand.AddCommand(searchCmd)
}
var searchCmd = &cobra.Command{
Use: "search [CHART]",
Short: "Search for charts",
Long: "", //TODO: add search command description
RunE: search,
}
func search(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("This command needs at least one argument")
}
results, err := searchCacheForPattern(args[0])
if err != nil {
return err
}
cmd.Println("Charts:")
for _, result := range results {
fmt.Println(result)
}
return nil
}
func searchCacheForPattern(name string) ([]string, error) {
dir := cacheDirectory(os.ExpandEnv(helmHome))
fileList := []string{}
filepath.Walk(dir, func(path string, f os.FileInfo, err error) error {
if !f.IsDir() {
fileList = append(fileList, path)
}
return nil
})
matches := []string{}
for _, f := range fileList {
cache, _ := repo.LoadCacheFile(f)
repoName := filepath.Base(strings.TrimRight(f, "-cache.txt"))
for k := range cache.Entries {
if strings.Contains(k, name) {
matches = append(matches, repoName+"/"+k)
}
}
}
return matches, nil
}

@ -0,0 +1,28 @@
package main
import (
"os"
"github.com/deis/tiller/pkg/repo"
"github.com/spf13/cobra"
)
var serveDesc = `This command starts a local chart repository server that serves the charts saved in your $HELM_HOME/local/ directory.`
//TODO: add repoPath flag to be passed in in case you want
// to serve charts from a different local dir
func init() {
RootCommand.AddCommand(serveCmd)
}
var serveCmd = &cobra.Command{
Use: "serve",
Short: "Start a local http web server",
Long: serveDesc,
Run: serve,
}
func serve(cmd *cobra.Command, args []string) {
repo.StartLocalRepo(localDirectory(os.ExpandEnv(helmHome)))
}

@ -0,0 +1,136 @@
package repo
import (
"fmt"
"io/ioutil"
"net/http"
"path/filepath"
"strings"
"github.com/deis/tiller/pkg/chart"
"gopkg.in/yaml.v2"
)
var localRepoPath string
// CacheFile represents the cache file in a chart repository
type CacheFile struct {
Entries map[string]*ChartRef
}
// ChartRef represents a chart entry in the CacheFile
type ChartRef struct {
Name string
URL string
}
// StartLocalRepo starts a web server and serves files from the given path
func StartLocalRepo(path string) {
fmt.Println("Now serving you on localhost:8879...")
localRepoPath = path
http.HandleFunc("/", rootHandler)
http.HandleFunc("/charts/", indexHandler)
http.ListenAndServe(":8879", nil)
}
func rootHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Welcome to the Kubernetes Package manager!\nBrowse charts on localhost:8879/charts!")
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
file := r.URL.Path[len("/charts/"):]
if len(strings.Split(file, ".")) > 1 {
serveFile(w, r, file)
} else if file == "" {
fmt.Fprintf(w, "list of charts should be here at some point")
} else if file == "cache" {
fmt.Fprintf(w, "cache file data should be here at some point")
} else {
fmt.Fprintf(w, "Ummm... Nothing to see here folks")
}
}
func serveFile(w http.ResponseWriter, r *http.Request, file string) {
http.ServeFile(w, r, filepath.Join(localRepoPath, file))
}
// AddChartToLocalRepo saves a chart in the given path and then reindexes the cache file
func AddChartToLocalRepo(ch *chart.Chart, path string) error {
name, err := chart.Save(ch, path)
if err != nil {
return err
}
err = ReindexCacheFile(ch, path+"/cache.yaml")
if err != nil {
return err
}
fmt.Printf("Saved %s to $HELM_HOME/local", name)
return nil
}
// LoadCacheFile takes a file at the given path and returns a CacheFile object
func LoadCacheFile(path string) (*CacheFile, error) {
b, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
//TODO: change variable name - y is not helpful :P
var y CacheFile
err = yaml.Unmarshal(b, &y)
if err != nil {
return nil, err
}
return &y, nil
}
// ReindexCacheFile adds an entry to the cache file at the given path
func ReindexCacheFile(ch *chart.Chart, path string) error {
name := ch.Chartfile().Name + "-" + ch.Chartfile().Version
y, err := LoadCacheFile(path)
if err != nil {
return err
}
found := false
for k := range y.Entries {
if k == name {
found = true
break
}
}
if !found {
url := "localhost:8879/charts/" + name + ".tgz"
out, err := y.insertChartEntry(name, url)
if err != nil {
return err
}
ioutil.WriteFile(path, out, 0644)
}
return nil
}
// UnmarshalYAML unmarshals the cache file
func (c *CacheFile) UnmarshalYAML(unmarshal func(interface{}) error) error {
var refs map[string]*ChartRef
if err := unmarshal(&refs); err != nil {
if _, ok := err.(*yaml.TypeError); !ok {
return err
}
}
c.Entries = refs
return nil
}
func (c *CacheFile) insertChartEntry(name string, url string) ([]byte, error) {
if c.Entries == nil {
c.Entries = make(map[string]*ChartRef)
}
entry := ChartRef{Name: name, URL: url}
c.Entries[name] = &entry
out, err := yaml.Marshal(&c.Entries)
if err != nil {
return nil, err
}
return out, nil
}
Loading…
Cancel
Save