Class: Vagrant::Action::Builtin::CloudInitSetup

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant/action/builtin/cloud_init_setup.rb

Constant Summary collapse

TEMP_PREFIX =
"vagrant-cloud-init-iso-temp-".freeze

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ CloudInitSetup

Returns a new instance of CloudInitSetup.


10
11
12
13
# File 'lib/vagrant/action/builtin/cloud_init_setup.rb', line 10

def initialize(app, env)
  @app    = app
  @logger = Log4r::Logger.new("vagrant::action::builtin::cloudinit::setup")
end

Instance Method Details

#attach_disk_config(machine, env, iso_path) ⇒ Object

Adds a new :dvd disk config with the given iso_path to be attached to the guest later

Parameters:


114
115
116
117
118
# File 'lib/vagrant/action/builtin/cloud_init_setup.rb', line 114

def attach_disk_config(machine, env, iso_path)
  @logger.info("Adding cloud_init iso '#{iso_path}' to disk config")
  machine.config.vm.disk :dvd, file: iso_path, name: "vagrant-cloud_init-disk"
  machine.config.vm.disks.each { |d| d.finalize! if d.type == :dvd && d.file == iso_path }
end

#call(env) ⇒ Object


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/vagrant/action/builtin/cloud_init_setup.rb', line 15

def call(env)
  machine = env[:machine]

  user_data_configs = machine.config.vm.cloud_init_configs
                                          .select { |c| c.type == :user_data }

  if !user_data_configs.empty?
    user_data = setup_user_data(machine, env, user_data_configs)
     = { "instance-id" => "i-#{machine.id.split('-').join}" }

    write_cfg_iso(machine, env, user_data, )
  end

  # Continue On
  @app.call(env)
end

#generate_cfg_msg(machine, text_cfgs) ⇒ Vagrant::Util::Mime::Multipart

Combines all known cloud_init configs into a multipart mixed MIME text message

Parameters:

Returns:


69
70
71
72
73
74
75
76
77
78
# File 'lib/vagrant/action/builtin/cloud_init_setup.rb', line 69

def generate_cfg_msg(machine, text_cfgs)
  msg = Vagrant::Util::Mime::Multipart.new
  msg.headers["MIME-Version"] = "1.0"

  text_cfgs.each do |c|
    msg.add(c)
  end

  msg
end

#read_text_cfg(machine, cfg) ⇒ Vagrant::Util::Mime::Entity

Reads an individual cloud_init config and stores its contents and the content_type as a MIME text

Parameters:

  • machine (Vagrant::Machine)
  • cfg (VagrantPlugins::Kernel_V2::VagrantConfigCloudInit)

Returns:


51
52
53
54
55
56
57
58
59
60
61
# File 'lib/vagrant/action/builtin/cloud_init_setup.rb', line 51

def read_text_cfg(machine, cfg)
  if cfg.path
    text = File.read(Pathname.new(cfg.path).expand_path(machine.env.root_path))
  else
    text = cfg.inline
  end

  text_msg = Vagrant::Util::Mime::Entity.new(text, cfg.content_type)
  text_msg.disposition = "attachment; filename=\"#{File.basename(cfg.content_disposition_filename).gsub('"', '\"')}\"" if cfg.content_disposition_filename
  text_msg
end

#setup_user_data(machine, env, user_data_cfgs) ⇒ Vagrant::Util::Mime::MultiPart

Returns user_data.

Parameters:

Returns:

  • (Vagrant::Util::Mime::MultiPart)

    user_data


36
37
38
39
40
41
42
43
# File 'lib/vagrant/action/builtin/cloud_init_setup.rb', line 36

def setup_user_data(machine, env, user_data_cfgs)
  machine.ui.info(I18n.t("vagrant.actions.vm.cloud_init_user_data_setup"))

  text_cfgs = user_data_cfgs.map { |cfg| read_text_cfg(machine, cfg) }

  user_data = generate_cfg_msg(machine, text_cfgs)
  user_data
end

#write_cfg_iso(machine, env, user_data, meta_data) ⇒ Object

Writes the contents of the guests cloud_init config to a tmp dir and passes that source directory along to the host cap to be written to an iso

Parameters:


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/vagrant/action/builtin/cloud_init_setup.rb', line 87

def write_cfg_iso(machine, env, user_data, )
  iso_path = nil

  if env[:env].host.capability?(:create_iso)
    begin
      source_dir = Pathname.new(Dir.mktmpdir(TEMP_PREFIX))
      File.open("#{source_dir}/user-data", 'w') { |file| file.write(user_data.to_s) }

      File.open("#{source_dir}/meta-data", 'w') { |file| file.write(.to_yaml) }

      iso_path = env[:env].host.capability(:create_iso,
                                           source_dir, volume_id: "cidata")
      attach_disk_config(machine, env, iso_path.to_path)
    ensure
      FileUtils.remove_entry(source_dir)
    end
  else
    raise Errors::CreateIsoHostCapNotFound
  end
end