forked from ngageoint/hootenanny-rpms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vagrantfile
302 lines (253 loc) · 9.34 KB
/
Vagrantfile
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# Copyright (C) 2018 Radiant Solutions (http://www.radiantsolutions.com)
# Copyright (C) 2016, 2017 DigitalGlobe (http://www.digitalglobe.com)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
require 'yaml'
require 'vagrant/errors'
require 'vagrant/ui'
require 'vagrant/util/downloader'
require 'vagrant/util/subprocess'
# Require Vagrant 2.0+.
Vagrant.require_version '>= 2.0.0'
# Setting up globals from YAML configuration file.
settings = YAML::load_file('config.yml')
$images = settings.fetch('images', {})
$maven = settings.fetch('maven', {})
$rpms = settings.fetch('rpms', {})
$pg_version = settings.fetch('versions')['postgresql']
$pg_dotless = $pg_version.gsub('.', '')
# Special workaround to have the `rpmbuild` UID and GID to match that of
# the user invoking Vagrant, which simplifies file permissions for host
# volume mounts. Setting the `RPMBUILD_UID_MATCH` variable to a value
# other than '1' forces the UID/GID in the configuration file to be honored.
if ENV.fetch('RPMBUILD_UID_MATCH', '1') == '1'
$images['base']['rpmbuild']['args']['rpmbuild_uid'] = Process.uid
$images['base']['rpmbuild']['args']['rpmbuild_gid'] = Process.gid
end
## Functions used by Vagrant containers.
def collect_rpms(filters)
collected = {}
filters.each do |filter|
$rpms.each do |name, options|
if (name == filter or
options.fetch('image', nil) == filter)
collected[name] = options
end
end
end
return collected
end
def make_hoot_dirs
['hootenanny', 'RPMS', 'cache', 'cache/m2', 'cache/npm'].each do |directory|
if ! File.directory?(directory)
Dir.mkdir(directory, 0755)
end
end
end
# Use Vagrant utilities to download and extract the Maven cache.
def maven_cache
if ! File.directory?('cache/m2/repository')
ui = Vagrant::UI::Basic.new()
ui.say('info', 'Downloading Maven cache')
tmp_file = '/var/tmp/m2-cache.tar.gz'
d = Vagrant::Util::Downloader.new(
$maven['cache_url'],
tmp_file,
{
:sha1 => $maven['cache_sha1'],
:ui => ui,
}
)
begin
d.download!
rescue Vagrant::Errors::VagrantError
raise Vagrant::Errors::VagrantError, output: 'Could not download maven cache!'
end
ui.say('info', 'Extracting Maven cache')
result = Vagrant::Util::Subprocess.execute(
'tar', '-C', 'cache/m2', '-xzf', tmp_file
)
if result.exit_code != 0
raise Vagrant::Errors::VagrantError, output: 'Could not extract Maven cache.'
end
end
end
def rpm_file(name, options)
arch = options.fetch('arch', 'x86_64')
dist = options.fetch('dist', '.el7')
return "RPMS/#{arch}/#{name}-#{options['version']}#{dist}.#{arch}.rpm"
end
def shared_folders(container, name, options, rpmbuild: false)
container.vm.synced_folder '.', '/vagrant', disabled: true
if options.fetch('rpmbuild', rpmbuild)
make_hoot_dirs()
# Container needs to be able to write RPMs via bind mounts.
container.vm.synced_folder 'RPMS', '/rpmbuild/RPMS'
container.vm.synced_folder 'SPECS', '/rpmbuild/SPECS'
container.vm.synced_folder 'SOURCES', '/rpmbuild/SOURCES'
# Additional directories need to be shared for Hootenanny builds.
if options.fetch('spec_file', '') == 'SPECS/hootenanny.spec'
if ENV.fetch('MAVEN_CACHE', '1') == '1'
maven_cache()
end
container.vm.synced_folder 'cache/m2', '/rpmbuild/.m2'
container.vm.synced_folder 'cache/npm', '/rpmbuild/.npm'
container.vm.synced_folder 'hootenanny', '/rpmbuild/hootenanny'
container.vm.synced_folder 'scripts', '/rpmbuild/scripts'
end
end
end
def build_container(config, name, options)
config.vm.define name do |container|
shared_folders(container, name, options)
container.vm.provider :docker do |d|
# On the containers we're building don't actually run anything.
d.build_dir = options.fetch('build_dir', '.')
d.cmd = options.fetch('cmd', [])
# Start build arguments.
build_args = []
args = options.fetch('args', {})
# Pull out `BuildRequires:` packages and add them to a `packages`
# build argument for the container.
if options.fetch('buildrequires', false)
build_packages = []
# Fill out dummy macros so `rpmspec` won't choke.
rpmspec_cmd = ['rpmspec', '-q', '--buildrequires']
{
'_topdir' => File.realpath(File.dirname(__FILE__)),
'hoot_version_gen' => '0.0.0',
'pg_dotless' => $pg_dotless,
'rpmbuild_version' => '0.0.0',
'rpmbuild_release' => '1',
'tomcat_version' => '0.0.0',
'c_ares_version' => '0.0.0',
'histogram_version' => '0.0.0',
'icu_version' => '0.0',
'libuv_version' => '0.0.0',
'llhttp_version' => '0.0.0',
'nghttp2_version' => '0.0.0',
'npm_version' => '0.0.0',
'punycode_version' => '0.0.0',
'uvwasi_version' => '0.0.0',
'v8_version' => '0.0.0.0',
}.each do |macro, expr|
rpmspec_cmd << '--define'
# Have to put in single quotes surrounding macro since this is a
# raw command.
rpmspec_cmd << "#{macro} #{expr}"
end
rpmspec_cmd << options.fetch(
'spec_file', "SPECS/#{name.gsub('rpmbuild-', '')}.spec"
)
result = Vagrant::Util::Subprocess.execute(*rpmspec_cmd)
if result.exit_code != 0
raise Vagrant::Errors::VagrantError, output: "Couldn't execute rpmspec for #{name}"
end
build_packages = result.stdout.split("\n")
if build_packages
build_args << '--build-arg'
build_args << "packages=#{build_packages.join(' ')}"
end
end
args.each do |arg, value|
build_args << '--build-arg'
build_args << "#{arg}=#{value}"
end
# Add any tags to the build arguments.
image_name = "hootenanny/#{name}"
options.fetch('tags', ['latest']).each do |tag|
build_args << '--tag'
build_args << "#{image_name}:#{tag}"
end
# Setting provisioner parameters.
d.build_args = build_args
# Set up basic container settings.
d.dockerfile = options.fetch('dockerfile', "docker/Dockerfile.#{name}")
d.remains_running = options.fetch('remains_running', false)
end
end
end
# Configure a container to be run from another image to execute `rpmbuild`.
def rpmbuild(config, name, options)
autostart = options.fetch('autostart', false)
config.vm.define name, autostart: autostart do |container|
shared_folders(container, name, options, rpmbuild: true)
image_name = "hootenanny/#{options['image']}"
container.vm.provider :docker do |d|
d.image = image_name
# Add `--rm` to the creation args so we don't need to keep the container
# (it's image is just ran to compile the RPM).
d.create_args = options.fetch(
'create_args', ['--rm']
)
d.remains_running = options.fetch(
'remains_running', true
)
# Start constructing the `rpmbuild` command.
rpmbuild_cmd = ['rpmbuild']
# Pass in the RPM version/release information via CLI define statements.
version, release = options['version'].split('-')
defines = options.fetch('defines', {})
defines.update(
{
'rpmbuild_version' => version,
'rpmbuild_release' => release,
}
)
defines.each do |macro, expr|
rpmbuild_cmd << '--define'
rpmbuild_cmd << "#{macro} #{expr}"
end
# Pass through any variables we want to undefine.
options.fetch('undefines', []).each do |macro|
rpmbuild_cmd << '--undefine'
rpmbuild_cmd << macro
end
# Set any with/without options.
options.fetch('with', []).each do |with_option|
rpmbuild_cmd << '--with'
rpmbuild_cmd << with_option
end
options.fetch('without', []).each do |without_option|
rpmbuild_cmd << '--without'
rpmbuild_cmd << without_option
end
# Default to using `rpmbuild -bb`.
rpmbuild_cmd << options.fetch('build_type', '-bb')
rpmbuild_cmd << options.fetch('spec_file', "SPECS/#{name}.spec")
d.cmd = rpmbuild_cmd
end
end
end
## Vagrant configuration
Vagrant.configure(2) do |config|
# Base images, including one for building Hootenanny, based on
# stable released dependencies, as well as those needed for
# GDAL (FileGDBAPI, libgeotiff, libkml) and NodeJS.
$images['base'].each do |name, options|
build_container(config, name, options)
end
# Generic, NodeJS, and GLPK dependency RPMS are built first.
collect_rpms(
['rpmbuild-generic',
'rpmbuild-glpk',
'rpmbuild-lcov',
'rpmbuild-liboauthcpp',
'rpmbuild-libphonenumber',
'rpmbuild-libpostal',
'rpmbuild-nodejs']
).each do |name, options|
rpmbuild(config, name, options)
end
end