class Cangallo::LibGuestfs

Constants

CUSTOMIZE_CMD

Public Class Methods

copy_in(str) click to toggle source
# File lib/cangallo/libguestfs.rb, line 108
def self.copy_in(str)
  local, remote = str.split(':')

  raise "Source '#{local}' does not exist" if !File.exist?(local)

  upload_local = local
  upload_remote = remote
  run_command = nil

  if File.directory?(local)
    upload_local_tmp = Tempfile.new("canga")
    upload_local_tmp.close

    upload_local = upload_local_tmp.path
    upload_remote = "/tmp/#{File.basename(upload_local)}"


    local_parent_dir = File.dirname(local)
    local_name = File.basename(local)
    rc = system("tar czf #{upload_local} -C #{local_parent_dir} #{local_name}")

    raise "Error creating tar archive for '#{local_name}'" if !rc

    run_command = "tar xf #{upload_remote} -C #{remote}"
  end

  upload_param = "#{upload_local}:#{upload_remote}"

  return upload_param, run_command
end
version() click to toggle source
# File lib/cangallo/libguestfs.rb, line 96
def self.version
  str = `virt-customize --version`

  m = str.match(/^virt-customize (\d+\.\d+\.\d+).*$/)

  if m
    m[1]
  else
    nil
  end
end
virt_customize(image, commands, params = "") click to toggle source
# File lib/cangallo/libguestfs.rb, line 30
def self.virt_customize(image, commands, params = "")
  version = self.version

  if !version
    raise "Could not get virt-customize version"
  end

  target_version = Gem::Version.new('1.30')
  current_version = Gem::Version.new(version)
  good_version = false

  customize_command = nil

  good_version = true if current_version >= target_version

  if good_version
    cmd_file = Tempfile.new("canga")

    cmd_file.puts(commands)
    cmd_file.close

    customize_command = "virt-customize -a #{image} #{params.join(" ")} " <<
                        "--commands-from-file #{cmd_file.path}"
  else
    cmd_params = commands.map do |line|
      m = line.match(CUSTOMIZE_CMD)
      if m
        command = m[1]
        parms = m[2].strip

        if command == 'copy-in'
          command = 'upload'
          parms, new_command = copy_in(parms)

          [
            "--" + command + " " + Shellwords.escape(parms),
            "--run-command " + Shellwords.escape(new_command)
          ]
        else
          "--" + command + " " + Shellwords.escape(parms)
        end
      else
        nil
      end
    end

    cmd_params.flatten!
    cmd_params.compact!

    customize_command = "virt-customize -a #{image} #{params.join(" ")} " <<
                        "#{cmd_params.join(" ")}"
  end

  rc = system(customize_command)

  cmd_file.unlink if good_version

  return rc
end
virt_sparsify(image) click to toggle source
# File lib/cangallo/libguestfs.rb, line 90
def self.virt_sparsify(image)
  rc = system("virt-sparsify --in-place #{image}")

  return rc
end