Class: Vagrant::LXC::Action::HandleBoxMetadata

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-lxc/action/handle_box_metadata.rb

Overview

Prepare arguments to be used for lxc-create

Constant Summary collapse

SUPPORTED_VERSIONS =
['1.0.0', '2', '3']

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ HandleBoxMetadata

Returns a new instance of HandleBoxMetadata.



8
9
10
11
# File 'lib/vagrant-lxc/action/handle_box_metadata.rb', line 8

def initialize(app, env)
  @app    = app
  @logger = Log4r::Logger.new("vagrant::lxc::action::handle_box_metadata")
end

Instance Method Details

#box_versionObject



90
91
92
# File 'lib/vagrant-lxc/action/handle_box_metadata.rb', line 90

def box_version
  @box..fetch('version')
end

#call(env) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/vagrant-lxc/action/handle_box_metadata.rb', line 13

def call(env)
  @env = env
  @box = @env[:machine].box

  @env[:ui].info I18n.t("vagrant.actions.vm.import.importing",
                        :name => @env[:machine].box.name)

  @logger.info 'Validating box contents'
  validate_box

  @logger.info 'Setting box options on environment'
  @env[:lxc_template_src]  = template_src
  @env[:lxc_template_opts] = template_opts

  # FIXME: Remove support for pre 1.0.0 boxes
  if box_version != '1.0.0'
    @env[:ui].warn "WARNING: You are using a base box that has a format that has been deprecated, please upgrade to a new one."
    @env[:lxc_template_opts].merge!(
      '--auth-key' => Vagrant.source_root.join('keys', 'vagrant.pub').expand_path.to_s
    )
  end

  if template_config_file.exist?
    @env[:lxc_box_config] = template_config_file.to_s
    @env[:lxc_template_opts].merge!('--config' => template_config_file.to_s)
  elsif old_template_config_file.exist?
    @env[:lxc_box_config] = old_template_config_file.to_s
    @env[:lxc_template_config] = old_template_config_file.to_s
  end

  @app.call env
end

#old_template_config_fileObject

TODO: Remove this once we remove compatibility for < 1.0.0 boxes



60
61
62
# File 'lib/vagrant-lxc/action/handle_box_metadata.rb', line 60

def old_template_config_file
  @old_template_config_file ||= @box.directory.join('lxc.conf')
end

#rootfs_tarballObject



70
71
72
# File 'lib/vagrant-lxc/action/handle_box_metadata.rb', line 70

def rootfs_tarball
  @rootfs_tarball ||= @box.directory.join('rootfs.tar.gz').to_s
end

#template_config_fileObject



55
56
57
# File 'lib/vagrant-lxc/action/handle_box_metadata.rb', line 55

def template_config_file
  @template_config_file ||= @box.directory.join('lxc-config')
end

#template_optsObject



64
65
66
67
68
# File 'lib/vagrant-lxc/action/handle_box_metadata.rb', line 64

def template_opts
  @template_opts ||= @box..fetch('template-opts', {}).dup.merge!(
    '--tarball'  => rootfs_tarball
  )
end

#template_srcObject



46
47
48
49
50
51
52
53
# File 'lib/vagrant-lxc/action/handle_box_metadata.rb', line 46

def template_src
  @template_src ||=
    if (box_template = @box.directory.join('lxc-template')).exist?
      box_template.to_s
    else
      Vagrant::LXC.source_root.join('scripts/lxc-template').to_s
    end
end

#validate_boxObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/vagrant-lxc/action/handle_box_metadata.rb', line 74

def validate_box
  unless SUPPORTED_VERSIONS.include? box_version
    raise Errors::IncompatibleBox.new name: @box.name,
                                      found: box_version,
                                      supported: SUPPORTED_VERSIONS.join(', ')
  end

  unless File.exists?(template_src)
    raise Errors::TemplateFileMissing.new name: @box.name
  end

  unless File.exists?(rootfs_tarball)
    raise Errors::RootFSTarballMissing.new name: @box.name
  end
end