-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
156 lines (131 loc) · 3.15 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"os/exec"
"strings"
)
type deploymentConfig struct {
// authorization key string
Secret string `json:"secret"`
// command to run
Command string `json:"command"`
}
var config map[string]deploymentConfig
func readConfig() {
config = make(map[string]deploymentConfig)
fname := os.Getenv("CONFIG_FILE")
if fname == "" {
fname = "config.json"
}
b, err := ioutil.ReadFile(fname)
if err != nil {
log.Fatalf("failed to read config file: %v", err)
}
if err := json.Unmarshal(b, &config); err != nil {
log.Fatalf("failed to parse config file: %v", err)
}
}
func logRequest(r *http.Request) {
log.Printf("%s %s: %s %s\n", r.RemoteAddr, r.Referer(), r.Method, r.RequestURI)
}
func badRequest(w http.ResponseWriter, addr string) {
log.Println("bad request")
w.WriteHeader(http.StatusBadRequest)
if _, err := w.Write([]byte("Bad request")); err != nil {
log.Printf(
"failed to write response to '%s': %v\n",
addr,
err,
)
}
}
func unauthorized(w http.ResponseWriter, addr string) {
log.Println("unauthorized")
w.WriteHeader(http.StatusUnauthorized)
if _, err := w.Write([]byte("Unauthorized")); err != nil {
log.Printf(
"failed to write response to '%s': %v\n",
addr,
err,
)
}
}
func runScript(name string) error {
path := fmt.Sprintf("./scripts/%s", name)
log.Printf("running %s", path)
cmd := exec.Command(
"bash",
"-c",
path,
)
out, err := cmd.CombinedOutput()
if err != nil {
return err
}
log.Println(string(out))
return nil
}
// DeployHandler runs some script to upgrade some service
func DeployHandler(w http.ResponseWriter, r *http.Request) {
logRequest(r)
w.Header().Add("Content-Type", "text/plain; charset=utf-8")
barePath := strings.Trim(r.URL.Path, "/")
parts := strings.Split(barePath, "/")
cfg, ok := config[parts[1]]
if !ok {
unauthorized(w, r.RemoteAddr)
return
}
values, err := url.ParseQuery(r.URL.RawQuery)
if err != nil {
badRequest(w, r.RemoteAddr)
return
}
secrets, ok := values["secret"]
if !ok || len(secrets) != 1 {
badRequest(w, r.RemoteAddr)
return
}
if cfg.Secret != secrets[0] {
unauthorized(w, r.RemoteAddr)
return
}
// sync mode
if values.Get("sync") == "true" {
if err = runScript(cfg.Command); err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Printf("failed to run script: %v", err)
if _, err := w.Write([]byte("deployment failed")); err != nil {
log.Printf("Failed to write response: %v", err)
}
return
}
log.Println("deployment succeed")
if _, err := w.Write([]byte("deployment succeed")); err != nil {
log.Printf("Failed to write response: %v", err)
}
return
}
// async mode
if _, err := w.Write([]byte("deploying...")); err != nil {
log.Printf("Failed to write response: %v", err)
}
go func() {
if err = runScript(cfg.Command); err != nil {
log.Printf("failed to run script: %v", err)
}
}()
}
func main() {
readConfig()
http.HandleFunc("/deploy/", DeployHandler)
if err := http.ListenAndServeTLS("0.0.0.0:443", "server.crt", "server.key", nil); err != nil {
log.Fatalf("failed to start server: %v", err)
}
}