-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
163 lines (129 loc) · 3.18 KB
/
client.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"github.com/gorilla/websocket"
"gopkg.in/yaml.v1"
)
type DeployOutputEntry struct {
Project string
Environment string
StdoutLine string
}
type DeployConfig struct {
Host string
Project string
RepoOwner string `yaml:"repo_owner"`
RepoName string `yaml:"repo_name"`
User string
}
const (
CONFIG_NAME = ".goship.yml"
)
type GoshipProjectStatus struct {
Name string
RepoOwner string
RepoName string
Environments []GoshipProjectEnvironment
}
type GoshipProjectEnvironment struct {
Name string
Deploy string
RepoPath string
LatestGitHubCommit string
IsDeployable bool
Hosts []struct {
URI string
LatestCommit string
}
}
func startDeployRequest(finished chan int, deployEnv string, config *DeployConfig) {
commits_url := fmt.Sprintf("http://%s/commits/%s", config.Host, config.Project)
commits_request, err := http.Get(commits_url)
if err != nil {
log.Fatal(err)
}
var status GoshipProjectStatus
var project_env *GoshipProjectEnvironment
commits_response, _ := ioutil.ReadAll(commits_request.Body)
err = json.Unmarshal(commits_response, &status)
if err != nil {
log.Fatal(err)
}
for _, v := range status.Environments {
if v.Name == deployEnv {
project_env = &v
}
}
if !project_env.IsDeployable {
log.Fatalf("%s is not deployable", deployEnv)
}
v := url.Values{}
v.Set("project", config.Project)
v.Add("repo_owner", status.RepoOwner)
v.Add("repo_name", status.RepoName)
v.Add("from_revision", project_env.Hosts[0].LatestCommit)
v.Add("to_revision", project_env.LatestGitHubCommit)
v.Add("environment", deployEnv)
v.Add("user", config.User)
_, err = http.PostForm(fmt.Sprintf("http://%s/deploy_handler", config.Host), v)
if err != nil {
log.Fatal(err)
}
finished <- 1
}
func deploy(deployEnv string) {
finished := make(chan int)
if len(deployEnv) == 0 {
log.Fatal("empty environment")
}
config := DeployConfig{}
configData, err := ioutil.ReadFile(CONFIG_NAME)
if err != nil {
log.Fatal("failed to read %s: %s", CONFIG_NAME, err)
}
err = yaml.Unmarshal(configData, &config)
if err != nil {
log.Fatalf("error: %v", err)
}
log.Printf("Deploying project `%s` to `%s`", config.Project, deployEnv)
var handshakeDialer = &websocket.Dialer{
Subprotocols: []string{"chat"},
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
origin_url := fmt.Sprintf("http://%s/", config.Host)
ws_url := fmt.Sprintf("ws://%s/web_push", config.Host)
ws, resp, err := handshakeDialer.Dial(ws_url, http.Header{"Origin": {origin_url}})
if err != nil {
log.Printf("Dial: %v", err)
} else {
defer ws.Close()
}
buf := new(bytes.Buffer)
buf.ReadFrom(resp.Body)
log.Printf("Connected: %s\n", resp.Status)
// launch deploy
go startDeployRequest(finished, deployEnv, &config)
go func(ch chan int) {
<-ch
os.Exit(0)
}(finished)
var result DeployOutputEntry
for {
_, r, err := ws.NextReader()
if err != nil {
log.Fatal(err)
break
}
rbuf, err := ioutil.ReadAll(r)
json.Unmarshal(rbuf, &result)
log.Printf("%s\n", result.StdoutLine)
}
}