-
Notifications
You must be signed in to change notification settings - Fork 6
/
worker.go
156 lines (140 loc) · 3.6 KB
/
worker.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
package main
/*
* SPDX-FileCopyrightText: 2023 Siemens AG
*
* SPDX-License-Identifier: Apache-2.0
*
* Author: Michael Adler <[email protected]>
*/
import (
"fmt"
"io"
"log"
"os"
"os/exec"
"path"
"time"
"github.com/go-openapi/strfmt"
"github.com/siemens/wfx/generated/client"
"github.com/siemens/wfx/generated/client/jobs"
"github.com/siemens/wfx/generated/model"
)
func worker() {
workflow := "wfx.workflow.config.deployment"
initialState := "NEW"
limit := int32(1)
queryParams := jobs.NewGetJobsParams().
WithClientID(&clientID).
WithWorkflow(&workflow).
WithState(&initialState).
WithLimit(&limit)
cfg := client.DefaultTransportConfig()
cfg.Host = fmt.Sprintf("%s:%d", host, port)
c := client.NewHTTPClientWithConfig(strfmt.Default, cfg)
for !done.Load() {
log.Println("\n>> Waiting for new job")
var job *model.Job
for {
if done.Load() {
break
}
resp, err := c.Jobs.GetJobs(queryParams)
if err == nil && len(resp.Payload.Content) > 0 {
job = resp.Payload.Content[0]
break
}
time.Sleep(pollInterval)
}
log.Println("Got new job with ID", job.ID)
if err := processJob(c, job); err != nil {
log.Println("Failed to process job:", err)
updateJobStatus(c, job, "FAILED", nil)
continue
}
log.Println("Successfully processed job")
updateJobStatus(c, job, "DONE", nil)
}
}
func processJob(c *client.WorkflowExecutor, job *model.Job) error {
tmpDir, err := os.MkdirTemp("", "config-deployer")
if err != nil {
return err
}
defer func() {
_ = os.RemoveAll(tmpDir)
}()
updateJobStatus(c, job, "DOWNLOADING", nil)
url := job.Definition["url"].(string)
log.Println("Downloading", url)
tmpFile, err := download(url, tmpDir)
if err != nil {
return err
}
log.Println("Download successful, starting installation")
updateJobStatus(c, job, "INSTALLING", nil)
// run preinstall script
if preinstall, ok := job.Definition["preinstall"].(string); ok {
log.Println("Running preinstall script")
if err := runScript(preinstall); err != nil {
log.Println("Failed to run preinstall script")
} else {
log.Println("Preinstall script was successful")
}
}
// deploy the artifact
if destination, ok := job.Definition["destination"].(string); ok {
_ = os.MkdirAll(path.Dir(destination), 0o755)
src, err := os.Open(tmpFile)
if err != nil {
return err
}
defer src.Close()
dst, err := os.Create(destination)
if err != nil {
return err
}
defer dst.Close()
log.Printf("Copying %s to %s\n", src.Name(), dst.Name())
if _, err := io.Copy(dst, src); err != nil {
return err
}
}
// run postinstall script
if postinstall, ok := job.Definition["postinstall"].(string); ok {
log.Println("Running postinstall script")
if err := runScript(postinstall); err != nil {
log.Println("Failed to run postinstall script")
} else {
log.Println("postinstall script was successful")
}
}
return nil
}
func updateJobStatus(c *client.WorkflowExecutor, job *model.Job, state string, err error) {
if err == nil {
log.Println("Setting job status to", state)
} else {
log.Printf("Setting job status to %s, err=%s\n", state, err)
}
job.Status = &model.JobStatus{State: state}
if err != nil {
job.Status.Context = map[string]any{
"error": err.Error(),
}
}
_, err = c.Jobs.PutJobsIDStatus(jobs.NewPutJobsIDStatusParams().
WithID(job.ID).
WithNewJobStatus(job.Status))
if err != nil {
log.Println("Failed to update job status:", err)
}
}
func runScript(script string) error {
cmd := exec.Command("sh", "-c", script)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return err
}
return nil
}