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 594
/
client.go
105 lines (95 loc) · 2.25 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
package testcluster
import (
"crypto/tls"
"errors"
"net/http"
"os"
"time"
"github.com/flynn/flynn/discoverd/client"
"github.com/flynn/flynn/pkg/httpclient"
tc "github.com/flynn/flynn/test/cluster"
)
type Client struct {
*httpclient.Client
cluster *tc.Cluster
size int
}
var ErrNotFound = errors.New("testcluster: resource not found")
func NewClient(endpoint string) (*Client, error) {
authKey := os.Getenv("TEST_RUNNER_AUTH_KEY")
if authKey == "" {
return nil, errors.New("missing TEST_RUNNER_AUTH_KEY environment variable")
}
httpClient := &http.Client{Transport: &http.Transport{TLSClientConfig: &tls.Config{ServerName: "ci.flynn.io"}}}
client := &Client{
Client: &httpclient.Client{
ErrNotFound: ErrNotFound,
URL: endpoint,
Key: authKey,
HTTP: httpClient,
},
}
var cluster tc.Cluster
if err := client.Get("", &cluster); err != nil {
return nil, err
}
client.cluster = &cluster
client.size = cluster.Size()
return client, nil
}
func (c *Client) Size() int {
return c.size
}
func (c *Client) AddHost(ch chan *discoverd.Event, vanilla bool) (*tc.Instance, error) {
path := ""
if vanilla {
path = "?vanilla=true"
}
var instance tc.Instance
if err := c.Post(path, nil, &instance); err != nil {
return nil, err
}
c.cluster.Instances = append(c.cluster.Instances, &instance)
if vanilla {
return &instance, nil
}
for {
select {
case e, ok := <-ch:
if !ok {
return nil, errors.New("channel closed unexpectedly")
}
if e.Kind != discoverd.EventKindUp {
continue
}
c.size++
return &instance, nil
case <-time.After(60 * time.Second):
return nil, errors.New("timed out waiting for new host")
}
}
}
func (c *Client) AddReleaseHosts() (*tc.BootResult, error) {
var res tc.BootResult
return &res, c.Post("/release", nil, &res)
}
func (c *Client) RemoveHost(ch chan *discoverd.Event, host *tc.Instance) error {
if err := c.Delete("/" + host.ID); err != nil {
return err
}
for {
select {
case e, ok := <-ch:
if !ok {
return errors.New("channel closed unexpectedly")
}
if e.Kind != discoverd.EventKindDown {
continue
}
c.size--
return nil
case <-time.After(60 * time.Second):
return errors.New("timed out waiting for host removal")
}
}
}