vagrant-aws 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. data/.gitignore +11 -2
  2. data/CHANGELOG.md +3 -0
  3. data/Gemfile +8 -2
  4. data/LICENSE +8 -0
  5. data/README.md +192 -65
  6. data/Rakefile +18 -7
  7. data/dummy.box +0 -0
  8. data/example_box/README.md +13 -0
  9. data/example_box/metadata.json +3 -0
  10. data/lib/vagrant-aws.rb +17 -13
  11. data/lib/vagrant-aws/action.rb +107 -0
  12. data/lib/vagrant-aws/action/connect_aws.rb +38 -0
  13. data/lib/vagrant-aws/action/is_created.rb +18 -0
  14. data/lib/vagrant-aws/action/message_already_created.rb +16 -0
  15. data/lib/vagrant-aws/action/message_not_created.rb +16 -0
  16. data/lib/vagrant-aws/action/read_ssh_info.rb +47 -0
  17. data/lib/vagrant-aws/action/read_state.rb +38 -0
  18. data/lib/vagrant-aws/action/run_instance.rb +148 -0
  19. data/lib/vagrant-aws/action/sync_folders.rb +57 -0
  20. data/lib/vagrant-aws/action/terminate_instance.rb +26 -0
  21. data/lib/vagrant-aws/action/timed_provision.rb +21 -0
  22. data/lib/vagrant-aws/action/warn_networks.rb +19 -0
  23. data/lib/vagrant-aws/config.rb +253 -38
  24. data/lib/vagrant-aws/errors.rb +15 -25
  25. data/lib/vagrant-aws/plugin.rb +73 -0
  26. data/lib/vagrant-aws/provider.rb +50 -0
  27. data/lib/vagrant-aws/util/timer.rb +17 -0
  28. data/lib/vagrant-aws/version.rb +4 -2
  29. data/locales/en.yml +65 -61
  30. data/spec/vagrant-aws/config_spec.rb +161 -0
  31. data/vagrant-aws.gemspec +54 -25
  32. metadata +79 -86
  33. data/lib/vagrant-aws/action/create.rb +0 -56
  34. data/lib/vagrant-aws/action/create_image.rb +0 -106
  35. data/lib/vagrant-aws/action/create_sshkey.rb +0 -39
  36. data/lib/vagrant-aws/action/deregister_image.rb +0 -27
  37. data/lib/vagrant-aws/action/populate_ssh.rb +0 -41
  38. data/lib/vagrant-aws/action/prepare_provisioners.rb +0 -127
  39. data/lib/vagrant-aws/action/resume.rb +0 -20
  40. data/lib/vagrant-aws/action/suspend.rb +0 -20
  41. data/lib/vagrant-aws/action/terminate.rb +0 -21
  42. data/lib/vagrant-aws/box.rb +0 -20
  43. data/lib/vagrant-aws/box_collection.rb +0 -15
  44. data/lib/vagrant-aws/command.rb +0 -186
  45. data/lib/vagrant-aws/environment.rb +0 -79
  46. data/lib/vagrant-aws/middleware.rb +0 -53
  47. data/lib/vagrant-aws/system.rb +0 -24
  48. data/lib/vagrant-aws/vm.rb +0 -94
  49. data/lib/vagrant_init.rb +0 -4
  50. data/test/test_helper.rb +0 -24
  51. data/test/vagrant-aws/action/create_image_test.rb +0 -63
  52. data/test/vagrant-aws/action/create_ssh_key_test.rb +0 -43
  53. data/test/vagrant-aws/action/create_test.rb +0 -65
  54. data/test/vagrant-aws/action/terminate_test.rb +0 -20
  55. data/test/vagrant-aws/command_test.rb +0 -21
  56. data/test/vagrant-aws/config_test.rb +0 -14
@@ -1,21 +0,0 @@
1
- module VagrantAWS
2
- class Action
3
- class Terminate
4
- def initialize(app, env)
5
- @app = app
6
- end
7
-
8
- def call(env)
9
- env.ui.info I18n.t("vagrant.actions.vm.destroy.destroying")
10
-
11
- env["vm"].vm.destroy
12
- env["vm"].vm = nil
13
-
14
- @app.call(env)
15
- end
16
-
17
- end
18
- end
19
- end
20
-
21
-
@@ -1,20 +0,0 @@
1
- module VagrantAWS
2
-
3
- class Box < Vagrant::Box
4
-
5
- %w{ ovf_file repackage destroy }.each do |method|
6
- undef_method(method)
7
- end
8
-
9
- def add
10
- raise Vagrant::Errors::BoxAlreadyExists, :name => name if File.directory?(directory)
11
- env.actions.run(:aws_add_image, { "box" => self, "validate" => false })
12
- end
13
-
14
- def remove(options=nil)
15
- env.actions.run(:aws_remove_image, { "box" => self, "validate" => false }.merge(options || {}))
16
- end
17
-
18
- end
19
-
20
- end
@@ -1,15 +0,0 @@
1
- module VagrantAWS
2
- class BoxCollection < Vagrant::BoxCollection
3
-
4
- def reload!
5
- @boxes.clear
6
- Dir.open(env.boxes_path) do |dir|
7
- dir.each do |d|
8
- next if d == "." || d == ".." || !File.directory?(env.boxes_path.join(d))
9
- @boxes << VagrantAWS::Box.new(env, d)
10
- end
11
- end
12
- end
13
-
14
- end
15
- end
@@ -1,186 +0,0 @@
1
- require 'fog'
2
-
3
- module VagrantAWS
4
-
5
- class AWSCommands < Vagrant::Command::GroupBase
6
- register "aws", "Commands to interact with Amazon AWS (EC2)"
7
-
8
- def initialize(*args)
9
- super
10
- initialize_aws_environment(*args)
11
- end
12
-
13
-
14
- desc "up [NAME]", "Creates the Vagrant environment on Amazon AWS."
15
- method_options :provision => true
16
- def up(name=nil)
17
- target_vms(name).each do |vm|
18
- if vm.created?
19
- vm.env.ui.info I18n.t("vagrant.commands.up.vm_created")
20
- else
21
- vm.env.actions.run(:aws_up, "provision.enabled" => options[:provision])
22
- end
23
- end
24
- end
25
-
26
-
27
- desc "destroy [NAME]", "Destroy the Vagrant AWS environment, terminating the created virtual machines."
28
- def destroy(name=nil)
29
- target_vms(name).each do |vm|
30
- if vm.created?
31
- vm.env.actions.run(:aws_destroy)
32
- else
33
- vm.env.ui.info I18n.t("vagrant.commands.common.vm_not_created")
34
- end
35
- end
36
- end
37
-
38
-
39
- desc "status", "Show the status of the current Vagrant AWS environment."
40
- def status
41
- state = nil
42
- results = target_vms.collect do |vm|
43
- state = vm.created? ? vm.vm.state.to_s : 'not_created'
44
- "#{vm.name.to_s.ljust(25)}#{state.gsub("_", " ")}"
45
- end
46
- state = target_vms.length == 1 ? state : "listing"
47
- @env.ui.info(I18n.t("vagrant.commands.status.output",
48
- :states => results.join("\n"),
49
- :message => I18n.t("vagrant.plugins.aws.commands.status.#{state}")),
50
- :prefix => false)
51
- end
52
-
53
-
54
- desc "ssh [NAME]", "SSH into the currently running Vagrant AWS environment."
55
- method_options %w( execute -e ) => :string
56
- def ssh(name=nil)
57
- raise Vagrant::Errors::MultiVMTargetRequired, :command => "ssh" if target_vms.length > 1
58
-
59
- ssh_vm = target_vms.first
60
- ssh_vm.env.actions.run(VagrantAWS::Action::PopulateSSH)
61
-
62
- if options[:execute]
63
- ssh_vm.ssh.execute do |ssh|
64
- ssh_vm.env.ui.info I18n.t("vagrant.commands.ssh.execute", :command => options[:execute])
65
- ssh.exec!(options[:execute]) do |channel, type, data|
66
- ssh_vm.env.ui.info "#{data}"
67
- end
68
- end
69
- else
70
- raise Vagrant::Errors::VMNotCreatedError if !ssh_vm.created?
71
- raise Vagrant::Errors::VMNotRunningError if !ssh_vm.vm.running?
72
- ssh_vm.ssh.connect
73
- end
74
- end
75
-
76
-
77
- desc "provision [NAME]", "Rerun the provisioning scripts on a running VM."
78
- def provision(name=nil)
79
- target_vms(name).each do |vm|
80
- if vm.created? && vm.vm.state == 'running'
81
- vm.env.actions.run(:aws_provision)
82
- else
83
- vm.env.ui.info I18n.t("vagrant.commands.common.vm_not_created")
84
- end
85
- end
86
- end
87
-
88
-
89
- desc "suspend [NAME]", "Suspend a running Vagrant AWS environment"
90
- def suspend(name=nil)
91
- target_vms(name).each do |vm|
92
- if vm.created?
93
- vm.env.actions.run(:aws_suspend)
94
- else
95
- vm.env.ui.info I18n.t("vagrant.commands.common.vm_not_created")
96
- end
97
- end
98
- end
99
-
100
-
101
- desc "resume [NAME]", "Resume a suspended Vagrant AWS environment"
102
- def resume(name=nil)
103
- target_vms(name).each do |vm|
104
- if vm.created?
105
- vm.env.actions.run(:aws_resume)
106
- else
107
- vm.env.ui.info I18n.t("vagrant.commands.common.vm_not_created")
108
- end
109
- end
110
- end
111
-
112
-
113
- desc "ssh_config [NAME]", "outputs .ssh/config valid syntax for connecting to this environment via ssh"
114
- method_options %w{ host_name -h } => :string
115
- def ssh_config(name=nil)
116
- raise Vagrant::Errors::MultiVMTargetRequired, :command => "ssh_config" if target_vms.length > 1
117
-
118
- ssh_vm = target_vms.first
119
- raise Vagrant::Errors::VMNotCreatedError if !ssh_vm.created?
120
- ssh_vm.env.actions.run(VagrantAWS::Action::PopulateSSH)
121
-
122
- $stdout.puts(Vagrant::Util::TemplateRenderer.render("ssh_config", {
123
- :host_key => options[:host] || "vagrant",
124
- :ssh_host => ssh_vm.env.config.ssh.host,
125
- :ssh_user => ssh_vm.env.config.ssh.username,
126
- :ssh_port => ssh_vm.ssh.port,
127
- :private_key_path => ssh_vm.env.config.ssh.private_key_path
128
- }))
129
- end
130
-
131
-
132
- desc "box_create [NAME]", "create image box from running Vagrant AWS VM"
133
- method_option :register, :aliases => '-r', :type => :boolean, :default => true, :banner => "register with AWS"
134
- method_option :image_name, :aliases => '-f', :type => :string, :banner => "name of created image"
135
- method_option :image_desc, :aliases => '-d', :type => :string, :banner => "description of created image"
136
- def box_create(name=nil)
137
- raise Vagrant::Errors::MultiVMTargetRequired, :command => "box_create" if target_vms.length > 1
138
-
139
- ami_vm = target_vms.first
140
- ami_vm.env.actions.run(:aws_create_image, {
141
- 'package.output' => options[:image_name] || env.config.package.name,
142
- 'image.register' => options[:register],
143
- 'image.name' => options[:image_name] || "vagrantaws_#{rand(36**8).to_s(36)}",
144
- 'image.desc' => options[:image_desc] || "Image created by vagrant-aws"
145
- })
146
-
147
- end
148
-
149
-
150
- desc "box_add NAME URI", "Add an AWS image box to the system"
151
- def box_add(name, uri)
152
- Box.add(env, name, uri)
153
- end
154
-
155
-
156
- desc "box_list", "list available AWS image boxes"
157
- def box_list
158
- boxes = env.boxes.sort
159
- return env.ui.warn(I18n.t("vagrant.commands.box.no_installed_boxes"), :prefix => false) if boxes.empty?
160
- boxes.each { |b| env.ui.info(b.name, :prefix => false) }
161
- end
162
-
163
-
164
- desc "box_remove NAME", "Remove named image box from system and optionally deregister image with AWS"
165
- method_option :deregister, :aliases => '-d', :type => :boolean, :default => false, :banner => "deregister with AWS"
166
- def box_remove(name)
167
- b = env.boxes.find(name)
168
- raise Vagrant::Errors::BoxNotFound, :name => name if !b
169
- b.remove({ 'image.deregister' => options[:deregister] })
170
- end
171
-
172
- protected
173
-
174
- # Reinitialize "AWS" environment
175
- def initialize_aws_environment(args, options, config)
176
- raise Errors::CLIMissingEnvironment if !config[:env]
177
- if config[:env].is_a?(VagrantAWS::Environment)
178
- @env = config[:env]
179
- else
180
- @env = VagrantAWS::Environment.new
181
- @env.ui = config[:env].ui # Touch up UI
182
- @env.load!
183
- end
184
- end
185
- end
186
- end
@@ -1,79 +0,0 @@
1
- require 'fog'
2
-
3
- module VagrantAWS
4
- # Represents a single Vagrant environment, overridden to not alias with
5
- # existing Vagrant data storage, VM implementation, etc.
6
- class Environment < Vagrant::Environment
7
- DEFAULT_DOTFILE = ".vagrantaws"
8
- FOGFILE = ".fog"
9
-
10
- def dotfile_path
11
- root_path.join(DEFAULT_DOTFILE) rescue nil
12
- end
13
-
14
- def aws_home_path
15
- home_path.join("aws")
16
- end
17
-
18
- def boxes_path
19
- aws_home_path.join("images")
20
- end
21
-
22
- def boxes
23
- return parent.boxes if parent
24
- @_boxes ||= VagrantAWS::BoxCollection.new(self)
25
- end
26
-
27
- def ssh_keys_path
28
- aws_home_path.join("keys")
29
- end
30
-
31
- def ssh_keys
32
- return parent.ssh_keys if parent
33
- Dir.chdir(ssh_keys_path) { |unused| Dir.entries('.').select { |f| File.file?(f) } }
34
- end
35
-
36
- def load!
37
- super
38
-
39
- # Setup fog credential path
40
- project_fog_path = root_path.join(FOGFILE) rescue nil
41
- Fog.credentials_path = File.expand_path(fogfile_path) if project_fog_path && File.exist?(project_fog_path)
42
-
43
- self
44
- end
45
-
46
- # Override to create "AWS" specific directories in 'home_dir'
47
- def load_home_directory!
48
- super
49
-
50
- dirs = %w{ images keys }.map { |d| aws_home_path.join(d) }
51
- dirs.each do |dir|
52
- next if File.directory?(dir)
53
- ui.info I18n.t("vagrant.general.creating_home_dir", :directory => dir)
54
- FileUtils.mkdir_p(dir)
55
- end
56
- end
57
-
58
- # Override to create "AWS" VM
59
- def load_vms!
60
- result = {}
61
-
62
- # Load the VM UUIDs from the local data store
63
- (local_data[:active] || {}).each do |name, desc|
64
- result[name.to_sym] = VagrantAWS::VM.find(desc, self, name.to_sym)
65
- end
66
-
67
- # For any VMs which aren't created, create a blank VM instance for
68
- # them
69
- all_keys = config.vm.defined_vm_keys
70
- all_keys = [DEFAULT_VM] if all_keys.empty?
71
- all_keys.each do |name|
72
- result[name] = VagrantAWS::VM.new(:name => name, :env => self) if !result.has_key?(name)
73
- end
74
-
75
- result
76
- end
77
-
78
- end
79
- end
@@ -1,53 +0,0 @@
1
- require 'vagrant-aws/action/create'
2
- require 'vagrant-aws/action/terminate'
3
- require 'vagrant-aws/action/create_sshkey'
4
- require 'vagrant-aws/action/populate_ssh'
5
- require 'vagrant-aws/action/prepare_provisioners'
6
- require 'vagrant-aws/action/suspend'
7
- require 'vagrant-aws/action/resume'
8
- require 'vagrant-aws/action/create_image'
9
- require 'vagrant-aws/action/deregister_image'
10
-
11
- module VagrantAWS
12
-
13
- Vagrant::Action.register(:aws_provision, Vagrant::Action::Builder.new do
14
- use Action::PopulateSSH
15
- use Action::PrepareProvisioners
16
- use Vagrant::Action[:provision]
17
- end)
18
-
19
- Vagrant::Action.register(:aws_up, Vagrant::Action::Builder.new do
20
- use Action::CreateSSHKey
21
- use Action::Create
22
- use Vagrant::Action[:aws_provision]
23
- end)
24
-
25
- Vagrant::Action.register(:aws_destroy, Vagrant::Action::Builder.new do
26
- use Action::Terminate
27
- end)
28
-
29
- Vagrant::Action.register(:aws_suspend, Vagrant::Action::Builder.new do
30
- use Action::Suspend
31
- end)
32
-
33
- Vagrant::Action.register(:aws_resume, Vagrant::Action::Builder.new do
34
- use Action::Resume
35
- end)
36
-
37
- Vagrant::Action.register(:aws_create_image, Vagrant::Action::Builder.new do
38
- use Action::CreateImage
39
- use Vagrant::Action::VM::Package
40
- end)
41
-
42
- Vagrant::Action.register(:aws_add_image, Vagrant::Action::Builder.new do
43
- use Vagrant::Action::Box::Download
44
- use Vagrant::Action::Box::Unpackage
45
- end)
46
-
47
- Vagrant::Action.register(:aws_remove_image, Vagrant::Action::Builder.new do
48
- use Action::DeregisterImage
49
- use Vagrant::Action::Box::Destroy
50
- end)
51
-
52
-
53
- end
@@ -1,24 +0,0 @@
1
- module Vagrant
2
- module Systems
3
- class Debian < Linux
4
- def bootstrap_chef
5
- vm.ssh.execute do |ssh|
6
- commands = [
7
- "apt-get -y --force-yes update",
8
- "apt-get -y --force-yes install ruby ruby-dev libopenssl-ruby irb build-essential wget ssl-cert",
9
- "cd /tmp && wget -nv http://production.cf.rubygems.org/rubygems/rubygems-1.7.2.tgz && tar zxf rubygems-1.7.2.tgz",
10
- "cd rubygems-1.7.2 && ruby setup.rb --no-format-executable",
11
- "gem install chef --no-ri --no-rdoc"
12
- ]
13
- ssh.sudo!(commands) do |channel, type, data|
14
- if type == :exit_status
15
- ssh.check_exit_status(data, commands)
16
- else
17
- vm.env.ui.info("#{data}: #{type}")
18
- end
19
- end
20
- end
21
- end
22
- end
23
- end
24
- end
@@ -1,94 +0,0 @@
1
- require 'fog'
2
-
3
- # Patch required by Vagrant::System
4
- module Fog
5
- module Compute
6
- class AWS
7
- class Server < Fog::Model
8
- def running?
9
- state == "running"
10
- end
11
- end
12
- end
13
- end
14
- end
15
-
16
-
17
- module VagrantAWS
18
-
19
- class VM < Vagrant::VM
20
-
21
- [:uuid, :package].each do |method|
22
- undef_method(method)
23
- end
24
-
25
- class << self
26
- def find(desc, env=nil, name=nil)
27
- env.ui.info I18n.t("vagrant.plugins.aws.general.getting_status") if env
28
-
29
- vm = Fog::Compute.new(:provider => 'AWS', :region => desc['region']).servers.get(desc['id'])
30
- my_vm = new(:vm => vm, :env => env, :name => name)
31
-
32
- # Recover key configuration values from data store not available from AWS directly
33
- unless my_vm.env.nil?
34
- my_vm.env.config.aws.region = desc['region']
35
- end
36
-
37
- my_vm
38
- end
39
- end
40
-
41
- # Copied from Vagrant VM, but modified to generate a VagrantAWS::Environment
42
- def initialize(opts=nil)
43
- defaults = { :vm => nil, :env => nil, :name => nil }
44
-
45
- opts = defaults.merge(opts || {})
46
-
47
- @vm = opts[:vm]
48
- @connection = @vm.connection if @vm # Initialize connection from intialized server
49
-
50
- @name = opts[:name]
51
-
52
- if !opts[:env].nil?
53
- # We have an environment, so we create a new child environment
54
- # specifically for this VM. This step will load any custom
55
- # config and such.
56
- @env = VagrantAWS::Environment.new({
57
- :cwd => opts[:env].cwd,
58
- :parent => opts[:env],
59
- :vm => self
60
- }).load!
61
-
62
- # Load the associated system.
63
- load_system!
64
- end
65
-
66
- @loaded_system_distro = false
67
- end
68
-
69
- def vm=(value)
70
- @vm = value
71
- env.local_data[:active] ||= {}
72
-
73
- if value && value.id
74
- env.local_data[:active][name.to_s] = {
75
- 'id' => value.id,
76
- 'region' => value.connection.instance_variable_get(:@region)
77
- }
78
- else
79
- env.local_data[:active].delete(name.to_s)
80
- end
81
-
82
- # Commit the local data so that the next time vagrant is initialized,
83
- # it realizes the VM exists
84
- env.local_data.commit
85
- end
86
-
87
- def connection(region = nil)
88
- @connection ||= Fog::Compute.new(
89
- :provider => 'AWS',
90
- :region => region || env.config.aws.region || nil
91
- )
92
- end
93
- end
94
- end