-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDockerImageCreator.rb
284 lines (218 loc) · 8.62 KB
/
DockerImageCreator.rb
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
272
273
274
275
276
277
278
279
280
281
282
283
284
# Common helpers for docker image creation for building projects that
# use RubySetupSystem
require 'optparse'
require_relative 'RubyCommon.rb'
def checkRunFolder(suggested)
buildFolder = File.join(suggested, 'build')
onError('Not ran from base folder (no build directory exists)') unless
File.exist?(buildFolder)
target = File.join suggested, 'build', 'docker'
FileUtils.mkdir_p target
target
end
def projectFolder(baseDir)
File.expand_path File.join(baseDir, '../../')
end
def getExtraOptions(opts)
opts.on('--build-docker', 'If specified builds a docker file automatically otherwise ' \
'only a Dockerfile is created') do |_b|
$options[:dockerbuild] = true
end
end
def extraHelp
puts $extraParser
end
require_relative 'RubySetupSystem.rb'
# Read extraOptions
$doBuild = $options.include?(:dockerbuild) ? $options[:dockerbuild] : false
# Overwrite the the operating system to work well with the fedora
# images
def getLinuxOS
'fedora'
end
def doDockerBuild(folder)
if runSystemSafe('docker', 'build', folder) != 0
warning 'Failed to run docker as normal user, trying sudo'
runOpen3Checked('sudo', 'docker', 'build', folder)
end
end
def writeCommonDockerFile(file, packageNames, extraSteps)
file.puts('FROM fedora:30')
file.puts('RUN dnf install -y --setopt=deltarpm=false ruby ruby-devel ' +
packageNames.join(' ') + ' gcc make redhat-rpm-config fedora-repos-rawhide ' \
'clang cmake && dnf clean all')
file.puts('RUN git lfs install') if packageNames.include? 'git-lfs'
file.puts('RUN gem install os colorize rubyzip json sha3')
# vnc setup part
# This doesn't seem to actually help with a missing x server
# file.puts("RUN dnf install -y x11vnc")
# file.puts("RUN mkdir /root/.vnc")
# file.puts(%q(RUN x11vnc -storepasswd "vncdocker" ~/.vnc/passwd))
# Rawhide overrides
# glm is no longer used
# file.puts("RUN dnf install -y --disablerepo=* --enablerepo=rawhide " +
# "--setopt=deltarpm=false glm-devel")
# Disable SVN password saving
file.puts('RUN mkdir /root/.subversion')
file.puts("RUN echo $'[global]\\n\\
store-plaintext-passwords = no\\n' > /root/.subversion/servers")
if extraSteps
extraSteps.each do |step|
file.puts step
end
end
end
# Main run method
def runDockerCreate(libsList, mainProjectAsDep = nil, extraPackages: [], extraSteps: [])
libsList.push mainProjectAsDep if mainProjectAsDep
packageNames = []
libsList.each do |lib|
unless lib.respond_to?(:installPrerequisites)
puts "Skipping #{lib.Name} which doesn't specify packages to install"
next
end
packageNames.push(*lib.depsList)
end
# We need lsb_release
packageNames.push 'redhat-lsb-core'
# Might as well install all the svc tools
packageNames.push 'git', 'svn', 'mercurial'
# And 7z
packageNames.push 'p7zip'
# And make sure tar with lzma support is there
packageNames.push 'tar'
# On ubuntu this is called xz-utils
packageNames.push 'xz'
# Optional
packageNames.concat extraPackages
packageNames.uniq!
puts ''
success 'Successfully ran package collection'
info "Detected packages: #{packageNames}"
info "Package count: #{packageNames.count}"
FileUtils.mkdir_p File.join(CurrentDir, 'simple')
FileUtils.mkdir_p File.join(CurrentDir, 'jenkins')
dockerFile = File.join CurrentDir, 'simple', 'Dockerfile'
puts "Writing docker file at '#{dockerFile}'"
File.open(dockerFile, 'w') do |file|
writeCommonDockerFile file, packageNames, extraSteps
end
jenkinsDocker = File.join CurrentDir, 'jenkins', 'Dockerfile'
jenkinsSetupSSHD = File.join CurrentDir, 'jenkins', 'setup-sshd'
puts "Writing docker (jenkins) file at '#{jenkinsDocker}'"
File.open(jenkinsDocker, 'w') do |file|
writeCommonDockerFile file, packageNames, extraSteps
# Needed things for jenkins.
# From here: https://wiki.jenkins.io/display/JENKINS/Docker+Plugin
file.puts('RUN dnf install -y --setopt=deltarpm=false openssh-server ' \
'java-1.8.0-openjdk && dnf clean all')
# This probably messes up everything as this probably runs before the keys are fine
# file.puts("RUN systemctl enable sshd")
# And stuff from: https://hub.docker.com/r/jenkins/ssh-slave/~/dockerfile/
# The MIT License
#
# Copyright (c) 2015, CloudBees, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
file.puts <<-END
ARG user=jenkins
ARG group=jenkins
ARG uid=1000
ARG gid=1000
ARG JENKINS_AGENT_HOME=/home/${user}
ENV JENKINS_AGENT_HOME ${JENKINS_AGENT_HOME}
RUN groupadd -g ${gid} ${group} \
&& useradd -d "${JENKINS_AGENT_HOME}" -u "${uid}" -g "${gid}" -m -s /bin/bash "${user}"
# setup SSH server
RUN sed -i 's/#PermitRootLogin.*/PermitRootLogin no/' /etc/ssh/sshd_config
RUN sed -i 's/#RSAAuthentication.*/RSAAuthentication yes/' /etc/ssh/sshd_config
RUN sed -i 's/#PasswordAuthentication.*/PasswordAuthentication no/' /etc/ssh/sshd_config
RUN sed -i 's/#SyslogFacility.*/SyslogFacility AUTH/' /etc/ssh/sshd_config
RUN sed -i 's/#LogLevel.*/LogLevel INFO/' /etc/ssh/sshd_config
RUN mkdir /var/run/sshd
VOLUME "${JENKINS_AGENT_HOME}" "/tmp" "/run" "/var/run"
WORKDIR "${JENKINS_AGENT_HOME}"
COPY setup-sshd /usr/bin/setup-sshd
EXPOSE 22
ENTRYPOINT ["setup-sshd"]
END
File.write(jenkinsSetupSSHD, <<-END
#!/bin/bash
set -ex
# The MIT License
#
# Copyright (c) 2015, CloudBees, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# Usage:
# docker run jenkinsci/ssh-slave <public key>
# or
# docker run -e "JENKINS_SLAVE_SSH_PUBKEY=<public key>" jenkinsci/ssh-slave
# key first
echo "Leviathan jenkins dep container ssh setup."
ssh-keygen -A
write_key() {
mkdir -p "${JENKINS_AGENT_HOME}/.ssh"
echo "$1" > "${JENKINS_AGENT_HOME}/.ssh/authorized_keys"
chown -Rf jenkins:jenkins "${JENKINS_AGENT_HOME}/.ssh"
chmod 0700 -R "${JENKINS_AGENT_HOME}/.ssh"
}
if [[ $JENKINS_SLAVE_SSH_PUBKEY == ssh-* ]]; then
write_key "${JENKINS_SLAVE_SSH_PUBKEY}"
fi
if [[ $# -gt 0 ]]; then
if [[ $1 == ssh-* ]]; then
write_key "$1"
shift 1
else
exec "$@"
fi
fi
# ensure variables passed to docker container are also exposed to ssh sessions
env | grep _ >> /etc/environment
exec /usr/sbin/sshd -D -e "${@}"
END
)
FileUtils.chmod '+x', jenkinsSetupSSHD
end
unless $doBuild
success "Skipping building. Run 'docker build' manually. All done."
exit 0
end
success 'Done writing docker file. Building the image(s)...'
doDockerBuild File.join(CurrentDir, 'simple')
doDockerBuild File.join(CurrentDir, 'jenkins')
success 'Done. See above output for the created image details'
end