-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbuilder.rb
440 lines (365 loc) · 12.8 KB
/
builder.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# frozen_string_literal: true
require 'logger'
module Puppet::Modulebuilder
# Class to build Puppet Modules from source
class Builder
# Due to the way how PathSpec generates the regular expression,
# `/*` doesn't match directories starting with a dot,
# so we need `/.*` as well.
IGNORED = [
'/**',
'/.*',
'!/CHANGELOG*',
'!/LICENSE',
'!/README*',
'!/REFERENCE.md',
'!/bolt_plugin.json',
'!/data/**',
'!/docs/**',
'!/examples/**',
'!/facts.d/**',
'!/files/**',
'!/functions/**',
'!/hiera.yaml',
'!/lib/**',
'!/locales/**',
'!/manifests/**',
'!/metadata.json',
'!/plans/**',
'!/scripts/**',
'!/tasks/**',
'!/templates/**',
'!/types/**',
].freeze
attr_reader :destination, :logger
def initialize(source, destination = nil, logger = nil)
unless logger.nil? || logger.is_a?(Logger)
raise ArgumentError,
format('logger is expected to be nil or a Logger. Got %<klass>s',
klass: logger.class)
end
@source_validated = false
@source = source
@destination = destination.nil? ? File.join(source, 'pkg') : destination
@logger = logger.nil? ? ::Logger.new(File.open(File::NULL, 'w')) : logger
end
# The source to build the module from
# @return [String]
def source
return @source if @source_validated
validate_source!
@source = File.realpath(@source)
end
# Build a module package from a module directory.
#
# @return [String] The path to the built package file.
def build
create_build_dir
stage_module_in_build_dir
build_package
package_file
ensure
cleanup_build_dir
end
# Return the path to the temporary build directory, which will be placed
# inside the target directory and match the release name
#
# @see #release_name
def build_dir
@build_dir ||= File.join(build_context[:parent_dir], build_context[:build_dir_name])
end
def build_context
{
parent_dir: destination,
build_dir_name: release_name,
}.freeze
end
# Iterate through all the files and directories in the module and stage
# them into the temporary build directory (unless ignored).
#
# @return nil
def stage_module_in_build_dir
require 'find'
directories = [source]
staged = Find.find(source) do |path|
next if path == source
if ignored_path?(path)
logger.debug("Ignoring #{path} from the build")
Find.prune
else
logger.debug("Staging #{path} for the build")
directories << path if file_directory?(path)
stage_path(path)
end
end
# Reset directory mtimes. This must happen after the files have been
# copied since that modifies a directory's mtime
directories.each do |directory|
copy_mtime(directory)
end
staged
end
# Stage a file or directory from the module into the build directory.
#
# @param path [String] The path to the file or directory.
#
# @return nil.
def stage_path(path)
require 'pathname'
relative_path = Pathname.new(path).relative_path_from(Pathname.new(source))
dest_path = File.join(build_dir, relative_path)
validate_path_encoding!(relative_path.to_path)
begin
if file_directory?(path)
fileutils_mkdir_p(dest_path, mode: file_stat(path).mode)
elsif file_symlink?(path)
warn_symlink(path)
else
validate_ustar_path!(relative_path.to_path)
fileutils_cp(path, dest_path, preserve: true)
end
rescue ArgumentError => e
raise format(
'%<message>s Rename the file or exclude it from the package by adding it to the .pdkignore file in your module.', message: e.message
)
end
end
def copy_mtime(path)
require 'pathname'
relative_path = Pathname.new(path).relative_path_from(Pathname.new(source))
dest_path = File.join(build_dir, relative_path)
validate_path_encoding!(relative_path.to_path)
fileutils_touch(dest_path, mtime: file_stat(path).mtime)
end
# Check if the given path matches one of the patterns listed in the
# ignore file.
#
# @param path [String] The path to be checked.
#
# @return [Boolean] true if the path matches and should be ignored.
def ignored_path?(path)
path = "#{path}/" if File.directory?(path)
ignored_files.match_path(path, source)
end
# Warn the user about a symlink that would have been included in the
# built package.
#
# @param path [String] The relative or absolute path to the symlink.
#
# @return nil.
def warn_symlink(path)
require 'pathname'
symlink_path = Pathname.new(path)
module_path = Pathname.new(source)
logger.warn format('Symlinks in modules are not supported and will not be included in the package. Please investigate symlink %<from>s -> %<to>s.',
from: symlink_path.relative_path_from(module_path), to: symlink_path.realpath.relative_path_from(module_path))
end
# Checks if the path contains any non-ASCII characters.
#
# Java will throw an error when it encounters a path containing
# characters that are not supported by the hosts locale. In order to
# maximise compatibility we limit the paths to contain only ASCII
# characters, which should be part of any locale character set.
#
# @param path [String] the relative path to be added to the tar file.
#
# @raise [ArgumentError] if the path contains non-ASCII characters.
#
# @return [nil]
def validate_path_encoding!(path)
return unless /[^\x00-\x7F]/.match?(path)
raise ArgumentError, format("'%<path>s' can only include ASCII characters in its path or " \
'filename in order to be compatible with a wide range of hosts.', path: path)
end
# Creates a gzip compressed tarball of the build directory.
#
# If the destination package already exists, it will be removed before
# creating the new tarball.
#
# @return nil.
def build_package
require 'zlib'
require 'minitar'
require 'find'
FileUtils.rm_f(package_file)
# The chdir necessary us due to Minitar entry not be able to separate the filename
# within the TAR versus the source filename to pack
Dir.chdir(build_context[:parent_dir]) do
gz = Zlib::GzipWriter.new(File.open(package_file, 'wb'))
begin
tar = Minitar::Output.new(gz)
Find.find(build_context[:build_dir_name]) do |entry|
entry_meta = {
name: entry,
}
orig_mode = File.stat(entry).mode
min_mode = Minitar.dir?(entry) ? 0o755 : 0o644
entry_meta[:mode] = orig_mode | min_mode
if entry_meta[:mode] != orig_mode
logger.debug(format('Updated permissions of packaged \'%<entry>s\' to %<new_mode>s', entry: entry,
new_mode: (entry_meta[:mode] & 0o7777).to_s(8)))
end
Minitar.pack_file(entry_meta, tar)
end
ensure
tar.close
end
end
end
# Instantiate a new PathSpec class and populate it with the pattern(s) of
# files to be ignored.
#
# @return [PathSpec] The populated ignore path matcher.
def ignored_files
require 'pathspec'
ignored = PathSpec.new(IGNORED)
ignored.add("/#{File.basename(destination)}/") if File.realdirpath(destination).start_with?(File.realdirpath(source))
ignored
end
# Create a temporary build directory where the files to be included in
# the package will be staged before building the tarball.
#
# If the directory already exists, remove it first.
def create_build_dir
cleanup_build_dir
fileutils_mkdir_p(build_dir)
end
# Remove the temporary build directory and all its contents from disk.
#
# @return nil.
def cleanup_build_dir
FileUtils.rm_rf(build_dir, secure: true)
end
# Read and parse the values from metadata.json for the module that is
# being built.
#
# @return [Hash{String => Object}] The hash of metadata values.
def metadata
return @metadata unless @metadata.nil?
metadata_json_path = File.join(source, 'metadata.json')
unless file_exists?(metadata_json_path)
raise ArgumentError,
format("'%<file>s' does not exist or is not a file.",
file: metadata_json_path)
end
unless file_readable?(metadata_json_path)
raise ArgumentError,
format("Unable to open '%<file>s' for reading.",
file: metadata_json_path)
end
require 'json'
begin
@metadata = JSON.parse(read_file(metadata_json_path))
rescue JSON::JSONError => e
raise ArgumentError, format('Invalid JSON in metadata.json: %<msg>s', msg: e.message)
end
@metadata.freeze
end
# Return the path where the built package file will be written to.
def package_file
@package_file ||= File.join(destination, "#{release_name}.tar.gz")
end
# Verify if there is an existing package in the target directory and prompts
# the user if they want to overwrite it.
def package_already_exists?
file_exists?(package_file)
end
# The release name is used for the build directory and resulting package
# file.
#
# The default combines the module name and version into a Forge-compatible
# dash separated string. Unless you have an unusual use case this isn't set
# manually.
#
# @return [String]
def release_name
@release_name ||= [
metadata['name'],
metadata['version'],
].join('-')
end
attr_writer :release_name
# Checks if the path length will fit into the POSIX.1-1998 (ustar) tar
# header format.
#
# POSIX.1-2001 (which allows paths of infinite length) was adopted by GNU
# tar in 2004 and is supported by minitar 0.7 and above.
#
# POSIX.1-1998 tar format does not allow for paths greater than 256 bytes,
# or paths that can't be split into a prefix of 155 bytes (max) and
# a suffix of 100 bytes (max).
#
# This logic was pretty much copied from the private method
# {Archive::Tar::Minitar::Writer#split_name}.
#
# @param path [String] the relative path to be added to the tar file.
#
# @raise [ArgumentError] if the path is too long or could not be split.
#
# @return [nil]
def validate_ustar_path!(path)
raise ArgumentError, format("The path '%<path>s' is longer than 256 bytes.", path: path) if path.bytesize > 256
if path.bytesize <= 100
prefix = ''
else
parts = path.split(File::SEPARATOR)
newpath = parts.pop
nxt = ''
loop do
nxt = parts.pop || ''
break if newpath.bytesize + 1 + nxt.bytesize >= 100
newpath = File.join(nxt, newpath)
end
prefix = File.join(*parts, nxt)
path = newpath
end
return unless path.bytesize > 100 || prefix.bytesize > 155
raise ArgumentError, \
format("'%<path>s' could not be split at a directory separator into two " \
'parts, the first having a maximum length of 155 bytes and the ' \
'second having a maximum length of 100 bytes.', path: path)
end
private
# Validates that source is able to be built
def validate_source!
unless file_directory?(@source) && file_readable?(@source)
raise ArgumentError,
format("Module source '%<source>s' does not exist as a directory is or is not readable", source: @source)
end
@source_validated = true
end
# @return [String] The file contents
def read_file(file, nil_on_error: false, open_args: 'r')
File.read(file, open_args: Array(open_args))
rescue StandardError => e
raise e unless nil_on_error
nil
end
# Filesystem wrapper methods.
# These are mocked in spec tests.
def file_exists?(*args)
File.file?(*args)
end
def file_readable?(*args)
File.readable?(*args)
end
def file_directory?(*args)
File.directory?(*args)
end
def file_symlink?(*args)
File.symlink?(*args)
end
def fileutils_cp(src, dest, **options)
FileUtils.cp(src, dest, **options)
end
def fileutils_mkdir_p(dir, **options)
FileUtils.mkdir_p(dir, **options)
end
def fileutils_touch(list, **options)
FileUtils.touch(list, **options)
end
def file_stat(*args)
File.stat(*args)
end
end
end