-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathapps.go
More file actions
314 lines (241 loc) · 6.54 KB
/
apps.go
File metadata and controls
314 lines (241 loc) · 6.54 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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package cmd
import (
"fmt"
"os"
"strings"
"time"
"github.com/deis/controller-sdk-go/api"
"github.com/deis/controller-sdk-go/apps"
"github.com/deis/controller-sdk-go/config"
"github.com/deis/controller-sdk-go/domains"
"github.com/deis/workflow-cli/pkg/git"
"github.com/deis/workflow-cli/pkg/logging"
"github.com/deis/workflow-cli/pkg/webbrowser"
"github.com/deis/workflow-cli/settings"
)
// AppCreate creates an app.
func (d *DeisCmd) AppCreate(id, buildpack, remote string, noRemote bool) error {
s, err := settings.Load(d.ConfigFile)
if err != nil {
return err
}
d.Print("Creating Application... ")
quit := progress(d.WOut)
app, err := apps.New(s.Client, id)
quit <- true
<-quit
if d.checkAPICompatibility(s.Client, err) != nil {
return err
}
d.Printf("done, created %s\n", app.ID)
if buildpack != "" {
configValues := api.Config{
Values: map[string]interface{}{
"BUILDPACK_URL": buildpack,
},
}
if _, err = config.Set(s.Client, app.ID, configValues); d.checkAPICompatibility(s.Client, err) != nil {
return err
}
}
if !noRemote {
if err = git.CreateRemote(git.DefaultCmd, s.Client.ControllerURL.Host, remote, app.ID); err != nil {
if strings.Contains(err.Error(), fmt.Sprintf("fatal: remote %s already exists.", remote)) {
msg := "A git remote with the name %s already exists. To overwrite this remote run:\n"
msg += "deis git:remote --force --remote %s --app %s"
return fmt.Errorf(msg, remote, remote, app.ID)
}
return err
}
d.Printf(remoteCreationMsg, remote, app.ID)
}
if noRemote {
d.Printf("If you want to add a git remote for this app later, use `deis git:remote -a %s`\n", app.ID)
}
return nil
}
// AppsList lists apps on the Deis controller.
func (d *DeisCmd) AppsList(results int) error {
s, err := settings.Load(d.ConfigFile)
if err != nil {
return err
}
if results == defaultLimit {
results = s.Limit
}
apps, count, err := apps.List(s.Client, results)
if d.checkAPICompatibility(s.Client, err) != nil {
return err
}
d.Printf("=== Apps%s", limitCount(len(apps), count))
for _, app := range apps {
d.Println(app.ID)
}
return nil
}
// AppInfo prints info about app.
func (d *DeisCmd) AppInfo(appID string) error {
s, appID, err := load(d.ConfigFile, appID)
if err != nil {
return err
}
app, err := apps.Get(s.Client, appID)
if d.checkAPICompatibility(s.Client, err) != nil {
return err
}
url, err := d.appURL(s, appID)
if err != nil {
return err
}
if url == "" {
url = fmt.Sprintf(noDomainAssignedMsg, appID)
}
d.Printf("=== %s Application\n", app.ID)
d.Println("updated: ", app.Updated)
d.Println("uuid: ", app.UUID)
d.Println("created: ", app.Created)
d.Println("url: ", url)
d.Println("owner: ", app.Owner)
d.Println("id: ", app.ID)
d.Println()
// print the app processes
if err = d.PsList(app.ID, defaultLimit); err != nil {
return err
}
d.Println()
// print the app domains
if err = d.DomainsList(app.ID, defaultLimit); err != nil {
return err
}
d.Println()
// print the app labels
if err = d.LabelsList(app.ID); err != nil {
return err
}
d.Println()
return nil
}
// AppOpen opens an app in the default webbrowser.
func (d *DeisCmd) AppOpen(appID string) error {
s, appID, err := load(d.ConfigFile, appID)
if err != nil {
return err
}
u, err := d.appURL(s, appID)
if err != nil {
return err
}
if u == "" {
return fmt.Errorf(noDomainAssignedMsg, appID)
}
if !(strings.HasPrefix(u, "http://") || strings.HasPrefix(u, "https://")) {
u = "http://" + u
}
return webbrowser.Webbrowser(u)
}
// AppLogs returns the logs from an app.
func (d *DeisCmd) AppLogs(appID string, lines int) error {
s, appID, err := load(d.ConfigFile, appID)
if err != nil {
return err
}
logs, err := apps.Logs(s.Client, appID, lines)
if d.checkAPICompatibility(s.Client, err) != nil {
return err
}
for _, log := range strings.Split(strings.TrimRight(logs, `\n`), `\n`) {
logging.PrintLog(os.Stdout, log)
}
return nil
}
// AppRun runs a one time command in the app.
func (d *DeisCmd) AppRun(appID, command string) error {
s, appID, err := load(d.ConfigFile, appID)
if err != nil {
return err
}
d.Printf("Running '%s'...\n", command)
out, err := apps.Run(s.Client, appID, command)
if d.checkAPICompatibility(s.Client, err) != nil {
return err
}
if out.ReturnCode == 0 {
d.Print(out.Output)
} else {
d.PrintErr(out.Output)
}
os.Exit(out.ReturnCode)
return nil
}
// AppDestroy destroys an app.
func (d *DeisCmd) AppDestroy(appID, confirm string) error {
gitSession := false
s, err := settings.Load(d.ConfigFile)
if err != nil {
return err
}
if appID == "" {
appID, err = git.DetectAppName(git.DefaultCmd, s.Client.ControllerURL.Host)
if err != nil {
return err
}
gitSession = true
}
if confirm == "" {
d.Printf(` ! WARNING: Potentially Destructive Action
! This command will destroy the application: %s
! To proceed, type "%s" or re-run this command with --confirm=%s
> `, appID, appID, appID)
fmt.Scanln(&confirm)
}
if confirm != appID {
return fmt.Errorf("App %s does not match confirm %s, aborting.", appID, confirm)
}
startTime := time.Now()
d.Printf("Destroying %s...\n", appID)
if err = apps.Delete(s.Client, appID); d.checkAPICompatibility(s.Client, err) != nil {
return err
}
d.Printf("done in %ds\n", int(time.Since(startTime).Seconds()))
if gitSession {
return d.GitRemove(appID)
}
return nil
}
// AppTransfer transfers app ownership to another user.
func (d *DeisCmd) AppTransfer(appID, username string) error {
s, appID, err := load(d.ConfigFile, appID)
if err != nil {
return err
}
d.Printf("Transferring %s to %s... ", appID, username)
err = apps.Transfer(s.Client, appID, username)
if d.checkAPICompatibility(s.Client, err) != nil {
return err
}
d.Println("done")
return nil
}
const noDomainAssignedMsg = "No domain assigned to %s"
// appURL grabs the first domain an app has and returns this.
func (d *DeisCmd) appURL(s *settings.Settings, appID string) (string, error) {
domains, _, err := domains.List(s.Client, appID, 1)
if d.checkAPICompatibility(s.Client, err) != nil {
return "", err
}
if len(domains) == 0 {
return "", nil
}
return expandURL(s.Client.ControllerURL.Host, domains[0].Domain), nil
}
// expandURL expands an app url if necessary.
func expandURL(host, u string) string {
if strings.Contains(u, ".") {
// If domain is a full url.
return u
}
// If domain is a subdomain, look up the controller url and replace the subdomain.
parts := strings.Split(host, ".")
parts[0] = u
return strings.Join(parts, ".")
}