forked from convox/rack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapps.go
More file actions
158 lines (127 loc) · 3.56 KB
/
apps.go
File metadata and controls
158 lines (127 loc) · 3.56 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
package controllers
import (
"net/http"
"os"
"sort"
"strings"
"time"
"github.com/convox/rack/api/httperr"
"github.com/convox/rack/api/models"
"github.com/convox/rack/api/structs"
"github.com/convox/rack/provider"
"github.com/gorilla/mux"
"golang.org/x/net/websocket"
)
// AppList lists installed apps
func AppList(rw http.ResponseWriter, r *http.Request) *httperr.Error {
apps, err := models.ListApps()
if err != nil {
return httperr.Server(err)
}
sort.Sort(apps)
return RenderJson(rw, apps)
}
// AppGet gets app information
func AppGet(rw http.ResponseWriter, r *http.Request) *httperr.Error {
app := mux.Vars(r)["app"]
if app == os.Getenv("RACK") {
return httperr.Errorf(404, "rack %s is not an app", app)
}
a, err := models.Provider().AppGet(app)
if err != nil {
if provider.ErrorNotFound(err) {
return httperr.Errorf(404, "no such app: %s", app)
}
return httperr.Server(err)
}
return RenderJson(rw, a)
}
// AppCancel cancels an app update
func AppCancel(rw http.ResponseWriter, r *http.Request) *httperr.Error {
app := mux.Vars(r)["app"]
err := models.Provider().AppCancel(app)
if provider.ErrorNotFound(err) {
return httperr.NotFound(err)
}
if err != nil {
return httperr.Server(err)
}
return RenderSuccess(rw)
}
// AppCreate creates an application
func AppCreate(rw http.ResponseWriter, r *http.Request) *httperr.Error {
name := r.FormValue("name")
if name == os.Getenv("RACK") {
return httperr.Errorf(403, "application name cannot match rack name (%s). Please choose a different name for your app.", name)
}
// Early check for unbound app only.
if app, err := models.GetAppUnbound(name); err == nil {
return httperr.Errorf(403, "there is already a legacy app named %s (%s). We recommend you delete this app and create it again.", name, app.Status)
}
// If unbound check fails this will result in a bound app.
app := &models.App{Name: name}
err := app.Create()
if awsError(err) == "AlreadyExistsException" {
app, err := models.GetApp(name)
if err != nil {
return httperr.Server(err)
}
return httperr.Errorf(403, "there is already an app named %s (%s)", name, app.Status)
}
if err != nil {
return httperr.Server(err)
}
app, err = models.GetApp(name)
if err != nil {
return httperr.Server(err)
}
return RenderJson(rw, app)
}
// AppDelete deletes an application
func AppDelete(rw http.ResponseWriter, r *http.Request) *httperr.Error {
name := mux.Vars(r)["app"]
app, err := models.GetApp(name)
if awsError(err) == "ValidationError" {
return httperr.Errorf(404, "no such app: %s", name)
}
if err != nil {
return httperr.Server(err)
}
if app.Tags["Type"] != "app" || app.Tags["System"] != "convox" || app.Tags["Rack"] != os.Getenv("RACK") {
return httperr.Errorf(404, "invalid app: %s", name)
}
err = app.Delete()
if err != nil {
return httperr.Server(err)
}
return RenderSuccess(rw)
}
// AppLogs show an app's logs
func AppLogs(ws *websocket.Conn) *httperr.Error {
app := mux.Vars(ws.Request())["app"]
header := ws.Request().Header
var err error
follow := true
if header.Get("Follow") == "false" {
follow = false
}
since := 2 * time.Minute
if s := header.Get("Since"); s != "" {
since, err = time.ParseDuration(s)
if err != nil {
return httperr.Errorf(403, "Invalid duration %s", s)
}
}
err = models.Provider().LogStream(app, ws, structs.LogStreamOptions{
Filter: header.Get("Filter"),
Follow: follow,
Since: time.Now().Add(-1 * since),
})
if err != nil {
if strings.HasSuffix(err.Error(), "write: broken pipe") {
return nil
}
return httperr.Server(err)
}
return nil
}