-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.go
174 lines (144 loc) · 3.79 KB
/
deploy.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"path"
"github.com/applikatoni/applikatoni/deploy"
"github.com/gorilla/websocket"
)
func streamDeploymentLog(c *Configuration, deploymentLogURL *url.URL) error {
urlStr := deploymentLogURL.String()
wsHeaders := http.Header{"Origin": {c.Host}, apiTokenHeaderName: {c.ApiToken}}
wsConn, _, err := websocket.DefaultDialer.Dial(urlStr, wsHeaders)
if err != nil {
return err
}
logs := make(chan deploy.LogEntry)
defer func() {
close(logs)
}()
go deploy.ConsoleLogger(logs)
for {
entry := deploy.LogEntry{}
err := wsConn.ReadJSON(&entry)
if err == io.EOF {
break
}
if err != nil {
return err
}
logs <- entry
}
return nil
}
type UnexpectedResponse struct {
ResponseCode int
ResponseBody string
}
func (ur UnexpectedResponse) Error() string {
if ur.ResponseCode == 302 {
return "Not authorized"
} else {
return fmt.Sprintf("%d - %s", ur.ResponseCode, ur.ResponseBody)
}
}
func createDeployment(c *Configuration, deploymentURL *url.URL, data url.Values) (string, error) {
req, err := http.NewRequest("POST", deploymentURL.String(), bytes.NewBufferString(data.Encode()))
req.Header.Set(apiTokenHeaderName, c.ApiToken)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := http.DefaultTransport.RoundTrip(req)
if err != nil {
return "", err
}
if resp.StatusCode != http.StatusSeeOther {
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return "", UnexpectedResponse{resp.StatusCode, string(body)}
}
location := resp.Header.Get("Location")
if location == "" {
return "", fmt.Errorf("createDeployment: no Location header is response")
}
return location, nil
}
func buildDeploymentData(target, sha, branch, comment string, stages []string) url.Values {
data := url.Values{}
data.Set("target", target)
data.Set("commitsha", commitSHA)
data.Set("branch", branch)
data.Set("comment", comment)
for _, s := range stages {
data.Add("stages[]", s)
}
return data
}
func buildDeploymentURL(hostURL, applicationName string) (*url.URL, error) {
u, err := url.Parse(hostURL)
if err != nil {
return nil, err
}
u.Path = path.Join(applicationName, "deployments")
return u, nil
}
func buildDeploymentLogURL(hostURL, deploymentPath string) (*url.URL, error) {
u, err := url.Parse(hostURL)
if err != nil {
return nil, err
}
u.Path = path.Join(deploymentPath, "log")
if u.Scheme == "https" {
u.Scheme = "wss"
} else {
u.Scheme = "ws"
}
return u, nil
}
func buildDeploymentKillURL(hostURL, deploymentPath string) (*url.URL, error) {
u, err := url.Parse(hostURL)
if err != nil {
return nil, err
}
u.Path = path.Join(deploymentPath, "kill")
return u, nil
}
func buildDeploymentDiffURL(hostURL, applicationName, target, sha string) (*url.URL, error) {
u, err := url.Parse(hostURL)
if err != nil {
return nil, err
}
u.Path = path.Join(applicationName, "diff")
q := u.Query()
q.Set("sha", sha)
q.Set("target", target)
u.RawQuery = q.Encode()
return u, nil
}
func getDeploymentDiff(c *Configuration, deploymentDiffURL *url.URL) (*Diff, error) {
req, err := http.NewRequest("GET", deploymentDiffURL.String(), nil)
req.Header.Set(apiTokenHeaderName, c.ApiToken)
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultTransport.RoundTrip(req)
if err != nil {
return nil, err
}
diff := &Diff{}
err = json.NewDecoder(resp.Body).Decode(diff)
if err != nil {
return nil, err
}
return diff, nil
}
func killDeployment(c *Configuration, deploymentKillURL *url.URL) error {
req, err := http.NewRequest("POST", deploymentKillURL.String(), &bytes.Buffer{})
req.Header.Set(apiTokenHeaderName, c.ApiToken)
_, err = http.DefaultTransport.RoundTrip(req)
return err
}