-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathImageBuilder.groovy
More file actions
271 lines (215 loc) · 12 KB
/
ImageBuilder.groovy
File metadata and controls
271 lines (215 loc) · 12 KB
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package com.eficode.devstack.util;
import com.eficode.devstack.container.impl.DoodContainer
import de.gesellix.docker.remote.api.ImageSummary
import java.util.concurrent.TimeoutException
/**
* A utility class intended to build docker images so that they match the docker engines CPU architecture
*
*/
class ImageBuilder extends DoodContainer {
LinkedHashMap<String, String> builderCommands = [:]
Map<String, ArrayList<String>>builderOut = [:]
long cmdTimeoutS = 800 //Will timeout individual container commands after this many seconds
ImageBuilder(String dockerHost, String dockerCertPath) {
assert setupSecureRemoteConnection(dockerHost, dockerCertPath): "Error setting up secure remote docker connection"
prepareBindMount("/var/run/docker.sock", "/var/run/docker.sock")
}
ImageBuilder() {
prepareBindMount("/var/run/docker.sock", "/var/run/docker.sock")
}
/**
* Add bash commands that should be run in the build container
* @param cmd The command to run
* @param expectedLastOut That line of console output expected when running $cmd. Will throw exception if not true, will ignore if set to ""
*
* Ex: cmd = "echo status:\$?
* Ex: expectedLastOut = "status:0
*/
void putBuilderCommand(String cmd, String expectedLastOut) {
builderCommands.put(cmd, expectedLastOut)
}
/**
* Will check out Atlassian docker repo and build a JSM image matching docker engine CPU arch.
* The new image will be named atlassian/jira-servicemanagement and tagged $version-$cpuArch
* ex: atlassian/jira-servicemanagement:5.3.1-x86_64
* @param jsmVersion The JSM versions to build
* @param force If true, will always create and potentially overwrite existing image. If false, will return a pre-existing image if available
* @return
*/
ImageSummary buildJsm(String jsmVersion, boolean force = false){
String imageName = "atlassian/jira-servicemanagement"
String artifactName = "atlassian-servicedesk"
String archType = dockerClient.engineArch
String archTypeSuffix = archType == "x86_64" ? "" : "-$archType"
String imageTag = "$imageName:$jsmVersion$archTypeSuffix"
containerName = imageTag.replaceAll(/[^a-zA-Z0-9_.-]/, "-").take(128-"-imageBuilder".length())
containerName += "-imageBuilder"
//Check first if an image with the expected tag already exists
if (!force) {
ArrayList<ImageSummary> existingImages = dockerClient.images().content
ImageSummary existingImage = existingImages.find {it.repoTags == [imageTag]}
if (existingImage) {
return existingImage
}
}
putBuilderCommand("apt install git -y && echo status:\$?", "status:0")
putBuilderCommand("git clone --recurse-submodule https://bitbucket.org/atlassian-docker/docker-atlassian-jira.git && echo status:\$?", "status:0")
putBuilderCommand("cd docker-atlassian-jira && docker build --tag $imageTag --build-arg JIRA_VERSION=$jsmVersion --build-arg ARTEFACT_NAME=$artifactName . && echo status:\$?", "status:0")
putBuilderCommand("pkill tail", "")
assert build() : "Error building the image."
ArrayList<ImageSummary> images = dockerClient.images().content
ImageSummary newImage = images.find {it.repoTags == [imageTag]}
log.debug("\tFinished building image:" + imageTag + ", ID:" + newImage.id[7..17])
return newImage
}
/*
ImageSummary buildFakeTimeJsm(String jsmVersion, boolean force = false){
String imageName = "atlassian/jira-servicemanagement"
String artifactName = "atlassian-servicedesk"
String archType = dockerClient.engineArch
String archTypeSuffix = archType == "x86_64" ? "" : "-$archType"
String imageTag = "$imageName:$jsmVersion$archTypeSuffix"
String fakeTimeRoot = "/faketimebuild"
String fakeTimeDockerFilePath = "$fakeTimeRoot/Dockerfile"
String fakeTimeAgentFilePath = "$fakeTimeRoot/faketime.cpp"
String fakeTimeImageTag = "$imageName-faketime:$jsmVersion$archTypeSuffix"
String fakeTimCpp = getClass().getResourceAsStream("/faketime.cpp").text
containerName = fakeTimeImageTag.replaceAll(/[^a-zA-Z0-9_.-]/, "-").take(128-"-IB".length())
containerName += "-IB"
log.info("my name is now $containerName")
//Check first if an image with the expected tag already exists
if (!force) {
ArrayList<ImageSummary> existingImages = dockerClient.images().content
ImageSummary existingImage = existingImages.find {it.repoTags == [fakeTimeImageTag]}
if (existingImage) {
return existingImage
}
}
String fakeTimeDockerFile = """
FROM $imageTag
WORKDIR /
RUN apt-get update && apt-get install -y wget g++ make
# RUN wget https://github.com/odnoklassniki/jvmti-tools/raw/master/faketime/faketime.cpp
COPY faketime.cpp .
RUN g++ -O2 -fPIC -shared -I \$JAVA_HOME/include -I \$JAVA_HOME/include/linux -olibfaketime.so faketime.cpp
ENV JVM_SUPPORT_RECOMMENDED_ARGS="-agentpath:/libfaketime.so=+2592000000"
"""
putBuilderCommand("mkdir -p $fakeTimeRoot", "")
putBuilderCommand("cat > $fakeTimeDockerFilePath <<- 'EOF'\n" + fakeTimeDockerFile + "\nEOF", "")
putBuilderCommand("cat > $fakeTimeAgentFilePath <<- 'EOF'\n" + fakeTimCpp + "\nEOF", "")
putBuilderCommand("cd $fakeTimeRoot && docker build --tag $fakeTimeImageTag --build-arg JIRA_VERSION=$jsmVersion --build-arg ARTEFACT_NAME=$artifactName . && echo status:\$?", "status:0")
putBuilderCommand("pkill tail", "")
assert build() : "Error building the image."
ArrayList<ImageSummary> images = dockerClient.images().content
ImageSummary newImage = images.find {it.repoTags == [fakeTimeImageTag]}
return newImage
}
*/
ImageSummary buildJvmFakeTime(ImageSummary originalImage, boolean force = false) {
String originalRepoTag = originalImage.repoTags.first()
String origImageName = originalRepoTag.substring(0,originalRepoTag.indexOf(":"))
String origImageTag = originalRepoTag.substring(originalRepoTag.indexOf(":")+ 1)
return buildJvmFakeTime(origImageName, origImageTag, force)
}
//Presumes srcImage has "apt-get" commands
ImageSummary buildJvmFakeTime(String srcImage, String srcImageTag, boolean force) {
String fakeTimeImageTag = "$srcImage-faketime:$srcImageTag"
containerName = fakeTimeImageTag.replaceAll(/[^a-zA-Z0-9_.-]/, "-").take(120-"-BuildFake".length())
String fakeTimeRoot = "/faketimebuild"
String fakeTimeDockerFilePath = "$fakeTimeRoot/Dockerfile"
String fakeTimeAgentFilePath = "$fakeTimeRoot/faketime.cpp"
String fakeTimCpp = getClass().getResourceAsStream("/faketime.cpp").text
//Check first if an image with the expected tag already exists
if (!force) {
ArrayList<ImageSummary> existingImages = dockerClient.images().content
ImageSummary existingImage = existingImages.find {it.repoTags == [fakeTimeImageTag]}
if (existingImage) {
return existingImage
}
}
String fakeTimeDockerFile = """
FROM $srcImage:$srcImageTag
WORKDIR /
RUN apt-get update && apt-get install -y wget g++ make
COPY faketime.cpp /faketime.cpp
RUN g++ -O2 -fPIC -shared -I \$JAVA_HOME/include -I \$JAVA_HOME/include/linux -olibfaketime.so faketime.cpp
"""
// #ENV JVM_SUPPORT_RECOMMENDED_ARGS="-agentpath:/libfaketime.so=+2592000000"
//#RUN apt-get update && apt-get install -y g++ make
// #RUN wget https://github.com/odnoklassniki/jvmti-tools/raw/master/faketime/faketime.cpp
putBuilderCommand("mkdir -p $fakeTimeRoot", "")
putBuilderCommand("cat > $fakeTimeDockerFilePath <<- 'EOF'\n" + fakeTimeDockerFile + "\nEOF", "")
putBuilderCommand("cat > $fakeTimeAgentFilePath <<- 'EOF'\n" + fakeTimCpp + "\nEOF", "")
putBuilderCommand("cd $fakeTimeRoot && docker build --tag $fakeTimeImageTag . && echo status:\$?", "status:0")
putBuilderCommand("pkill tail", "")
assert build() : "Error building the image."
ArrayList<ImageSummary> images = dockerClient.images().content
ImageSummary newImage = images.find {it.repoTags == [fakeTimeImageTag]}
return newImage
}
/**
* Will check out Atlassian docker repo and build a Bitbucket image matching docker engine CPU arch.
* The new image will be named atlassian/bitbucket and tagged $version-$cpuArch
* ex: atlassian/bitbucket:5.3.1-x86_64
* @param bbVersion Version of bitbucket to build
* @param force If true, will always create and potentially overwrite existing image. If false, will return a pre-existing image if available
* @return
*/
ImageSummary buildBb(String bbVersion, boolean force = false){
String imageName = "atlassian/bitbucket"
String archType = dockerClient.engineArch
String imageTag = "$imageName:$bbVersion-$archType"
containerName = imageTag.replaceAll(/[^a-zA-Z0-9_.-]/, "-").take(128-"-imageBuilder".length())
containerName += "-imageBuilder"
//Check first if an image with the expected tag already exists
if (!force) {
ArrayList<ImageSummary> existingImages = dockerClient.images().content
ImageSummary existingImage = existingImages.find {it.repoTags == [imageTag]}
if (existingImage) {
return existingImage
}
}
putBuilderCommand("apt install git -y && echo status:\$?", "status:0")
putBuilderCommand("git clone --recurse-submodule https://bitbucket.org/atlassian-docker/docker-atlassian-bitbucket-server/ && echo status:\$?", "status:0")
putBuilderCommand("cd docker-atlassian-bitbucket-server && docker build --tag $imageTag --build-arg BITBUCKET_VERSION=$bbVersion . && echo status:\$?", "status:0")
putBuilderCommand("pkill tail", "")
assert build() : "Error building the image."
ArrayList<ImageSummary> images = dockerClient.images().content
ImageSummary newImage = images.find {it.repoTags == [imageTag]}
log.debug("\tFinished building image:" + imageTag + ", ID:" + newImage.id[7..17])
return newImage
}
/**
* Takes care of setting up a Dood container and running the commands submitted with putBuilderCommand
* The putBuilderCommand-commands should take care of killing the tail process with ex: pkill tail
* @return true on success. Cmd output can be found in builderOut
*/
private boolean build() {
log.info("Creating and starting Image Builder container")
long start = System.currentTimeSeconds()
log.info("\tStarting container, waiting for it to finish")
createContainer([],["/bin/bash" ,"-c" ,"trap \"exit\" SIGINT SIGTERM && tail -F /var/log/*"])
startContainer()
log.info("\t\tContainer finish after ${System.currentTimeSeconds() - start}s")
stopAndRemoveContainer()
return true
}
@Override
boolean runAfterDockerSetup(){
builderCommands.each {cmd, expectedLastOut ->
log.info("Running container command:" + cmd)
log.info("\tExpecting last output from command:" + expectedLastOut)
ArrayList<String> output = runBashCommandInContainer(cmd, cmdTimeoutS)
log.debug("\tCmd returned output:")
output.each {log.debug("\t\t" + it)}
builderOut.put(cmd, output)
if (expectedLastOut != "" && expectedLastOut != output.last()) {
String error = "Container cmd \"$cmd\" returned unexpected output \"${output.last()}\", but expected \"$expectedLastOut\""
log.error(error)
throw new InputMismatchException(error)
}
log.debug("\tSuccessful validation of cmd output")
}
return true
}
}