-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsetup.go
182 lines (150 loc) · 4.87 KB
/
setup.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
// Copyright (c) 2022 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.
package executor
import (
"fmt"
"strings"
"github.com/go-vela/sdk-go/vela"
"github.com/go-vela/worker/executor/linux"
"github.com/go-vela/worker/executor/local"
"github.com/go-vela/worker/runtime"
"github.com/go-vela/types/constants"
"github.com/go-vela/types/library"
"github.com/go-vela/types/pipeline"
"github.com/sirupsen/logrus"
)
// Setup represents the configuration necessary for
// creating a Vela engine capable of integrating
// with a configured executor.
type Setup struct {
// https://pkg.go.dev/github.com/sirupsen/logrus#Entry
Logger *logrus.Entry
// Executor Configuration
// Mock should only be true for tests.
Mock bool
// specifies the executor driver to use
Driver string
// specifies the executor method used to publish logs
LogMethod string
// specifies the maximum log size
MaxLogSize uint
// specifies the executor hostname
Hostname string
// specifies the executor version
Version string
// API client for sending requests to Vela
Client *vela.Client
// engine used for creating runtime resources
Runtime runtime.Engine
// Vela Resource Configuration
// resource for storing build information in Vela
Build *library.Build
// resource for storing pipeline information in Vela
Pipeline *pipeline.Build
// resource for storing repo information in Vela
Repo *library.Repo
// resource for storing user information in Vela
User *library.User
}
// Darwin creates and returns a Vela engine capable of
// integrating with a Darwin executor.
func (s *Setup) Darwin() (Engine, error) {
logrus.Trace("creating darwin executor client from setup")
return nil, fmt.Errorf("unsupported executor driver: %s", constants.DriverDarwin)
}
// Linux creates and returns a Vela engine capable of
// integrating with a Linux executor.
func (s *Setup) Linux() (Engine, error) {
logrus.Trace("creating linux executor client from setup")
// create new Linux executor engine
//
// https://pkg.go.dev/github.com/go-vela/worker/executor/linux?tab=doc#New
return linux.New(
linux.WithBuild(s.Build),
linux.WithLogMethod(s.LogMethod),
linux.WithMaxLogSize(s.MaxLogSize),
linux.WithHostname(s.Hostname),
linux.WithPipeline(s.Pipeline),
linux.WithRepo(s.Repo),
linux.WithRuntime(s.Runtime),
linux.WithUser(s.User),
linux.WithVelaClient(s.Client),
linux.WithVersion(s.Version),
linux.WithLogger(s.Logger),
)
}
// Local creates and returns a Vela engine capable of
// integrating with a local executor.
func (s *Setup) Local() (Engine, error) {
logrus.Trace("creating local executor client from setup")
// create new Local executor engine
//
// https://pkg.go.dev/github.com/go-vela/worker/executor/local?tab=doc#New
return local.New(
local.WithBuild(s.Build),
local.WithHostname(s.Hostname),
local.WithPipeline(s.Pipeline),
local.WithRepo(s.Repo),
local.WithRuntime(s.Runtime),
local.WithUser(s.User),
local.WithVelaClient(s.Client),
local.WithVersion(s.Version),
local.WithMockStdout(s.Mock),
)
}
// Windows creates and returns a Vela engine capable of
// integrating with a Windows executor.
func (s *Setup) Windows() (Engine, error) {
logrus.Trace("creating windows executor client from setup")
return nil, fmt.Errorf("unsupported executor driver: %s", constants.DriverWindows)
}
// Validate verifies the necessary fields for the
// provided configuration are populated correctly.
func (s *Setup) Validate() error {
logrus.Trace("validating executor setup for client")
// check if an executor driver was provided
if len(s.Driver) == 0 {
return fmt.Errorf("no executor driver provided in setup")
}
// check if a Vela pipeline was provided
if s.Pipeline == nil {
return fmt.Errorf("no Vela pipeline provided in setup")
}
// check if a runtime engine was provided
if s.Runtime == nil {
return fmt.Errorf("no runtime engine provided in setup")
}
// check if the local driver is provided
if strings.EqualFold(constants.DriverLocal, s.Driver) {
// all other fields are not required
// for the local executor
return nil
}
// handle the executor log method provided
switch s.LogMethod {
case "byte-chunks", "time-chunks":
case "":
return fmt.Errorf("empty executor log method provided in setup")
default:
return fmt.Errorf("invalid executor log method provided in setup: %s", s.LogMethod)
}
// check if a Vela client was provided
if s.Client == nil {
return fmt.Errorf("no Vela client provided in setup")
}
// check if a Vela build was provided
if s.Build == nil {
return fmt.Errorf("no Vela build provided in setup")
}
// check if a Vela repo was provided
if s.Repo == nil {
return fmt.Errorf("no Vela repo provided in setup")
}
// check if a Vela user was provided
if s.User == nil {
return fmt.Errorf("no Vela user provided in setup")
}
// setup is valid
return nil
}