forked from bmorton/deployster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
134 lines (109 loc) · 2.85 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
package fleet
import (
"bytes"
"encoding/json"
"fmt"
"net"
"net/http"
)
type client struct {
http *http.Client
}
type Client interface {
Units() ([]Unit, error)
StartUnit(name string, options []UnitOption) (*http.Response, error)
DestroyUnit(name string) (*http.Response, error)
UnitState(name string) (UnitState, error)
}
type Unit struct {
CurrentState string `json:"currentState,omitempty"`
DesiredState string `json:"desiredState"`
MachineID string `json:"machineID,omitempty"`
Name string `json:"name,omitempty"`
Options []UnitOption `json:"options"`
}
type UnitOption struct {
Section string `json:"section"`
Name string `json:"name"`
Value string `json:"value"`
}
type UnitState struct {
Name string `json:"name"`
Hash string `json:"hash"`
MachineID string `json:"machineID"`
LoadState string `json:"systemdLoadState"`
ActiveState string `json:"systemdActiveState"`
SubState string `json:"systemdSubState"`
}
type UnitsResponse struct {
Units []Unit `json:"units"`
}
type StatesResponse struct {
States []UnitState `json:"states"`
}
func NewClient(path string) client {
dialFunc := func(string, string) (net.Conn, error) {
return net.Dial("unix", path)
}
httpClient := http.Client{
Transport: &http.Transport{
Dial: dialFunc,
},
}
return client{&httpClient}
}
func (self *client) Units() ([]Unit, error) {
response, err := self.http.Get("http://sock/fleet/v1/units")
if err != nil {
return nil, err
}
decoder := json.NewDecoder(response.Body)
var parsedResponse UnitsResponse
err = decoder.Decode(&parsedResponse)
if err != nil {
return nil, err
}
return parsedResponse.Units, nil
}
func (self *client) StartUnit(name string, options []UnitOption) (*http.Response, error) {
url := fmt.Sprintf("http://sock/fleet/v1/units/%s", name)
unit := Unit{
DesiredState: "launched",
Options: options,
}
var b bytes.Buffer
enc := json.NewEncoder(&b)
enc.Encode(unit)
r, err := http.NewRequest("PUT", url, &b)
if err != nil {
return nil, err
}
r.Header.Add("Content-Type", "application/json")
return self.http.Do(r)
}
func (self *client) DestroyUnit(name string) (*http.Response, error) {
url := fmt.Sprintf("http://sock/fleet/v1/units/%s", name)
r, err := http.NewRequest("DELETE", url, nil)
if err != nil {
return nil, err
}
return self.http.Do(r)
}
func (self *client) UnitState(name string) (UnitState, error) {
url := fmt.Sprintf("http://sock/fleet/v1/state?unitName=%s", name)
response, err := self.http.Get(url)
if err != nil {
return UnitState{}, err
}
decoder := json.NewDecoder(response.Body)
var parsedResponse StatesResponse
err = decoder.Decode(&parsedResponse)
if err != nil {
return UnitState{}, err
}
if len(parsedResponse.States) > 0 {
return parsedResponse.States[0], nil
} else {
return UnitState{}, nil
}
}