-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapps.go
More file actions
161 lines (118 loc) · 3.79 KB
/
apps.go
File metadata and controls
161 lines (118 loc) · 3.79 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
156
157
158
159
160
161
package apps
import (
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"
"github.com/deis/workflow-cli/controller/api"
"github.com/deis/workflow-cli/controller/client"
)
const workflowURLPrefix = "deis."
// List lists apps on a Deis controller.
func List(c *client.Client, results int) ([]api.App, int, error) {
body, count, err := c.LimitedRequest("/v2/apps/", results)
if err != nil {
return []api.App{}, -1, err
}
var apps []api.App
if err = json.Unmarshal([]byte(body), &apps); err != nil {
return []api.App{}, -1, err
}
for name, app := range apps {
// Add in app URL based on controller hostname, port included
app.URL = fmt.Sprintf("%s.%s", app.ID, strings.TrimPrefix(c.ControllerURL.Host, workflowURLPrefix))
apps[name] = app
}
return apps, count, 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, err := c.BasicRequest("POST", "/v2/apps/", body)
if err != nil {
return api.App{}, err
}
app := api.App{}
if err = json.Unmarshal([]byte(resBody), &app); err != nil {
return api.App{}, err
}
// Add in app URL based on controller hostname, port included
app.URL = fmt.Sprintf("%s.%s", app.ID, strings.TrimPrefix(c.ControllerURL.Host, workflowURLPrefix))
return app, nil
}
// Get app details from a Deis controller.
func Get(c *client.Client, appID string) (api.App, error) {
u := fmt.Sprintf("/v2/apps/%s/", appID)
body, err := c.BasicRequest("GET", u, nil)
if err != nil {
return api.App{}, err
}
app := api.App{}
if err = json.Unmarshal([]byte(body), &app); err != nil {
return api.App{}, err
}
// Add in app URL based on controller hostname, port included
app.URL = fmt.Sprintf("%s.%s", app.ID, strings.TrimPrefix(c.ControllerURL.Host, workflowURLPrefix))
return app, nil
}
// Logs retrieves logs from an app.
func Logs(c *client.Client, appID string, lines int) (string, error) {
u := fmt.Sprintf("/v2/apps/%s/logs", appID)
if lines > 0 {
u += "?log_lines=" + strconv.Itoa(lines)
}
body, err := c.BasicRequest("GET", u, nil)
if err != nil || len(body) < 1 {
return "", errors.New(
`There are currently no log messages. Please check the following things:
1) Logger and fluentd pods are running.
2) The application is writing logs to the logger component by checking that an entry in the ring buffer was created: kubectl logs <logger pod> --namespace=deis
3) Making sure that the container logs were mounted properly into the fluentd pod: kubectl exec <fluentd pod> --namespace=deis ls /var/log/containers`)
}
// We need to trim a few characters off the front and end of the string
return body[2 : len(body)-1], 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("/v2/apps/%s/run", appID)
resBody, err := c.BasicRequest("POST", u, body)
if err != nil {
return api.AppRunResponse{}, err
}
res := api.AppRunResponse{}
if err = json.Unmarshal([]byte(resBody), &res); err != nil {
return api.AppRunResponse{}, err
}
return res, nil
}
// Delete an app.
func Delete(c *client.Client, appID string) error {
u := fmt.Sprintf("/v2/apps/%s/", appID)
_, err := c.BasicRequest("DELETE", u, nil)
return err
}
// Transfer an app to another user.
func Transfer(c *client.Client, appID string, username string) error {
u := fmt.Sprintf("/v2/apps/%s/", appID)
req := api.AppUpdateRequest{Owner: username}
body, err := json.Marshal(req)
if err != nil {
return err
}
_, err = c.BasicRequest("POST", u, body)
return err
}