forked from solo-io/unik
/
instances.go
132 lines (122 loc) · 4.29 KB
/
instances.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
package client
import (
"encoding/json"
"fmt"
"github.com/emc-advanced-dev/pkg/errors"
"github.com/emc-advanced-dev/unik/pkg/daemon"
"github.com/emc-advanced-dev/unik/pkg/types"
"github.com/layer-x/layerx-commons/lxhttpclient"
"io"
"net/http"
)
type instances struct {
unikIP string
}
func (i *instances) All() ([]*types.Instance, error) {
resp, body, err := lxhttpclient.Get(i.unikIP, "/instances", nil)
if err != nil {
return nil, errors.New("request failed", err)
}
if resp.StatusCode != http.StatusOK {
return nil, errors.New(fmt.Sprintf("failed with status %v: %s", resp.StatusCode, string(body)), nil)
}
var instances []*types.Instance
if err := json.Unmarshal(body, &instances); err != nil {
return nil, errors.New(fmt.Sprintf("response body %s did not unmarshal to type []*types.Instance", string(body)), err)
}
return instances, nil
}
func (i *instances) Get(id string) (*types.Instance, error) {
resp, body, err := lxhttpclient.Get(i.unikIP, "/instances/"+id, nil)
if err != nil {
return nil, errors.New("request failed", err)
}
if resp.StatusCode != http.StatusOK {
return nil, errors.New(fmt.Sprintf("failed with status %v: %s", resp.StatusCode, string(body)), nil)
}
var instance types.Instance
if err := json.Unmarshal(body, &instance); err != nil {
return nil, errors.New(fmt.Sprintf("response body %s did not unmarshal to type *types.Instance", string(body)), err)
}
return &instance, nil
}
func (i *instances) Delete(id string, force bool) error {
query := buildQuery(map[string]interface{}{
"force": force,
})
resp, body, err := lxhttpclient.Delete(i.unikIP, "/instances/"+id+query, nil)
if err != nil {
return errors.New("request failed", err)
}
if resp.StatusCode != http.StatusNoContent {
return errors.New(fmt.Sprintf("failed with status %v: %s", resp.StatusCode, string(body)), err)
}
return nil
}
func (i *instances) GetLogs(id string) (string, error) {
resp, body, err := lxhttpclient.Get(i.unikIP, "/instances/"+id+"/logs", nil)
if err != nil {
return "", errors.New("request failed", err)
}
if resp.StatusCode != http.StatusOK {
return "", errors.New(fmt.Sprintf("failed with status %v: %s", resp.StatusCode, string(body)), err)
}
return string(body), nil
}
func (i *instances) AttachLogs(id string, deleteOnDisconnect bool) (io.ReadCloser, error) {
query := buildQuery(map[string]interface{}{
"follow": true,
"delete": deleteOnDisconnect,
})
resp, err := lxhttpclient.GetAsync(i.unikIP, "/instances/"+id+"/logs"+query, nil)
if err != nil {
return nil, errors.New("request failed", err)
}
if resp.StatusCode != http.StatusOK {
return nil, errors.New(fmt.Sprintf("failed with status %v", resp.StatusCode), err)
}
return resp.Body, nil
}
func (i *instances) Run(instanceName, imageName string, mountPointsToVols, env map[string]string, memoryMb int, noCleanup, debugMode bool) (*types.Instance, error) {
runInstanceRequest := daemon.RunInstanceRequest{
InstanceName: instanceName,
ImageName: imageName,
Mounts: mountPointsToVols,
Env: env,
MemoryMb: memoryMb,
NoCleanup: noCleanup,
DebugMode: debugMode,
}
resp, body, err := lxhttpclient.Post(i.unikIP, "/instances/run", nil, runInstanceRequest)
if err != nil {
return nil, errors.New("request failed", err)
}
if resp.StatusCode != http.StatusCreated {
return nil, errors.New(fmt.Sprintf("failed with status %v: %s", resp.StatusCode, string(body)), err)
}
var instance types.Instance
if err := json.Unmarshal(body, &instance); err != nil {
return nil, errors.New(fmt.Sprintf("response body %s did not unmarshal to type *types.Instance", string(body)), err)
}
return &instance, nil
}
func (i *instances) Start(id string) error {
resp, body, err := lxhttpclient.Post(i.unikIP, "/instances/"+id+"/start", nil, nil)
if err != nil {
return errors.New("request failed", err)
}
if resp.StatusCode != http.StatusOK {
return errors.New(fmt.Sprintf("failed with status %v: %s", resp.StatusCode, string(body)), err)
}
return nil
}
func (i *instances) Stop(id string) error {
resp, body, err := lxhttpclient.Post(i.unikIP, "/instances/"+id+"/stop", nil, nil)
if err != nil {
return errors.New("request failed", err)
}
if resp.StatusCode != http.StatusOK {
return errors.New(fmt.Sprintf("failed with status %v: %s", resp.StatusCode, string(body)), err)
}
return nil
}