feat: upload config
This commit is contained in:
parent
bf26fce534
commit
115fb9b48f
@ -12,8 +12,10 @@ var (
|
|||||||
configFromDir = flag.String("config-from-dir", "", "Build configuration from this directory")
|
configFromDir = flag.String("config-from-dir", "", "Build configuration from this directory")
|
||||||
)
|
)
|
||||||
|
|
||||||
func readConfig() (config *localconfig.Config, err error) {
|
func configFilePath() string {
|
||||||
configFile := filepath.Join(*dataDir, "config.yaml")
|
return filepath.Join(*dataDir, "config.yaml")
|
||||||
|
}
|
||||||
return localconfig.FromFile(configFile)
|
|
||||||
|
func readConfig() (config *localconfig.Config, err error) {
|
||||||
|
return localconfig.FromFile(configFilePath())
|
||||||
}
|
}
|
||||||
|
@ -3,11 +3,16 @@ package main
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"novit.nc/direktil/pkg/localconfig"
|
"novit.nc/direktil/pkg/localconfig"
|
||||||
)
|
)
|
||||||
@ -248,3 +253,59 @@ func serveCluster(w http.ResponseWriter, r *http.Request) {
|
|||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func uploadConfig(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if !authorizeHosts(r) { // FIXME admin token instead
|
||||||
|
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())
|
||||||
|
|
||||||
|
func() {
|
||||||
|
defer out.Close()
|
||||||
|
if _, err = io.Copy(out, r.Body); 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
|
||||||
|
}
|
||||||
|
|
||||||
|
backupPath := filepath.Join(archivesPath, "config."+time.Now().Format(time.RFC3339)+".yaml")
|
||||||
|
err = os.Rename(cfgPath, backupPath)
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = os.Rename(out.Name(), configFilePath())
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeError(w http.ResponseWriter, err error) {
|
||||||
|
log.Print("request failed: ", err)
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
w.Write([]byte(http.StatusText(http.StatusInternalServerError)))
|
||||||
|
}
|
||||||
|
@ -36,6 +36,8 @@ func main() {
|
|||||||
// by default, serve a host resource by its IP
|
// by default, serve a host resource by its IP
|
||||||
http.HandleFunc("/", serveHostByIP)
|
http.HandleFunc("/", serveHostByIP)
|
||||||
|
|
||||||
|
http.HandleFunc("/configs", uploadConfig)
|
||||||
|
|
||||||
http.HandleFunc("/hosts", serveHosts)
|
http.HandleFunc("/hosts", serveHosts)
|
||||||
http.HandleFunc("/hosts/", serveHost)
|
http.HandleFunc("/hosts/", serveHost)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user