Update setup to decouple disk attachment from user_data setup

This commit is contained in:
Brian Cain 2020-06-18 13:41:08 -07:00
parent d6fc70dd68
commit 3caca048c8
No known key found for this signature in database
GPG Key ID: 9FC4639B2E4510A0
2 changed files with 22 additions and 25 deletions

View File

@ -18,7 +18,12 @@ module Vagrant
user_data_configs = machine.config.vm.cloud_init_configs
.select { |c| c.type == :user_data }
setup_user_data(machine, env, user_data_configs)
if !user_data_configs.empty?
user_data = setup_user_data(machine, env, user_data_configs)
meta_data = { "instance-id": "i-#{machine.id.split('-').join}" }
write_cfg_iso(machine, env, user_data, meta_data)
end
# Continue On
@app.call(env)
@ -27,9 +32,8 @@ module Vagrant
# @param [Vagrant::Machine] machine
# @param [Vagrant::Environment] env
# @param [Array<#VagrantPlugins::Kernel_V2::VagrantConfigCloudInit>] user_data_cfgs
# @return [MIME::Text] user_data
def setup_user_data(machine, env, user_data_cfgs)
return if user_data_cfgs.empty?
machine.ui.info(I18n.t("vagrant.actions.vm.cloud_init_user_data_setup"))
text_cfgs = []
@ -37,10 +41,8 @@ module Vagrant
text_cfgs << read_text_cfg(machine, cfg)
end
msg = generate_cfg_msg(machine, text_cfgs)
iso_path = write_cfg_iso(machine, env, msg)
attach_disk_config(machine, env, iso_path)
user_data = generate_cfg_msg(machine, text_cfgs)
user_data
end
# Reads an individual cloud_init config and stores its contents and the
@ -89,27 +91,26 @@ module Vagrant
# @param [Vagrant::Machine] machine
# @param [MIME::Multipart::Mixed] msg
# @return [String] iso_path
def write_cfg_iso(machine, env, msg)
def write_cfg_iso(machine, env, user_data, meta_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(msg.to_s) }
File.open("#{source_dir}/user-data", 'w') { |file| file.write(user_data.to_s) }
metadata = { "instance-id": "i-#{machine.id.split('-').join}" }
File.open("#{source_dir}/meta-data", 'w') { |file| file.write(metadata.to_s) }
File.open("#{source_dir}/meta-data", 'w') { |file| file.write(meta_data.to_s) }
iso_path = env[:env].host.capability(:create_iso, env[:env],
source_dir, volume_id: "cidata")
attach_disk_config(machine, env, iso_path)
ensure
FileUtils.remove_entry source_dir
end
else
raise Errors::CreateIsoHostCapNotFound
end
iso_path
end
# Adds a new :dvd disk config with the given iso_path to be attached

View File

@ -25,6 +25,8 @@ describe Vagrant::Action::Builtin::CloudInitSetup do
let(:text_cfgs) { [MIME::Text.new("data: true", "cloud-config"),
MIME::Text.new("data: false", "cloud-config") ] }
let(:meta_data) { { "instance-id": "i-123456789" } }
let(:subject) { described_class.new(app, env) }
@ -35,6 +37,7 @@ describe Vagrant::Action::Builtin::CloudInitSetup do
expect(app).to receive(:call).with(env).ordered
expect(subject).to receive(:setup_user_data).and_return(true)
expect(subject).to receive(:write_cfg_iso).and_return(true)
subject.call(env)
end
@ -44,6 +47,8 @@ describe Vagrant::Action::Builtin::CloudInitSetup do
expect(app).to receive(:call).with(env).ordered
expect(subject).not_to receive(:setup_user_data)
expect(subject).not_to receive(:write_cfg_iso)
expect(subject).not_to receive(:attack_disk_config)
subject.call(env)
@ -51,20 +56,10 @@ describe Vagrant::Action::Builtin::CloudInitSetup do
end
describe "#setup_user_data" do
it "does nothing if passed in configs are empty" do
expect(subject).not_to receive(:read_text_cfg)
expect(subject).not_to receive(:generate_cfg_msg)
expect(subject).not_to receive(:write_cfg_iso)
expect(subject).not_to receive(:attach_disk_config)
subject.setup_user_data(machine, env, [])
end
it "builds a MIME message and prepares a disc to be attached" do
expect(subject).to receive(:read_text_cfg).twice
expect(subject).to receive(:generate_cfg_msg)
expect(subject).to receive(:write_cfg_iso)
expect(subject).to receive(:attach_disk_config)
subject.setup_user_data(machine, env, cloud_init_configs)
end
@ -101,7 +96,7 @@ describe Vagrant::Action::Builtin::CloudInitSetup do
message = subject.generate_cfg_msg(machine, text_cfgs)
allow(host).to receive(:capability?).with(:create_iso).and_return(false)
expect{subject.write_cfg_iso(machine, env, message)}.to raise_error(Vagrant::Errors::CreateIsoHostCapNotFound)
expect{subject.write_cfg_iso(machine, env, message, {})}.to raise_error(Vagrant::Errors::CreateIsoHostCapNotFound)
end
it "creates a temp dir with the cloud_init config and generates an iso" do
@ -112,8 +107,9 @@ describe Vagrant::Action::Builtin::CloudInitSetup do
expect(File).to receive(:open).with("#{source_dir}/meta-data", 'w').and_return(true)
expect(FileUtils).to receive(:remove_entry).with(source_dir).and_return(true)
allow(host).to receive(:capability).with(:create_iso, machine_env, source_dir, volume_id: "cidata").and_return(iso_path)
expect(vm.disks).to receive(:map)
subject.write_cfg_iso(machine, env, message)
subject.write_cfg_iso(machine, env, message, {})
end
end