This repository has been archived by the owner on Sep 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 595
/
host.go
270 lines (235 loc) · 7.8 KB
/
host.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package cluster
import (
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/http"
"net/url"
"strings"
"time"
ct "github.com/flynn/flynn/controller/types"
"github.com/flynn/flynn/host/types"
"github.com/flynn/flynn/host/volume"
"github.com/flynn/flynn/pkg/httpclient"
"github.com/flynn/flynn/pkg/stream"
)
// Host is a client for a host daemon.
type Host struct {
id string
tags map[string]string
c *httpclient.Client
}
// NewHost creates a new Host that uses client to communicate with it.
// addr is used by Attach.
func NewHost(id string, addr string, h *http.Client, tags map[string]string) *Host {
if h == nil {
h = http.DefaultClient
}
if !strings.HasPrefix(addr, "http") {
addr = "http://" + addr
}
return &Host{
id: id,
tags: tags,
c: &httpclient.Client{
ErrNotFound: ErrNotFound,
URL: addr,
HTTP: h,
},
}
}
// ID returns the ID of the host this client communicates with.
func (c *Host) ID() string {
return c.id
}
// Tags returns the hosts tags
func (c *Host) Tags() map[string]string {
return c.tags
}
// Addr returns the IP/port that the host API is listening on.
func (c *Host) Addr() string {
u, err := url.Parse(c.c.URL)
if err != nil {
return ""
}
return u.Host
}
func (c *Host) GetStatus() (*host.HostStatus, error) {
var res host.HostStatus
err := c.c.Get("/host/status", &res)
return &res, err
}
func WaitForHostStatus(hostIP string, desired func(*host.HostStatus) bool) (*host.HostStatus, error) {
const waitInterval = 500 * time.Millisecond
const logInterval = time.Minute
start := time.Now()
lastLogged := time.Now()
h := NewHost("", fmt.Sprintf("http://%s:1113", hostIP), nil, nil)
for {
status, err := h.GetStatus()
if err == nil && desired(status) {
return status, nil
}
if time.Since(lastLogged) > logInterval {
log.Printf("desired host status still not reached after %s", time.Since(start))
lastLogged = time.Now()
}
time.Sleep(waitInterval)
}
}
// ListJobs lists all jobs on the host.
func (c *Host) ListJobs() (map[string]host.ActiveJob, error) {
var jobs map[string]host.ActiveJob
err := c.c.Get("/host/jobs", &jobs)
return jobs, err
}
// ListActiveJobs lists starting or running jobs on the host.
func (c *Host) ListActiveJobs() (map[string]host.ActiveJob, error) {
var jobs map[string]host.ActiveJob
err := c.c.Get("/host/jobs?active=true", &jobs)
return jobs, err
}
// AddJob runs a job on the host.
func (c *Host) AddJob(job *host.Job) error {
return c.c.Put(fmt.Sprintf("/host/jobs/%s", job.ID), job, nil)
}
// GetJob retrieves job details by ID.
func (c *Host) GetJob(id string) (*host.ActiveJob, error) {
var res host.ActiveJob
err := c.c.Get(fmt.Sprintf("/host/jobs/%s", id), &res)
return &res, err
}
// StopJob stops a running job.
func (c *Host) StopJob(id string) error {
return c.c.Delete(fmt.Sprintf("/host/jobs/%s", id))
}
// SignalJob sends a signal to a running job.
func (c *Host) SignalJob(id string, sig int) error {
return c.c.Put(fmt.Sprintf("/host/jobs/%s/signal/%d", id, sig), nil, nil)
}
// DiscoverdDeregisterJob requests a job to deregister from service discovery.
func (c *Host) DiscoverdDeregisterJob(id string) error {
return c.c.Put(fmt.Sprintf("/host/jobs/%s/discoverd-deregister", id), nil, nil)
}
// StreamEvents about job state changes to ch. id may be "all" or a single
// job ID.
func (c *Host) StreamEvents(id string, ch chan *host.Event) (stream.Stream, error) {
r := fmt.Sprintf("/host/jobs/%s", id)
if id == "all" {
r = "/host/jobs"
}
return c.c.ResumingStream("GET", r, ch)
}
// CreateVolume a new volume with the given configuration.
// When in doubt, use a providerId of "default".
func (c *Host) CreateVolume(providerId string, info *volume.Info) error {
return c.c.Post(fmt.Sprintf("/storage/providers/%s/volumes", providerId), info, info)
}
// GetVolume gets a volume by ID
func (c *Host) GetVolume(volumeID string) (*volume.Info, error) {
var volume volume.Info
return &volume, c.c.Get(fmt.Sprintf("/storage/volumes/%s", volumeID), &volume)
}
// ListVolume returns a list of volume IDs
func (c *Host) ListVolumes() ([]*volume.Info, error) {
var volumes []*volume.Info
return volumes, c.c.Get("/storage/volumes", &volumes)
}
// StreamVolumes streams volume events to the given channel
func (c *Host) StreamVolumes(ch chan *volume.Event) (stream.Stream, error) {
return c.c.ResumingStream("GET", "/storage/volumes", ch)
}
// DestroyVolume deletes a volume by ID
func (c *Host) DestroyVolume(volumeID string) error {
return c.c.Delete(fmt.Sprintf("/storage/volumes/%s", volumeID))
}
// Create snapshot creates a snapshot of a volume on a host.
func (c *Host) CreateSnapshot(volumeID string) (*volume.Info, error) {
var res volume.Info
err := c.c.Put(fmt.Sprintf("/storage/volumes/%s/snapshot", volumeID), nil, &res)
return &res, err
}
// PullSnapshot requests the host pull a snapshot from another host onto one of
// its volumes. Returns the info for the new snapshot.
func (c *Host) PullSnapshot(receiveVolID string, sourceHostID string, sourceSnapID string) (*volume.Info, error) {
var res volume.Info
pull := volume.PullCoordinate{
HostID: sourceHostID,
SnapshotID: sourceSnapID,
}
err := c.c.Post(fmt.Sprintf("/storage/volumes/%s/pull_snapshot", receiveVolID), pull, &res)
return &res, err
}
// SendSnapshot requests transfer of volume snapshot data (this is used by other
// hosts in service of the PullSnapshot request).
func (c *Host) SendSnapshot(snapID string, assumeHaves []json.RawMessage) (io.ReadCloser, error) {
header := http.Header{
"Accept": []string{"application/vnd.zfs.snapshot-stream"},
}
res, err := c.c.RawReq("GET", fmt.Sprintf("/storage/volumes/%s/send", snapID), header, assumeHaves, nil)
if err != nil {
return nil, err
}
return res.Body, nil
}
// PullImages pulls images from a TUF repository using the local TUF file in tufDB
func (c *Host) PullImages(repository, configDir, version string, tufDB io.Reader, ch chan *ct.ImagePullInfo) (stream.Stream, error) {
header := http.Header{"Content-Type": {"application/octet-stream"}}
query := make(url.Values)
query.Set("repository", repository)
query.Set("config-dir", configDir)
query.Set("version", version)
path := "/host/pull/images?" + query.Encode()
return c.c.StreamWithHeader("POST", path, header, tufDB, ch)
}
// PullBinariesAndConfig pulls binaries and config from a TUF repository using the local TUF file in tufDB
func (c *Host) PullBinariesAndConfig(repository, binDir, configDir, version string, tufDB io.Reader) (map[string]string, error) {
query := make(url.Values)
query.Set("repository", repository)
query.Set("bin-dir", binDir)
query.Set("config-dir", configDir)
query.Set("version", version)
path := "/host/pull/binaries?" + query.Encode()
var paths map[string]string
return paths, c.c.Post(path, tufDB, &paths)
}
func (c *Host) ResourceCheck(request host.ResourceCheck) error {
return c.c.Post("/host/resource-check", request, nil)
}
func (c *Host) Update(name string, args ...string) (pid int, err error) {
return c.update(&host.Command{
Path: name,
Args: args,
})
}
func (c *Host) UpdateWithShutdownDelay(name string, delay time.Duration, args ...string) (pid int, err error) {
return c.update(&host.Command{
Path: name,
Args: args,
ShutdownDelay: &delay,
})
}
func (c *Host) update(cmd *host.Command) (pid int, err error) {
return cmd.PID, c.c.Post("/host/update", cmd, cmd)
}
func (c *Host) UpdateTags(tags map[string]string) error {
return c.c.Post("/host/tags", tags, nil)
}
func (c *Host) GetSinks() ([]*ct.Sink, error) {
var sinks []*ct.Sink
return sinks, c.c.Get("/sinks", &sinks)
}
func (c *Host) AddSink(info *ct.Sink) error {
if info.ID == "" {
return errors.New("missing ID")
}
return c.c.Put("/sinks/"+info.ID, info, nil)
}
func (c *Host) RemoveSink(id string) error {
if id == "" {
return errors.New("missing ID")
}
return c.c.Delete("/sinks/" + id)
}