github.com/danielqsj/helm@v2.0.0-alpha.4.0.20160908204436-976e0ba5199b+incompatible/pkg/repo/local.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors All rights reserved. 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 repo 18 19 import ( 20 "fmt" 21 "io/ioutil" 22 "net/http" 23 "path/filepath" 24 "strings" 25 26 "k8s.io/helm/pkg/chartutil" 27 "k8s.io/helm/pkg/proto/hapi/chart" 28 ) 29 30 var localRepoPath string 31 32 // StartLocalRepo starts a web server and serves files from the given path 33 func StartLocalRepo(path string) { 34 fmt.Println("Now serving you on localhost:8879...") 35 localRepoPath = path 36 http.HandleFunc("/", rootHandler) 37 http.HandleFunc("/charts/", indexHandler) 38 http.ListenAndServe(":8879", nil) 39 } 40 func rootHandler(w http.ResponseWriter, r *http.Request) { 41 fmt.Fprintf(w, "Welcome to the Kubernetes Package manager!\nBrowse charts on localhost:8879/charts!") 42 } 43 func indexHandler(w http.ResponseWriter, r *http.Request) { 44 file := r.URL.Path[len("/charts/"):] 45 if len(strings.Split(file, ".")) > 1 { 46 serveFile(w, r, file) 47 } else if file == "" { 48 fmt.Fprintf(w, "list of charts should be here at some point") 49 } else if file == "index" { 50 fmt.Fprintf(w, "index file data should be here at some point") 51 } else { 52 fmt.Fprintf(w, "Ummm... Nothing to see here folks") 53 } 54 } 55 56 func serveFile(w http.ResponseWriter, r *http.Request, file string) { 57 http.ServeFile(w, r, filepath.Join(localRepoPath, file)) 58 } 59 60 // AddChartToLocalRepo saves a chart in the given path and then reindexes the index file 61 func AddChartToLocalRepo(ch *chart.Chart, path string) error { 62 _, err := chartutil.Save(ch, path) 63 if err != nil { 64 return err 65 } 66 return Reindex(ch, path+"/index.yaml") 67 } 68 69 // Reindex adds an entry to the index file at the given path 70 func Reindex(ch *chart.Chart, path string) error { 71 name := ch.Metadata.Name + "-" + ch.Metadata.Version 72 y, err := LoadIndexFile(path) 73 if err != nil { 74 return err 75 } 76 found := false 77 for k := range y.Entries { 78 if k == name { 79 found = true 80 break 81 } 82 } 83 if !found { 84 url := "localhost:8879/charts/" + name + ".tgz" 85 86 out, err := y.addEntry(name, url) 87 if err != nil { 88 return err 89 } 90 91 ioutil.WriteFile(path, out, 0644) 92 } 93 return nil 94 }