Class: Vagrant::Action::Builtin::CloudInitSetup
- Inherits:
-
Object
- Object
- Vagrant::Action::Builtin::CloudInitSetup
- 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
-
#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.
- #call(env) ⇒ Object
-
#generate_cfg_msg(machine, text_cfgs) ⇒ MIME::Multipart::Mixed
Combines all known cloud_init configs into a multipart mixed MIME text message.
-
#initialize(app, env) ⇒ CloudInitSetup
constructor
A new instance of CloudInitSetup.
-
#read_text_cfg(machine, cfg) ⇒ MIME::Text
Reads an individual cloud_init config and stores its contents and the content_type as a MIME text.
-
#setup_user_data(machine, env, user_data_cfgs) ⇒ MIME::Text
User_data.
-
#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.
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
119 120 121 122 123 |
# File 'lib/vagrant/action/builtin/cloud_init_setup.rb', line 119 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) ⇒ MIME::Multipart::Mixed
Combines all known cloud_init configs into a multipart mixed MIME text message
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/vagrant/action/builtin/cloud_init_setup.rb', line 74 def generate_cfg_msg(machine, text_cfgs) msg = MIME::Multipart::Mixed.new msg.headers.set("MIME-Version", "1.0") text_cfgs.each do |c| msg.add(c) end msg end |
#read_text_cfg(machine, cfg) ⇒ MIME::Text
Reads an individual cloud_init config and stores its contents and the content_type as a MIME text
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# 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).(machine.env.root_path)) else text = cfg.inline end # Note: content_type must remove the leading `text/` because # the MIME::Text initializer hardcodes `text/` already to the type. # We assume content_type is correct due to the validation step # in VagrantConfigCloudInit. content_type = cfg.content_type.split('/', 2).last text_msg = MIME::Text.new(text, content_type) text_msg end |
#setup_user_data(machine, env, user_data_cfgs) ⇒ MIME::Text
Returns 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
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/vagrant/action/builtin/cloud_init_setup.rb', line 92 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 |