vagrant-conoha 0.1.0

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 (109) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +22 -0
  3. data/.rubocop.yml +35 -0
  4. data/CHANGELOG.md +3 -0
  5. data/Gemfile +19 -0
  6. data/LICENSE +23 -0
  7. data/Rakefile +25 -0
  8. data/Vagrantfile +71 -0
  9. data/dummy.box +0 -0
  10. data/example_box/README.md +13 -0
  11. data/example_box/metadata.json +3 -0
  12. data/functional_tests/Vagrantfile +58 -0
  13. data/functional_tests/keys/vagrant-openstack +27 -0
  14. data/functional_tests/keys/vagrant-openstack.pub +1 -0
  15. data/functional_tests/run_tests.sh +142 -0
  16. data/lib/vagrant-conoha.rb +29 -0
  17. data/lib/vagrant-conoha/action.rb +227 -0
  18. data/lib/vagrant-conoha/action/abstract_action.rb +22 -0
  19. data/lib/vagrant-conoha/action/connect_openstack.rb +60 -0
  20. data/lib/vagrant-conoha/action/create_server.rb +154 -0
  21. data/lib/vagrant-conoha/action/create_stack.rb +68 -0
  22. data/lib/vagrant-conoha/action/delete_server.rb +53 -0
  23. data/lib/vagrant-conoha/action/delete_stack.rb +73 -0
  24. data/lib/vagrant-conoha/action/message.rb +19 -0
  25. data/lib/vagrant-conoha/action/provision.rb +60 -0
  26. data/lib/vagrant-conoha/action/read_ssh_info.rb +72 -0
  27. data/lib/vagrant-conoha/action/read_state.rb +43 -0
  28. data/lib/vagrant-conoha/action/resume.rb +24 -0
  29. data/lib/vagrant-conoha/action/start_server.rb +24 -0
  30. data/lib/vagrant-conoha/action/stop_server.rb +25 -0
  31. data/lib/vagrant-conoha/action/suspend.rb +24 -0
  32. data/lib/vagrant-conoha/action/sync_folders.rb +129 -0
  33. data/lib/vagrant-conoha/action/wait_accessible.rb +61 -0
  34. data/lib/vagrant-conoha/action/wait_active.rb +33 -0
  35. data/lib/vagrant-conoha/action/wait_stop.rb +33 -0
  36. data/lib/vagrant-conoha/catalog/openstack_catalog.rb +67 -0
  37. data/lib/vagrant-conoha/client/cinder.rb +39 -0
  38. data/lib/vagrant-conoha/client/domain.rb +159 -0
  39. data/lib/vagrant-conoha/client/glance.rb +65 -0
  40. data/lib/vagrant-conoha/client/heat.rb +49 -0
  41. data/lib/vagrant-conoha/client/http_utils.rb +116 -0
  42. data/lib/vagrant-conoha/client/keystone.rb +77 -0
  43. data/lib/vagrant-conoha/client/neutron.rb +48 -0
  44. data/lib/vagrant-conoha/client/nova.rb +212 -0
  45. data/lib/vagrant-conoha/client/openstack.rb +59 -0
  46. data/lib/vagrant-conoha/client/request_logger.rb +23 -0
  47. data/lib/vagrant-conoha/client/rest_utils.rb +25 -0
  48. data/lib/vagrant-conoha/command/abstract_command.rb +51 -0
  49. data/lib/vagrant-conoha/command/flavor_list.rb +24 -0
  50. data/lib/vagrant-conoha/command/image_list.rb +29 -0
  51. data/lib/vagrant-conoha/command/main.rb +51 -0
  52. data/lib/vagrant-conoha/command/network_list.rb +25 -0
  53. data/lib/vagrant-conoha/command/openstack_command.rb +16 -0
  54. data/lib/vagrant-conoha/command/reset.rb +20 -0
  55. data/lib/vagrant-conoha/command/subnet_list.rb +22 -0
  56. data/lib/vagrant-conoha/command/utils.rb +22 -0
  57. data/lib/vagrant-conoha/command/volume_list.rb +25 -0
  58. data/lib/vagrant-conoha/config.rb +390 -0
  59. data/lib/vagrant-conoha/config/http.rb +39 -0
  60. data/lib/vagrant-conoha/config_resolver.rb +285 -0
  61. data/lib/vagrant-conoha/errors.rb +187 -0
  62. data/lib/vagrant-conoha/logging.rb +39 -0
  63. data/lib/vagrant-conoha/plugin.rb +48 -0
  64. data/lib/vagrant-conoha/provider.rb +50 -0
  65. data/lib/vagrant-conoha/utils.rb +26 -0
  66. data/lib/vagrant-conoha/version.rb +15 -0
  67. data/lib/vagrant-conoha/version_checker.rb +76 -0
  68. data/locales/en.yml +393 -0
  69. data/spec/vagrant-conoha/action/connect_openstack_spec.rb +695 -0
  70. data/spec/vagrant-conoha/action/create_server_spec.rb +225 -0
  71. data/spec/vagrant-conoha/action/create_stack_spec.rb +99 -0
  72. data/spec/vagrant-conoha/action/delete_server_spec.rb +89 -0
  73. data/spec/vagrant-conoha/action/delete_stack_spec.rb +63 -0
  74. data/spec/vagrant-conoha/action/message_spec.rb +33 -0
  75. data/spec/vagrant-conoha/action/provision_spec.rb +104 -0
  76. data/spec/vagrant-conoha/action/read_ssh_info_spec.rb +190 -0
  77. data/spec/vagrant-conoha/action/read_state_spec.rb +81 -0
  78. data/spec/vagrant-conoha/action/resume_server_spec.rb +49 -0
  79. data/spec/vagrant-conoha/action/start_server_spec.rb +49 -0
  80. data/spec/vagrant-conoha/action/stop_server_spec.rb +49 -0
  81. data/spec/vagrant-conoha/action/suspend_server_spec.rb +49 -0
  82. data/spec/vagrant-conoha/action/sync_folders_spec.rb +155 -0
  83. data/spec/vagrant-conoha/action/wait_accessible_spec.rb +67 -0
  84. data/spec/vagrant-conoha/action/wait_active_spec.rb +53 -0
  85. data/spec/vagrant-conoha/action/wait_stop_spec.rb +53 -0
  86. data/spec/vagrant-conoha/action_spec.rb +120 -0
  87. data/spec/vagrant-conoha/client/cinder_spec.rb +127 -0
  88. data/spec/vagrant-conoha/client/glance_spec.rb +143 -0
  89. data/spec/vagrant-conoha/client/heat_spec.rb +128 -0
  90. data/spec/vagrant-conoha/client/keystone_spec.rb +150 -0
  91. data/spec/vagrant-conoha/client/neutron_spec.rb +171 -0
  92. data/spec/vagrant-conoha/client/nova_spec.rb +757 -0
  93. data/spec/vagrant-conoha/client/utils_spec.rb +176 -0
  94. data/spec/vagrant-conoha/command/flavor_list_spec.rb +43 -0
  95. data/spec/vagrant-conoha/command/image_list_spec.rb +95 -0
  96. data/spec/vagrant-conoha/command/network_list_spec.rb +65 -0
  97. data/spec/vagrant-conoha/command/reset_spec.rb +24 -0
  98. data/spec/vagrant-conoha/command/subnet_list_spec.rb +45 -0
  99. data/spec/vagrant-conoha/command/volume_list_spec.rb +40 -0
  100. data/spec/vagrant-conoha/config_resolver_spec.rb +860 -0
  101. data/spec/vagrant-conoha/config_spec.rb +373 -0
  102. data/spec/vagrant-conoha/e2e_spec.rb.save +27 -0
  103. data/spec/vagrant-conoha/provider_spec.rb +13 -0
  104. data/spec/vagrant-conoha/spec_helper.rb +37 -0
  105. data/spec/vagrant-conoha/utils_spec.rb +129 -0
  106. data/spec/vagrant-conoha/version_checker_spec.rb +39 -0
  107. data/stackrc +25 -0
  108. data/vagrant-conoha.gemspec +32 -0
  109. metadata +343 -0
@@ -0,0 +1,68 @@
1
+ require 'log4r'
2
+ require 'socket'
3
+ require 'timeout'
4
+ require 'sshkey'
5
+ require 'yaml'
6
+
7
+ require 'vagrant-conoha/config_resolver'
8
+ require 'vagrant-conoha/utils'
9
+ require 'vagrant-conoha/action/abstract_action'
10
+ require 'vagrant/util/retryable'
11
+
12
+ module VagrantPlugins
13
+ module ConoHa
14
+ module Action
15
+ class CreateStack < AbstractAction
16
+ def initialize(app, _env)
17
+ @app = app
18
+ @logger = Log4r::Logger.new('vagrant_openstack::action::create_stack')
19
+ end
20
+
21
+ def execute(env)
22
+ @logger.info 'Start create stacks action'
23
+
24
+ config = env[:machine].provider_config
25
+
26
+ heat = env[:openstack_client].heat
27
+
28
+ config.stacks.each do |stack|
29
+ env[:ui].info(I18n.t('vagrant_openstack.create_stack'))
30
+ env[:ui].info(" -- Stack Name : #{stack[:name]}")
31
+ env[:ui].info(" -- Template : #{stack[:template]}")
32
+
33
+ create_opts = {
34
+ name: stack[:name],
35
+ template: YAML.load_file(stack[:template])
36
+ }
37
+
38
+ stack_id = heat.create_stack(env, create_opts)
39
+
40
+ file_path = "#{env[:machine].data_dir}/stack_#{stack[:name]}_id"
41
+ File.write(file_path, stack_id)
42
+
43
+ waiting_for_stack_to_be_created(env, stack[:name], stack_id)
44
+ end unless config.stacks.nil?
45
+
46
+ @app.call(env)
47
+ end
48
+
49
+ private
50
+
51
+ def waiting_for_stack_to_be_created(env, stack_name, stack_id, retry_interval = 3)
52
+ @logger.info "Waiting for the stack with id #{stack_id} to be built..."
53
+ env[:ui].info(I18n.t('vagrant_openstack.waiting_for_stack'))
54
+ config = env[:machine].provider_config
55
+ timeout(config.stack_create_timeout, Errors::Timeout) do
56
+ stack_status = 'CREATE_IN_PROGRESS'
57
+ until stack_status == 'CREATE_COMPLETE'
58
+ @logger.debug('Waiting for stack to be CREATED')
59
+ stack_status = env[:openstack_client].heat.get_stack_details(env, stack_name, stack_id)['stack_status']
60
+ fail Errors::StackStatusError, stack: stack_id if stack_status == 'CREATE_FAILED'
61
+ sleep retry_interval
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,53 @@
1
+ require 'log4r'
2
+
3
+ require 'vagrant-conoha/action/abstract_action'
4
+
5
+ module VagrantPlugins
6
+ module ConoHa
7
+ module Action
8
+ # This deletes the running server, if there is one.
9
+ class DeleteServer < AbstractAction
10
+ def initialize(app, _env)
11
+ @app = app
12
+ @logger = Log4r::Logger.new('vagrant_openstack::action::delete_server')
13
+ end
14
+
15
+ def execute(env)
16
+ if env[:machine].id
17
+ @logger.info "Deleting server #{env[:machine].id}..."
18
+ env[:ui].info(I18n.t('vagrant_openstack.deleting_server'))
19
+ env[:openstack_client].nova.delete_server(env, env[:machine].id)
20
+ env[:openstack_client].nova.delete_keypair_if_vagrant(env, env[:machine].id)
21
+
22
+ waiting_for_instance_to_be_deleted(env, env[:machine].id)
23
+
24
+ end
25
+
26
+ @app.call(env)
27
+ end
28
+
29
+ private
30
+
31
+ def waiting_for_instance_to_be_deleted(env, instance_id, retry_interval = 3)
32
+ @logger.info "Waiting for the instance with id #{instance_id} to be deleted..."
33
+ env[:ui].info(I18n.t('vagrant_openstack.waiting_deleted'))
34
+ config = env[:machine].provider_config
35
+ timeout(config.server_delete_timeout, Errors::Timeout) do
36
+ delete_ok = false
37
+ until delete_ok
38
+ begin
39
+ @logger.debug('Waiting for instance to be DELETED')
40
+ server_status = env[:openstack_client].nova.get_server_details(env, instance_id)['status']
41
+ fail Errors::ServerStatusError, server: instance_id if server_status == 'ERROR'
42
+ break if server_status == 'DELETED'
43
+ sleep retry_interval
44
+ rescue Errors::InstanceNotFound
45
+ delete_ok = true
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,73 @@
1
+ require 'log4r'
2
+ require 'socket'
3
+ require 'timeout'
4
+ require 'sshkey'
5
+ require 'yaml'
6
+
7
+ require 'vagrant-conoha/config_resolver'
8
+ require 'vagrant-conoha/utils'
9
+ require 'vagrant-conoha/action/abstract_action'
10
+ require 'vagrant/util/retryable'
11
+
12
+ module VagrantPlugins
13
+ module ConoHa
14
+ module Action
15
+ class DeleteStack < AbstractAction
16
+ def initialize(app, _env)
17
+ @app = app
18
+ @logger = Log4r::Logger.new('vagrant_openstack::action::delete_stack')
19
+ end
20
+
21
+ def execute(env)
22
+ @logger.info 'Start delete stacks action'
23
+
24
+ heat = env[:openstack_client].heat
25
+
26
+ list_stack_files(env).each do |stack|
27
+ env[:ui].info(I18n.t('vagrant_openstack.delete_stack'))
28
+ env[:ui].info(" -- Stack Name : #{stack[:name]}")
29
+ env[:ui].info(" -- Stack Id : #{stack[:id]}")
30
+
31
+ heat.delete_stack(env, stack[:name], stack[:id])
32
+
33
+ waiting_for_stack_to_be_deleted(env, stack[:name], stack[:id])
34
+ end
35
+
36
+ # This will remove all files in the .vagrant instance directory
37
+ env[:machine].id = nil
38
+
39
+ @app.call(env)
40
+ end
41
+
42
+ private
43
+
44
+ def list_stack_files(env)
45
+ stack_files = []
46
+ Dir.glob("#{env[:machine].data_dir}/stack_*_id") do |stack_file|
47
+ file_name = stack_file.split('/')[-1]
48
+ stack_files << {
49
+ name: file_name[6, (file_name.length) - 9],
50
+ id: File.read("#{stack_file}")
51
+ }
52
+ end
53
+ stack_files
54
+ end
55
+
56
+ def waiting_for_stack_to_be_deleted(env, stack_name, stack_id, retry_interval = 3)
57
+ @logger.info "Waiting for the stack with id #{stack_id} to be deleted..."
58
+ env[:ui].info(I18n.t('vagrant_openstack.waiting_for_stack_deleted'))
59
+ config = env[:machine].provider_config
60
+ timeout(config.stack_delete_timeout, Errors::Timeout) do
61
+ stack_status = 'DELETE_IN_PROGRESS'
62
+ until stack_status == 'DELETE_COMPLETE'
63
+ @logger.debug('Waiting for stack to be DELETED')
64
+ stack_status = env[:openstack_client].heat.get_stack_details(env, stack_name, stack_id)['stack_status']
65
+ fail Errors::StackStatusError, stack: stack_id if stack_status == 'DELETE_FAILED'
66
+ sleep retry_interval
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,19 @@
1
+ require 'vagrant-conoha/action/abstract_action'
2
+
3
+ module VagrantPlugins
4
+ module ConoHa
5
+ module Action
6
+ class Message < AbstractAction
7
+ def initialize(app, _env, message)
8
+ @app = app
9
+ @message = message
10
+ end
11
+
12
+ def execute(env)
13
+ env[:ui].info(@message)
14
+ @app.call(env)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,60 @@
1
+ require 'log4r'
2
+
3
+ require 'vagrant/action/builder'
4
+
5
+ require 'vagrant-conoha/action/abstract_action'
6
+ require 'vagrant-conoha/action/read_ssh_info'
7
+
8
+ module VagrantPlugins
9
+ module ConoHa
10
+ module Action
11
+ include Vagrant::Action::Builtin
12
+
13
+ class ProvisionWrapper < AbstractAction
14
+ def initialize(app, env)
15
+ @app = app
16
+ @env = env
17
+ @logger = Log4r::Logger.new('vagrant_openstack::action::provision_wrapper')
18
+ end
19
+
20
+ def execute(env)
21
+ @logger.info 'Run provisioning'
22
+ InternalProvisionWrapper.new(@app, @env).call(@env)
23
+ @app.call(env)
24
+ end
25
+ end
26
+
27
+ class InternalProvisionWrapper < Vagrant::Action::Builtin::Provision
28
+ def initialize(app, env)
29
+ @logger = Log4r::Logger.new('vagrant_openstack::action::internal_provision_wrapper')
30
+ super app, env
31
+ end
32
+
33
+ def run_provisioner(env)
34
+ if env[:provisioner].class == VagrantPlugins::Shell::Provisioner
35
+ handle_shell_meta_args(env)
36
+ end
37
+ env[:provisioner].provision
38
+ end
39
+
40
+ private
41
+
42
+ def handle_shell_meta_args(env)
43
+ config = env[:provisioner].config
44
+ args = config.args.nil? ? [] : [config.args].flatten
45
+ config.args = []
46
+ @logger.info "Shell provisioner args: #{args}"
47
+ args.each do |arg|
48
+ if '@@ssh_ip@@'.eql? arg
49
+ ssh_info = VagrantPlugins::ConoHa::Action.get_ssh_info(env)
50
+ @logger.info "Replace meta-arg #{arg} by value #{ssh_info[:host]}"
51
+ config.args << ssh_info[:host]
52
+ else
53
+ config.args << arg
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,72 @@
1
+ require 'log4r'
2
+
3
+ require 'vagrant-conoha/config_resolver'
4
+ require 'vagrant-conoha/utils'
5
+ require 'vagrant-conoha/action/abstract_action'
6
+
7
+ module VagrantPlugins
8
+ module ConoHa
9
+ module Action
10
+ # This action reads the SSH info for the machine and puts it into the
11
+ # `:machine_ssh_info` key in the environment.
12
+
13
+ class ReadSSHInfo < AbstractAction
14
+ def initialize(app, _env, resolver = ConfigResolver.new, utils = Utils.new)
15
+ @app = app
16
+ @logger = Log4r::Logger.new('vagrant_openstack::action::read_ssh_info')
17
+ @resolver = resolver
18
+ @utils = utils
19
+ end
20
+
21
+ def execute(env)
22
+ @logger.info 'Reading SSH info'
23
+ server_id = env[:machine].id.to_sym
24
+ SSHInfoHolder.instance.tap do |holder|
25
+ holder.synchronize do
26
+ holder.ssh_info[server_id] = read_ssh_info(env) if holder.ssh_info[server_id].nil?
27
+ env[:machine_ssh_info] = holder.ssh_info[server_id]
28
+ end
29
+ end
30
+ @app.call(env)
31
+ end
32
+
33
+ private
34
+
35
+ def read_ssh_info(env)
36
+ config = env[:machine].provider_config
37
+ env[:ui].warn('SSH is disabled in the provider config. The action you are attempting is likely to fail') if config.ssh_disabled
38
+ hash = {
39
+ host: @utils.get_ip_address(env),
40
+ port: @resolver.resolve_ssh_port(env),
41
+ username: @resolver.resolve_ssh_username(env)
42
+ }
43
+ hash[:private_key_path] = "#{env[:machine].data_dir}/#{get_keypair_name(env)}" unless config.keypair_name || config.public_key_path
44
+ # Should work silently when https://github.com/mitchellh/vagrant/issues/4637 is fixed
45
+ hash[:log_level] = 'ERROR'
46
+ hash
47
+ end
48
+
49
+ def get_keypair_name(env)
50
+ env[:openstack_client].nova.get_server_details(env, env[:machine].id)['key_name']
51
+ end
52
+ end
53
+
54
+ class SSHInfoHolder < Mutex
55
+ include Singleton
56
+
57
+ #
58
+ # Keys are machine ids
59
+ #
60
+ attr_accessor :ssh_info
61
+
62
+ def initialize
63
+ @ssh_info = {}
64
+ end
65
+ end
66
+
67
+ def self.get_ssh_info(env)
68
+ SSHInfoHolder.instance.ssh_info[env[:machine].id.to_sym]
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,43 @@
1
+ require 'log4r'
2
+
3
+ require 'vagrant-conoha/action/abstract_action'
4
+
5
+ module VagrantPlugins
6
+ module ConoHa
7
+ module Action
8
+ # This action reads the state of the machine and puts it in the
9
+ # `:machine_state_id` key in the environment.
10
+ class ReadState < AbstractAction
11
+ def initialize(app, _env)
12
+ @app = app
13
+ @logger = Log4r::Logger.new('vagrant_openstack::action::read_state')
14
+ end
15
+
16
+ def execute(env)
17
+ env[:machine_state_id] = read_state(env)
18
+ @app.call(env)
19
+ end
20
+
21
+ def read_state(env)
22
+ machine = env[:machine]
23
+ return :not_created if machine.id.nil?
24
+
25
+ # Find the machine
26
+ server = env[:openstack_client].nova.get_server_details(env, machine.id)
27
+ if server.nil? || server['status'] == 'DELETED'
28
+ # The machine can't be found
29
+ @logger.info('Machine not found or deleted, assuming it got destroyed.')
30
+ machine.id = nil
31
+ return :not_created
32
+ end
33
+
34
+ if !server['OS-EXT-STS:task_state'].nil?
35
+ server['OS-EXT-STS:task_state'].downcase.to_sym
36
+ else
37
+ server['status'].downcase.to_sym
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,24 @@
1
+ require 'vagrant-conoha/action/abstract_action'
2
+
3
+ module VagrantPlugins
4
+ module ConoHa
5
+ module Action
6
+ class Resume < AbstractAction
7
+ def initialize(app, _env)
8
+ @app = app
9
+ @logger = Log4r::Logger.new('vagrant_openstack::action::resume_server')
10
+ end
11
+
12
+ def execute(env)
13
+ if env[:machine].id
14
+ @logger.info "Resuming suspended VM #{env[:machine].id}..."
15
+ env[:ui].info I18n.t('vagrant.actions.vm.resume.resuming')
16
+ env[:openstack_client].nova.resume_server(env, env[:machine].id)
17
+ end
18
+
19
+ @app.call(env)
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,24 @@
1
+ require 'log4r'
2
+
3
+ require 'vagrant-conoha/action/abstract_action'
4
+
5
+ module VagrantPlugins
6
+ module ConoHa
7
+ module Action
8
+ class StartServer < AbstractAction
9
+ def initialize(app, _env)
10
+ @app = app
11
+ @logger = Log4r::Logger.new('vagrant_openstack::action::start_server')
12
+ end
13
+
14
+ def execute(env)
15
+ if env[:machine].id
16
+ env[:ui].info(I18n.t('vagrant_openstack.starting_server'))
17
+ env[:openstack_client].nova.start_server(env, env[:machine].id)
18
+ end
19
+ @app.call(env)
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end