end
class ImageCreator
- attr_accessor :name, :dirs, :src_host, :img_path_base
+ attr_accessor :name, :dirs, :src_host, :img_path_base, :run_cmds
MiB = 1024 ** 2
GiB = 1024 ** 3
- def initialize(name, dirs, src_host: nil)
+ def initialize(name, dirs, src_host: nil, run_cmds: nil)
@name = name
@dirs = dirs
@src_host = src_host || name
+ @run_cmds = run_cmds
@img_path_base = "#{name}_#{Time.now.strftime '%FT%T%z'}"
end
cfg = File.read "#{dir}/boot/grub/grub.cfg"
cfg.gsub! %r{mapper/loop0p}, "sda"
File.write "#{dir}/boot/grub/grub.cfg", cfg
+
+ unless Array(run_cmds).empty?
+ Array(run_cmds).each do |cmd|
+ system({'DEBIAN_FRONTEND' => 'noninteractive'}, "chroot", dir, *Array(cmd)) or raise "Failed to execute command (#{cmd}): #{$!}"
+ end
+ end
ensure
system("umount", "#{dir}/dev")
system("umount", "#{dir}/proc")
File.write "#{img_path_base}.json", JSON.pretty_generate(jdef)
end
-
def run
create_disk
create_fs
list.each do |imgdef|
name = nil
dirs = []
+ opts = {}
if imgdef.kind_of?(Hash)
name = imgdef['name']
(imgdef['dirs'] || {}).each do |path, opts|
opts.kind_of?(Hash) or opts = {size: opts}
dirs << SyncDirDef.new({path: path}.merge(opts.keys.map(&:to_sym).zip(opts.values).to_h))
end
+ imgdef.keys.each do |k|
+ next if %w[dirs name].member?(k)
+ opts[k.to_sym] = imgdef[k]
+ end
else
name = imgdef
end
limit_pat.match?(name) or next
end
dirs.empty? and dirs << SyncDirDef.new
- ImageCreator.new(name, dirs).run
+ ImageCreator.new(name, dirs, **opts).run
end
end