Class: VagrantAWS::Action::CreateImage

Inherits:
Object
  • Object
show all
Includes:
Vagrant::Util
Defined in:
lib/vagrant-aws/action/create_image.rb

Constant Summary collapse

PACKAGE_VAGRANTFILE =
<<EOT
Vagrant::Config.run do |config|
  # This Vagrantfile is auto-generated by `vagrant aws create_image` to contain
  # the image id and region. Custom configuration should be placed in
  # the actual `Vagrantfile` in this box.
  config.vm.base_mac = "000000000000"  # Unneeded
	config.aws.image = "<%= image %>"
	config.aws.region = "<%= region %>"
end
EOT

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ CreateImage

Returns a new instance of CreateImage.



40
41
42
43
# File 'lib/vagrant-aws/action/create_image.rb', line 40

def initialize(app, env)
	@app = app
	@env = env
end

Instance Attribute Details

#imageObject (readonly)

Returns the value of attribute image.



38
39
40
# File 'lib/vagrant-aws/action/create_image.rb', line 38

def image
  @image
end

#temp_dirObject (readonly)

Returns the value of attribute temp_dir.



38
39
40
# File 'lib/vagrant-aws/action/create_image.rb', line 38

def temp_dir
  @temp_dir
end

Instance Method Details

#call(env) ⇒ Object

Raises:

  • (Vagrant::Errors::VMNotCreatedError)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/vagrant-aws/action/create_image.rb', line 45

def call(env)
	@env = env

	raise Vagrant::Errors::VMNotCreatedError if !@env["vm"].created?
	raise Vagrant::Errors::VMNotRunningError if !@env["vm"].vm.running?
	raise VagrantAWS::Errors::EBSDeviceRequired, :command => "box_create" if @env["image.register"] and @env["vm"].vm.root_device_type != "ebs"

	if @env["image.register"]
		@env.ui.info I18n.t("vagrant.plugins.aws.actions.create_image.creating")
		@image = @env["vm"].connection.create_image(@env["vm"].vm.id, @env['image.name'], @env['image.desc'])
		@image = @env["vm"].connection.images.new({ :id => @image.body['imageId'] })
		@image.wait_for { state == "available" }
	else
		@image = @env["vm"].connection.images.get(@env["vm"].vm.image_id)
	end

	setup_temp_dir
	export

	@app.call(@env)
	
	cleanup_temp_dir
end

#cleanup_temp_dirObject



77
78
79
80
81
# File 'lib/vagrant-aws/action/create_image.rb', line 77

def cleanup_temp_dir
	if temp_dir && File.exist?(temp_dir)
		FileUtils.rm_rf(temp_dir)
	end
end

#exportObject

Write a Vagrantfile with the relevant information



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/vagrant-aws/action/create_image.rb', line 90

def export
	File.open(File.join(@env["export.temp_dir"], 'Vagrantfile'), "w") do |f|
		f.write(TemplateRenderer.render_string(PACKAGE_VAGRANTFILE, {
			:image => @image.id,
			:region => @env["config"].aws.region
		}))
	end
	File.open(File.join(@env["export.temp_dir"], 'image.json'), "w") do |f|
		f.write(@image.to_json)
	end
end

#recover(env) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/vagrant-aws/action/create_image.rb', line 69

def recover(env)
	if env["image.register"]
		env.ui.info I18n.t("vagrant.plugins.aws.actions.deregister_image.deregistering", :image => @image.id)
		@image.deregister(!@image.root_device_name.nil?)  # Don't try to delete backing snapshot if it was not created
	end
	cleanup_temp_dir
end

#setup_temp_dirObject



83
84
85
86
87
# File 'lib/vagrant-aws/action/create_image.rb', line 83

def setup_temp_dir
	@env.ui.info I18n.t("vagrant.actions.vm.export.create_dir")
	@temp_dir = @env["export.temp_dir"] = @env.env.tmp_path.join(Time.now.to_i.to_s)
	FileUtils.mkpath(@env["export.temp_dir"])
end