forked from tsuru/tsuru
/
apps.go
271 lines (242 loc) · 5.74 KB
/
apps.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// Copyright 2012 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package tsuru
import (
"encoding/json"
"fmt"
"github.com/globocom/tsuru/cmd"
"io"
"io/ioutil"
"launchpad.net/gnuflag"
"net/http"
"strings"
)
var AppName = gnuflag.String("app", "", "App name for running app related commands.")
var AssumeYes = gnuflag.Bool("assume-yes", false, "Don't ask for confirmation on operations.")
var LogLines = gnuflag.Int("lines", 10, "The number of log lines to display")
var LogSource = gnuflag.String("source", "", "The log from the given source")
type AppInfo struct {
GuessingCommand
}
func (c *AppInfo) Info() *cmd.Info {
return &cmd.Info{
Name: "app-info",
Usage: "app-info [--app appname]",
Desc: `show information about your app.
If you don't provide the app name, tsuru will try to guess it.`,
MinArgs: 0,
}
}
func (c *AppInfo) Run(context *cmd.Context, client cmd.Doer) error {
appName, err := c.Guess()
if err != nil {
return err
}
url := cmd.GetUrl(fmt.Sprintf("/apps/%s", appName))
request, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
response, err := client.Do(request)
if err != nil {
return err
}
if response.StatusCode == http.StatusNoContent {
return nil
}
defer response.Body.Close()
result, err := ioutil.ReadAll(response.Body)
if err != nil {
return err
}
return c.Show(result, context)
}
type unit struct {
Name string
Ip string
State string
}
type app struct {
Name string
Framework string
Repository string
State string
Teams []string
Units []unit
}
func (a *app) String() string {
format := `Application: %s
State: %s
Repository: %s
Platform: %s
Teams: %s
`
teams := strings.Join(a.Teams, ", ")
units := cmd.NewTable()
units.Headers = cmd.Row([]string{"Unit", "Ip", "State"})
for _, unit := range a.Units {
units.AddRow(cmd.Row([]string{unit.Name, unit.Ip, unit.State}))
}
args := []interface{}{a.Name, a.State, a.Repository, a.Framework, teams}
if len(a.Units) > 0 {
format += "Units:\n%s"
args = append(args, units)
}
return fmt.Sprintf(format, args...)
}
func (c *AppInfo) Show(result []byte, context *cmd.Context) error {
var a app
err := json.Unmarshal(result, &a)
if err != nil {
return err
}
fmt.Fprintln(context.Stdout, &a)
return nil
}
type AppGrant struct {
GuessingCommand
}
func (c *AppGrant) Info() *cmd.Info {
return &cmd.Info{
Name: "app-grant",
Usage: "app-grant <teamname> [--app appname]",
Desc: `grants access to an app to a team.
If you don't provide the app name, tsuru will try to guess it.`,
MinArgs: 1,
}
}
func (c *AppGrant) Run(context *cmd.Context, client cmd.Doer) error {
appName, err := c.Guess()
if err != nil {
return err
}
teamName := context.Args[0]
url := cmd.GetUrl(fmt.Sprintf("/apps/%s/%s", appName, teamName))
request, err := http.NewRequest("PUT", url, nil)
if err != nil {
return err
}
_, err = client.Do(request)
if err != nil {
return err
}
fmt.Fprintf(context.Stdout, `Team "%s" was added to the "%s" app`+"\n", teamName, appName)
return nil
}
type AppRevoke struct {
GuessingCommand
}
func (c *AppRevoke) Info() *cmd.Info {
return &cmd.Info{
Name: "app-revoke",
Usage: "app-revoke <teamname> [--app appname]",
Desc: `revokes access to an app from a team.
If you don't provide the app name, tsuru will try to guess it.`,
MinArgs: 1,
}
}
func (c *AppRevoke) Run(context *cmd.Context, client cmd.Doer) error {
appName, err := c.Guess()
if err != nil {
return err
}
teamName := context.Args[0]
url := cmd.GetUrl(fmt.Sprintf("/apps/%s/%s", appName, teamName))
request, err := http.NewRequest("DELETE", url, nil)
if err != nil {
return err
}
_, err = client.Do(request)
if err != nil {
return err
}
fmt.Fprintf(context.Stdout, `Team "%s" was removed from the "%s" app`+"\n", teamName, appName)
return nil
}
type AppModel struct {
Name string
State string
Units []Units
}
type Units struct {
Ip string
}
type AppList struct{}
func (c *AppList) Run(context *cmd.Context, client cmd.Doer) error {
request, err := http.NewRequest("GET", cmd.GetUrl("/apps"), nil)
if err != nil {
return err
}
response, err := client.Do(request)
if err != nil {
return err
}
if response.StatusCode == http.StatusNoContent {
return nil
}
defer response.Body.Close()
result, err := ioutil.ReadAll(response.Body)
if err != nil {
return err
}
return c.Show(result, context)
}
func (c *AppList) Show(result []byte, context *cmd.Context) error {
var apps []AppModel
err := json.Unmarshal(result, &apps)
if err != nil {
return err
}
table := cmd.NewTable()
table.Headers = cmd.Row([]string{"Application", "State", "Ip"})
for _, app := range apps {
ip := ""
if len(app.Units) > 0 {
ip = app.Units[0].Ip
}
table.AddRow(cmd.Row([]string{app.Name, app.State, ip}))
}
context.Stdout.Write(table.Bytes())
return nil
}
func (c *AppList) Info() *cmd.Info {
return &cmd.Info{
Name: "app-list",
Usage: "app-list",
Desc: "list all your apps.",
}
}
type AppRestart struct {
GuessingCommand
}
func (c *AppRestart) Run(context *cmd.Context, client cmd.Doer) error {
appName, err := c.Guess()
if err != nil {
return err
}
url := cmd.GetUrl(fmt.Sprintf("/apps/%s/restart", appName))
request, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
response, err := client.Do(request)
if err != nil {
return err
}
defer response.Body.Close()
_, err = io.Copy(context.Stdout, response.Body)
if err != nil {
return err
}
return nil
}
func (c *AppRestart) Info() *cmd.Info {
return &cmd.Info{
Name: "restart",
Usage: "restart [--app appname]",
Desc: `restarts an app.
If you don't provide the app name, tsuru will try to guess it.`,
MinArgs: 0,
}
}