-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapps.go
More file actions
155 lines (113 loc) · 2.89 KB
/
apps.go
File metadata and controls
155 lines (113 loc) · 2.89 KB
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
package apps
import (
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"
"github.com/deis/deis/client-go/controller/api"
"github.com/deis/deis/client-go/controller/client"
)
// List lists apps on a Deis controller.
func List(c *client.Client) ([]api.App, error) {
body, status, err := c.BasicRequest("GET", "/v1/apps/", nil)
if err != nil {
return []api.App{}, err
}
if status != 200 {
return []api.App{}, errors.New(body)
}
apps := api.Apps{}
if err = json.Unmarshal([]byte(body), &apps); err != nil {
return []api.App{}, err
}
return apps.Apps, nil
}
// New creates a new app.
func New(c *client.Client, id string) (api.App, error) {
body := []byte{}
var err error
if id != "" {
req := api.AppCreateRequest{ID: id}
body, err = json.Marshal(req)
if err != nil {
return api.App{}, err
}
}
resBody, status, err := c.BasicRequest("POST", "/v1/apps/", body)
if err != nil {
return api.App{}, err
}
if status != 201 {
return api.App{}, errors.New(resBody)
}
app := api.App{}
if err = json.Unmarshal([]byte(resBody), &app); err != nil {
return api.App{}, err
}
return app, nil
}
// Get app details from a Deis controller.
func Get(c *client.Client, appID string) (api.App, error) {
u := fmt.Sprintf("/v1/apps/%s/", appID)
body, status, err := c.BasicRequest("GET", u, nil)
if err != nil {
return api.App{}, err
}
if status != 200 {
return api.App{}, errors.New(body)
}
app := api.App{}
if err = json.Unmarshal([]byte(body), &app); err != nil {
return api.App{}, err
}
return app, nil
}
// Logs retrieves logs from an app.
func Logs(c *client.Client, appID string, lines int) (string, error) {
u := fmt.Sprintf("/v1/apps/%s/logs", appID)
if lines > 0 {
u += "?log_lines=" + strconv.Itoa(lines)
}
body, status, err := c.BasicRequest("GET", u, nil)
if err != nil {
return "", err
}
if status != 200 {
return body, errors.New(body)
}
return strings.Trim(body, `"`), nil
}
// Run one time command in an app.
func Run(c *client.Client, appID string, command string) (api.AppRunResponse, error) {
req := api.AppRunRequest{Command: command}
body, err := json.Marshal(req)
if err != nil {
return api.AppRunResponse{}, err
}
u := fmt.Sprintf("/v1/apps/%s/run", appID)
resBody, status, err := c.BasicRequest("POST", u, body)
if err != nil {
return api.AppRunResponse{}, err
}
if status != 200 {
return api.AppRunResponse{}, errors.New(resBody)
}
out := make([]interface{}, 2)
if err = json.Unmarshal([]byte(resBody), &out); err != nil {
return api.AppRunResponse{}, err
}
return api.AppRunResponse{Output: out[1].(string), ReturnCode: int(out[0].(float64))}, nil
}
// Delete an app.
func Delete(c *client.Client, appID string) error {
u := fmt.Sprintf("/v1/apps/%s/", appID)
body, status, err := c.BasicRequest("DELETE", u, nil)
if err != nil {
return err
}
if status != 204 {
return errors.New(body)
}
return nil
}