vagrant-nitrousio 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. checksums.yaml +7 -0
  2. data/.env-example +4 -0
  3. data/.gitignore +17 -0
  4. data/.rspec +1 -0
  5. data/Gemfile +7 -0
  6. data/LICENSE +10 -0
  7. data/README.md +97 -0
  8. data/Rakefile +26 -0
  9. data/example_box/README.md +13 -0
  10. data/example_box/metadata.json +3 -0
  11. data/lib/vagrant-nitrousio/action/add_pub_key.rb +44 -0
  12. data/lib/vagrant-nitrousio/action/connect_nitrousio.rb +31 -0
  13. data/lib/vagrant-nitrousio/action/create.rb +79 -0
  14. data/lib/vagrant-nitrousio/action/is_created.rb +18 -0
  15. data/lib/vagrant-nitrousio/action/is_running.rb +18 -0
  16. data/lib/vagrant-nitrousio/action/is_stopped.rb +18 -0
  17. data/lib/vagrant-nitrousio/action/is_terminated.rb +18 -0
  18. data/lib/vagrant-nitrousio/action/message_already_created.rb +16 -0
  19. data/lib/vagrant-nitrousio/action/message_already_terminated.rb +16 -0
  20. data/lib/vagrant-nitrousio/action/message_cannot_halt.rb +16 -0
  21. data/lib/vagrant-nitrousio/action/message_cannot_terminate.rb +16 -0
  22. data/lib/vagrant-nitrousio/action/message_not_created.rb +16 -0
  23. data/lib/vagrant-nitrousio/action/message_not_running.rb +16 -0
  24. data/lib/vagrant-nitrousio/action/message_provisioning_not_yet_supported.rb +16 -0
  25. data/lib/vagrant-nitrousio/action/read_ssh_info.rb +60 -0
  26. data/lib/vagrant-nitrousio/action/read_state.rb +36 -0
  27. data/lib/vagrant-nitrousio/action/remove_machine_id.rb +19 -0
  28. data/lib/vagrant-nitrousio/action/start.rb +31 -0
  29. data/lib/vagrant-nitrousio/action/stop.rb +31 -0
  30. data/lib/vagrant-nitrousio/action/sync_folders.rb +57 -0
  31. data/lib/vagrant-nitrousio/action/terminate.rb +34 -0
  32. data/lib/vagrant-nitrousio/action/timed_provision.rb +21 -0
  33. data/lib/vagrant-nitrousio/action/warn_networks.rb +19 -0
  34. data/lib/vagrant-nitrousio/action.rb +166 -0
  35. data/lib/vagrant-nitrousio/client.rb +57 -0
  36. data/lib/vagrant-nitrousio/config.rb +94 -0
  37. data/lib/vagrant-nitrousio/errors.rb +55 -0
  38. data/lib/vagrant-nitrousio/plugin.rb +73 -0
  39. data/lib/vagrant-nitrousio/provider.rb +50 -0
  40. data/lib/vagrant-nitrousio/util/env.rb +12 -0
  41. data/lib/vagrant-nitrousio/util/timer.rb +17 -0
  42. data/lib/vagrant-nitrousio/version.rb +5 -0
  43. data/lib/vagrant-nitrousio.rb +18 -0
  44. data/locales/en.yml +139 -0
  45. data/nitrousio.box +0 -0
  46. data/sample/cookbooks/test/recipes/default.rb +1 -0
  47. data/sample/provision.sh +3 -0
  48. data/sample/puppet/manifests/site.pp +2 -0
  49. data/sample/puppet/modules/baseconfig/files/bashrc +107 -0
  50. data/sample/puppet/modules/baseconfig/manifests/init.pp +21 -0
  51. data/sample/test/hello.txt +1 -0
  52. data/spec/vagrant-nitrousio/action/create_spec.rb +36 -0
  53. data/spec/vagrant-nitrousio/action/read_ssh_info_spec.rb +59 -0
  54. data/spec/vagrant-nitrousio/action/read_state_spec.rb +46 -0
  55. data/spec/vagrant-nitrousio/action/start_spec.rb +32 -0
  56. data/spec/vagrant-nitrousio/action/stop_spec.rb +20 -0
  57. data/spec/vagrant-nitrousio/action/terminate_spec.rb +20 -0
  58. data/spec/vagrant-nitrousio/client_spec.rb +209 -0
  59. data/spec/vagrant-nitrousio/config_spec.rb +48 -0
  60. data/vagrant-nitrousio.gemspec +59 -0
  61. metadata +193 -0
@@ -0,0 +1,31 @@
1
+ require 'log4r'
2
+ require 'vagrant/util/retryable'
3
+ require 'vagrant-nitrousio/errors'
4
+
5
+ module VagrantPlugins
6
+ module NitrousIO
7
+ module Action
8
+ # This runs the configured instance.
9
+ class Start
10
+ include Vagrant::Util::Retryable
11
+
12
+ def initialize(app, env)
13
+ @app = app
14
+ @logger = Log4r::Logger.new('vagrant_nitrousio::action::start_instance')
15
+ end
16
+
17
+ def call(env)
18
+ nitrousio = env[:nitrousio]
19
+ machine = env[:machine]
20
+ privileged = machine.provider_config.privileged
21
+
22
+ env[:ui].info(I18n.t('vagrant_nitrousio.starting_box'))
23
+ nitrousio.request(:post, "/containers/#{machine.id}/start", body: { privileged: privileged })
24
+ env[:ui].info(I18n.t('vagrant_nitrousio.box_ready'))
25
+
26
+ @app.call(env)
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,31 @@
1
+ require 'log4r'
2
+ require 'vagrant/util/retryable'
3
+ require 'vagrant-nitrousio/errors'
4
+
5
+ module VagrantPlugins
6
+ module NitrousIO
7
+ module Action
8
+ # This runs the configured instance.
9
+ class Stop
10
+ include Vagrant::Util::Retryable
11
+
12
+ def initialize(app, env)
13
+ @app = app
14
+ @logger = Log4r::Logger.new('vagrant_nitrousio::action::stop_instance')
15
+ end
16
+
17
+ def call(env)
18
+ nitrousio = env[:nitrousio]
19
+ machine = env[:machine]
20
+
21
+ env[:ui].info(I18n.t('vagrant_nitrousio.stopping_box'))
22
+ nitrousio.request(:post, "/containers/#{machine.id}/stop")
23
+
24
+ env[:ui].info(I18n.t('vagrant_nitrousio.stopped'))
25
+
26
+ @app.call(env)
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,57 @@
1
+ require 'log4r'
2
+
3
+ require 'vagrant/util/subprocess'
4
+
5
+ module VagrantPlugins
6
+ module NitrousIO
7
+ module Action
8
+ # This middleware uses `rsync` to sync the folders over to the
9
+ # Nitrous.IO box.
10
+ class SyncFolders
11
+ def initialize(app, env)
12
+ @app = app
13
+ @logger = Log4r::Logger.new("vagrant_nitrousio::action::sync_folders")
14
+ end
15
+
16
+ def call(env)
17
+ @app.call(env)
18
+
19
+ ssh_info = env[:machine].ssh_info
20
+
21
+ env[:machine].config.vm.synced_folders.each do |id, data|
22
+ hostpath = File.expand_path(data[:hostpath], env[:root_path])
23
+ guestpath = data[:guestpath]
24
+
25
+ # Make sure there is a trailing slash on the host path to
26
+ # avoid creating an additional directory with rsync
27
+ hostpath = "#{hostpath}/" if hostpath !~ /\/$/
28
+
29
+ env[:ui].info(I18n.t("vagrant_nitrousio.rsync_folder",
30
+ :hostpath => hostpath,
31
+ :guestpath => guestpath))
32
+
33
+ # Create the guest path
34
+ env[:machine].communicate.sudo("mkdir -p '#{guestpath}'")
35
+ env[:machine].communicate.sudo(
36
+ "chown #{ssh_info[:username]} '#{guestpath}'")
37
+
38
+ # Rsync over to the guest path using the SSH info
39
+ command = [
40
+ "rsync", "--verbose", "--archive", "-z",
41
+ "-e", "ssh -p #{ssh_info[:port]} -i '#{ssh_info[:private_key_path]}' -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=FATAL",
42
+ hostpath,
43
+ "#{ssh_info[:username]}@#{ssh_info[:host]}:#{guestpath}"]
44
+
45
+ r = Vagrant::Util::Subprocess.execute(*command)
46
+ if r.exit_code != 0
47
+ raise Errors::RsyncError,
48
+ :guestpath => guestpath,
49
+ :hostpath => hostpath,
50
+ :stderr => r.stderr
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,34 @@
1
+ require 'log4r'
2
+ require 'vagrant/util/retryable'
3
+ require 'vagrant-nitrousio/errors'
4
+
5
+ module VagrantPlugins
6
+ module NitrousIO
7
+ module Action
8
+ # This terminates the running instance.
9
+ class Terminate
10
+ include Vagrant::Util::Retryable
11
+
12
+ def initialize(app, env)
13
+ @app = app
14
+ @logger = Log4r::Logger.new('vagrant_nitrousio::action::terminate_instance')
15
+ end
16
+
17
+ def call(env)
18
+ nitrousio = env[:nitrousio]
19
+ machine = env[:machine]
20
+
21
+ # Destroy the server and remove the tracking ID
22
+ env[:ui].info(I18n.t('vagrant_nitrousio.terminating_box'))
23
+ nitrousio.request(:delete, "/containers/#{machine.id}")
24
+
25
+ machine.id = nil
26
+
27
+ env[:ui].info(I18n.t('vagrant_nitrousio.terminated'))
28
+
29
+ @app.call(env)
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,21 @@
1
+ require 'vagrant-nitrousio/util/timer'
2
+
3
+ module VagrantPlugins
4
+ module NitrousIO
5
+ module Action
6
+ # This is the same as the builtin provision except it times the
7
+ # provisioner runs.
8
+ class TimedProvision < Vagrant::Action::Builtin::Provision
9
+ def run_provisioner(env, prov)
10
+ timer = Util::Timer.time do
11
+ super
12
+ end
13
+
14
+ env[:metrics] ||= {}
15
+ env[:metrics]["provisioner_times"] ||= []
16
+ env[:metrics]["provisioner_times"] << [prov.class.to_s, timer]
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,19 @@
1
+ module VagrantPlugins
2
+ module NitrousIO
3
+ module Action
4
+ class WarnNetworks
5
+ def initialize(app, env)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ if env[:machine].config.vm.networks.length > 0
11
+ env[:ui].warn(I18n.t('vagrant_nitrousio.warn_networks'))
12
+ end
13
+
14
+ @app.call(env)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,166 @@
1
+ require 'pathname'
2
+
3
+ require 'vagrant/action/builder'
4
+
5
+ module VagrantPlugins
6
+ module NitrousIO
7
+ module Action
8
+ # Include the built-in modules so we can use them as top-level things.
9
+ include Vagrant::Action::Builtin
10
+
11
+ # This action is called to halt the remote machine.
12
+ def self.action_halt
13
+ Vagrant::Action::Builder.new.tap do |b|
14
+ b.use ConfigValidate
15
+ b.use ConnectNitrousIO
16
+ b.use Call, IsRunning do |env, b2|
17
+ if !env[:result]
18
+ b2.use MessageNotRunning
19
+ next
20
+ end
21
+
22
+ b2.use Stop
23
+ end
24
+ end
25
+ end
26
+
27
+ # This action is called to terminate the remote machine.
28
+ def self.action_destroy
29
+ Vagrant::Action::Builder.new.tap do |b|
30
+ b.use ConfigValidate
31
+ b.use ConnectNitrousIO
32
+ b.use Call, IsTerminated do |env, b2|
33
+ if env[:result]
34
+ b2.use MessageAlreadyTerminated
35
+ b2.use RemoveMachineId
36
+ next
37
+ end
38
+
39
+ b2.use Call, IsRunning do |env, b3|
40
+ if env[:result]
41
+ b3.use Stop
42
+ end
43
+
44
+ b3.use Call, IsStopped do |env, b4|
45
+ if !env[:result]
46
+ b4.use MessageCannotTerminate
47
+ next
48
+ end
49
+ b4.use Terminate
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+
56
+ # This action is called when `vagrant provision` is called.
57
+ def self.action_provision
58
+ Vagrant::Action::Builder.new.tap do |b|
59
+ b.use ConfigValidate
60
+ b.use Call, IsCreated do |env, b2|
61
+ if !env[:result]
62
+ b2.use MessageNotCreated
63
+ next
64
+ end
65
+
66
+ b2.use Provision
67
+ b2.use SyncFolders
68
+ end
69
+ end
70
+ end
71
+
72
+ # This action is called to read the SSH info of the machine. The
73
+ # resulting state is expected to be put into the `:machine_ssh_info`
74
+ # key.
75
+ def self.action_read_ssh_info
76
+ Vagrant::Action::Builder.new.tap do |b|
77
+ b.use ConfigValidate
78
+ b.use ConnectNitrousIO
79
+ b.use ReadSSHInfo
80
+ end
81
+ end
82
+
83
+ # This action is called to read the state of the machine. The
84
+ # resulting state is expected to be put into the `:machine_state_id`
85
+ # key.
86
+ def self.action_read_state
87
+ Vagrant::Action::Builder.new.tap do |b|
88
+ b.use ConfigValidate
89
+ b.use ConnectNitrousIO
90
+ b.use ReadState
91
+ end
92
+ end
93
+
94
+ # This action is called to SSH into the machine.
95
+ def self.action_ssh
96
+ Vagrant::Action::Builder.new.tap do |b|
97
+ b.use ConfigValidate
98
+ b.use Call, IsCreated do |env, b2|
99
+ if !env[:result]
100
+ b2.use MessageNotCreated
101
+ next
102
+ end
103
+
104
+ b2.use Call, IsRunning do |env, b3|
105
+ if !env[:result]
106
+ b3.use MessageNotRunning
107
+ next
108
+ end
109
+
110
+ b3.use SSHExec
111
+ end
112
+ end
113
+ end
114
+ end
115
+
116
+ # This action is called to bring the box up from nothing.
117
+ def self.action_up
118
+ Vagrant::Action::Builder.new.tap do |b|
119
+ b.use ConfigValidate
120
+ b.use ConnectNitrousIO
121
+
122
+ b.use Call, IsCreated do |env, b2|
123
+ if env[:result]
124
+ b2.use MessageAlreadyCreated
125
+ b2.use Call, IsStopped do |env, b3|
126
+ if env[:result]
127
+ b3.use Start
128
+ end
129
+ end
130
+ next
131
+ end
132
+
133
+ b2.use Create
134
+ b2.use AddPubKey
135
+ b2.use action_provision
136
+ end
137
+ end
138
+ end
139
+
140
+ # The autoload farm
141
+ action_root = Pathname.new(File.expand_path('../action', __FILE__))
142
+ autoload :AddPubKey, action_root.join('add_pub_key')
143
+ autoload :ConnectNitrousIO, action_root.join('connect_nitrousio')
144
+ autoload :Create, action_root.join('create')
145
+ autoload :IsCreated, action_root.join('is_created')
146
+ autoload :IsRunning, action_root.join('is_running')
147
+ autoload :IsStopped, action_root.join('is_stopped')
148
+ autoload :IsTerminated, action_root.join('is_terminated')
149
+ autoload :MessageAlreadyCreated, action_root.join('message_already_created')
150
+ autoload :MessageAlreadyTerminated, action_root.join('message_already_terminated')
151
+ autoload :MessageCannotTerminate, action_root.join('message_cannot_terminate')
152
+ autoload :MessageNotCreated, action_root.join('message_not_created')
153
+ autoload :MessageNotRunning, action_root.join('message_not_running')
154
+ autoload :MessageProvisioningNotYetSupported, action_root.join('message_provisioning_not_yet_supported')
155
+ autoload :ReadSSHInfo, action_root.join('read_ssh_info')
156
+ autoload :ReadState, action_root.join('read_state')
157
+ autoload :RemoveMachineId, action_root.join('remove_machine_id')
158
+ autoload :Start, action_root.join('start')
159
+ autoload :Stop, action_root.join('stop')
160
+ autoload :SyncFolders, action_root.join('sync_folders')
161
+ autoload :Terminate, action_root.join("terminate")
162
+ autoload :TimedProvision, action_root.join('timed_provision')
163
+ autoload :WarnNetworks, action_root.join('warn_networks')
164
+ end
165
+ end
166
+ end
@@ -0,0 +1,57 @@
1
+ require 'vagrant-nitrousio/util/env'
2
+ require 'vagrant-nitrousio/errors'
3
+ require 'vagrant-nitrousio/version'
4
+ require 'oauth2'
5
+
6
+ module VagrantPlugins
7
+ module NitrousIO
8
+ class Client
9
+ HOST = Util::Env.read_with_default('VAGRANT_NITROUSIO_HOST', 'https://pro.nitrous.io')
10
+ API_PATH_PREFIX = '/api/v0'
11
+ USER_AGENT = "Vagrant-NitrousIO/#{VERSION} (Vagrant #{Vagrant::VERSION}; #{RUBY_DESCRIPTION})"
12
+
13
+ attr_reader :connection, :access
14
+
15
+ def initialize
16
+ @connection = OAuth2::Client.new('', '', site: HOST, token_url: "#{API_PATH_PREFIX}/oauth/token")
17
+ end
18
+
19
+ def authorize(username, password)
20
+ @access = @connection.password.get_token(username, password)
21
+ rescue => e
22
+ if e.code == "invalid_grant"
23
+ raise Errors::AuthenticationFailedError, description: e.description
24
+ end
25
+ raise Errors::APIError, message: e.description
26
+ end
27
+
28
+ def request(verb, path, options={})
29
+ options = { parse: :json }.merge options
30
+ options[:headers] ||= {}
31
+ options[:headers]['User-Agent'] = USER_AGENT
32
+
33
+ @access.request verb.to_sym, "#{API_PATH_PREFIX}#{path}", options
34
+ rescue => e
35
+ case e.code
36
+ when "host_not_found"
37
+ raise Errors::HostNotFoundError
38
+ when "container_not_found"
39
+ raise Errors::ContainerNotFoundError
40
+ when "image_not_found"
41
+ raise Errors::ImageNotFoundError
42
+ else
43
+ raise Errors::APIError, message: e.description
44
+ end
45
+ end
46
+
47
+ def fetch_box_state(box_id)
48
+ response = request(:get, "/containers/#{box_id}")
49
+ response.parsed['container']['state'].to_sym
50
+ rescue Errors::ContainerNotFoundError => e
51
+ return nil
52
+ rescue => e
53
+ raise e
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,94 @@
1
+ require 'vagrant'
2
+
3
+ module VagrantPlugins
4
+ module NitrousIO
5
+ class Config < Vagrant.plugin('2', :config)
6
+ # The username for accessing Nitrous.IO API.
7
+ #
8
+ # @return [String]
9
+ attr_accessor :username
10
+
11
+ # The password for accessing Nitrous.IO API.
12
+ #
13
+ # @return [String]
14
+ attr_accessor :password
15
+
16
+ # The slug of the Nitrous.IO host yout want to provision the box.
17
+ #
18
+ # @return [String]
19
+ attr_accessor :host
20
+
21
+ # The image repository to be used as the base when creating the box.
22
+ #
23
+ # @return [String]
24
+ attr_accessor :image
25
+
26
+ # Whether the container is privileged or not
27
+ #
28
+ # @return [Boolean]
29
+ attr_accessor :privileged
30
+
31
+ # The path to the SSH private key to use with the Nitrous.IO box.
32
+ # This overrides the `config.ssh.private_key_path` variable.
33
+ #
34
+ # @return [String]
35
+ attr_accessor :ssh_private_key_path
36
+
37
+ def initialize
38
+ @username = UNSET_VALUE
39
+ @password = UNSET_VALUE
40
+ @host = UNSET_VALUE
41
+ @image = UNSET_VALUE
42
+ @ssh_private_key_path = UNSET_VALUE
43
+ @privileged = UNSET_VALUE
44
+
45
+ # Internal state (prefix with __ so they aren't automatically
46
+ # merged)
47
+ @__finalized = false
48
+ end
49
+
50
+ #-------------------------------------------------------------------
51
+ # Internal methods.
52
+ #-------------------------------------------------------------------
53
+
54
+ def finalize!
55
+ # The username default to nil
56
+ @username = nil if @username == UNSET_VALUE
57
+
58
+ # The password default to nil
59
+ @password = nil if @password == UNSET_VALUE
60
+
61
+ # The host default to nil
62
+ @host = nil if @host == UNSET_VALUE
63
+
64
+ # The image default to nitrousio/vagrant-base
65
+ @image = 'nitrousio/vagrant:latest' if @image == UNSET_VALUE
66
+
67
+ # The SSH values by default are nil, and the top-level config
68
+ # `config.ssh` values are used.
69
+ @ssh_private_key_path = nil if @ssh_private_key_path == UNSET_VALUE
70
+
71
+ # The privileged flag is set to false by default
72
+ @privileged = false if @privileged == UNSET_VALUE
73
+
74
+ # Mark that we finalized
75
+ @__finalized = true
76
+ end
77
+
78
+ def validate(machine)
79
+ errors = []
80
+
81
+ errors << I18n.t('vagrant_nitrousio.config.username_required') if @username.nil?
82
+ errors << I18n.t('vagrant_nitrousio.config.password_required') if @password.nil?
83
+ errors << I18n.t('vagrant_nitrousio.config.host_required') if @host.nil?
84
+ errors << I18n.t('vagrant_nitrousio.config.ssh_private_key_path_required') if @ssh_private_key_path.nil?
85
+
86
+ if @ssh_private_key_path && !File.file?(File.expand_path(@ssh_private_key_path, machine.env.root_path))
87
+ errors << I18n.t('vagrant_nitrousio.config.private_key_missing')
88
+ end
89
+
90
+ { 'Nitrous.IO Provider' => errors }
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,55 @@
1
+ require 'vagrant'
2
+
3
+ module VagrantPlugins
4
+ module NitrousIO
5
+ module Errors
6
+ class VagrantNitrousIOError < Vagrant::Errors::VagrantError
7
+ error_namespace('vagrant_nitrousio.errors')
8
+ end
9
+
10
+ class APIError < VagrantNitrousIOError
11
+ error_key(:api_error)
12
+ end
13
+
14
+ class AuthenticationFailedError < VagrantNitrousIOError
15
+ error_key(:authentication_failed_error)
16
+ end
17
+
18
+ class HostNotFoundError < VagrantNitrousIOError
19
+ error_key(:host_not_found_error)
20
+ end
21
+
22
+ class ContainerNotFoundError < VagrantNitrousIOError
23
+ error_key(:container_not_found_error)
24
+ end
25
+
26
+ class ImageNotFoundError < VagrantNitrousIOError
27
+ error_key(:image_not_found_error)
28
+ end
29
+
30
+ class PublicKeyError < VagrantNitrousIOError
31
+ error_key(:public_key_error)
32
+ end
33
+
34
+ class BoxNotYetStartedError < VagrantNitrousIOError
35
+ error_key(:box_not_yet_started_error)
36
+ end
37
+
38
+ class BoxNotYetStoppedError < VagrantNitrousIOError
39
+ error_key(:box_not_yet_stopped_error)
40
+ end
41
+
42
+ class BoxNotYetTerminatedError < VagrantNitrousIOError
43
+ error_key(:box_not_yet_terminated_error)
44
+ end
45
+
46
+ class RsyncError < VagrantNitrousIOError
47
+ error_key(:rsync_error)
48
+ end
49
+
50
+ class TimeoutError < VagrantNitrousIOError
51
+ error_key(:timeout_error)
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,73 @@
1
+ begin
2
+ require 'vagrant'
3
+ rescue LoadError
4
+ raise 'The Vagrant Nitrous.IO plugin must be run within Vagrant.'
5
+ end
6
+
7
+ # This is a sanity check to make sure no one is attempting to install
8
+ # this into an early Vagrant version.
9
+ if Vagrant::VERSION < '1.6.0'
10
+ raise 'The Vagrant Nitrous.IO plugin is only compatible with Vagrant 1.6+'
11
+ end
12
+
13
+ module VagrantPlugins
14
+ module NitrousIO
15
+ class Plugin < Vagrant.plugin('2')
16
+ name 'Nitrous.IO'
17
+ description <<-DESC
18
+ This plugin installs a provider that allows Vagrant to manage
19
+ boxes in Nitrous.IO.
20
+ DESC
21
+
22
+ config(:nitrousio, :provider) do
23
+ require_relative 'config'
24
+ Config
25
+ end
26
+
27
+ provider(:nitrousio, parallel: true) do
28
+ # Setup logging and i18n
29
+ setup_logging
30
+ setup_i18n
31
+
32
+ # Return the provider
33
+ require_relative 'provider'
34
+ Provider
35
+ end
36
+
37
+ # This initializes the internationalization strings.
38
+ def self.setup_i18n
39
+ I18n.load_path << File.expand_path('locales/en.yml', NitrousIO.source_root)
40
+ I18n.reload!
41
+ end
42
+
43
+ # This sets up our log level to be whatever VAGRANT_LOG is.
44
+ def self.setup_logging
45
+ require 'log4r'
46
+
47
+ level = nil
48
+ begin
49
+ level = Log4r.const_get(ENV['VAGRANT_LOG'].upcase)
50
+ rescue NameError
51
+ # This means that the logging constant wasn't found,
52
+ # which is fine. We just keep `level` as `nil`. But
53
+ # we tell the user.
54
+ level = nil
55
+ end
56
+
57
+ # Some constants, such as "true" resolve to booleans, so the
58
+ # above error checking doesn't catch it. This will check to make
59
+ # sure that the log level is an integer, as Log4r requires.
60
+ level = nil if !level.is_a?(Integer)
61
+
62
+ # Set the logging level on all "vagrant" namespaced
63
+ # logs as long as we have a valid level.
64
+ if level
65
+ logger = Log4r::Logger.new('vagrant_nitrousio')
66
+ logger.outputters = Log4r::Outputter.stderr
67
+ logger.level = level
68
+ logger = nil
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,50 @@
1
+ require 'log4r'
2
+ require 'vagrant'
3
+
4
+ module VagrantPlugins
5
+ module NitrousIO
6
+ class Provider < Vagrant.plugin('2', :provider)
7
+ def initialize(machine)
8
+ @machine = machine
9
+ end
10
+
11
+ def action(name)
12
+ # Attempt to get the action method from the Action class if it
13
+ # exists, otherwise return nil to show that we don't support the
14
+ # given action.
15
+ action_method = "action_#{name}"
16
+ return Action.send(action_method) if Action.respond_to?(action_method)
17
+ nil
18
+ end
19
+
20
+ def ssh_info
21
+ # Run a custom action called 'read_ssh_info' which does what it
22
+ # says and puts the resulting SSH info into the `:machine_ssh_info`
23
+ # key in the environment.
24
+ env = @machine.action('read_ssh_info')
25
+ env[:machine_ssh_info]
26
+ end
27
+
28
+ def state
29
+ # Run a custom action we define called 'read_state' which does
30
+ # what it says. It puts the state in the `:machine_state_id`
31
+ # key in the environment.
32
+ env = @machine.action('read_state')
33
+
34
+ state_id = env[:machine_state_id]
35
+
36
+ # Get the short and long description
37
+ short = I18n.t("vagrant_nitrousio.states.short_#{state_id}")
38
+ long = I18n.t("vagrant_nitrousio.states.long_#{state_id}")
39
+
40
+ # Return the MachineState object
41
+ Vagrant::MachineState.new(state_id, short, long)
42
+ end
43
+
44
+ def to_s
45
+ id = @machine.id.nil? ? 'new' : @machine.id
46
+ "Nitrous.IO (#{id})"
47
+ end
48
+ end
49
+ end
50
+ end