vagrant-haipa 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +19 -0
  3. data/.vscode/launch.json +29 -0
  4. data/Gemfile +20 -0
  5. data/LICENSE +373 -0
  6. data/README.md +111 -0
  7. data/Rakefile +24 -0
  8. data/box/haipa.box +0 -0
  9. data/box/metadata.json +3 -0
  10. data/lib/vagrant-haipa.rb +20 -0
  11. data/lib/vagrant-haipa/actions.rb +209 -0
  12. data/lib/vagrant-haipa/actions/check_state.rb +19 -0
  13. data/lib/vagrant-haipa/actions/connect_haipa.rb +24 -0
  14. data/lib/vagrant-haipa/actions/create_machine.rb +56 -0
  15. data/lib/vagrant-haipa/actions/delete_machine.rb +31 -0
  16. data/lib/vagrant-haipa/actions/is_created.rb +18 -0
  17. data/lib/vagrant-haipa/actions/is_stopped.rb +18 -0
  18. data/lib/vagrant-haipa/actions/message_already_created.rb +16 -0
  19. data/lib/vagrant-haipa/actions/message_not_created.rb +16 -0
  20. data/lib/vagrant-haipa/actions/message_will_not_destroy.rb +16 -0
  21. data/lib/vagrant-haipa/actions/modify_provision_path.rb +38 -0
  22. data/lib/vagrant-haipa/actions/set_name.rb +53 -0
  23. data/lib/vagrant-haipa/actions/shut_down.rb +33 -0
  24. data/lib/vagrant-haipa/actions/start_machine.rb +34 -0
  25. data/lib/vagrant-haipa/actions/stop_machine.rb +33 -0
  26. data/lib/vagrant-haipa/actions/wait_for_ip_address.rb +32 -0
  27. data/lib/vagrant-haipa/config.rb +54 -0
  28. data/lib/vagrant-haipa/errors.rb +41 -0
  29. data/lib/vagrant-haipa/helpers/client.rb +111 -0
  30. data/lib/vagrant-haipa/helpers/result.rb +40 -0
  31. data/lib/vagrant-haipa/plugin.rb +22 -0
  32. data/lib/vagrant-haipa/provider.rb +118 -0
  33. data/lib/vagrant-haipa/version.rb +5 -0
  34. data/locales/en.yml +95 -0
  35. data/test/Vagrantfile +43 -0
  36. data/test/cookbooks/test/recipes/default.rb +1 -0
  37. data/test/scripts/provision.sh +3 -0
  38. data/test/test.sh +13 -0
  39. data/test/test_id_rsa +27 -0
  40. data/test/test_id_rsa.pub +1 -0
  41. data/vagrant +27 -0
  42. data/vagrant-haipa.gemspec +21 -0
  43. metadata +132 -0
@@ -0,0 +1,31 @@
1
+ require 'vagrant-haipa/helpers/client'
2
+
3
+ module VagrantPlugins
4
+ module Haipa
5
+ module Actions
6
+ class DeleteMachine
7
+ include Helpers::Client
8
+
9
+ def initialize(app, env)
10
+ @app = app
11
+ @machine = env[:machine]
12
+ @client = client
13
+ @logger = Log4r::Logger.new('vagrant::haipa::DeleteMachine')
14
+ end
15
+
16
+ def call(env)
17
+ # submit delete machine request
18
+ result = @client.delete("/odata/Machines(#{@machine.id})")
19
+
20
+ env[:ui].info I18n.t('vagrant_haipa.info.destroying')
21
+ @client.wait_for_event(env, result['Id'])
22
+
23
+ # set the machine id to nil to cleanup local vagrant state
24
+ @machine.id = nil
25
+
26
+ @app.call(env)
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,18 @@
1
+ module VagrantPlugins
2
+ module Haipa
3
+ module Actions
4
+ class IsCreated
5
+ def initialize(app, env)
6
+ @app = app
7
+ @machine = env[:machine]
8
+ @logger = Log4r::Logger.new('vagrant::haipa::is_created')
9
+ end
10
+
11
+ def call(env)
12
+ env[:result] = env[:machine].state.id != :not_created
13
+ @app.call(env)
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ module VagrantPlugins
2
+ module Haipa
3
+ module Actions
4
+ class IsStopped
5
+ def initialize(app, env)
6
+ @app = app
7
+ @machine = env[:machine]
8
+ @logger = Log4r::Logger.new('vagrant::haipa::is_stopped')
9
+ end
10
+
11
+ def call(env)
12
+ env[:result] = env[:machine].state.id == :Stopped
13
+ @app.call(env)
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,16 @@
1
+ module VagrantPlugins
2
+ module Haipa
3
+ module Actions
4
+ class MessageAlreadyCreated
5
+ def initialize(app, _)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ env[:ui].info(I18n.t('vagrant_haipa.info.already_status', :status => "created"))
11
+ @app.call(env)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ module VagrantPlugins
2
+ module Haipa
3
+ module Actions
4
+ class MessageNotCreated
5
+ def initialize(app, _)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ env[:ui].info I18n.t('vagrant_haipa.info.not_created')
11
+ @app.call(env)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ module VagrantPlugins
2
+ module Haipa
3
+ module Actions
4
+ class MessageWillNotDestroy
5
+ def initialize(app, _)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ env[:ui].info(I18n.t('vagrant_haipa.info.will_not_destroy', name: env[:machine].name))
11
+ @app.call(env)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,38 @@
1
+ module VagrantPlugins
2
+ module Haipa
3
+ module Actions
4
+ class ModifyProvisionPath
5
+ def initialize(app, env)
6
+ @app = app
7
+ @machine = env[:machine]
8
+ @logger =
9
+ Log4r::Logger.new('vagrant::haipa::modify_provision_path')
10
+ end
11
+
12
+ def call(env)
13
+ # check if provisioning is enabled
14
+ enabled = true
15
+ enabled = env[:provision_enabled] if env.has_key?(:provision_enabled)
16
+ return @app.call(env) if !enabled
17
+
18
+ username = @machine.ssh_info()[:username]
19
+
20
+ # change ownership of the provisioning path recursively to the
21
+ # ssh user
22
+ #
23
+ # TODO submit patch to vagrant to set appropriate permissions
24
+ # based on ssh username
25
+ @machine.config.vm.provisioners.each do |provisioner|
26
+ cfg = provisioner.config
27
+ path = cfg.upload_path if cfg.respond_to? :upload_path
28
+ path = cfg.provisioning_path if cfg.respond_to? :provisioning_path
29
+ @machine.communicate.sudo("chown -R #{username} #{path}",
30
+ :error_check => false)
31
+ end
32
+
33
+ @app.call(env)
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,53 @@
1
+ require 'vagrant-haipa'
2
+
3
+ module VagrantPlugins
4
+ module Haipa
5
+ module Actions
6
+ class SetName
7
+ include Helpers::Client
8
+
9
+ def initialize(app, env)
10
+ @app = app
11
+ @machine = env[:machine]
12
+ @client = client
13
+ @logger = Log4r::Logger.new('vagrant::haipa::set_name')
14
+ end
15
+
16
+ def call(env)
17
+ name = @machine.provider_config.name
18
+
19
+ # If we already set the name before, then don't do anything
20
+ sentinel = @machine.data_dir.join("action_set_name")
21
+ if !name && sentinel.file?
22
+ @logger.info("Default name was already set before, not doing it again.")
23
+ return @app.call(env)
24
+ end
25
+
26
+ # If no name was manually set, then use a default
27
+ if !name
28
+ prefix = "#{env[:root_path].basename.to_s}_#{@machine.name}"
29
+ prefix.gsub!(/[^-a-z0-9_]/i, "")
30
+
31
+ # milliseconds + random number suffix to allow for simultaneous
32
+ # `vagrant up` of the same box in different dirs
33
+ name = prefix + "_#{(Time.now.to_f * 1000.0).to_i}_#{rand(100000)}"
34
+ end
35
+
36
+ # Verify the name is not taken
37
+ haipa_machine = Provider.haipa_machines(@machine).find { |d| d['Name'].to_s == name }
38
+ raise Vagrant::Errors::VMNameExists, name: name if haipa_machine
39
+
40
+ env[:generated_name] = name
41
+ # Create the sentinel
42
+ sentinel.open("w") do |f|
43
+ f.write(Time.now.to_i.to_s)
44
+ end
45
+
46
+ @app.call(env)
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+
53
+
@@ -0,0 +1,33 @@
1
+ require 'vagrant-haipa'
2
+
3
+ module VagrantPlugins
4
+ module Haipa
5
+ module Actions
6
+ class ShutDown
7
+ include Helpers::Client
8
+
9
+ def initialize(app, env)
10
+ @app = app
11
+ @machine = env[:machine]
12
+ @client = client
13
+ @logger = Log4r::Logger.new('vagrant::haipa::shut_down')
14
+ end
15
+
16
+ def call(env)
17
+ # submit shutdown machine request
18
+ result = @client.post("/odata/Machines(#{@machine.id})/Stop")
19
+
20
+ # wait for request to complete
21
+ env[:ui].info I18n.t('vagrant_haipa.info.shutting_down')
22
+ @client.wait_for_event(env, result['Id'])
23
+
24
+ # refresh machine state with provider
25
+ Provider.haipa_machine(@machine, :refresh => true)
26
+
27
+ @app.call(env)
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+
@@ -0,0 +1,34 @@
1
+ require 'vagrant-haipa'
2
+
3
+ module VagrantPlugins
4
+ module Haipa
5
+ module Actions
6
+ class StartMachine
7
+ include Helpers::Client
8
+
9
+ def initialize(app, env)
10
+ @app = app
11
+ @machine = env[:machine]
12
+ @client = client
13
+ @logger = Log4r::Logger.new('vagrant::haipa::power_on')
14
+ end
15
+
16
+ def call(env)
17
+ # submit power on machine request
18
+ result = @client.post("/odata/Machines(#{@machine.id})/Start")
19
+
20
+ # wait for request to complete
21
+ env[:ui].info I18n.t('vagrant_haipa.info.powering_on')
22
+ @client.wait_for_event(env, result['Id'])
23
+
24
+ # refresh machine state with provider
25
+ Provider.haipa_machine(@machine, :refresh => true)
26
+
27
+ @app.call(env)
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+
34
+
@@ -0,0 +1,33 @@
1
+ require 'vagrant-haipa'
2
+ #TODO: --force
3
+ module VagrantPlugins
4
+ module Haipa
5
+ module Actions
6
+ class StopMachine
7
+ include Helpers::Client
8
+
9
+ def initialize(app, env)
10
+ @app = app
11
+ @machine = env[:machine]
12
+ @client = client
13
+ @logger = Log4r::Logger.new('vagrant::haipa::power_off')
14
+ end
15
+
16
+ def call(env)
17
+ # submit power off machine request
18
+ result = @client.post("/odata/Machines(#{@machine.id})/Stop")
19
+
20
+ # wait for request to complete
21
+ env[:ui].info I18n.t('vagrant_haipa.info.powering_off')
22
+ @client.wait_for_event(env, result['Id'])
23
+
24
+ # refresh machine state with provider
25
+ Provider.haipa_machine(@machine, :refresh => true)
26
+
27
+ @app.call(env)
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+
@@ -0,0 +1,32 @@
1
+ module VagrantPlugins
2
+ module Haipa
3
+ module Actions
4
+ class WaitForIpAddress
5
+ include Vagrant::Util::Retryable
6
+
7
+ def initialize(app, env)
8
+ @app = app
9
+ @machine = env[:machine]
10
+ @logger = Log4r::Logger.new('vagrant::haipa::wait_for_ip_address')
11
+ end
12
+
13
+ def call(env)
14
+ # refresh machine state with provider and output ip address
15
+ retryable(:tries => 20, :sleep => 10) do
16
+ next if env[:interrupted]
17
+
18
+ haipa_machine = Provider.haipa_machine(@machine, :refresh => true)
19
+ addresses = haipa_machine['Networks'].map{|x| x['IpV4Addresses']}.flatten
20
+ addresses.reject! { |s| s.nil? || s.strip.empty? }
21
+ address = addresses.first
22
+ raise 'not ready' unless address
23
+
24
+ env[:machine_ip] ||= address
25
+
26
+ end
27
+ @app.call(env)
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,54 @@
1
+ module VagrantPlugins
2
+ module Haipa
3
+ class Config < Vagrant.plugin('2', :config)
4
+ attr_accessor :token
5
+ attr_accessor :image
6
+ attr_accessor :region
7
+ attr_accessor :flavor
8
+
9
+ attr_accessor :name
10
+
11
+ attr_accessor :vm_config
12
+ attr_accessor :provision
13
+
14
+
15
+ def initialize
16
+ @token = UNSET_VALUE
17
+ @image = UNSET_VALUE
18
+ @region = UNSET_VALUE
19
+ @flavor = UNSET_VALUE
20
+
21
+ @name = UNSET_VALUE
22
+ @vm_config = UNSET_VALUE
23
+ @provision = UNSET_VALUE
24
+ end
25
+
26
+ def finalize!
27
+ @token = ENV['DO_TOKEN'] if @token == UNSET_VALUE
28
+ @image = 'ubuntu-14-04-x64' if @image == UNSET_VALUE
29
+ @region = 'nyc2' if @region == UNSET_VALUE
30
+ @flavor = 'default' if @size == UNSET_VALUE
31
+ @name = nil if @name == UNSET_VALUE
32
+ @vm_config = [] if @vm_config == UNSET_VALUE
33
+ @provision = [] if @provision == UNSET_VALUE
34
+ end
35
+
36
+ def validate(machine)
37
+ errors = []
38
+ #errors << I18n.t('vagrant_haipa.config.token') if !@token
39
+
40
+ key = machine.config.ssh.private_key_path
41
+ key = key[0] if key.is_a?(Array)
42
+ # if !key
43
+ # errors << I18n.t('vagrant_haipa.config.private_key')
44
+ # elsif !File.file?(File.expand_path("#{key}.pub", machine.env.root_path))
45
+ # errors << I18n.t('vagrant_haipa.config.public_key', {
46
+ # :key => "#{key}.pub"
47
+ # })
48
+ # end
49
+
50
+ { 'Haipa Provider' => errors }
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,41 @@
1
+ module VagrantPlugins
2
+ module Haipa
3
+ module Errors
4
+ class HaipaError < Vagrant::Errors::VagrantError
5
+ error_namespace("vagrant_haipa.errors")
6
+ end
7
+
8
+ class APIStatusError < HaipaError
9
+ error_key(:api_status)
10
+ end
11
+
12
+ class JSONError < HaipaError
13
+ error_key(:json)
14
+ end
15
+
16
+ class ResultMatchError < HaipaError
17
+ error_key(:result_match)
18
+ end
19
+
20
+ class CertificateError < HaipaError
21
+ error_key(:certificate)
22
+ end
23
+
24
+ class LocalIPError < HaipaError
25
+ error_key(:local_ip)
26
+ end
27
+
28
+ class PublicKeyError < HaipaError
29
+ error_key(:public_key)
30
+ end
31
+
32
+ class RsyncError < HaipaError
33
+ error_key(:rsync)
34
+ end
35
+
36
+ class OperationNotCompleted < HaipaError
37
+ error_key(:rsync)
38
+ end
39
+ end
40
+ end
41
+ end