OSDN Git Service

Add commannd runner option.
authorTatsuki SUGIURA <sugi@osdn.jp>
Sun, 28 Jun 2020 08:30:44 +0000 (17:30 +0900)
committerTatsuki SUGIURA <sugi@osdn.jp>
Sun, 28 Jun 2020 08:30:44 +0000 (17:30 +0900)
create-image

index 2fead9d..1a28e7a 100755 (executable)
@@ -20,14 +20,15 @@ class SyncDirDef
 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
 
@@ -168,6 +169,12 @@ class ImageCreator
           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")
@@ -195,7 +202,6 @@ class ImageCreator
     File.write "#{img_path_base}.json", JSON.pretty_generate(jdef)
   end
 
-
   def run
     create_disk
     create_fs
@@ -218,12 +224,17 @@ if $0 == __FILE__
   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
@@ -231,6 +242,6 @@ if $0 == __FILE__
       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