/
types.go
274 lines (235 loc) · 7.84 KB
/
types.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
package workerruntime
import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"github.com/ovh/cds/sdk/cdsclient"
cdslog "github.com/ovh/cds/sdk/log"
"github.com/rockbears/log"
"github.com/ovh/cds/sdk"
"github.com/spf13/afero"
)
type V2RunResultRequest struct {
RunResult *sdk.V2WorkflowRunResult
CDNItemLink sdk.CDNItemLink // TODO
}
type V2AddResultResponse struct {
RunResult *sdk.V2WorkflowRunResult
CDNSignature string
CDNAddress string
}
type V2GetResultResponse struct {
RunResults []sdk.V2WorkflowRunResult
CDNSignature string
}
type V2UpdateResultResponse struct {
RunResult *sdk.V2WorkflowRunResult
}
type V2FilterRunResult struct {
Pattern string
Type []sdk.V2WorkflowRunResultType
}
type WorkerConfig struct {
Name string `json:"name"`
Basedir string `json:"basedir"`
Log cdslog.Conf `json:"log"`
HatcheryName string `json:"hatchery_name"`
APIEndpoint string `json:"api_endpoint"`
APIEndpointInsecure bool `json:"api_endpoint_insecure,omitempty"`
APIToken string `json:"api_token"`
CDNEndpoint string `json:"cdn_endpoint"`
GelfServiceAddr string `json:"gelf_service_addr"`
GelfServiceAddrEnableTLS bool `json:"gelf_service_addr_enable_tls,omitempty"`
Model string `json:"model"`
BookedJobID int64 `json:"booked_job_id,omitempty"`
RunJobID string `json:"run_job_id,omitempty"`
Region string `json:"region,omitempty"`
InjectEnvVars map[string]string `json:"inject_env_vars,omitempty"`
}
func (cfg WorkerConfig) EncodeBase64() string {
btes, _ := json.Marshal(cfg)
return base64.StdEncoding.EncodeToString(btes)
}
type DownloadArtifact struct {
Workflow string `json:"workflow"`
Number int64 `json:"number"`
Pattern string `json:"pattern" cli:"pattern"`
Tag string `json:"tag" cli:"tag"`
Destination string `json:"destination"`
}
type UploadArtifact struct {
Name string `json:"name"`
Tag string `json:"tag"`
WorkingDirectory string `json:"working_directory"`
}
type FilePath struct {
Path string `json:"path"`
}
type OutputRequest struct {
Name string `json:"name"`
Value string `json:"value"`
StepOnly bool `json:"step_only"`
WorkflowRunID string `json:"workflow_run_id"`
WorkflowRunJobID string `json:"workflow_run_job_id"`
}
type KeyResponse struct {
PKey string `json:"pkey"`
Type sdk.KeyType `json:"type"`
Content []byte `json:"-"`
}
type TmplPath struct {
Path string `json:"path"`
Destination string `json:"destination"`
}
type CDSVersionSet struct {
Value string `json:"value"`
}
type Level string
const (
LevelDebug Level = "DEBUG"
LevelInfo Level = "INFO"
LevelWarn Level = "WARN"
LevelError Level = "ERROR"
)
type (
contextKey int
)
const (
jobID contextKey = iota
stepOrder
stepName
workDir
keysDir
tmpDir
runJobID
readiness
)
type Runtime interface {
Name() string
Register(ctx context.Context) error
Take(ctx context.Context, job sdk.WorkflowNodeJobRun) error
ProcessJob(job sdk.WorkflowNodeJobRunData) sdk.Result
SendLog(ctx context.Context, level Level, format string)
RunResultSignature(fileName string, perm uint32, t sdk.WorkflowRunResultType) (string, error)
WorkerCacheSignature(tag string) (string, error)
FeatureEnabled(featureName sdk.FeatureName) bool
GetIntegrationPlugin(pluginType string) *sdk.GRPCPlugin
GetActionPlugin(pluginName string) *sdk.GRPCPlugin
SetActionPlugin(p *sdk.GRPCPlugin)
GetJobIdentifiers() (int64, int64, int64)
CDNHttpURL() string
InstallKey(key sdk.Variable) (*KeyResponse, error)
InstallKeyTo(key sdk.Variable, destinationPath string) (*KeyResponse, error)
Unregister(ctx context.Context) error
Client() cdsclient.WorkerInterface
ClientV2() cdsclient.V2WorkerInterface
BaseDir() afero.Fs
Environ() []string
Blur(interface{}) error
HTTPPort() int32
Parameters() []sdk.Parameter
PluginGet(pluginName string) (*sdk.GRPCPlugin, error)
PluginGetBinary(name, os, arch string, w io.Writer) error
V2AddRunResult(ctx context.Context, req V2RunResultRequest) (*V2AddResultResponse, error)
V2UpdateRunResult(ctx context.Context, req V2RunResultRequest) (*V2UpdateResultResponse, error)
AddStepOutput(ctx context.Context, outputName string, outputValue string)
V2GetRunResult(ctx context.Context, filter V2FilterRunResult) (*V2GetResultResponse, error)
V2GetIntegrationByName(ctx context.Context, name string) (*sdk.ProjectIntegration, error)
V2GetJobRun(ctx context.Context) *sdk.V2WorkflowRunJob
V2GetJobContext(ctx context.Context) *sdk.WorkflowRunJobsContext
}
func JobID(ctx context.Context) (int64, error) {
jobIDStr := ctx.Value(jobID)
jobID, ok := jobIDStr.(int64)
if !ok {
return -1, errors.New("unable to get job ID")
}
return jobID, nil
}
func SetJobID(ctx context.Context, i int64) context.Context {
return context.WithValue(ctx, jobID, i)
}
func RunJobID(ctx context.Context) string {
if ctx.Value(runJobID) == nil {
return ""
}
return ctx.Value(runJobID).(string)
}
func SetRunJobID(ctx context.Context, i string) context.Context {
return context.WithValue(ctx, runJobID, i)
}
func StepOrder(ctx context.Context) (int, error) {
stepOrderStr := ctx.Value(stepOrder)
stepOrder, ok := stepOrderStr.(int)
if !ok {
return -1, fmt.Errorf("unable to get step order: got %v", stepOrder)
}
return stepOrder, nil
}
func IsReadinessServices(ctx context.Context) (bool, error) {
readinessStr := ctx.Value(readiness)
readiness, ok := readinessStr.(bool)
if !ok {
return false, fmt.Errorf("unable to get readiness service: got %v", readiness)
}
return readiness, nil
}
func SetIsReadinessServices(ctx context.Context, i bool) context.Context {
return context.WithValue(ctx, readiness, i)
}
func SetStepOrder(ctx context.Context, i int) context.Context {
return context.WithValue(ctx, stepOrder, i)
}
func StepName(ctx context.Context) (string, error) {
stepNameInt := ctx.Value(stepName)
stepName, ok := stepNameInt.(string)
if !ok {
return "", fmt.Errorf("unable to get step name: got %v", stepName)
}
return stepName, nil
}
func SetStepName(ctx context.Context, name string) context.Context {
return context.WithValue(ctx, stepName, name)
}
func WorkingDirectory(ctx context.Context) (afero.File, error) {
wdi := ctx.Value(workDir)
wd, ok := wdi.(afero.File)
if !ok {
return nil, sdk.WithStack(errors.New("unable to get working directory"))
}
log.Debug(ctx, "WorkingDirectory> working directory is : %s", wd.Name())
return wd, nil
}
func SetWorkingDirectory(ctx context.Context, s afero.File) context.Context {
log.Debug(ctx, "SetWorkingDirectory> working directory is: %s", s.Name())
return context.WithValue(ctx, workDir, s)
}
func KeysDirectory(ctx context.Context) (afero.File, error) {
wdi := ctx.Value(keysDir)
wd, ok := wdi.(afero.File)
if !ok {
return nil, fmt.Errorf("unable to get key directory (%T) %v", wdi, wdi)
}
log.Debug(ctx, "KeysDirectory> working directory is : %s", wd.Name())
return wd, nil
}
func SetKeysDirectory(ctx context.Context, s afero.File) context.Context {
log.Debug(ctx, "SetKeysDirectory> working directory is: %s", s.Name())
return context.WithValue(ctx, keysDir, s)
}
func TmpDirectory(ctx context.Context) (afero.File, error) {
wdi := ctx.Value(tmpDir)
wd, ok := wdi.(afero.File)
if !ok {
return nil, fmt.Errorf("unable to get tmp directory (%T) %v", wdi, wdi)
}
log.Debug(ctx, "TmpDirectory> working directory is : %s", wd.Name())
return wd, nil
}
func SetTmpDirectory(ctx context.Context, s afero.File) context.Context {
log.Debug(ctx, "SetTmpDirectory> working directory is: %s", s.Name())
return context.WithValue(ctx, tmpDir, s)
}