-
Notifications
You must be signed in to change notification settings - Fork 3
/
jenkins_server.py
245 lines (203 loc) · 7.4 KB
/
jenkins_server.py
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
import jenkins
# https://python-jenkins.readthedocs.io/en/latest/
# https://opendev.org/jjb/python-jenkins
# pip install python-jenkins
import time
import xmltodict
import json
from config import *
from string import Template
import shlex
class JenkinsServer:
def __init__(self, host, username, password, retries=5):
self.server = jenkins.Jenkins(host, username=username, password=password)
def hasJenkinsJob(self, id):
return self.server.job_exists(id)
def build_job(self, job_id):
# https://python-jenkins.readthedocs.io/en/latest/api.html#jenkins.Jenkins.build_job
return self.server.build_job(job_id)
def get_job_info(self, job_id):
return self.server.get_job_info(job_id, depth=0, fetch_all_builds=False)
def createJob(self, id, app_spec, overwrite=False, skip_image_push=False):
# format https://github.com/user/repo.git#v1.0
version = app_spec["version"]
source = app_spec.get("source", None)
if not source:
raise Exception("field source empty")
git_url = source.get("url", "")
tag = source.get("tag") or ""
branch = source.get("branch") or ""
if tag != "":
git_branch = f"refs/tags/{tag}"
elif branch != "":
git_branch = branch
else:
raise Exception("neither tag nor branch specified")
git_directory = source.get("directory", ".")
git_directory = git_directory.removeprefix("/")
git_dockerfile = source.get("dockerfile", "./Dockerfile")
platforms = source.get("architectures", [])
if len(platforms) == 0:
raise Exception("No architectures specified")
platforms_str = ",".join(platforms)
build_args = source.get("build_args", {})
build_args_command_line = ""
for key in build_args:
value = build_args[key]
build_args_command_line += f" --build-arg {key}={value}"
if docker_build_args != "":
build_args_command_line += f" {docker_build_args}"
actual_namespace = ""
namespace = app_spec.get("namespace", "")
if len(namespace) > 0:
actual_namespace = namespace
else:
actual_namespace = app_spec.get("owner", "")
name = app_spec["name"]
# TODO(sean) Decide if / host to restore test feature and understand its relationship to profiling. We should consider a unified approach to those.
# TODO(sean) Add test case for submodule clone.
# fix dockerfile support
template = Template(
"""pipeline {
agent any
stages {
stage ("Checkout") {
steps {
checkout scmGit(
branches: [[name: '${branch}']],
extensions: [cloneOption(shallow: true)],
userRemoteConfigs: [[url: '${url}']],
poll: false)
script {
dir("$${env.WORKSPACE}") {
sh "git submodule update --init --recursive"
}
}
}
}
stage ("Build") {
steps {
script {
stage("Build") {
currentBuild.displayName = "${version}"
dir("$${env.WORKSPACE}/${directory}") {
sh "${build_command}"
}
}
}
}
}
}
post {
always {
cleanWs(cleanWhenNotBuilt: true)
}
}
}
"""
)
# add validation here!!!
options = ["--opt", f"platform={','.join(platforms)}"]
# specifies an alternative dockerfile filename
if git_dockerfile and git_dockerfile != "":
options += ["--opt", f"filename={git_dockerfile}"]
output_args = [
"type=image",
f"name={docker_registry_url}/{actual_namespace}/{name}:{version}",
]
# specifies that we should push the image to the registry
if docker_registry_push_allowed:
output_args += ["push=true"]
# specifies that the registry is insecure (served over http instead of https). should only be used for local testing!
if docker_registry_insecure:
output_args += ["registry.insecure=true"]
build_command = shlex.join(
[
"buildctl",
"--addr",
buildkitd_address,
"build",
"--frontend=dockerfile.v0",
"--local",
"context=.",
"--local",
"dockerfile=.",
*options,
"--output",
",".join(output_args),
]
)
try:
jenkinsfile = template.substitute(
url=git_url,
branch=git_branch,
directory=git_directory,
# dockerfile=git_dockerfile, # add this back in
version=version,
build_command=build_command,
)
except Exception as e:
raise Exception(
f"template failed: url={git_url}, branch={git_branch}, directory={git_directory}, e={str(e)}"
)
# print(jenkins.EMPTY_CONFIG_XML)
newJob = createPipelineJobConfig(jenkinsfile, f"{actual_namespace}/{name}")
print(newJob)
newJob_xml = xmltodict.unparse(newJob) # .decode("utf-8")
# print("------")
# print(newJob_xml)
# print("------")
# print(jenkins.EMPTY_CONFIG_XML)
# print("------")
if overwrite:
self.server.reconfig_job(id, newJob_xml)
else:
self.server.create_job(id, newJob_xml)
timeout = 10
while True:
try:
return self.server.get_job_config(id)
except jenkins.NotFoundException as e: # pragma: no cover
pass
except Exception as e: # pragma: no cover
raise
time.sleep(2)
timeout -= 2
if timeout <= 0:
raise Exception(f"timout afer job creation")
def createPipelineJobConfig(jenkinsfile, displayName):
jenkins_job_example_xml = """<?xml version='1.1' encoding='UTF-8'?>
<flow-definition plugin="[email protected]">
<displayName>overwrite_me</displayName>
<description></description>
<keepDependencies>false</keepDependencies>
<properties/>
<definition class="org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition" plugin="[email protected]">
<script>pipeline {
agent any
stages {
stage('Hello') {
steps {
echo 'Hello World'
}
}
}
}
</script>
<sandbox>true</sandbox>
</definition>
<triggers/>
<quietPeriod>0</quietPeriod>
<disabled>false</disabled>
</flow-definition>
"""
job = xmltodict.parse(jenkins_job_example_xml)
# jenkinsfile = 'pipeline {}'
print(json.dumps(job, indent=4))
job["flow-definition"]["definition"][
"script"
] = jenkinsfile # cgi.escape(jenkinsfile)
job["flow-definition"]["displayName"] = displayName
# job["project"]["scm"]["userRemoteConfigs"]["hudson.plugins.git.UserRemoteConfig"]["url"] = 'https://github.com/sagecontinuum/sage-cli.git'
print(json.dumps(job, indent=4))
return job