/
runner.go
240 lines (192 loc) · 6.1 KB
/
runner.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
package runner
import (
"embed"
"encoding/json"
"fmt"
"io/fs"
"io/ioutil"
"os"
"path"
log "github.com/sirupsen/logrus"
)
//go:embed ansible
var ansibleFS embed.FS
const (
executionChannelSize = 99
AnsibleMain = "ansible/check.yml"
AnsibleMeta = "ansible/meta.yml"
AnsibleConfigFile = "ansible/ansible.cfg"
AnsibleInventories = "ansible/inventories/%s/ansible_hosts"
executionStartedEvent = "execution_started"
)
//go:generate mockery --name=RunnerService --inpackage --filename=runner_mock.go
type RunnerService interface {
IsCatalogReady() bool
BuildCatalog() error
GetCatalog() *Catalog
GetChannel() chan *ExecutionEvent
ScheduleExecution(e *ExecutionEvent) error
Execute(e *ExecutionEvent) error
}
type runnerService struct {
config *Config
workerPoolChannel chan *ExecutionEvent
callbacksClient CallbacksClient
catalog *Catalog
ready bool
}
func NewRunnerService(config *Config) (*runnerService, error) {
runner := &runnerService{
config: config,
workerPoolChannel: make(chan *ExecutionEvent, executionChannelSize),
callbacksClient: NewCallbacksClient(config.CallbacksUrl),
ready: false,
}
return runner, nil
}
func (c *runnerService) IsCatalogReady() bool {
return c.ready
}
func (c *runnerService) BuildCatalog() error {
if err := createAnsibleFiles(c.config.AnsibleFolder); err != nil {
return err
}
metaRunner, err := NewAnsibleMetaRunner(c.config)
if err != nil {
return err
}
// The checks catalog metadata playbook creates the checks catalog in the provider file path
if err = metaRunner.RunPlaybook(); err != nil {
log.Errorf("Error running the catalog meta-playbook")
return err
}
// After the playbook is done, recover back the file content
catalogFile, err := ioutil.ReadFile(metaRunner.Envs[CatalogDestination])
if err != nil {
log.Fatal("Error when opening the catalog file: ", err)
}
var catalog *Catalog
err = json.Unmarshal(catalogFile, &catalog)
if err != nil {
log.Fatal("Error during Unmarshal(): ", err)
}
c.catalog = catalog
c.ready = true
return nil
}
func (c *runnerService) GetCatalog() *Catalog {
return c.catalog
}
func (c *runnerService) GetChannel() chan *ExecutionEvent {
return c.workerPoolChannel
}
func (c *runnerService) ScheduleExecution(e *ExecutionEvent) error {
if len(c.workerPoolChannel) == executionChannelSize {
return fmt.Errorf("Cannot process more executions")
}
c.workerPoolChannel <- e
log.Infof("Scheduled event: %s", e.ExecutionID.String())
return nil
}
func (c *runnerService) Execute(e *ExecutionEvent) error {
log.Infof("Executing event: %s on cluster: %s", e.ExecutionID.String(), e.ClusterID.String())
executionStartedPayload := map[string]string{"cluster_id": e.ClusterID.String()}
if err := c.callbacksClient.Callback(e.ExecutionID, executionStartedEvent, executionStartedPayload); err != nil {
log.Errorf(
"Error running callback. Execution ID: %s, Event: %s. Err: %s", e.ExecutionID.String(), executionStartedEvent, err)
return err
}
checksRunner, err := NewAnsibleCheckRunner(c.config, e)
if err != nil {
return err
}
defer os.RemoveAll(path.Dir(checksRunner.Inventory))
if err := checksRunner.RunPlaybook(); err != nil {
log.Errorf("Error running the checks playbook")
return err
}
return nil
}
func createAnsibleFiles(folder string) error {
log.Infof("Creating the ansible file structure in %s", folder)
// Clean the folder if it stores old files
ansibleFolder := path.Join(folder, "ansible")
err := os.RemoveAll(ansibleFolder)
if err != nil {
log.Error(err)
return err
}
err = os.MkdirAll(ansibleFolder, 0755)
if err != nil {
log.Error(err)
return err
}
// Create the ansible file structure from the FS
err = fs.WalkDir(ansibleFS, "ansible", func(fileName string, dir fs.DirEntry, err error) error {
if err != nil {
return err
}
if !dir.IsDir() {
content, err := ansibleFS.ReadFile(fileName)
if err != nil {
log.Errorf("Error reading file %s", fileName)
return err
}
f, err := os.Create(path.Join(folder, fileName))
if err != nil {
log.Errorf("Error creating file %s", fileName)
return err
}
fmt.Fprintf(f, "%s", content)
} else {
os.Mkdir(path.Join(folder, fileName), 0755)
}
return nil
})
if err != nil {
log.Errorf("An error ocurred during the ansible file structure creation: %s", err)
return err
}
log.Info("Ansible file structure successfully created")
return nil
}
func NewAnsibleMetaRunner(config *Config) (*AnsibleRunner, error) {
playbookPath := path.Join(config.AnsibleFolder, AnsibleMeta)
ansibleRunner := DefaultAnsibleRunner()
if err := ansibleRunner.SetPlaybook(playbookPath); err != nil {
return ansibleRunner, err
}
configFile := path.Join(config.AnsibleFolder, AnsibleConfigFile)
ansibleRunner.SetConfigFile(configFile)
destination := path.Join(config.AnsibleFolder, CatalogDestinationFile)
ansibleRunner.SetCatalogDestination(destination)
return ansibleRunner, nil
}
func NewAnsibleCheckRunner(config *Config, executionEvent *ExecutionEvent) (*AnsibleRunner, error) {
playbookPath := path.Join(config.AnsibleFolder, AnsibleMain)
ansibleRunner := DefaultAnsibleRunner()
if err := ansibleRunner.SetPlaybook(playbookPath); err != nil {
return ansibleRunner, err
}
ansibleRunner.Check = true
configFile := path.Join(config.AnsibleFolder, AnsibleConfigFile)
ansibleRunner.SetConfigFile(configFile)
ansibleRunner.SetTrentoCallbacksUrl(config.CallbacksUrl)
ansibleRunner.SetTrentoExecutionID(executionEvent.ExecutionID.String())
inventoryContent, err := NewClusterInventoryContent(executionEvent)
if err != nil {
log.Errorf("Error generating inventory content: %s", err)
return nil, err
}
inventoryFile := path.Join(
config.AnsibleFolder, fmt.Sprintf(AnsibleInventories, executionEvent.ExecutionID.String()))
if err := CreateInventory(inventoryFile, inventoryContent); err != nil {
log.Errorf("Error creating the inventory file: %s", err)
return nil, err
}
if err := ansibleRunner.SetInventory(inventoryFile); err != nil {
log.Errorf("Error setting the inventory file")
return nil, err
}
return ansibleRunner, nil
}