nixme-vagrant 0.7.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (240) hide show
  1. data/.gitignore +15 -0
  2. data/.yardopts +1 -0
  3. data/CHANGELOG.md +261 -0
  4. data/Gemfile +21 -0
  5. data/LICENSE +21 -0
  6. data/README.md +61 -0
  7. data/Rakefile +22 -0
  8. data/bin/vagrant +22 -0
  9. data/config/default.rb +39 -0
  10. data/contrib/README.md +12 -0
  11. data/contrib/emacs/vagrant.el +8 -0
  12. data/contrib/vim/vagrantfile.vim +9 -0
  13. data/keys/README.md +17 -0
  14. data/keys/vagrant +27 -0
  15. data/keys/vagrant.ppk +26 -0
  16. data/keys/vagrant.pub +1 -0
  17. data/lib/vagrant.rb +42 -0
  18. data/lib/vagrant/action.rb +134 -0
  19. data/lib/vagrant/action/box.rb +11 -0
  20. data/lib/vagrant/action/box/destroy.rb +21 -0
  21. data/lib/vagrant/action/box/download.rb +72 -0
  22. data/lib/vagrant/action/box/package.rb +19 -0
  23. data/lib/vagrant/action/box/unpackage.rb +55 -0
  24. data/lib/vagrant/action/box/verify.rb +23 -0
  25. data/lib/vagrant/action/builder.rb +124 -0
  26. data/lib/vagrant/action/builtin.rb +104 -0
  27. data/lib/vagrant/action/env.rb +7 -0
  28. data/lib/vagrant/action/env/set.rb +18 -0
  29. data/lib/vagrant/action/environment.rb +50 -0
  30. data/lib/vagrant/action/general.rb +8 -0
  31. data/lib/vagrant/action/general/package.rb +109 -0
  32. data/lib/vagrant/action/general/validate.rb +19 -0
  33. data/lib/vagrant/action/vm.rb +31 -0
  34. data/lib/vagrant/action/vm/boot.rb +48 -0
  35. data/lib/vagrant/action/vm/check_box.rb +28 -0
  36. data/lib/vagrant/action/vm/check_guest_additions.rb +30 -0
  37. data/lib/vagrant/action/vm/clean_machine_folder.rb +43 -0
  38. data/lib/vagrant/action/vm/clear_forwarded_ports.rb +39 -0
  39. data/lib/vagrant/action/vm/clear_nfs_exports.rb +20 -0
  40. data/lib/vagrant/action/vm/clear_shared_folders.rb +32 -0
  41. data/lib/vagrant/action/vm/customize.rb +21 -0
  42. data/lib/vagrant/action/vm/destroy.rb +19 -0
  43. data/lib/vagrant/action/vm/destroy_unused_network_interfaces.rb +30 -0
  44. data/lib/vagrant/action/vm/discard_state.rb +22 -0
  45. data/lib/vagrant/action/vm/export.rb +52 -0
  46. data/lib/vagrant/action/vm/forward_ports.rb +133 -0
  47. data/lib/vagrant/action/vm/forward_ports_helpers.rb +28 -0
  48. data/lib/vagrant/action/vm/halt.rb +29 -0
  49. data/lib/vagrant/action/vm/host_name.rb +21 -0
  50. data/lib/vagrant/action/vm/import.rb +35 -0
  51. data/lib/vagrant/action/vm/match_mac_address.rb +21 -0
  52. data/lib/vagrant/action/vm/network.rb +139 -0
  53. data/lib/vagrant/action/vm/nfs.rb +159 -0
  54. data/lib/vagrant/action/vm/nfs_helpers.rb +11 -0
  55. data/lib/vagrant/action/vm/package.rb +23 -0
  56. data/lib/vagrant/action/vm/package_vagrantfile.rb +33 -0
  57. data/lib/vagrant/action/vm/provision.rb +41 -0
  58. data/lib/vagrant/action/vm/resume.rb +20 -0
  59. data/lib/vagrant/action/vm/share_folders.rb +70 -0
  60. data/lib/vagrant/action/vm/suspend.rb +20 -0
  61. data/lib/vagrant/action/warden.rb +79 -0
  62. data/lib/vagrant/box.rb +90 -0
  63. data/lib/vagrant/box_collection.rb +53 -0
  64. data/lib/vagrant/cli.rb +55 -0
  65. data/lib/vagrant/command.rb +25 -0
  66. data/lib/vagrant/command/base.rb +106 -0
  67. data/lib/vagrant/command/box.rb +33 -0
  68. data/lib/vagrant/command/destroy.rb +17 -0
  69. data/lib/vagrant/command/group_base.rb +107 -0
  70. data/lib/vagrant/command/halt.rb +18 -0
  71. data/lib/vagrant/command/helpers.rb +33 -0
  72. data/lib/vagrant/command/init.rb +14 -0
  73. data/lib/vagrant/command/named_base.rb +14 -0
  74. data/lib/vagrant/command/package.rb +41 -0
  75. data/lib/vagrant/command/provision.rb +17 -0
  76. data/lib/vagrant/command/reload.rb +17 -0
  77. data/lib/vagrant/command/resume.rb +17 -0
  78. data/lib/vagrant/command/ssh.rb +42 -0
  79. data/lib/vagrant/command/ssh_config.rb +26 -0
  80. data/lib/vagrant/command/status.rb +22 -0
  81. data/lib/vagrant/command/suspend.rb +17 -0
  82. data/lib/vagrant/command/up.rb +19 -0
  83. data/lib/vagrant/command/upgrade_to_060.rb +45 -0
  84. data/lib/vagrant/command/version.rb +13 -0
  85. data/lib/vagrant/config.rb +123 -0
  86. data/lib/vagrant/config/base.rb +85 -0
  87. data/lib/vagrant/config/error_recorder.rb +19 -0
  88. data/lib/vagrant/config/nfs.rb +10 -0
  89. data/lib/vagrant/config/package.rb +9 -0
  90. data/lib/vagrant/config/ssh.rb +33 -0
  91. data/lib/vagrant/config/top.rb +61 -0
  92. data/lib/vagrant/config/vagrant.rb +16 -0
  93. data/lib/vagrant/config/vm.rb +137 -0
  94. data/lib/vagrant/config/vm/provisioner.rb +56 -0
  95. data/lib/vagrant/config/vm/sub_vm.rb +17 -0
  96. data/lib/vagrant/data_store.rb +70 -0
  97. data/lib/vagrant/downloaders.rb +7 -0
  98. data/lib/vagrant/downloaders/base.rb +23 -0
  99. data/lib/vagrant/downloaders/file.rb +22 -0
  100. data/lib/vagrant/downloaders/http.rb +64 -0
  101. data/lib/vagrant/environment.rb +392 -0
  102. data/lib/vagrant/errors.rb +332 -0
  103. data/lib/vagrant/hosts.rb +7 -0
  104. data/lib/vagrant/hosts/base.rb +77 -0
  105. data/lib/vagrant/hosts/bsd.rb +52 -0
  106. data/lib/vagrant/hosts/linux.rb +47 -0
  107. data/lib/vagrant/plugin.rb +57 -0
  108. data/lib/vagrant/provisioners.rb +9 -0
  109. data/lib/vagrant/provisioners/base.rb +63 -0
  110. data/lib/vagrant/provisioners/chef.rb +130 -0
  111. data/lib/vagrant/provisioners/chef_server.rb +97 -0
  112. data/lib/vagrant/provisioners/chef_solo.rb +139 -0
  113. data/lib/vagrant/provisioners/puppet.rb +137 -0
  114. data/lib/vagrant/provisioners/puppet_server.rb +55 -0
  115. data/lib/vagrant/provisioners/shell.rb +52 -0
  116. data/lib/vagrant/ssh.rb +173 -0
  117. data/lib/vagrant/ssh/session.rb +119 -0
  118. data/lib/vagrant/systems.rb +10 -0
  119. data/lib/vagrant/systems/base.rb +87 -0
  120. data/lib/vagrant/systems/debian.rb +36 -0
  121. data/lib/vagrant/systems/freebsd.rb +84 -0
  122. data/lib/vagrant/systems/gentoo.rb +27 -0
  123. data/lib/vagrant/systems/linux.rb +79 -0
  124. data/lib/vagrant/systems/linux/config.rb +21 -0
  125. data/lib/vagrant/systems/linux/error.rb +9 -0
  126. data/lib/vagrant/systems/redhat.rb +39 -0
  127. data/lib/vagrant/systems/solaris.rb +62 -0
  128. data/lib/vagrant/test_helpers.rb +128 -0
  129. data/lib/vagrant/ui.rb +69 -0
  130. data/lib/vagrant/util.rb +13 -0
  131. data/lib/vagrant/util/busy.rb +59 -0
  132. data/lib/vagrant/util/hash_with_indifferent_access.rb +63 -0
  133. data/lib/vagrant/util/plain_logger.rb +25 -0
  134. data/lib/vagrant/util/platform.rb +58 -0
  135. data/lib/vagrant/util/resource_logger.rb +63 -0
  136. data/lib/vagrant/util/retryable.rb +25 -0
  137. data/lib/vagrant/util/stacked_proc_runner.rb +35 -0
  138. data/lib/vagrant/util/template_renderer.rb +83 -0
  139. data/lib/vagrant/version.rb +6 -0
  140. data/lib/vagrant/vm.rb +176 -0
  141. data/templates/chef_server_client.erb +18 -0
  142. data/templates/chef_solo_solo.erb +19 -0
  143. data/templates/commands/init/Vagrantfile.erb +63 -0
  144. data/templates/config/validation_failed.erb +7 -0
  145. data/templates/locales/en.yml +531 -0
  146. data/templates/network_entry_debian.erb +8 -0
  147. data/templates/network_entry_gentoo.erb +7 -0
  148. data/templates/network_entry_redhat.erb +8 -0
  149. data/templates/nfs/exports.erb +5 -0
  150. data/templates/nfs/exports_linux.erb +5 -0
  151. data/templates/package_Vagrantfile.erb +11 -0
  152. data/templates/ssh_config.erb +9 -0
  153. data/test/locales/en.yml +8 -0
  154. data/test/test_helper.rb +26 -0
  155. data/test/vagrant/action/box/destroy_test.rb +18 -0
  156. data/test/vagrant/action/box/download_test.rb +125 -0
  157. data/test/vagrant/action/box/package_test.rb +25 -0
  158. data/test/vagrant/action/box/unpackage_test.rb +84 -0
  159. data/test/vagrant/action/box/verify_test.rb +30 -0
  160. data/test/vagrant/action/builder_test.rb +203 -0
  161. data/test/vagrant/action/env/set_test.rb +24 -0
  162. data/test/vagrant/action/environment_test.rb +27 -0
  163. data/test/vagrant/action/general/package_test.rb +268 -0
  164. data/test/vagrant/action/general/validate_test.rb +31 -0
  165. data/test/vagrant/action/vm/boot_test.rb +66 -0
  166. data/test/vagrant/action/vm/check_box_test.rb +56 -0
  167. data/test/vagrant/action/vm/check_guest_additions_test.rb +9 -0
  168. data/test/vagrant/action/vm/clean_machine_folder_test.rb +84 -0
  169. data/test/vagrant/action/vm/clear_forwarded_ports_test.rb +72 -0
  170. data/test/vagrant/action/vm/clear_nfs_exports_test.rb +22 -0
  171. data/test/vagrant/action/vm/clear_shared_folders_test.rb +49 -0
  172. data/test/vagrant/action/vm/customize_test.rb +30 -0
  173. data/test/vagrant/action/vm/destroy_test.rb +25 -0
  174. data/test/vagrant/action/vm/destroy_unused_network_interfaces_test.rb +49 -0
  175. data/test/vagrant/action/vm/discard_state_test.rb +45 -0
  176. data/test/vagrant/action/vm/export_test.rb +107 -0
  177. data/test/vagrant/action/vm/forward_ports_helpers_test.rb +70 -0
  178. data/test/vagrant/action/vm/forward_ports_test.rb +194 -0
  179. data/test/vagrant/action/vm/halt_test.rb +79 -0
  180. data/test/vagrant/action/vm/host_name_test.rb +36 -0
  181. data/test/vagrant/action/vm/import_test.rb +66 -0
  182. data/test/vagrant/action/vm/match_mac_address_test.rb +36 -0
  183. data/test/vagrant/action/vm/network_test.rb +286 -0
  184. data/test/vagrant/action/vm/nfs_helpers_test.rb +26 -0
  185. data/test/vagrant/action/vm/nfs_test.rb +260 -0
  186. data/test/vagrant/action/vm/package_test.rb +25 -0
  187. data/test/vagrant/action/vm/package_vagrantfile_test.rb +46 -0
  188. data/test/vagrant/action/vm/provision_test.rb +90 -0
  189. data/test/vagrant/action/vm/resume_test.rb +35 -0
  190. data/test/vagrant/action/vm/share_folders_test.rb +139 -0
  191. data/test/vagrant/action/vm/suspend_test.rb +35 -0
  192. data/test/vagrant/action/warden_test.rb +119 -0
  193. data/test/vagrant/action_test.rb +89 -0
  194. data/test/vagrant/box_collection_test.rb +45 -0
  195. data/test/vagrant/box_test.rb +74 -0
  196. data/test/vagrant/cli_test.rb +35 -0
  197. data/test/vagrant/command/base_test.rb +23 -0
  198. data/test/vagrant/command/group_base_test.rb +15 -0
  199. data/test/vagrant/command/helpers_test.rb +88 -0
  200. data/test/vagrant/command/package_test.rb +27 -0
  201. data/test/vagrant/config/base_test.rb +52 -0
  202. data/test/vagrant/config/error_recorder_test.rb +18 -0
  203. data/test/vagrant/config/ssh_test.rb +12 -0
  204. data/test/vagrant/config/vagrant_test.rb +35 -0
  205. data/test/vagrant/config/vm/provisioner_test.rb +92 -0
  206. data/test/vagrant/config/vm_test.rb +86 -0
  207. data/test/vagrant/config_test.rb +162 -0
  208. data/test/vagrant/data_store_test.rb +77 -0
  209. data/test/vagrant/downloaders/base_test.rb +28 -0
  210. data/test/vagrant/downloaders/file_test.rb +48 -0
  211. data/test/vagrant/downloaders/http_test.rb +80 -0
  212. data/test/vagrant/environment_test.rb +508 -0
  213. data/test/vagrant/errors_test.rb +42 -0
  214. data/test/vagrant/hosts/base_test.rb +46 -0
  215. data/test/vagrant/hosts/bsd_test.rb +53 -0
  216. data/test/vagrant/hosts/linux_test.rb +54 -0
  217. data/test/vagrant/plugin_test.rb +9 -0
  218. data/test/vagrant/provisioners/base_test.rb +63 -0
  219. data/test/vagrant/provisioners/chef_server_test.rb +186 -0
  220. data/test/vagrant/provisioners/chef_solo_test.rb +219 -0
  221. data/test/vagrant/provisioners/chef_test.rb +179 -0
  222. data/test/vagrant/provisioners/puppet_server_test.rb +68 -0
  223. data/test/vagrant/provisioners/puppet_test.rb +182 -0
  224. data/test/vagrant/provisioners/shell_test.rb +68 -0
  225. data/test/vagrant/ssh/session_test.rb +40 -0
  226. data/test/vagrant/ssh_test.rb +342 -0
  227. data/test/vagrant/systems/base_test.rb +18 -0
  228. data/test/vagrant/systems/linux_test.rb +114 -0
  229. data/test/vagrant/ui_test.rb +29 -0
  230. data/test/vagrant/util/busy_test.rb +106 -0
  231. data/test/vagrant/util/hash_with_indifferent_access_test.rb +39 -0
  232. data/test/vagrant/util/plain_logger_test.rb +17 -0
  233. data/test/vagrant/util/platform_test.rb +18 -0
  234. data/test/vagrant/util/resource_logger_test.rb +78 -0
  235. data/test/vagrant/util/retryable_test.rb +38 -0
  236. data/test/vagrant/util/stacked_proc_runner_test.rb +43 -0
  237. data/test/vagrant/util/template_renderer_test.rb +145 -0
  238. data/test/vagrant/vm_test.rb +291 -0
  239. data/vagrant.gemspec +36 -0
  240. metadata +442 -0
@@ -0,0 +1,128 @@
1
+ module Vagrant
2
+ # Test helpers provided by Vagrant to allow for plugin developers
3
+ # to write automated tests for their code. This module simply provides
4
+ # methods which can be included into any test framework (`test/unit`,
5
+ # RSpec, Shoulda, etc.)
6
+ module TestHelpers
7
+ #------------------------------------------------------------
8
+ # Environment creation helpers
9
+ #------------------------------------------------------------
10
+ # Creates a "vagrant_app" directory in the test tmp folder
11
+ # which can be used for creating test Vagrant environments.
12
+ # Returns the root directory of the app. This typically doesn't need
13
+ # to be called directly unless you're setting up a custom application.
14
+ # See the examples for common use cases.
15
+ def vagrant_app(*path)
16
+ root = tmp_path.join("vagrant_app")
17
+ FileUtils.rm_rf(root)
18
+ FileUtils.mkdir_p(root)
19
+ root.join(*path)
20
+ end
21
+
22
+ # Creates a Vagrantfile with the given contents in the given
23
+ # app directory. If no app directory is specified, then a default
24
+ # Vagrant app is used.
25
+ def vagrantfile(*args)
26
+ path = args.shift.join("Vagrantfile") if Pathname === args.first
27
+ path ||= vagrant_app("Vagrantfile")
28
+
29
+ # Create this box so that it exists
30
+ vagrant_box("base")
31
+
32
+ str = args.shift || ""
33
+ File.open(path.to_s, "w") do |f|
34
+ f.puts "ENV['VAGRANT_HOME'] = '#{home_path}'"
35
+ f.puts "Vagrant::Config.run do |config|"
36
+ f.puts "config.vm.base_mac = 'foo' if !config.vm.base_mac"
37
+ f.puts "config.vm.box = 'base'"
38
+ f.puts str
39
+ f.puts "end"
40
+ end
41
+
42
+ path.parent
43
+ end
44
+
45
+ # Creates and _loads_ a Vagrant environment at the given path.
46
+ # If no path is given, then a default {#vagrantfile} is used.
47
+ def vagrant_env(*args)
48
+ path = args.shift if Pathname === args.first
49
+ path ||= vagrantfile
50
+ Vagrant::Environment.new(:cwd => path).load!
51
+ end
52
+
53
+ # Creates the folder to contain a vagrant box. This allows for
54
+ # "fake" boxes to be made with the specified name.
55
+ #
56
+ # @param [String] name
57
+ # @return [Pathname]
58
+ def vagrant_box(name)
59
+ result = boxes_path.join(name)
60
+ FileUtils.mkdir_p(result)
61
+ result
62
+ end
63
+
64
+ # Returns an instantiated downloader with a mocked tempfile
65
+ # which can be passed into it.
66
+ #
67
+ # @param [Class] klass The downloader class
68
+ # @return [Array] Returns an array of `downloader` `tempfile`
69
+ def vagrant_mock_downloader(klass)
70
+ tempfile = mock("tempfile")
71
+ tempfile.stubs(:write)
72
+
73
+ _, env = action_env
74
+ [klass.new(env), tempfile]
75
+ end
76
+
77
+ # Returns a blank app (callable) and action environment with the
78
+ # given vagrant environment. This allows for testing of middlewares.
79
+ def action_env(v_env = nil)
80
+ v_env ||= vagrant_env
81
+ app = lambda { |env| }
82
+ env = Vagrant::Action::Environment.new(v_env)
83
+ env["vagrant.test"] = true
84
+ [app, env]
85
+ end
86
+
87
+ #------------------------------------------------------------
88
+ # Path helpers
89
+ #------------------------------------------------------------
90
+ # Path to the tmp directory for the tests.
91
+ #
92
+ # @return [Pathname]
93
+ def tmp_path
94
+ result = Vagrant.source_root.join("test", "tmp")
95
+ FileUtils.mkdir_p(result)
96
+ result
97
+ end
98
+
99
+ # Path to the "home" directory for the tests
100
+ #
101
+ # @return [Pathname]
102
+ def home_path
103
+ result = tmp_path.join("home")
104
+ FileUtils.mkdir_p(result)
105
+ result
106
+ end
107
+
108
+ # Path to the boxes directory in the home directory
109
+ #
110
+ # @return [Pathname]
111
+ def boxes_path
112
+ result = home_path.join("boxes")
113
+ FileUtils.mkdir_p(result)
114
+ result
115
+ end
116
+
117
+ # Cleans all the test temp paths, which includes the boxes path,
118
+ # home path, etc. This allows for cleaning between tests.
119
+ def clean_paths
120
+ FileUtils.rm_rf(tmp_path)
121
+
122
+ # Call these methods only to rebuild the directories
123
+ tmp_path
124
+ home_path
125
+ boxes_path
126
+ end
127
+ end
128
+ end
@@ -0,0 +1,69 @@
1
+ require 'mario'
2
+
3
+ module Vagrant
4
+ # Vagrant UIs handle communication with the outside world (typically
5
+ # through a shell). They must respond to the typically logger methods
6
+ # of `warn`, `error`, `info`, and `confirm`.
7
+ class UI
8
+ attr_accessor :env
9
+
10
+ def initialize(env)
11
+ @env = env
12
+ end
13
+
14
+ [:warn, :error, :info, :confirm, :say_with_vm, :report_progress, :ask, :no?, :yes?].each do |method|
15
+ # By default these methods don't do anything. A silent UI.
16
+ define_method(method) { |*args| }
17
+ end
18
+
19
+ # A shell UI, which uses a `Thor::Shell` object to talk with
20
+ # a terminal.
21
+ class Shell < UI
22
+ def initialize(env, shell)
23
+ super(env)
24
+
25
+ @shell = shell
26
+ end
27
+
28
+ [[:warn, :yellow], [:error, :red], [:info, nil], [:confirm, :green]].each do |method, color|
29
+ class_eval <<-CODE
30
+ def #{method}(message, opts=nil)
31
+ @shell.say("\#{line_reset}\#{format_message(message, opts)}", #{color.inspect})
32
+ end
33
+ CODE
34
+ end
35
+
36
+ [:ask, :no?, :yes?].each do |method|
37
+ class_eval <<-CODE
38
+ def #{method}(message, opts=nil)
39
+ opts ||= {}
40
+ @shell.send(#{method.inspect}, format_message(message, opts), opts[:color])
41
+ end
42
+ CODE
43
+ end
44
+
45
+ def report_progress(progress, total, show_parts=true)
46
+ percent = (progress.to_f / total.to_f) * 100
47
+ line = "Progress: #{percent.to_i}%"
48
+ line << " (#{progress} / #{total})" if show_parts
49
+ line = "#{line_reset}#{line}"
50
+
51
+ @shell.say(line, nil, false)
52
+ end
53
+
54
+ protected
55
+
56
+ def format_message(message, opts=nil)
57
+ opts = { :prefix => true }.merge(opts || {})
58
+ message = "[#{env.resource}] #{message}" if opts[:prefix]
59
+ message
60
+ end
61
+
62
+ def line_reset
63
+ reset = "\r"
64
+ reset += "\e[0K" unless Mario::Platform.windows?
65
+ reset
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,13 @@
1
+ module Vagrant
2
+ module Util
3
+ autoload :Busy, 'vagrant/util/busy'
4
+ autoload :GlobLoader, 'vagrant/util/glob_loader'
5
+ autoload :HashWithIndifferentAccess, 'vagrant/util/hash_with_indifferent_access'
6
+ autoload :PlainLogger, 'vagrant/util/plain_logger'
7
+ autoload :Platform, 'vagrant/util/platform'
8
+ autoload :ResourceLogger, 'vagrant/util/resource_logger'
9
+ autoload :Retryable, 'vagrant/util/retryable'
10
+ autoload :StackedProcRunner, 'vagrant/util/stacked_proc_runner'
11
+ autoload :TemplateRenderer, 'vagrant/util/template_renderer'
12
+ end
13
+ end
@@ -0,0 +1,59 @@
1
+ module Vagrant
2
+ module Util
3
+ # Utility class which allows blocks of code to be marked as "busy"
4
+ # with a specified interrupt handler. During busy areas of code, it
5
+ # is often undesirable for SIGINTs to immediately kill the application.
6
+ # This class is a helper to cleanly register callbacks to handle this
7
+ # situation.
8
+ class Busy
9
+ @@registered = []
10
+ @@mutex = Mutex.new
11
+
12
+ class << self
13
+ # Mark a given block of code as a "busy" block of code, which will
14
+ # register a SIGINT handler for the duration of the block. When a
15
+ # SIGINT occurs, the `sig_callback` proc will be called. It is up
16
+ # to the callback to behave properly and exit the application.
17
+ def busy(sig_callback)
18
+ register(sig_callback)
19
+ yield
20
+ ensure
21
+ unregister(sig_callback)
22
+ end
23
+
24
+ # Registers a SIGINT handler. This typically is called from {busy}.
25
+ # Callbacks are only registered once, so calling this multiple times
26
+ # with the same callback has no consequence.
27
+ def register(sig_callback)
28
+ @@mutex.synchronize do
29
+ registered << sig_callback
30
+ registered.uniq!
31
+
32
+ # Register the handler if this is our first callback.
33
+ Signal.trap("INT") { fire_callbacks } if registered.length == 1
34
+ end
35
+ end
36
+
37
+ # Unregisters a SIGINT handler.
38
+ def unregister(sig_callback)
39
+ @@mutex.synchronize do
40
+ registered.delete(sig_callback)
41
+
42
+ # Remove the signal trap if no more registered callbacks exist
43
+ Signal.trap("INT", "DEFAULT") if registered.empty?
44
+ end
45
+ end
46
+
47
+ # Fires all the registered callbacks.
48
+ def fire_callbacks
49
+ registered.each { |r| r.call }
50
+ end
51
+
52
+ # Helper method to get access to the class variable. This is mostly
53
+ # exposed for tests. This shouldn't be mucked with directly, since it's
54
+ # structure may change at any time.
55
+ def registered; @@registered; end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,63 @@
1
+ module Vagrant
2
+ module Util
3
+ # A hash with indifferent access. Mostly taken from Thor/Rails (thanks).
4
+ # Normally I'm not a fan of using an indifferent access hash since Symbols
5
+ # are basically memory leaks in Ruby, but since Vagrant is typically a quick
6
+ # one-off binary run and it doesn't use too many hash keys where this is
7
+ # used, the effect should be minimal.
8
+ #
9
+ # hash[:foo] #=> 'bar'
10
+ # hash['foo'] #=> 'bar'
11
+ #
12
+ class HashWithIndifferentAccess < ::Hash
13
+ def initialize(hash={}, &block)
14
+ super(&block)
15
+
16
+ hash.each do |key, value|
17
+ self[convert_key(key)] = value
18
+ end
19
+ end
20
+
21
+ def [](key)
22
+ super(convert_key(key))
23
+ end
24
+
25
+ def []=(key, value)
26
+ super(convert_key(key), value)
27
+ end
28
+
29
+ def delete(key)
30
+ super(convert_key(key))
31
+ end
32
+
33
+ def values_at(*indices)
34
+ indices.collect { |key| self[convert_key(key)] }
35
+ end
36
+
37
+ def merge(other)
38
+ dup.merge!(other)
39
+ end
40
+
41
+ def merge!(other)
42
+ other.each do |key, value|
43
+ self[convert_key(key)] = value
44
+ end
45
+ self
46
+ end
47
+
48
+ def key?(key)
49
+ super(convert_key(key))
50
+ end
51
+
52
+ alias_method :include?, :key?
53
+ alias_method :has_key?, :key?
54
+ alias_method :member?, :key?
55
+
56
+ protected
57
+
58
+ def convert_key(key)
59
+ key.is_a?(Symbol) ? key.to_s : key
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,25 @@
1
+ require 'logger'
2
+
3
+ module Vagrant
4
+ module Util
5
+ # Subclass of the standard library logger which has no format on
6
+ # its own. The message sent to the logger is outputted as-is.
7
+ class PlainLogger < ::Logger
8
+ # This is the method which is called for all debug, info, error,
9
+ # etc. methods by the logger. This is overriden to verify that
10
+ # the output is always flushed.
11
+ #
12
+ # Logger by default syncs all log devices but this just verifies
13
+ # it is truly flushed.
14
+ def add(*args)
15
+ super
16
+ @logdev.dev.flush if @logdev
17
+ end
18
+
19
+ def format_message(level, time, progname, msg)
20
+ # We do no formatting, its up to the user
21
+ "#{msg}\n"
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,58 @@
1
+ require 'rbconfig'
2
+ require 'mario'
3
+
4
+ module Vagrant
5
+ module Util
6
+ # This class just contains some platform checking code.
7
+ class Platform
8
+ class << self
9
+ def tiger?
10
+ platform.include?("darwin8")
11
+ end
12
+
13
+ def leopard?
14
+ platform.include?("darwin9")
15
+ end
16
+
17
+ [:darwin, :bsd, :linux].each do |type|
18
+ define_method("#{type}?") do
19
+ platform.include?(type.to_s)
20
+ end
21
+ end
22
+
23
+ def windows?
24
+ %W[mingw mswin].each do |text|
25
+ return true if platform.include?(text)
26
+ end
27
+
28
+ false
29
+ end
30
+
31
+ # Returns boolean noting whether this is a 64-bit CPU. This
32
+ # is not 100% accurate and there could easily be false negatives.
33
+ #
34
+ # @return [Boolean]
35
+ def bit64?
36
+ ["x86_64", "amd64"].include?(RbConfig::CONFIG["host_cpu"])
37
+ end
38
+
39
+ # Returns boolean noting whether this is a 32-bit CPU. This
40
+ # can easily throw false positives since it relies on {#bit64?}.
41
+ #
42
+ # @return [Boolean]
43
+ def bit32?
44
+ !bit64?
45
+ end
46
+
47
+ def tar_file_options
48
+ # create, write only, fail if the file exists, binary if windows
49
+ File::WRONLY|File::EXCL|File::CREAT|(Mario::Platform.windows? ? File::BINARY : 0)
50
+ end
51
+
52
+ def platform
53
+ RbConfig::CONFIG["host_os"].downcase
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,63 @@
1
+ require 'thread'
2
+
3
+ module Vagrant
4
+ module Util
5
+ # Represents a logger for a specific resource within Vagrant. Each
6
+ # logger should be initialized and set to represent a single
7
+ # resource. Each logged message will then appear in the following
8
+ # format:
9
+ #
10
+ # [resource] message
11
+ #
12
+ # This class is thread safe. The backing class which actually does
13
+ # all the logging IO is protected.
14
+ class ResourceLogger
15
+ @@singleton_logger = nil
16
+ @@writer_lock = Mutex.new
17
+
18
+ # The resource which this logger represents.
19
+ attr_reader :resource
20
+
21
+ # The environment that this logger is part of
22
+ attr_reader :env
23
+
24
+ # The backing logger which actually handles the IO. This logger
25
+ # should be a subclass of the standard library Logger, in general.
26
+ # IMPORTANT: This logger must be thread-safe.
27
+ attr_reader :logger
28
+
29
+ class << self
30
+ # Returns a singleton logger. If one has not yet be
31
+ # instantiated, then the given environment will be used to
32
+ # create a new logger.
33
+ def singleton_logger(env=nil)
34
+ return PlainLogger.new(nil) if !env.loaded?
35
+
36
+ @@singleton_logger ||= begin
37
+ file = env.log_path.join("#{Time.now.to_i}.log")
38
+ PlainLogger.new(file)
39
+ end
40
+ end
41
+
42
+ # Resets the singleton logger (only used for testing).
43
+ def reset_singleton_logger!
44
+ @@singleton_logger = nil
45
+ end
46
+ end
47
+
48
+ def initialize(resource, env)
49
+ @resource = resource
50
+ @env = env
51
+ @logger = self.class.singleton_logger(env)
52
+ end
53
+
54
+ [:debug, :info, :error, :fatal].each do |method|
55
+ define_method(method) do |message|
56
+ @@writer_lock.synchronize do
57
+ logger.send(method, "[#{resource}] #{message}")
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end