Class: Cangallo::LibGuestfs

Inherits:
Object
  • Object
show all
Defined in:
lib/cangallo/libguestfs.rb

Constant Summary collapse

CUSTOMIZE_CMD =
/^([^\s]+)\s+(.*)$/

Class Method Summary collapse

Class Method Details

.copy_in(str) ⇒ Object



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
# 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

.versionObject



96
97
98
99
100
101
102
103
104
105
106
# 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 = "") ⇒ Object



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
# 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) ⇒ Object



90
91
92
93
94
# File 'lib/cangallo/libguestfs.rb', line 90

def self.virt_sparsify(image)
  rc = system("virt-sparsify --in-place #{image}")

  return rc
end