-
Notifications
You must be signed in to change notification settings - Fork 3
/
deployment.go
167 lines (145 loc) · 3.79 KB
/
deployment.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
package reco
import (
"errors"
"fmt"
"io"
"net"
"strings"
"time"
"github.com/ReconfigureIO/reco/logger"
"github.com/ReconfigureIO/reco/printer"
humanize "github.com/dustin/go-humanize"
"github.com/skratchdot/open-golang/open"
)
// DeploymentProxy proxies to a running deployment instance.
type DeploymentProxy interface {
// Connect performs a proxy connection.
Connect(id string, openBrowser bool) error
}
var _ Job = deploymentJob{}
var _ DeploymentProxy = deploymentJob{}
type deploymentJob struct {
*clientImpl
}
func (p deploymentJob) Start(args Args) (string, error) {
buildID := String(args.At(0))
command := String(args.At(1))
wait := String(args.Last())
cmdArgs := StringSlice(args.At(2))
req := p.apiRequest(endpoints.deployments.String())
if len(args) > 0 {
command += " " + strings.Join(cmdArgs, " ")
}
logger.Info.Println("creating deployment")
reqBody := M{
"build_id": buildID,
"command": command,
}
resp, err := req.Do("POST", reqBody)
if err != nil {
return "", err
}
var respJSON struct {
Value struct {
ID string `json:"id"`
} `json:"value"`
Error string `json:"error"`
}
if err := decodeJSON(resp.Body, &respJSON); err != nil {
return "", err
}
if respJSON.Error != "" {
return "", errors.New(respJSON.Error)
}
if respJSON.Value.ID == "" {
return "", errUnknownError
}
logger.Info.Println("done. Deployment ID: ", respJSON.Value.ID)
logger.Info.Println(`you can run "reco deployment log `, respJSON.Value.ID, `" to manually stream logs`)
if wait == "true" {
return respJSON.Value.ID, p.waitAndLog("deployment", respJSON.Value.ID)
} else if wait == "http" {
err := p.waitForStatus("deployment", respJSON.Value.ID, StatusStarted)
if err == nil {
err = p.Connect(respJSON.Value.ID, false)
}
return respJSON.Value.ID, err
}
return "", nil
}
func (p deploymentJob) Status(id string) string {
return p.clientImpl.getStatus("deployment", id)
}
func (p deploymentJob) Stop(id string) error {
resp, err := p.clientImpl.getJob("deployment", id)
if err != nil {
return err
}
if !resp.IsCompleted() {
return p.clientImpl.stopJob("deployment", id)
} else {
return nil
}
}
func (p deploymentJob) List(filter M) (printer.Table, error) {
var table printer.Table
allProjects := filter.Bool("all")
deployments, err := p.clientImpl.listDeployments(filter)
if err != nil {
return table, err
}
var body [][]string
for _, deployment := range deployments {
buildTime := "-"
if !deployment.Time.IsZero() {
buildTime = humanize.Time(deployment.Time)
}
row := []string{
deployment.ID,
deployment.Build,
deployment.Command,
deployment.Status,
buildTime,
timeRounder(deployment.Duration).Nearest(time.Second),
}
if allProjects {
row = append(row, deployment.Project)
}
body = append(body, row)
}
table = printer.Table{
Header: []string{"deployment id", "build id", "command", "status", "started", "duration"},
Body: body,
}
if allProjects {
table.Header = append(table.Header, "project")
}
return table, nil
}
func (p deploymentJob) Log(id string, writer io.Writer) error {
return p.clientImpl.logJob("deployment", id)
}
func (p deploymentJob) Connect(id string, openBrowser bool) error {
logger.Info.Println("Waiting for deployment to listen on port 80")
for {
resp, err := p.clientImpl.getJob("deployment", id)
if err != nil {
return err
}
if resp.IsCompleted() {
return errors.New("instance has shutdown")
}
if resp.IPAddress != "" {
conn, err := net.DialTimeout("tcp", resp.IPAddress+":80", 5*time.Second)
if conn != nil && err == nil {
_ = conn.Close()
logger.Info.Printf("Deployment ready at http://%s/", resp.IPAddress)
if openBrowser {
return open.Run(fmt.Sprintf("http://%s/", resp.IPAddress))
}
return nil
}
}
time.Sleep(10 * time.Second)
}
}