forked from tsuru/tsuru
-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions.go
214 lines (207 loc) · 5.42 KB
/
actions.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
// Copyright 2014 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 service
import (
stderrors "errors"
"net/http"
"sync"
"github.com/tsuru/tsuru/action"
"github.com/tsuru/tsuru/app/bind"
"github.com/tsuru/tsuru/db"
"github.com/tsuru/tsuru/errors"
"github.com/tsuru/tsuru/log"
"gopkg.in/mgo.v2/bson"
)
// createServiceInstance in an action that calls the service endpoint
// to creates the service instance.
//
// The first argument in the context must be an Service.
// The second argument in the context must be an ServiceInstance.
var createServiceInstance = action.Action{
Name: "create-service-instance",
Forward: func(ctx action.FWContext) (action.Result, error) {
service, ok := ctx.Params[0].(Service)
if !ok {
return nil, stderrors.New("First parameter must be a Service.")
}
endpoint, err := service.getClient("production")
if err != nil {
return nil, err
}
instance, ok := ctx.Params[1].(ServiceInstance)
if !ok {
return nil, stderrors.New("Second parameter must be a ServiceInstance.")
}
user, ok := ctx.Params[2].(string)
if !ok {
return nil, stderrors.New("Third parameter must be a string.")
}
err = endpoint.Create(&instance, user)
if err != nil {
return nil, err
}
return instance, nil
},
Backward: func(ctx action.BWContext) {
service, ok := ctx.Params[0].(Service)
if !ok {
return
}
endpoint, err := service.getClient("production")
if err != nil {
return
}
instance, ok := ctx.Params[1].(ServiceInstance)
if !ok {
return
}
endpoint.Destroy(&instance)
},
MinParams: 2,
}
// insertServiceInstance is an action that inserts an instance in the database.
//
// The first argument in the context must be a Service Instance.
var insertServiceInstance = action.Action{
Name: "insert-service-instance",
Forward: func(ctx action.FWContext) (action.Result, error) {
instance, ok := ctx.Params[1].(ServiceInstance)
if !ok {
return nil, stderrors.New("Second parameter must be a ServiceInstance.")
}
conn, err := db.Conn()
if err != nil {
return nil, err
}
defer conn.Close()
err = conn.ServiceInstances().Insert(&instance)
if err != nil {
return nil, err
}
return nil, nil
},
Backward: func(ctx action.BWContext) {
instance, ok := ctx.Params[1].(ServiceInstance)
if !ok {
return
}
conn, err := db.Conn()
if err != nil {
return
}
defer conn.Close()
conn.ServiceInstances().Remove(bson.M{"name": instance.Name})
},
MinParams: 2,
}
var addAppToServiceInstance = action.Action{
Name: "add-app-to-service-instance",
Forward: func(ctx action.FWContext) (action.Result, error) {
si, ok := ctx.Params[1].(ServiceInstance)
if !ok {
return nil, stderrors.New("Second parameter must be a ServiceInstance.")
}
conn, err := db.Conn()
if err != nil {
return nil, err
}
defer conn.Close()
if err := conn.ServiceInstances().Find(bson.M{"name": si.Name}).One(&si); err != nil {
return nil, err
}
a, ok := ctx.Params[0].(bind.App)
if !ok {
return nil, stderrors.New("First parameter must be a bind.App.")
}
if err := si.AddApp(a.GetName()); err != nil {
return nil, &errors.HTTP{Code: http.StatusConflict, Message: "This app is already bound to this service instance."}
}
if err := si.update(); err != nil {
return nil, err
}
return nil, nil
},
Backward: func(ctx action.BWContext) {
si, ok := ctx.Params[1].(ServiceInstance)
if !ok {
log.Error("Second parameter must be a ServiceInstance.")
}
a, ok := ctx.Params[0].(bind.App)
if !ok {
log.Error("First parameter must be a bind.App.")
}
si.RemoveApp(a.GetName())
if err := si.update(); err != nil {
log.Errorf("Could not remove app from service instance: %s", err.Error())
}
},
MinParams: 2,
}
var setEnvironVariablesToApp = action.Action{
Name: "set-environ-variables-to-app",
Forward: func(ctx action.FWContext) (action.Result, error) {
si, ok := ctx.Params[1].(ServiceInstance)
if !ok {
msg := "Second parameter must be a ServiceInstance."
log.Error(msg)
return nil, stderrors.New(msg)
}
app, ok := ctx.Params[0].(bind.App)
if !ok {
msg := "First parameter must be a bind.App."
log.Error(msg)
return nil, stderrors.New(msg)
}
units := app.GetUnits()
if len(units) == 0 {
return nil, nil
}
envsChan := make(chan map[string]string, len(units)+1)
errChan := make(chan error, len(units)+1)
var wg sync.WaitGroup
wg.Add(len(units))
for _, unit := range units {
go func(unit bind.Unit) {
defer wg.Done()
vars, err := si.BindUnit(app, unit)
if err != nil {
errChan <- err
return
}
envsChan <- vars
}(unit)
}
wg.Wait()
close(errChan)
err, ok := <-errChan
if ok {
log.Error(err.Error())
return nil, err
}
var envVars []bind.EnvVar
envs := <-envsChan
envVars = make([]bind.EnvVar, 0, len(envs))
for k, v := range envs {
envVars = append(envVars, bind.EnvVar{
Name: k,
Value: v,
Public: false,
InstanceName: si.Name,
})
}
return envVars, app.SetEnvs(envVars, false, nil)
},
Backward: func(ctx action.BWContext) {
app, ok := ctx.Params[0].(bind.App)
if !ok {
log.Error("First parameter must be a bind.App.")
}
result := ctx.FWResult.([]bind.EnvVar)
envNames := make([]string, len(result))
for k, envVar := range result {
envNames[k] = envVar.Name
}
app.UnsetEnvs(envNames, true, nil)
},
}