forked from forj-oss/forjj-jenkins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_struct.go
208 lines (186 loc) · 5.01 KB
/
run_struct.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
package main
import (
"fmt"
log "forjj-jenkins/reportlogs"
"os"
"path"
)
type RunStruct struct {
StepName string `yaml:"name,omitempty"`
RunCommand string `yaml:"run"`
Description string
Env map[string]EnvStruct `yaml:"environment"`
Files RunFilesStruct
}
// run execute the defined command with the context model and docker authentication given
//
// Use reportLog or Errorf to report to the end users.
// But errors detected is not interrupting the program immediatelly in order to report all issues to
// fix
func (r RunStruct) run(instance, sourcePath, deployPath string, model *JenkinsPluginModel, auths *DockerAuths) (err error) {
// start a command as described by the source code.
// Errors detected if true.
log.Printf("Checking command to run and context")
if r.RunCommand == "" {
log.Errorf("Unable to run a command: Command is empty")
}
for server := range auths.Auths {
if err = auths.authenticate(server); err != nil {
return
}
}
env := r.defineEnv(instance, sourcePath, deployPath, model)
if err = r.Files.createFiles(model, deployPath); err != nil {
return
}
if log.HasReportedErrors() {
return fmt.Errorf("Unable to run command. Errors detected")
}
if err = os.Chdir(deployPath); err != nil {
return fmt.Errorf("Unable to move to '%s'. %s", deployPath, err)
}
log.Reportf("Running '%s' from '%s'", r.RunCommand, deployPath)
err = runFlowCmd("/bin/sh", env,
func(line string) {
log.Reportf("%s", line)
},
func(line string) {
log.Reportf("%s", line)
}, "-c", r.RunCommand)
r.Files.deleteFiles()
return
}
type envMapFunc struct {
key string
more func(par, value string) (string, []string, bool)
}
type envMapFuncs []envMapFunc
func (r RunStruct) setEnv(parameters envMapFuncs) (env []string) {
env = []string{}
for _, envFunc := range parameters {
v := os.Getenv(envFunc.key)
value, envToAdd, ignore := envFunc.more(envFunc.key, v)
if envToAdd != nil {
env = append(env, envToAdd...)
for _, theEnv := range envToAdd {
log.Printf("More predefined env added : '%s'", theEnv)
}
}
if ignore || (v == "" && value == "") {
continue
}
if value != "" {
v = value
}
env = append(env, envFunc.key+"="+v)
log.Printf("Predefined env added : '%s' = '%s'", envFunc.key, v)
}
return
}
func (r RunStruct) noEnvFunc(_, _ string) (_ string, _ []string, _ bool) {
return
}
func (r RunStruct) defineEnv(instance, sourcePath, deployPath string, model *JenkinsPluginModel) (env []string) {
dood := false
doodBecome := false
env = r.setEnv([]envMapFunc{
envMapFunc{"DOCKER_DOOD",
func(par, value string) (ret string, _ []string, ignore bool) {
if value != "" {
log.Printf("DOOD detected.")
dood = true
return
}
ignore = true
return
},
},
envMapFunc{"DOCKER_DOOD_PROXY", r.noEnvFunc},
envMapFunc{"DOOD_SOURCE", r.noEnvFunc},
envMapFunc{"DOOD_SRC",
func(par, value string) (ret string, env []string, ignore bool) {
if value == "" || !dood {
env = []string{"SRC=" + sourcePath}
ignore = !dood
return
}
ret = path.Join(value, instance) + "/"
env = []string{"SRC=" + ret}
return
},
},
envMapFunc{"DOOD_DEPLOY",
func(par, value string) (ret string, env []string, ignore bool) {
if value == "" || !dood {
env = []string{"DEPLOY=" + deployPath}
ignore = !dood
return
}
ret = path.Join(value, instance) + "/"
env = []string{"DEPLOY=" + ret}
return
},
},
envMapFunc{"DOCKER_DOOD_BECOME",
func(par, value string) (ret string, _ []string, ignore bool) {
if value != "" {
log.Printf("DOOD_BECOME detected.")
doodBecome = true
return
}
ignore = true
return
},
},
envMapFunc{"GID",
func(par, value string) (ret string, _ []string, ignore bool) {
ignore = !doodBecome
return
},
},
envMapFunc{"UID",
func(par, value string) (ret string, _ []string, ignore bool) {
ignore = !doodBecome
return
},
},
envMapFunc{"LOGNAME", r.noEnvFunc},
envMapFunc{"PATH", r.noEnvFunc},
envMapFunc{"TERM", r.noEnvFunc},
envMapFunc{"HOSTNAME", r.noEnvFunc},
envMapFunc{"http_proxy", r.noEnvFunc},
envMapFunc{"https_proxy", r.noEnvFunc},
envMapFunc{"no_proxy", r.noEnvFunc},
envMapFunc{"SELF_SRC",
func(par, value string) (ret string, _ []string, _ bool) {
ret = sourcePath
return
},
},
envMapFunc{"SELF_DEPLOY",
func(par, value string) (ret string, _ []string, _ bool) {
ret = deployPath
return
},
},
})
for key, envToSet := range r.Env {
if envToSet.If != "" {
// check if If evaluation return something or not. if not, the environment key won't be created.
if v, err := Evaluate(envToSet.If, model); err != nil {
log.Errorf("Error in evaluating '%s'. %s", key, err)
} else {
if v == "" {
continue
}
}
}
if v, err := Evaluate(envToSet.Value, model); err != nil {
log.Errorf("Error in evaluating '%s'. %s", key, err)
} else {
env = append(env, key+"="+v)
log.Printf("Env added : '%s' = '%s'", key, v)
}
}
return
}