Class: VagrantPlugins::AVF::LinuxCloudInitSeed

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant_provider_avf/linux_cloud_init_seed.rb

Constant Summary collapse

SEED_LABEL =
"cidata".freeze
SEED_SIZE_MB =
8
DEFAULT_USERNAME =
"vagrant".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(machine_data_dir:, public_key_path: Pathname.new(File.expand_path("data/avf_insecure_key.pub", __dir__)), runner: nil) ⇒ LinuxCloudInitSeed

Returns a new instance of LinuxCloudInitSeed.



13
14
15
16
17
18
19
20
21
# File 'lib/vagrant_provider_avf/linux_cloud_init_seed.rb', line 13

def initialize(
  machine_data_dir:,
  public_key_path: Pathname.new(File.expand_path("data/avf_insecure_key.pub", __dir__)),
  runner: nil
)
  @path = Pathname.new(machine_data_dir).join("linux-seed.img")
  @public_key_path = Pathname.new(public_key_path)
  @runner = runner || method(:run_command)
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



23
24
25
# File 'lib/vagrant_provider_avf/linux_cloud_init_seed.rb', line 23

def path
  @path
end

Instance Method Details

#write(machine_id:, mac_address:) ⇒ 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
# File 'lib/vagrant_provider_avf/linux_cloud_init_seed.rb', line 25

def write(machine_id:, mac_address:)
  public_key = @public_key_path.read.strip
  @path.dirname.mkpath
  create_blank_image

  disk = nil
  Dir.mktmpdir("avf-linux-seed") do |directory|
    disk = attach_disk
    format_disk(disk)
    mount_path = Pathname.new(directory).join("mount")
    mount_path.mkpath

    begin
      mount_disk(disk, mount_path)
      write_seed_files(mount_path, machine_id, mac_address, public_key)
    ensure
      unmount_disk(mount_path)
    end
  end

  @path
rescue Errors::LinuxCloudInitSeedFailed
  raise
rescue StandardError => error
  raise Errors::LinuxCloudInitSeedFailed, error.message
ensure
  detach_disk(disk) if disk
end