bmabey-vagrant 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (115) hide show
  1. data/.gitignore +12 -0
  2. data/Gemfile +17 -0
  3. data/LICENSE +21 -0
  4. data/README.md +53 -0
  5. data/Rakefile +41 -0
  6. data/VERSION +1 -0
  7. data/bin/.gitignore +0 -0
  8. data/bin/vagrant +15 -0
  9. data/bin/vagrant-box +34 -0
  10. data/bin/vagrant-down +27 -0
  11. data/bin/vagrant-halt +28 -0
  12. data/bin/vagrant-init +27 -0
  13. data/bin/vagrant-package +29 -0
  14. data/bin/vagrant-reload +29 -0
  15. data/bin/vagrant-resume +27 -0
  16. data/bin/vagrant-ssh +27 -0
  17. data/bin/vagrant-status +29 -0
  18. data/bin/vagrant-suspend +27 -0
  19. data/bin/vagrant-up +29 -0
  20. data/config/default.rb +26 -0
  21. data/keys/README.md +10 -0
  22. data/keys/vagrant +27 -0
  23. data/keys/vagrant.pub +1 -0
  24. data/lib/vagrant.rb +19 -0
  25. data/lib/vagrant/actions/base.rb +118 -0
  26. data/lib/vagrant/actions/box/add.rb +22 -0
  27. data/lib/vagrant/actions/box/destroy.rb +14 -0
  28. data/lib/vagrant/actions/box/download.rb +72 -0
  29. data/lib/vagrant/actions/box/unpackage.rb +43 -0
  30. data/lib/vagrant/actions/collection.rb +36 -0
  31. data/lib/vagrant/actions/runner.rb +132 -0
  32. data/lib/vagrant/actions/vm/boot.rb +47 -0
  33. data/lib/vagrant/actions/vm/customize.rb +17 -0
  34. data/lib/vagrant/actions/vm/destroy.rb +23 -0
  35. data/lib/vagrant/actions/vm/down.rb +12 -0
  36. data/lib/vagrant/actions/vm/export.rb +41 -0
  37. data/lib/vagrant/actions/vm/forward_ports.rb +46 -0
  38. data/lib/vagrant/actions/vm/halt.rb +14 -0
  39. data/lib/vagrant/actions/vm/import.rb +18 -0
  40. data/lib/vagrant/actions/vm/move_hard_drive.rb +51 -0
  41. data/lib/vagrant/actions/vm/package.rb +65 -0
  42. data/lib/vagrant/actions/vm/provision.rb +49 -0
  43. data/lib/vagrant/actions/vm/reload.rb +17 -0
  44. data/lib/vagrant/actions/vm/resume.rb +16 -0
  45. data/lib/vagrant/actions/vm/shared_folders.rb +81 -0
  46. data/lib/vagrant/actions/vm/start.rb +18 -0
  47. data/lib/vagrant/actions/vm/suspend.rb +16 -0
  48. data/lib/vagrant/actions/vm/up.rb +40 -0
  49. data/lib/vagrant/active_list.rb +73 -0
  50. data/lib/vagrant/box.rb +152 -0
  51. data/lib/vagrant/busy.rb +73 -0
  52. data/lib/vagrant/commands.rb +219 -0
  53. data/lib/vagrant/config.rb +183 -0
  54. data/lib/vagrant/downloaders/base.rb +16 -0
  55. data/lib/vagrant/downloaders/file.rb +17 -0
  56. data/lib/vagrant/downloaders/http.rb +47 -0
  57. data/lib/vagrant/environment.rb +263 -0
  58. data/lib/vagrant/provisioners/base.rb +29 -0
  59. data/lib/vagrant/provisioners/chef.rb +103 -0
  60. data/lib/vagrant/provisioners/chef_server.rb +84 -0
  61. data/lib/vagrant/provisioners/chef_solo.rb +97 -0
  62. data/lib/vagrant/ssh.rb +104 -0
  63. data/lib/vagrant/util.rb +51 -0
  64. data/lib/vagrant/util/errors.rb +36 -0
  65. data/lib/vagrant/util/stacked_proc_runner.rb +35 -0
  66. data/lib/vagrant/util/template_renderer.rb +83 -0
  67. data/lib/vagrant/vm.rb +61 -0
  68. data/templates/Vagrantfile.erb +8 -0
  69. data/templates/errors.yml +117 -0
  70. data/test/test_helper.rb +163 -0
  71. data/test/vagrant/actions/base_test.rb +32 -0
  72. data/test/vagrant/actions/box/add_test.rb +37 -0
  73. data/test/vagrant/actions/box/destroy_test.rb +18 -0
  74. data/test/vagrant/actions/box/download_test.rb +131 -0
  75. data/test/vagrant/actions/box/unpackage_test.rb +100 -0
  76. data/test/vagrant/actions/collection_test.rb +110 -0
  77. data/test/vagrant/actions/runner_test.rb +265 -0
  78. data/test/vagrant/actions/vm/boot_test.rb +55 -0
  79. data/test/vagrant/actions/vm/customize_test.rb +16 -0
  80. data/test/vagrant/actions/vm/destroy_test.rb +36 -0
  81. data/test/vagrant/actions/vm/down_test.rb +32 -0
  82. data/test/vagrant/actions/vm/export_test.rb +88 -0
  83. data/test/vagrant/actions/vm/forward_ports_test.rb +104 -0
  84. data/test/vagrant/actions/vm/halt_test.rb +27 -0
  85. data/test/vagrant/actions/vm/import_test.rb +43 -0
  86. data/test/vagrant/actions/vm/move_hard_drive_test.rb +108 -0
  87. data/test/vagrant/actions/vm/package_test.rb +181 -0
  88. data/test/vagrant/actions/vm/provision_test.rb +108 -0
  89. data/test/vagrant/actions/vm/reload_test.rb +47 -0
  90. data/test/vagrant/actions/vm/resume_test.rb +27 -0
  91. data/test/vagrant/actions/vm/shared_folders_test.rb +176 -0
  92. data/test/vagrant/actions/vm/start_test.rb +36 -0
  93. data/test/vagrant/actions/vm/suspend_test.rb +27 -0
  94. data/test/vagrant/actions/vm/up_test.rb +107 -0
  95. data/test/vagrant/active_list_test.rb +190 -0
  96. data/test/vagrant/box_test.rb +151 -0
  97. data/test/vagrant/busy_test.rb +83 -0
  98. data/test/vagrant/commands_test.rb +307 -0
  99. data/test/vagrant/config_test.rb +256 -0
  100. data/test/vagrant/downloaders/base_test.rb +27 -0
  101. data/test/vagrant/downloaders/file_test.rb +26 -0
  102. data/test/vagrant/downloaders/http_test.rb +40 -0
  103. data/test/vagrant/environment_test.rb +607 -0
  104. data/test/vagrant/provisioners/base_test.rb +33 -0
  105. data/test/vagrant/provisioners/chef_server_test.rb +187 -0
  106. data/test/vagrant/provisioners/chef_solo_test.rb +149 -0
  107. data/test/vagrant/provisioners/chef_test.rb +117 -0
  108. data/test/vagrant/ssh_test.rb +222 -0
  109. data/test/vagrant/util/errors_test.rb +57 -0
  110. data/test/vagrant/util/stacked_proc_runner_test.rb +43 -0
  111. data/test/vagrant/util/template_renderer_test.rb +138 -0
  112. data/test/vagrant/util_test.rb +64 -0
  113. data/test/vagrant/vm_test.rb +114 -0
  114. data/vagrant.gemspec +216 -0
  115. metadata +285 -0
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ begin
3
+ require File.expand_path('../../.bundle/environment', __FILE__)
4
+ rescue LoadError
5
+ # Fallback on rubygems
6
+ require "rubygems"
7
+ end
8
+
9
+ require 'git-style-binary/command'
10
+
11
+ # Get library
12
+ libdir = File.join(File.dirname(__FILE__), '..', 'lib')
13
+ require File.expand_path('vagrant', libdir)
14
+
15
+ GitStyleBinary.command do
16
+ short_desc "create the vagrant environment"
17
+ banner <<-EOS
18
+ Usage: #{command.full_name} #{all_options_string}
19
+
20
+ Create the vagrant environment if it doesn't exist,
21
+ otherwise start the vagrant environment if its not
22
+ already running.
23
+
24
+ EOS
25
+
26
+ run do |command|
27
+ Vagrant::Commands.execute(:up)
28
+ end
29
+ end
@@ -0,0 +1,26 @@
1
+ Vagrant::Config.run do |config|
2
+ # default config goes here
3
+ config.vagrant.log_output = STDOUT
4
+ config.vagrant.dotfile_name = ".vagrant"
5
+ config.vagrant.home = "~/.vagrant"
6
+
7
+ config.ssh.username = "vagrant"
8
+ config.ssh.password = "vagrant"
9
+ config.ssh.host = "localhost"
10
+ config.ssh.forwarded_port_key = "ssh"
11
+ config.ssh.max_tries = 10
12
+ config.ssh.timeout = 30
13
+ config.ssh.private_key_path = File.join(PROJECT_ROOT, 'keys', 'vagrant')
14
+
15
+ config.vm.box_ovf = "box.ovf"
16
+ config.vm.base_mac = "0800279C2E42"
17
+ config.vm.project_directory = "/vagrant"
18
+ config.vm.forward_port("ssh", 22, 2222)
19
+ config.vm.disk_image_format = 'VMDK'
20
+ config.vm.provisioner = nil
21
+ config.vm.shared_folder_uid = nil
22
+ config.vm.shared_folder_gid = nil
23
+
24
+ config.package.name = 'vagrant'
25
+ config.package.extension = '.box'
26
+ end
@@ -0,0 +1,10 @@
1
+ # Insecure Keypair
2
+
3
+ These keys are the "insecure" public/private keypair we offer to
4
+ [base box creators](http://vagrantup.com/docs/base_boxes.html) for use in their base boxes so that
5
+ vagrant installations can automatically SSH into the boxes.
6
+
7
+ If you're working with a team or company or with a custom box and
8
+ you want more secure SSH, you should create your own keypair
9
+ and configure the private key in the Vagrantfile with
10
+ `config.ssh.private_key_path`
@@ -0,0 +1,27 @@
1
+ -----BEGIN RSA PRIVATE KEY-----
2
+ MIIEogIBAAKCAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzI
3
+ w+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoP
4
+ kcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2
5
+ hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NO
6
+ Td0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcW
7
+ yLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQIBIwKCAQEA4iqWPJXtzZA68mKd
8
+ ELs4jJsdyky+ewdZeNds5tjcnHU5zUYE25K+ffJED9qUWICcLZDc81TGWjHyAqD1
9
+ Bw7XpgUwFgeUJwUlzQurAv+/ySnxiwuaGJfhFM1CaQHzfXphgVml+fZUvnJUTvzf
10
+ TK2Lg6EdbUE9TarUlBf/xPfuEhMSlIE5keb/Zz3/LUlRg8yDqz5w+QWVJ4utnKnK
11
+ iqwZN0mwpwU7YSyJhlT4YV1F3n4YjLswM5wJs2oqm0jssQu/BT0tyEXNDYBLEF4A
12
+ sClaWuSJ2kjq7KhrrYXzagqhnSei9ODYFShJu8UWVec3Ihb5ZXlzO6vdNQ1J9Xsf
13
+ 4m+2ywKBgQD6qFxx/Rv9CNN96l/4rb14HKirC2o/orApiHmHDsURs5rUKDx0f9iP
14
+ cXN7S1uePXuJRK/5hsubaOCx3Owd2u9gD6Oq0CsMkE4CUSiJcYrMANtx54cGH7Rk
15
+ EjFZxK8xAv1ldELEyxrFqkbE4BKd8QOt414qjvTGyAK+OLD3M2QdCQKBgQDtx8pN
16
+ CAxR7yhHbIWT1AH66+XWN8bXq7l3RO/ukeaci98JfkbkxURZhtxV/HHuvUhnPLdX
17
+ 3TwygPBYZFNo4pzVEhzWoTtnEtrFueKxyc3+LjZpuo+mBlQ6ORtfgkr9gBVphXZG
18
+ YEzkCD3lVdl8L4cw9BVpKrJCs1c5taGjDgdInQKBgHm/fVvv96bJxc9x1tffXAcj
19
+ 3OVdUN0UgXNCSaf/3A/phbeBQe9xS+3mpc4r6qvx+iy69mNBeNZ0xOitIjpjBo2+
20
+ dBEjSBwLk5q5tJqHmy/jKMJL4n9ROlx93XS+njxgibTvU6Fp9w+NOFD/HvxB3Tcz
21
+ 6+jJF85D5BNAG3DBMKBjAoGBAOAxZvgsKN+JuENXsST7F89Tck2iTcQIT8g5rwWC
22
+ P9Vt74yboe2kDT531w8+egz7nAmRBKNM751U/95P9t88EDacDI/Z2OwnuFQHCPDF
23
+ llYOUI+SpLJ6/vURRbHSnnn8a/XG+nzedGH5JGqEJNQsz+xT2axM0/W/CRknmGaJ
24
+ kda/AoGANWrLCz708y7VYgAtW2Uf1DPOIYMdvo6fxIB5i9ZfISgcJ/bbCUkFrhoH
25
+ +vq/5CIWxCPp0f85R4qxxQ5ihxJ0YDQT9Jpx4TMss4PSavPaBH3RXow5Ohe+bYoQ
26
+ NE5OgEXk2wVfZczCZpigBKbKZHNYcelXtTt/nP3rsCuGcM4h53s=
27
+ -----END RSA PRIVATE KEY-----
@@ -0,0 +1 @@
1
+ ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ== johnbender@vagrant.local
@@ -0,0 +1,19 @@
1
+ libdir = File.dirname(__FILE__)
2
+ PROJECT_ROOT = File.join(libdir, '..') unless defined?(PROJECT_ROOT)
3
+
4
+ # The libs which must be loaded prior to the rest
5
+ %w{tempfile open-uri json pathname logger uri net/http virtualbox net/ssh archive/tar/minitar
6
+ net/scp fileutils}.each do |lib|
7
+ require lib
8
+ end
9
+
10
+ # The vagrant specific files which must be loaded prior to the rest
11
+ %w{vagrant/util vagrant/util/stacked_proc_runner vagrant/actions/base vagrant/downloaders/base vagrant/actions/collection
12
+ vagrant/actions/runner vagrant/config vagrant/provisioners/base vagrant/provisioners/chef}.each do |f|
13
+ require File.expand_path(f, libdir)
14
+ end
15
+
16
+ # Glob require the rest
17
+ Dir[File.join(libdir, "vagrant", "**", "*.rb")].each do |f|
18
+ require File.expand_path(f)
19
+ end
@@ -0,0 +1,118 @@
1
+ module Vagrant
2
+ module Actions
3
+ # Base class for any command actions.
4
+ #
5
+ # Actions are the smallest unit of functionality found within
6
+ # Vagrant. Vagrant composes many actions together to execute
7
+ # its complex tasks while keeping the individual pieces of a
8
+ # task as discrete reusable actions. Actions are ran exclusively
9
+ # by an {Runner action runner} which is simply a subclass of {Runner}.
10
+ #
11
+ # Actions work by implementing any or all of the following methods
12
+ # which a {Runner} executes:
13
+ #
14
+ # * `prepare` - Called once for each action before any action has `execute!`
15
+ # called. This is meant for basic setup.
16
+ # * `execute!` - This is where the meat of the action typically goes;
17
+ # the main code which executes the action.
18
+ # * `cleanup` - This is called exactly once for each action after every
19
+ # other action is completed. It is meant for cleaning up any resources.
20
+ # * `rescue` - This is called if an exception occurs in _any action_. This
21
+ # gives every other action a chance to clean itself up.
22
+ #
23
+ # For details of each step of an action, read the specific function call
24
+ # documentation below.
25
+ class Base
26
+ # The {Runner runner} which is executing the action
27
+ attr_reader :runner
28
+
29
+ # Included so subclasses don't need to include it themselves.
30
+ include Vagrant::Util
31
+
32
+ # Initialization of the action, passing any arguments which may have
33
+ # been given to the {Runner runner}. This method can be used by subclasses
34
+ # to save any of the configuration options which are passed in.
35
+ def initialize(runner, *args)
36
+ @runner = runner
37
+ end
38
+
39
+ # This method is called once per action, allowing the action
40
+ # to setup any callbacks, add more events, etc. Prepare is
41
+ # called in the order the actions are defined, and the action
42
+ # itself has no control over this.
43
+ #
44
+ # Examples of its usage:
45
+ #
46
+ # Perhaps we need an additional action only if a configuration is set:
47
+ #
48
+ # def prepare
49
+ # @vm.actions << FooAction if Vagrant.config[:foo] == :bar
50
+ # end
51
+ #
52
+ def prepare; end
53
+
54
+ # This method is called once, after preparing, to execute the
55
+ # actual task. This method is responsible for calling any
56
+ # callbacks. Adding new actions here will have unpredictable
57
+ # effects and should never be done.
58
+ #
59
+ # Examples of its usage:
60
+ #
61
+ # def execute!
62
+ # @vm.invoke_callback(:before_oven, "cookies")
63
+ # # Do lots of stuff here
64
+ # @vm.invoke_callback(:after_oven, "more", "than", "one", "option")
65
+ # end
66
+ #
67
+ def execute!; end
68
+
69
+ # This method is called after all actions have finished executing.
70
+ # It is meant as a place where final cleanup code can be done, knowing
71
+ # that all other actions are finished using your data.
72
+ def cleanup; end
73
+
74
+ # This method is only called if some exception occurs in the chain
75
+ # of actions. If an exception is raised in any action in the current
76
+ # chain, then every action part of that chain has {#rescue} called
77
+ # before raising the exception further. This method should be used to
78
+ # perform any cleanup necessary in the face of errors.
79
+ #
80
+ # **Warning:** Since this method is called when an exception is already
81
+ # raised, be _extra careful_ when implementing this method to handle
82
+ # all your own exceptions, otherwise it'll mask the initially raised
83
+ # exception.
84
+ def rescue(exception); end
85
+
86
+ # The following two methods are used for declaring action dependencies.
87
+ # For example, you require that the reload action be in place before
88
+ # a your new FooAction you might do the following
89
+ #
90
+ # def follows; [Reload] end
91
+
92
+ # This method is called when the runner is determining the actions that
93
+ # must precede a given action. You would say "This action follows [Action1, Action2]"
94
+ def follows; [] end
95
+
96
+ # This method is called when the runner is determining the actions that
97
+ # must follow a given action. You would say "This action precedes [Action3, Action4]
98
+ def precedes; [] end
99
+ end
100
+
101
+ # An exception which occured within an action. This should be used instead of
102
+ # {Vagrant::Util#error_and_exit error_and_exit}, since it allows the {Runner} to call
103
+ # {Base#rescue rescue} on all the actions and properly exit. Any message
104
+ # passed into the {ActionException} is then shown and and vagrant exits.
105
+ class ActionException < Exception
106
+ attr_reader :key
107
+ attr_reader :data
108
+
109
+ def initialize(key, data = {})
110
+ @key = key
111
+ @data = data
112
+
113
+ message = Vagrant::Util::Errors.error_string(key, data)
114
+ super(message)
115
+ end
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,22 @@
1
+ module Vagrant
2
+ module Actions
3
+ module Box
4
+ # A meta-action which adds a box by downloading and unpackaging it.
5
+ # This action downloads and unpackages a box with a given URI. This
6
+ # is a _meta action_, meaning it simply adds more actions to the
7
+ # action chain, and those actions do the work.
8
+ #
9
+ # This is the action called by {Box#add}.
10
+ class Add < Base
11
+ def prepare
12
+ if File.exists?(@runner.directory)
13
+ raise ActionException.new(:box_add_already_exists, :box_name => @runner.name)
14
+ end
15
+
16
+ @runner.add_action(Download)
17
+ @runner.add_action(Unpackage)
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,14 @@
1
+ module Vagrant
2
+ module Actions
3
+ module Box
4
+ # Action to destroy a box. This action is not reversible and expects
5
+ # to be called by a {Box} object.
6
+ class Destroy < Base
7
+ def execute!
8
+ logger.info "Deleting box directory..."
9
+ FileUtils.rm_rf(@runner.directory)
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,72 @@
1
+ module Vagrant
2
+ module Actions
3
+ module Box
4
+ # An action which acts on a box by downloading the box file from
5
+ # the given URI into a temporary location. This action parses a
6
+ # given URI and handles downloading it via one of the many Vagrant
7
+ # downloads (such as {Vagrant::Downloaders::File}).
8
+ #
9
+ # This action cleans itself up by removing the downloaded box file.
10
+ class Download < Base
11
+ BASENAME = "box"
12
+ BUFFERSIZE = 1048576 # 1 MB
13
+
14
+ attr_reader :downloader
15
+
16
+ def prepare
17
+ # Parse the URI given and prepare a downloader
18
+ uri = URI.parse(@runner.uri)
19
+ uri_map = [[URI::HTTP, Downloaders::HTTP], [URI::Generic, Downloaders::File]]
20
+
21
+ uri_map.find do |uri_type, downloader_klass|
22
+ if uri.is_a?(uri_type)
23
+ logger.info "#{uri_type} for URI, downloading via #{downloader_klass}..."
24
+ @downloader = downloader_klass.new
25
+ end
26
+ end
27
+
28
+ raise ActionException.new(:box_download_unknown_type) unless @downloader
29
+
30
+ # Prepare the downloader
31
+ @downloader.prepare(@runner.uri)
32
+ end
33
+
34
+ def execute!
35
+ with_tempfile do |tempfile|
36
+ download_to(tempfile)
37
+ @runner.temp_path = tempfile.path
38
+ end
39
+ end
40
+
41
+ def cleanup
42
+ if @runner.temp_path && File.exist?(@runner.temp_path)
43
+ logger.info "Cleaning up downloaded box..."
44
+ File.unlink(@runner.temp_path)
45
+ end
46
+ end
47
+
48
+ def rescue(exception)
49
+ cleanup
50
+ end
51
+
52
+ def with_tempfile
53
+ logger.info "Creating tempfile for storing box file..."
54
+
55
+ # create, write only, fail if the file exists
56
+ File.open(file_temp_path, File::WRONLY | File::EXCL | File::CREAT) do |tempfile|
57
+ yield tempfile
58
+ end
59
+ end
60
+
61
+ def file_temp_path
62
+ File.join(@runner.env.tmp_path, BASENAME + Time.now.to_i.to_s)
63
+ end
64
+
65
+ def download_to(f)
66
+ logger.info "Copying box to temporary location..."
67
+ downloader.download!(@runner.uri, f)
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,43 @@
1
+ module Vagrant
2
+ module Actions
3
+ module Box
4
+ # This action unpackages a downloaded box file into its final
5
+ # box destination within the vagrant home folder.
6
+ class Unpackage < Base
7
+
8
+ def execute!
9
+ @runner.invoke_around_callback(:unpackage) do
10
+ setup_box_dir
11
+ decompress
12
+ end
13
+ end
14
+
15
+ def rescue(exception)
16
+ if File.directory?(box_dir)
17
+ logger.info "An error occurred, rolling back box unpackaging..."
18
+ FileUtils.rm_rf(box_dir)
19
+ end
20
+ end
21
+
22
+ def setup_box_dir
23
+ if File.directory?(box_dir)
24
+ error_and_exit(:box_already_exists, :box_name => @runner.name)
25
+ end
26
+
27
+ FileUtils.mkdir_p(box_dir)
28
+ end
29
+
30
+ def box_dir
31
+ @runner.directory
32
+ end
33
+
34
+ def decompress
35
+ Dir.chdir(box_dir) do
36
+ logger.info "Extracting box to #{box_dir}..."
37
+ Archive::Tar::Minitar.unpack(@runner.temp_path, box_dir)
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,36 @@
1
+ module Vagrant
2
+ module Actions
3
+ class Collection < Array
4
+ def dependencies!
5
+ each_with_index do |action, i|
6
+ action.follows.each do |klass|
7
+ unless self[0..i].klasses.include?(klass)
8
+ raise DependencyNotSatisfiedException.new("#{action.class} action must follow #{klass}")
9
+ end
10
+ end
11
+
12
+ action.precedes.each do |klass|
13
+ unless self[i..length].klasses.include?(klass)
14
+ raise DependencyNotSatisfiedException.new("#{action.class} action must precede #{klass}")
15
+ end
16
+ end
17
+ end
18
+ end
19
+
20
+ def duplicates?
21
+ klasses.uniq.size != size
22
+ end
23
+
24
+ def duplicates!
25
+ raise DuplicateActionException.new if duplicates?
26
+ end
27
+
28
+ def klasses
29
+ map { |o| o.class }
30
+ end
31
+ end
32
+
33
+ class DuplicateActionException < Exception; end
34
+ class DependencyNotSatisfiedException < Exception; end
35
+ end
36
+ end
@@ -0,0 +1,132 @@
1
+ module Vagrant
2
+ module Actions
3
+ # Base class for any class which will act as a runner
4
+ # for actions. A runner handles queueing up and executing actions,
5
+ # and executing the methods of an action in the proper order. The
6
+ # action runner also handles invoking callbacks that actions may
7
+ # request.
8
+ #
9
+ # # Executing Actions
10
+ #
11
+ # Actions can be executed by adding them and executing them all
12
+ # at once:
13
+ #
14
+ # runner = Vagrant::Actions::Runner.new
15
+ # runner.add_action(FooAction)
16
+ # runner.add_action(BarAction)
17
+ # runner.add_action(BazAction)
18
+ # runner.execute!
19
+ #
20
+ # Single actions have a shorthand to be executed:
21
+ #
22
+ # Vagrant::Actions::Runner.execute!(FooAction)
23
+ #
24
+ # Arguments may be passed into added actions by adding them after
25
+ # the action class:
26
+ #
27
+ # runner.add_action(FooAction, "many", "arguments", "may", "follow")
28
+ #
29
+ class Runner
30
+ include Vagrant::Util
31
+
32
+ class << self
33
+ # Executes a specific action, optionally passing in any arguments to that
34
+ # action. This method is shorthand to initializing a runner, adding a single
35
+ # action, and executing it.
36
+ def execute!(action_klass, *args)
37
+ runner = new
38
+ runner.add_action(action_klass, *args)
39
+ runner.execute!
40
+ end
41
+ end
42
+
43
+ # Returns an array of all the actions in queue. Because this
44
+ # will persist accross calls (calling {#actions} twice will yield
45
+ # exactly the same object), to clear or modify it, use the ruby
46
+ # array methods which act on `self`, such as `Array#clear`.
47
+ #
48
+ # @return [Array]
49
+ def actions
50
+ @actions ||= Actions::Collection.new
51
+ end
52
+
53
+ # Returns the first action instance which matches the given class.
54
+ #
55
+ # @param [Class] action_klass The action to search for in the queue
56
+ # @return [Object]
57
+ def find_action(action_klass)
58
+ actions.find { |a| a.is_a?(action_klass) }
59
+ end
60
+
61
+ # Add an action to the list of queued actions to execute. This method
62
+ # appends the given action class to the end of the queue. Any arguments
63
+ # given after the class are passed into the class constructor.
64
+ def add_action(action_klass, *args)
65
+ actions << action_klass.new(self, *args)
66
+ end
67
+
68
+ # Execute the actions in queue. This method can also optionally be used
69
+ # to execute a single action on an instance. The syntax for executing a
70
+ # single method on an instance is the same as the {execute!} class method.
71
+ def execute!(single_action=nil, *args)
72
+
73
+ if single_action
74
+ actions.clear
75
+ add_action(single_action, *args)
76
+ end
77
+
78
+ actions.duplicates!
79
+ actions.dependencies!
80
+
81
+ # Call the prepare method on each once its
82
+ # initialized, then call the execute! method
83
+ begin
84
+ [:prepare, :execute!, :cleanup].each do |method|
85
+ actions.each do |action|
86
+ action.send(method)
87
+ end
88
+ end
89
+ rescue Exception => e
90
+ # Run the rescue code to do any emergency cleanup
91
+ actions.each do |action|
92
+ action.rescue(e)
93
+ end
94
+
95
+ # If its an ActionException, error and exit the message
96
+ if e.is_a?(ActionException)
97
+ error_and_exit(e.key, e.data)
98
+ return
99
+ end
100
+
101
+ # Finally, reraise the exception
102
+ raise
103
+ end
104
+
105
+ # Clear the actions
106
+ actions.clear
107
+ end
108
+
109
+ # Invokes an "around callback" which invokes before_name and
110
+ # after_name for the given callback name, yielding a block between
111
+ # callback invokations.
112
+ def invoke_around_callback(name, *args)
113
+ invoke_callback("before_#{name}".to_sym, *args)
114
+ yield
115
+ invoke_callback("after_#{name}".to_sym, *args)
116
+ end
117
+
118
+ # Invokes a single callback. This method will go through each action
119
+ # and call the method given in the parameter `name` if the action
120
+ # responds to it.
121
+ def invoke_callback(name, *args)
122
+ # Attempt to call the method for the callback on each of the
123
+ # actions
124
+ results = []
125
+ actions.each do |action|
126
+ results << action.send(name, *args) if action.respond_to?(name)
127
+ end
128
+ results
129
+ end
130
+ end
131
+ end
132
+ end