From 4d07db7b2c2eae20d8905daf14fdf41f1d8732c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=C3=ABl=20Cluseau?= Date: Fri, 1 Feb 2019 18:35:50 +1100 Subject: [PATCH] restful /configs --- .gitignore | 1 + cmd/dkl-local-server/http.go | 78 ---------------------------------- cmd/dkl-local-server/main.go | 2 +- cmd/dkl-local-server/ws.go | 82 +++++++++++++++++++++++++++++++++++- 4 files changed, 82 insertions(+), 81 deletions(-) diff --git a/.gitignore b/.gitignore index 4c933b8..07515b7 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ *.sw[po] +modd-local.conf diff --git a/cmd/dkl-local-server/http.go b/cmd/dkl-local-server/http.go index 7870167..c4214e3 100644 --- a/cmd/dkl-local-server/http.go +++ b/cmd/dkl-local-server/http.go @@ -1,16 +1,11 @@ package main import ( - "compress/gzip" "encoding/json" "flag" - "io" - "io/ioutil" "log" "net" "net/http" - "os" - "path/filepath" "regexp" "strings" @@ -236,79 +231,6 @@ func serveCluster(w http.ResponseWriter, r *http.Request) { } } -func uploadConfig(w http.ResponseWriter, r *http.Request) { - if !authorizeAdmin(r) { - forbidden(w, r) - return - } - - if r.Method != "POST" { - http.NotFound(w, r) - return - } - - out, err := ioutil.TempFile(*dataDir, ".config-upload") - if err != nil { - writeError(w, err) - return - } - - defer os.Remove(out.Name()) - - _, err = io.Copy(out, r.Body) - out.Close() - if err != nil { - writeError(w, err) - return - } - - archivesPath := filepath.Join(*dataDir, "archives") - cfgPath := configFilePath() - - err = os.MkdirAll(archivesPath, 0700) - if err != nil { - writeError(w, err) - return - } - - err = func() (err error) { - backupPath := filepath.Join(archivesPath, "config."+ulid()+".yaml.gz") - - bck, err := os.Create(backupPath) - if err != nil { - return - } - - defer bck.Close() - - in, err := os.Open(cfgPath) - if err != nil { - return - } - - gz, err := gzip.NewWriterLevel(bck, 2) - if err != nil { - return - } - - _, err = io.Copy(gz, in) - gz.Close() - in.Close() - return - }() - - if err != nil { - writeError(w, err) - return - } - - err = os.Rename(out.Name(), cfgPath) - if err != nil { - writeError(w, err) - return - } -} - func writeError(w http.ResponseWriter, err error) { log.Print("request failed: ", err) w.WriteHeader(http.StatusInternalServerError) diff --git a/cmd/dkl-local-server/main.go b/cmd/dkl-local-server/main.go index a8d8234..fdde307 100644 --- a/cmd/dkl-local-server/main.go +++ b/cmd/dkl-local-server/main.go @@ -42,7 +42,7 @@ func main() { // by default, serve a host resource by its IP //http.HandleFunc("/", serveHostByIP) - http.HandleFunc("/configs", uploadConfig) + //http.HandleFunc("/configs", uploadConfig) http.HandleFunc("/hosts", serveHosts) //http.HandleFunc("/hosts/", serveHost) diff --git a/cmd/dkl-local-server/ws.go b/cmd/dkl-local-server/ws.go index d9a13f8..1b2df3e 100644 --- a/cmd/dkl-local-server/ws.go +++ b/cmd/dkl-local-server/ws.go @@ -1,8 +1,14 @@ package main import ( + "compress/gzip" + "io" + "io/ioutil" "log" "net" + "net/http" + "os" + "path/filepath" "strings" "github.com/emicklei/go-restful" @@ -59,6 +65,78 @@ func detectHost(req *restful.Request) string { return host.Name } -func wsUploadConfig(req *restful.Request, res *restful.Response) { - // TODO +func wsUploadConfig(req *restful.Request, resp *restful.Response) { + r := req.Request + w := resp.ResponseWriter + + if !authorizeAdmin(r) { + forbidden(w, r) + return + } + + if r.Method != "POST" { + http.NotFound(w, r) + return + } + + out, err := ioutil.TempFile(*dataDir, ".config-upload") + if err != nil { + writeError(w, err) + return + } + + defer os.Remove(out.Name()) + + _, err = io.Copy(out, r.Body) + out.Close() + if err != nil { + writeError(w, err) + return + } + + archivesPath := filepath.Join(*dataDir, "archives") + cfgPath := configFilePath() + + err = os.MkdirAll(archivesPath, 0700) + if err != nil { + writeError(w, err) + return + } + + err = func() (err error) { + backupPath := filepath.Join(archivesPath, "config."+ulid()+".yaml.gz") + + bck, err := os.Create(backupPath) + if err != nil { + return + } + + defer bck.Close() + + in, err := os.Open(cfgPath) + if err != nil { + return + } + + gz, err := gzip.NewWriterLevel(bck, 2) + if err != nil { + return + } + + _, err = io.Copy(gz, in) + gz.Close() + in.Close() + return + }() + + if err != nil { + writeError(w, err) + return + } + + err = os.Rename(out.Name(), cfgPath) + if err != nil { + writeError(w, err) + return + } }