-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
157 lines (139 loc) · 3.55 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
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
package flex
import (
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
"path"
)
// Client can talk to a flex daemon.
type Client struct {
config Config
http http.Client
baseURL string
}
// NewClient returns a new flex client.
func NewClient(config *Config) (*Client, error) {
c := Client{
config: *config,
http: http.Client{
// Added on Go 1.3. Wait until it's more popular.
//Timeout: 10 * time.Second,
},
}
if config.DefaultRemote == "" || config.DefaultRemote == "local" {
c.baseURL = "http://unix.socket"
c.http.Transport = &unixTransport
} else if r, ok := config.Remotes[config.DefaultRemote]; ok {
c.baseURL = "http://" + r.Addr
} else {
return nil, fmt.Errorf("unknown remote name: %q", config.DefaultRemote)
}
if err := c.Ping(); err != nil {
return nil, err
}
return &c, nil
}
// Ping pings the daemon to see if it is up listening and working.
func (c *Client) Ping() error {
Debugf("pinging the daemon")
data, err := c.getstr("/ping", nil)
if err != nil {
return err
}
if data != "pong" {
return fmt.Errorf("unexpected response to daemon ping: %q", data)
}
Debugf("pong received")
return nil
}
func (c *Client) List() (string, error) {
Debugf("Getting list from the daemon")
data, err := c.getstr("/list", nil)
if err != nil {
return "fail", err
}
return data, err
}
func (c *Client) Attach(name string, cmd string, secret string) (string, error) {
data, err := c.getstr("/attach", map[string]string{
"name": name,
"command": cmd,
"secret": secret,
})
if err != nil {
return "fail", err
}
return data, err
}
func (c *Client) Create(name string, distro string, release string, arch string) (string, error) {
data, err := c.getstr("/create", map[string]string{
"name": name,
"distro": distro,
"release": release,
"arch": arch,
})
if err != nil {
return "fail", err
}
return data, err
}
// Call a function in the flex API by name (i.e. this has nothing to do with
// the parameter passing schemed :)
func (c *Client) CallByName(function string, name string) (string, error) {
data, err := c.getstr("/"+function, map[string]string{"name": name})
if err != nil {
return "", err
}
return data, err
}
func (c *Client) Destroy(name string) (string, error) {
return c.CallByName("destroy", name)
}
func (c *Client) Reboot(name string) (string, error) {
return c.CallByName("reboot", name)
}
func (c *Client) Start(name string) (string, error) {
return c.CallByName("start", name)
}
func (c *Client) Stop(name string) (string, error) {
return c.CallByName("stop", name)
}
func (c *Client) Status(name string) (string, error) {
return c.CallByName("status", name)
}
func (c *Client) getstr(base string, args map[string]string) (string, error) {
vs := url.Values{}
for k, v := range args {
vs.Set(k, v)
}
data, err := c.get(base + "?" + vs.Encode())
if err != nil {
return "", err
}
return string(data), nil
}
func (c *Client) get(elem ...string) ([]byte, error) {
resp, err := c.http.Get(c.url(elem...))
if err != nil {
return nil, err
}
defer resp.Body.Close()
return ioutil.ReadAll(resp.Body)
}
func (c *Client) url(elem ...string) string {
return c.baseURL + path.Join(elem...)
}
var unixTransport = http.Transport{
Dial: func(network, addr string) (net.Conn, error) {
if addr != "unix.socket:80" {
return nil, fmt.Errorf("non-unix-socket addresses not supported yet")
}
raddr, err := net.ResolveUnixAddr("unix", varPath("unix.socket"))
if err != nil {
return nil, fmt.Errorf("cannot resolve unix socket address: %v", err)
}
return net.DialUnix("unix", nil, raddr)
},
}