blob: 79658a17f451951b6f8a25135230e9202bea1f98 [file] [log] [blame]
giolekvad5a58c42020-05-07 22:18:35 +04001package main
2
3import (
giolekvaa4a153b2020-05-12 11:49:53 +04004 "flag"
5 "fmt"
6 "io"
7 "log"
8 "net/http"
9 "os"
giolekvad5a58c42020-05-07 22:18:35 +040010
giolekvaa4a153b2020-05-12 11:49:53 +040011 // "github.com/golang/glog"
12
13 app "github.com/giolekva/pcloud/appmanager"
giolekvad5a58c42020-05-07 22:18:35 +040014)
15
giolekvaa4a153b2020-05-12 11:49:53 +040016var port = flag.Int("port", 1234, "Port to listen on.")
17
18var helmUploadPage = `
19<html>
20<head>
21 <title>Upload Helm chart</title>
22</head>
23<body>
24<form enctype="multipart/form-data" action="/" method="post">
25 <input type="file" name="chartfile" />
26 <input type="submit" value="upload" />
27</form>
28</body>
29</html>
30`
31
32func helmHandler(w http.ResponseWriter, r *http.Request) {
33 if r.Method == "GET" {
34 _, err := io.WriteString(w, helmUploadPage)
35 if err != nil {
36 http.Error(w, err.Error(), http.StatusInternalServerError)
37 }
38 } else if r.Method == "POST" {
39 r.ParseMultipartForm(1000000)
40 file, handler, err := r.FormFile("chartfile")
41 if err != nil {
42 http.Error(w, err.Error(), http.StatusInternalServerError)
43 return
44 }
45 defer file.Close()
46 p := "/tmp/" + handler.Filename
47 f, err := os.OpenFile(p, os.O_WRONLY|os.O_CREATE, 0666)
48 if err != nil {
49 fmt.Println(err)
50 return
51 }
52 defer f.Close()
53 _, err = io.Copy(f, file)
54 if err != nil {
55 http.Error(w, err.Error(), http.StatusInternalServerError)
56 return
57 }
58 if err = installHelmChart(p); err != nil {
59 http.Error(w, err.Error(), http.StatusInternalServerError)
60 return
61 }
62 w.Write([]byte("Installed"))
giolekvad5a58c42020-05-07 22:18:35 +040063 }
giolekvaa4a153b2020-05-12 11:49:53 +040064}
65
66func installHelmChart(path string) error {
67 h, err := app.HelmChartFromDir("/Users/lekva/dev/go/src/github.com/giolekva/pcloud/apps/rpuppy/chart")
68 if err != nil {
69 return err
70 }
71 // err = app.InstallSchema(h.Schema, "http://localhost:1111/add_schema")
72 // if err != nil {
73 // return err
74 // }
75 // glog.Infof("Installed schema: %s", h.Schema)
76 err = h.Install(
77 "/usr/local/bin/helm",
78 map[string]string{})
79 return err
80}
81
82func main() {
83 flag.Parse()
84 http.HandleFunc("/", helmHandler)
85 log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *port), nil))
86
giolekvad5a58c42020-05-07 22:18:35 +040087}