forked from bmorton/deployster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploys_resource.go
139 lines (116 loc) · 3.81 KB
/
deploys_resource.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
package main
import (
"bytes"
"fmt"
"github.com/bmorton/deployster/fleet"
"github.com/coreos/fleet/schema"
"github.com/coreos/fleet/unit"
"log"
"net/http"
"net/url"
"text/template"
"time"
)
const (
destroyPreviousCheckTimeout time.Duration = 5 * time.Minute
destroyPreviousCheckDelay time.Duration = 1 * time.Second
)
type DeploysResource struct {
Fleet fleet.Client
}
type DeployRequest struct {
Deploy Deploy `json:"deploy"`
}
type Deploy struct {
Version string `json:"version"`
DestroyPrevious bool `json:"destroy_previous"`
}
type UnitTemplate struct {
Name string
Version string
DockerHubUsername string
}
func (self *DeploysResource) Create(u *url.URL, h http.Header, req *DeployRequest) (int, http.Header, interface{}, error) {
serviceName := u.Query().Get("name")
options := getUnitOptions(serviceName, req.Deploy.Version)
fleetName := fleetServiceName(serviceName, req.Deploy.Version)
if req.Deploy.DestroyPrevious {
units, err := self.Fleet.Units()
if err != nil {
log.Printf("%#v\n", err)
return http.StatusInternalServerError, nil, nil, err
}
versions := FindServiceVersions(u.Query().Get("name"), units)
if len(versions) != 1 {
log.Printf("Can't destroy previous versions (%d previous versions), disabling destroy.")
req.Deploy.DestroyPrevious = false
} else {
go self.destroyPrevious(serviceName, versions[0], req.Deploy.Version)
}
}
_, err := self.Fleet.StartUnit(fleetName, options)
if err != nil {
return http.StatusInternalServerError, nil, nil, err
}
return http.StatusCreated, nil, nil, nil
}
func (self *DeploysResource) Destroy(u *url.URL, h http.Header, req interface{}) (int, http.Header, interface{}, error) {
fleetName := fleetServiceName(u.Query().Get("name"), u.Query().Get("version"))
_, err := self.Fleet.DestroyUnit(fleetName)
if err != nil {
return http.StatusInternalServerError, nil, nil, err
}
return http.StatusNoContent, nil, nil, nil
}
func getUnitOptions(name string, version string) []fleet.UnitOption {
var unitTemplate bytes.Buffer
t, _ := template.New("test").Parse(dockerUnitTemplate)
t.Execute(&unitTemplate, UnitTemplate{name, version, dockerHubUsername})
unitFile, _ := unit.NewUnitFile(unitTemplate.String())
return schemaToLocalUnit(schema.MapUnitFileToSchemaUnitOptions(unitFile))
}
func schemaToLocalUnit(options []*schema.UnitOption) []fleet.UnitOption {
convertedOptions := []fleet.UnitOption{}
for _, o := range options {
convertedOptions = append(convertedOptions, fleet.UnitOption{
Section: o.Section,
Name: o.Name,
Value: o.Value,
})
}
return convertedOptions
}
func fleetServiceName(name string, version string) string {
return fmt.Sprintf("%s-%s@1.service", name, version)
}
func (self *DeploysResource) destroyPrevious(name string, previousVersion string, currentVersion string) {
timeoutChan := make(chan bool, 1)
go func() {
time.Sleep(destroyPreviousCheckTimeout)
timeoutChan <- true
}()
for {
startCheck := time.After(destroyPreviousCheckDelay)
select {
case <-startCheck:
log.Printf("Checking if %s:%s has finished launching...\n", name, currentVersion)
state, err := self.Fleet.UnitState(fleetServiceName(name, currentVersion))
if err != nil {
log.Println(err)
break
}
if state.SubState == "running" {
log.Printf("%s:%s has launched, destroying %s.\n", name, currentVersion, previousVersion)
_, err := self.Fleet.DestroyUnit(fleetServiceName(name, previousVersion))
if err != nil {
log.Printf("%#v\n", err)
}
return
}
log.Printf("%s:%s isn't running (currently %s). Trying again in %s.\n", name, currentVersion, state.SubState, destroyPreviousCheckDelay)
case <-timeoutChan:
log.Printf("Timed out trying to destroy %s:%s after %s.\n", name, previousVersion, destroyPreviousCheckTimeout)
return
}
}
}