-
Notifications
You must be signed in to change notification settings - Fork 245
/
gaia.go
376 lines (302 loc) · 11.3 KB
/
gaia.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
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package gaia
import (
"os"
"time"
"github.com/golang-jwt/jwt"
"github.com/hashicorp/go-hclog"
"github.com/robfig/cron"
)
// PipelineType represents supported plugin types
type PipelineType string
// CreatePipelineType represents the different status types
// a create pipeline can have.
type CreatePipelineType string
// PipelineRunStatus represents the different status a run
// can have.
type PipelineRunStatus string
// JobStatus represents the different status a job can have
type JobStatus string
// Mode represents the different modes for Gaia
type Mode string
// WorkerStatus represents the different status a worker can have
type WorkerStatus string
const (
// PTypeUnknown unknown plugin type
PTypeUnknown PipelineType = "unknown"
// PTypeGolang golang plugin type
PTypeGolang PipelineType = "golang"
// PTypeJava java plugin type
PTypeJava PipelineType = "java"
// PTypePython python plugin type
PTypePython PipelineType = "python"
// PTypeCpp C++ plugin type
PTypeCpp PipelineType = "cpp"
// PTypeRuby ruby plugin type
PTypeRuby PipelineType = "ruby"
// PTypeNodeJS NodeJS plugin type
PTypeNodeJS PipelineType = "nodejs"
// CreatePipelineFailed status
CreatePipelineFailed CreatePipelineType = "failed"
// CreatePipelineRunning status
CreatePipelineRunning CreatePipelineType = "running"
// CreatePipelineSuccess status
CreatePipelineSuccess CreatePipelineType = "success"
// RunNotScheduled status
RunNotScheduled PipelineRunStatus = "not scheduled"
// RunScheduled status
RunScheduled PipelineRunStatus = "scheduled"
// RunFailed status
RunFailed PipelineRunStatus = "failed"
// RunSuccess status
RunSuccess PipelineRunStatus = "success"
// RunRunning status
RunRunning PipelineRunStatus = "running"
// RunCancelled status
RunCancelled PipelineRunStatus = "cancelled"
// RunReschedule status
RunReschedule PipelineRunStatus = "reschedule"
// JobWaitingExec status
JobWaitingExec JobStatus = "waiting for execution"
// JobSuccess status
JobSuccess JobStatus = "success"
// JobFailed status
JobFailed JobStatus = "failed"
// JobRunning status
JobRunning JobStatus = "running"
// ModeServer mode
ModeServer Mode = "server"
// ModeWorker mode
ModeWorker Mode = "worker"
// WorkerActive status
WorkerActive WorkerStatus = "active"
// WorkerInactive status
WorkerInactive WorkerStatus = "inactive"
// WorkerSuspended status
WorkerSuspended WorkerStatus = "suspended"
// LogsFolderName represents the Name of the logs folder in pipeline run folder
LogsFolderName = "logs"
// LogsFileName represents the file name of the logs output
LogsFileName = "output.log"
// APIVersion represents the current API version
APIVersion = "v1"
// SrcFolder folder name where the sources are stored
SrcFolder = "src"
// TmpFolder is the temp folder for temporary files
TmpFolder = "tmp"
// TmpPythonFolder is the name of the python temporary folder
TmpPythonFolder = "python"
// TmpGoFolder is the name of the golang temporary folder
TmpGoFolder = "golang"
// TmpCppFolder is the name of the c++ temporary folder
TmpCppFolder = "cpp"
// TmpRubyFolder is the name of the ruby temporary folder
TmpRubyFolder = "ruby"
// TmpNodeJSFolder is the name of the nodejs temporary folder
TmpNodeJSFolder = "nodejs"
// TmpJavaFolder is the name of the java temporary folder
TmpJavaFolder = "java"
// WorkerRegisterKey is the used key for worker registration secret
WorkerRegisterKey = "WORKER_REGISTER_KEY"
// ExecutablePermission is the permission used for gaia created executables.
ExecutablePermission = 0700
// StartReasonRemote label for pipelines which were triggered through a remote token.
StartReasonRemote = "remote"
// StartReasonManual label for pipelines which were triggered through the admin site.
StartReasonManual = "manual"
// StartReasonScheduled label for pipelines which were triggered automated process, i.e. cron job.
StartReasonScheduled = "scheduled"
// SecretNamePrefix defines the prefix for github secrets for pipelines.
SecretNamePrefix = "GITHUB_WEBHOOK_SECRET_"
// LegacySecretName is the old name for a secret that has been created by previous versions.
// Deprecated
LegacySecretName = "GITHUB_WEBHOOK_SECRET"
)
// JwtExpiry is the default JWT expiry.
const JwtExpiry = 12 * 60 * 60
// JwtCustomClaims is the custom JWT claims for a Gaia session.
type JwtCustomClaims struct {
Username string `json:"username"`
Roles []string `json:"roles"`
jwt.StandardClaims
}
// User is the user object
type User struct {
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
DisplayName string `json:"display_name,omitempty"`
Tokenstring string `json:"tokenstring,omitempty"`
JwtExpiry int64 `json:"jwtexpiry,omitempty"`
LastLogin time.Time `json:"lastlogin,omitempty"`
TriggerToken string `json:"trigger_token,omitempty"`
}
// UserPermission is stored in its own data structure away from the core user. It represents all permission data
// for a single user.
type UserPermission struct {
Username string `json:"username"`
Roles []string `json:"roles"`
Groups []string `json:"groups"`
}
// UserRoleCategory represents the top-level of the permission role system
type UserRoleCategory struct {
Name string `json:"name"`
Description string `json:"description"`
Roles []*UserRole `json:"roles"`
}
// UserRole represents a single permission role.
type UserRole struct {
Name string `json:"name"`
Description string `json:"description"`
APIEndpoint []*UserRoleEndpoint `json:"api_endpoints"`
}
// UserRoleEndpoint represents the path and method of the API endpoint to be secured.
type UserRoleEndpoint struct {
Path string `json:"path"`
Method string `json:"method"`
}
// Pipeline represents a single pipeline
type Pipeline struct {
ID int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Repo *GitRepo `json:"repo,omitempty"`
Type PipelineType `json:"type,omitempty"`
ExecPath string `json:"execpath,omitempty"`
SHA256Sum []byte `json:"sha256sum,omitempty"`
Jobs []*Job `json:"jobs,omitempty"`
Created time.Time `json:"created,omitempty"`
UUID string `json:"uuid,omitempty"`
IsNotValid bool `json:"notvalid,omitempty"`
PeriodicSchedules []string `json:"periodicschedules,omitempty"`
TriggerToken string `json:"trigger_token,omitempty"`
Tags []string `json:"tags,omitempty"`
Docker bool `json:"docker"`
CronInst *cron.Cron `json:"-"`
}
// GitRepo represents a single git repository
type GitRepo struct {
URL string `json:"url,omitempty"`
Username string `json:"user,omitempty"`
Password string `json:"password,omitempty"`
PrivateKey PrivateKey `json:"privatekey,omitempty"`
SelectedBranch string `json:"selectedbranch,omitempty"`
Branches []string `json:"branches,omitempty"`
LocalDest string `json:"-"`
}
// Job represents a single job of a pipeline
type Job struct {
ID uint32 `json:"id,omitempty"`
Title string `json:"title,omitempty"`
Description string `json:"desc,omitempty"`
DependsOn []*Job `json:"dependson,omitempty"`
Status JobStatus `json:"status,omitempty"`
Args []*Argument `json:"args,omitempty"`
FailPipeline bool `json:"failpipeline,omitempty"`
}
// Argument represents a single argument of a job
type Argument struct {
Description string `json:"desc,omitempty"`
Type string `json:"type,omitempty"`
Key string `json:"key,omitempty"`
Value string `json:"value,omitempty"`
}
// CreatePipeline represents a pipeline which is not yet
// compiled.
type CreatePipeline struct {
ID string `json:"id,omitempty"`
Pipeline Pipeline `json:"pipeline,omitempty"`
Status int `json:"status,omitempty"`
StatusType CreatePipelineType `json:"statustype,omitempty"`
Output string `json:"output,omitempty"`
Created time.Time `json:"created,omitempty"`
GitHubToken string `json:"githubtoken,omitempty"`
}
// PrivateKey represents a pem encoded private key
type PrivateKey struct {
Key string `json:"key,omitempty"`
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
}
// PipelineRun represents a single run of a pipeline.
type PipelineRun struct {
UniqueID string `json:"uniqueid"`
ID int `json:"id"`
PipelineID int `json:"pipelineid"`
StartDate time.Time `json:"startdate,omitempty"`
StartReason string `json:"started_reason"`
FinishDate time.Time `json:"finishdate,omitempty"`
ScheduleDate time.Time `json:"scheduledate,omitempty"`
Status PipelineRunStatus `json:"status,omitempty"`
Jobs []*Job `json:"jobs,omitempty"`
PipelineType PipelineType `json:"pipelinetype,omitempty"`
PipelineTags []string `json:"pipelinetags,omitempty"`
Docker bool `json:"docker,omitempty"`
DockerWorkerID string `json:"dockerworkerid,omitempty"`
}
// Worker represents a single registered worker.
type Worker struct {
UniqueID string `json:"uniqueid"`
Name string `json:"name"`
Status WorkerStatus `json:"status"`
Slots int32 `json:"slots"`
RegisterDate time.Time `json:"registerdate"`
LastContact time.Time `json:"lastcontact"`
FinishedRuns int64 `json:"finishedruns"`
Tags []string `json:"tags"`
}
// SHAPair struct contains the original sha of a pipeline executable and the
// new sha which was created when the worker had to rebuild it.
type SHAPair struct {
Original []byte `json:"original"`
Worker []byte `json:"worker"`
PipelineID int `json:"pipelineid"`
}
// Cfg represents the global config instance
var Cfg = &Config{}
// Config holds all config options
type Config struct {
DevMode bool
ModeRaw string
Mode Mode
VersionSwitch bool
Poll bool
PVal int
ListenPort string
HomePath string
Hostname string
VaultPath string
DataPath string
PipelinePath string
WorkspacePath string
Worker int
JwtPrivateKeyPath string
JWTKey interface{}
Logger hclog.Logger
CAPath string
WorkerServerPort string
PreventPrimaryWork bool
AutoDockerMode bool
DockerHostURL string
DockerRunImage string
DockerWorkerHostURL string
DockerWorkerGRPCHostURL string
RBACEnabled bool
RBACDebug bool
// Worker
WorkerName string
WorkerHostURL string
WorkerGRPCHostURL string
WorkerSecret string
WorkerTags string
Bolt struct {
Mode os.FileMode
}
}
// StoreConfig defines config settings to be stored in DB.
type StoreConfig struct {
ID int
Poll bool
RBACEnabled bool
}
// String returns a pipeline type string back
func (p PipelineType) String() string {
return string(p)
}