Class: VagrantPlugins::ProviderLibvirt::Action::PackageDomain

Inherits:
Object
  • Object
show all
Includes:
Util::Ui
Defined in:
lib/vagrant-libvirt/action/package_domain.rb

Overview

Action for create new box for Libvirt provider

Instance Method Summary collapse

Methods included from Util::Ui

#rewriting

Constructor Details

#initialize(app, env) ⇒ PackageDomain

Returns a new instance of PackageDomain.



17
18
19
20
21
22
23
# File 'lib/vagrant-libvirt/action/package_domain.rb', line 17

def initialize(app, env)
  @logger = Log4r::Logger.new('vagrant_libvirt::action::package_domain')
  @app = app

  @options = ENV.fetch('VAGRANT_LIBVIRT_VIRT_SYSPREP_OPTIONS', '')
  @operations = ENV.fetch('VAGRANT_LIBVIRT_VIRT_SYSPREP_OPERATIONS', 'defaults,-ssh-userdir,-ssh-hostkeys,-customize')
end

Instance Method Details

#call(env) ⇒ Object



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
# File 'lib/vagrant-libvirt/action/package_domain.rb', line 25

def call(env)
  env[:ui].info(I18n.t('vagrant_libvirt.package_domain'))
  libvirt_domain = env[:machine].provider.driver.connection.client.lookup_domain_by_uuid(
    env[:machine].id
  )
  domain = env[:machine].provider.driver.connection.servers.get(env[:machine].id.to_s)

  volumes = domain.volumes.select { |x| !x.nil? }
  root_disk = volumes.select do |x|
    x.name == libvirt_domain.name + '.img'
  end.first
  raise Errors::NoDomainVolume if root_disk.nil?

  package_func = method(:package_v1)

  box_format = ENV.fetch('VAGRANT_LIBVIRT_BOX_FORMAT_VERSION', nil)

  case box_format
  when nil
    if volumes.length() > 1
      msg = "Detected more than one volume for machine, in the future this will switch to using the v2 "
      msg += "box format v2 automatically."
      msg += "\nIf you want to include the additional disks attached when packaging please set the "
      msg += "env variable VAGRANT_LIBVIRT_BOX_FORMAT_VERSION=v2 to use the new format. If you want "
      msg += "to ensure that your box uses the old format for single disk only, please set the "
      msg += "environment variable explicitly to 'v1'"
      env[:ui].warn(msg)
    end
  when 'v2'
    package_func = method(:package_v2)
  when 'v1'
  else
    env[:ui].warn("Unrecognized value for 'VAGRANT_LIBVIRT_BOX_FORMAT_VERSION', defaulting to v1")
  end

   = package_func.call(env, volumes)

  # metadata / Vagrantfile
  package_directory = env["package.directory"]
  File.write(package_directory + '/metadata.json', )
  File.write(package_directory + '/Vagrantfile', vagrantfile_content(env))

  @app.call(env)
end

#metadata_content_v1(filesize) ⇒ Object



123
124
125
126
127
128
129
130
131
# File 'lib/vagrant-libvirt/action/package_domain.rb', line 123

def (filesize)
  <<-EOF.unindent
    {
      "provider": "libvirt",
      "format": "qcow2",
      "virtual_size": #{filesize}
    }
  EOF
end

#metadata_content_v2(disks) ⇒ Object



133
134
135
136
137
138
139
140
141
142
# File 'lib/vagrant-libvirt/action/package_domain.rb', line 133

def (disks)
  data = {
    "provider": "libvirt",
    "format": "qcow2",
    "disks": disks.each do |disk|
      {'path': disk[:path]}
    end
  }
  JSON.pretty_generate(data)
end

#package_v1(env, volumes) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/vagrant-libvirt/action/package_domain.rb', line 70

def package_v1(env, volumes)
  domain_img = download_volume(env, volumes.first, 'box.img')

  sysprep_domain(domain_img)
  sparsify_volume(domain_img)

  info = JSON.parse(`qemu-img info --output=json #{domain_img}`)
  img_size = (Float(info['virtual-size'])/(1024**3)).ceil

  return (img_size)
end

#package_v2(env, volumes) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/vagrant-libvirt/action/package_domain.rb', line 82

def package_v2(env, volumes)
  disks = []
  volumes.each_with_index do |vol, idx|
    disk = {:path => "box_#{idx+1}.img"}
    volume_img = download_volume(env, vol, disk[:path])

    if idx == 0
      sysprep_domain(volume_img)
    end

    sparsify_volume(volume_img)

    disks.push(disk)
  end

  return (disks)
end

#vagrantfile_content(env) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/vagrant-libvirt/action/package_domain.rb', line 100

def vagrantfile_content(env)
  include_vagrantfile = ""

  if env["package.vagrantfile"]
    include_vagrantfile = <<-EOF

      # Load include vagrant file if it exists after the auto-generated
      # so it can override any of the settings
      include_vagrantfile = File.expand_path("../include/_Vagrantfile", __FILE__)
      load include_vagrantfile if File.exist?(include_vagrantfile)
    EOF
  end

  <<-EOF.unindent
    Vagrant.configure("2") do |config|
      config.vm.provider :libvirt do |libvirt|
        libvirt.driver = "kvm"
      end
    #{include_vagrantfile}
    end
  EOF
end