-
Notifications
You must be signed in to change notification settings - Fork 520
/
actions.go
238 lines (226 loc) · 6.37 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// Copyright 2023 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 job
import (
"context"
"fmt"
"reflect"
"github.com/pkg/errors"
"github.com/tsuru/tsuru/action"
"github.com/tsuru/tsuru/auth"
"github.com/tsuru/tsuru/db"
"github.com/tsuru/tsuru/servicemanager"
authTypes "github.com/tsuru/tsuru/types/auth"
jobTypes "github.com/tsuru/tsuru/types/job"
"gopkg.in/mgo.v2/bson"
)
var provisionJob = action.Action{
Name: "provision-job",
Forward: func(ctx action.FWContext) (action.Result, error) {
var job *jobTypes.Job
switch ctx.Params[0].(type) {
case *jobTypes.Job:
job = ctx.Params[0].(*jobTypes.Job)
default:
return nil, errors.New("first parameter must be *Job")
}
prov, err := getProvisioner(ctx.Context, job)
if err != nil {
return nil, err
}
err = prov.EnsureJob(ctx.Context, job)
return nil, err
},
Backward: func(ctx action.BWContext) {
var job *jobTypes.Job
switch ctx.Params[0].(type) {
case *jobTypes.Job:
job = ctx.Params[0].(*jobTypes.Job)
default:
return
}
prov, err := getProvisioner(ctx.Context, job)
if err == nil {
prov.DestroyJob(ctx.Context, job)
}
},
MinParams: 1,
}
var triggerCron = action.Action{
Name: "trigger-cronjob",
Forward: func(ctx action.FWContext) (action.Result, error) {
var job *jobTypes.Job
switch ctx.Params[0].(type) {
case *jobTypes.Job:
job = ctx.Params[0].(*jobTypes.Job)
default:
return nil, errors.New("first parameter must be *Job")
}
prov, err := getProvisioner(ctx.Context, job)
if err != nil {
return nil, err
}
return nil, prov.TriggerCron(ctx.Context, job.Name, job.Pool)
},
MinParams: 1,
}
var updateJobProv = action.Action{
Name: "update-job",
Forward: func(ctx action.FWContext) (action.Result, error) {
var job *jobTypes.Job
switch ctx.Params[0].(type) {
case *jobTypes.Job:
job = ctx.Params[0].(*jobTypes.Job)
default:
return nil, errors.New("first parameter must be *Job")
}
return nil, servicemanager.Job.UpdateJobProv(ctx.Context, job)
},
MinParams: 1,
}
// updateJob is an action that updates a job in the database in Forward and
// does nothing in the Backward.
//
// The first argument in the context must be a Job or a pointer to a Job.
var jobUpdateDB = action.Action{
Name: "update-job-db",
Forward: func(ctx action.FWContext) (action.Result, error) {
var j *jobTypes.Job
switch ctx.Params[0].(type) {
case *jobTypes.Job:
j = ctx.Params[0].(*jobTypes.Job)
default:
return nil, errors.New("first parameter must be *Job")
}
return nil, updateJobDB(ctx.Context, j)
},
MinParams: 1,
}
// insertJob is an action that inserts a job in the database in Forward and
// removes it in the Backward.
// insert job must always be run after provision-job because it depends on
// the value of ctx.Previous
//
// The first argument in the context must be a Job or a pointer to a Job.
var insertJob = action.Action{
Name: "insert-job",
Forward: func(ctx action.FWContext) (action.Result, error) {
var j *jobTypes.Job
switch ctx.Params[0].(type) {
case *jobTypes.Job:
j = ctx.Params[0].(*jobTypes.Job)
default:
return nil, errors.New("first parameter must be *Job")
}
err := insertJobDB(ctx.Context, j)
if err != nil {
return nil, err
}
return j, nil
},
Backward: func(ctx action.BWContext) {
job := ctx.FWResult.(*jobTypes.Job)
servicemanager.Job.RemoveJob(ctx.Context, job)
},
MinParams: 1,
}
func insertJobDB(ctx context.Context, job *jobTypes.Job) error {
conn, err := db.Conn()
if err != nil {
return err
}
defer conn.Close()
_, err = servicemanager.Job.GetByName(ctx, job.Name)
if err == jobTypes.ErrJobNotFound {
return conn.Jobs().Insert(job)
} else if err == nil {
return jobTypes.ErrJobAlreadyExists
}
return err
}
func updateJobDB(ctx context.Context, job *jobTypes.Job) error {
conn, err := db.Conn()
if err != nil {
return err
}
defer conn.Close()
oldJob, err := servicemanager.Job.GetByName(ctx, job.Name)
if err != nil {
return err
}
if reflect.DeepEqual(*oldJob, *job) {
return errors.New(fmt.Sprintf("no new values to be patched into job %s", job.Name))
}
return conn.Jobs().Update(bson.M{"name": job.Name}, job)
}
var reserveTeamCronjob = action.Action{
Name: "reserve-team-job",
Forward: func(ctx action.FWContext) (action.Result, error) {
var job *jobTypes.Job
switch ctx.Params[0].(type) {
case *jobTypes.Job:
job = ctx.Params[0].(*jobTypes.Job)
default:
return nil, errors.New("first parameter must be *Job")
}
if err := servicemanager.TeamQuota.Inc(ctx.Context, &authTypes.Team{Name: job.TeamOwner}, 1); err != nil {
return nil, err
}
return map[string]string{"job": job.Name, "team": job.TeamOwner}, nil
},
Backward: func(ctx action.BWContext) {
m := ctx.FWResult.(map[string]string)
if teamStr, ok := m["team"]; ok {
servicemanager.TeamQuota.Inc(ctx.Context, &authTypes.Team{Name: teamStr}, -1)
}
},
MinParams: 2,
}
// reserveUserCronjob reserves the job for the user, only if the user has a quota
// of jobs. If the user does not have a quota, meaning that it's unlimited,
// reserveUserCronjob.Forward just returns nil.
var reserveUserCronjob = action.Action{
Name: "reserve-user-cronjob",
Forward: func(ctx action.FWContext) (action.Result, error) {
var job *jobTypes.Job
switch ctx.Params[0].(type) {
case *jobTypes.Job:
job = ctx.Params[0].(*jobTypes.Job)
default:
return nil, errors.New("first parameter must be *Job")
}
var user authTypes.User
switch ctx.Params[1].(type) {
case authTypes.User:
user = ctx.Params[1].(authTypes.User)
case *authTypes.User:
user = *ctx.Params[1].(*authTypes.User)
default:
return nil, errors.New("second parameter must be auth.User or *auth.User")
}
if user.FromToken {
// there's no quota to update as the user was generated from team token.
return map[string]string{"job": job.Name}, nil
}
u := auth.User(user)
if err := servicemanager.UserQuota.Inc(ctx.Context, &u, 1); err != nil {
return nil, err
}
return map[string]string{"job": job.Name, "user": user.Email}, nil
},
Backward: func(ctx action.BWContext) {
m, found := ctx.FWResult.(map[string]string)
if !found {
return
}
email, found := m["user"]
if !found {
return
}
if user, err := auth.GetUserByEmail(email); err == nil {
servicemanager.UserQuota.Inc(ctx.Context, user, -1)
}
},
MinParams: 2,
}