vagrant-niftycloud 0.1.0.dev

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 (34) hide show
  1. data/.gitignore +18 -0
  2. data/CHANGELOG.md +1 -0
  3. data/Gemfile +10 -0
  4. data/LICENSE +8 -0
  5. data/README.md +256 -0
  6. data/Rakefile +21 -0
  7. data/dummy.box +0 -0
  8. data/example_box/README.md +13 -0
  9. data/example_box/metadata.json +3 -0
  10. data/lib/vagrant-niftycloud.rb +18 -0
  11. data/lib/vagrant-niftycloud/action.rb +178 -0
  12. data/lib/vagrant-niftycloud/action/connect_niftycloud.rb +56 -0
  13. data/lib/vagrant-niftycloud/action/is_created.rb +18 -0
  14. data/lib/vagrant-niftycloud/action/message_already_created.rb +16 -0
  15. data/lib/vagrant-niftycloud/action/message_not_created.rb +16 -0
  16. data/lib/vagrant-niftycloud/action/message_will_not_destroy.rb +16 -0
  17. data/lib/vagrant-niftycloud/action/read_ssh_info.rb +52 -0
  18. data/lib/vagrant-niftycloud/action/read_state.rb +56 -0
  19. data/lib/vagrant-niftycloud/action/resume_instance.rb +56 -0
  20. data/lib/vagrant-niftycloud/action/run_instance.rb +131 -0
  21. data/lib/vagrant-niftycloud/action/suspend_instance.rb +56 -0
  22. data/lib/vagrant-niftycloud/action/sync_folders.rb +67 -0
  23. data/lib/vagrant-niftycloud/action/terminate_instance.rb +61 -0
  24. data/lib/vagrant-niftycloud/action/timed_provision.rb +21 -0
  25. data/lib/vagrant-niftycloud/config.rb +210 -0
  26. data/lib/vagrant-niftycloud/errors.rb +35 -0
  27. data/lib/vagrant-niftycloud/plugin.rb +73 -0
  28. data/lib/vagrant-niftycloud/provider.rb +50 -0
  29. data/lib/vagrant-niftycloud/util/timer.rb +17 -0
  30. data/lib/vagrant-niftycloud/version.rb +5 -0
  31. data/locales/en.yml +91 -0
  32. data/spec/vagrant-niftycloud/config_spec.rb +154 -0
  33. data/vagrant-niftycloud.gemspec +57 -0
  34. metadata +157 -0
@@ -0,0 +1,56 @@
1
+ # -*- coding: utf-8 -*-
2
+ require "NIFTY"
3
+ require "log4r"
4
+ NIFTY::LOG.level = Logger::DEBUG
5
+
6
+ module VagrantPlugins
7
+ module NiftyCloud
8
+ module Action
9
+ # This action connects to NiftyCloud, verifies credentials work, and
10
+ # puts the NiftyCloud connection object into the `:niftycloud_compute` key
11
+ # in the environment.
12
+ class ConnectNiftyCloud
13
+ def initialize(app, env)
14
+ @app = app
15
+ @logger = Log4r::Logger.new("vagrant_niftycloud::action::connect_niftycloud")
16
+ end
17
+
18
+ def call(env)
19
+ # Get the zone we're going to booting up in
20
+ zone = env[:machine].provider_config.zone
21
+
22
+ # Get the configs
23
+ zone_config = env[:machine].provider_config.get_zone_config(zone)
24
+
25
+ # Build the fog config
26
+ niftycloud_config = {
27
+ :access_key => zone_config.access_key_id,
28
+ :secret_key => zone_config.secret_access_key
29
+ }
30
+
31
+ # 例外の定義は以下参照
32
+ # http://cloud.nifty.com/api/sdk/rdoc/
33
+ begin
34
+ @logger.info("Connecting to NiftyCloud...")
35
+ env[:niftycloud_compute] = NIFTY::Cloud::Base.new(niftycloud_config)
36
+ rescue NIFTY::ConfigurationError => e
37
+ raise VagrantPlugins::NiftyCloud::Errors::NiftyCloudConfigurationError,
38
+ :message => e.message
39
+ rescue NIFTY::ArgumentError => e
40
+ raise VagrantPlugins::NiftyCloud::Errors::NiftyCloudArgumentError,
41
+ :message => e.message
42
+ rescue NIFTY::ResponseFormatError => e
43
+ raise VagrantPlugins::NiftyCloud::Errors::NiftyCloudResponseFormatError,
44
+ :message => e.message
45
+ rescue NIFTY::ResponseError => e
46
+ raise VagrantPlugins::NiftyCloud::Errors::NiftyCloudResponseError,
47
+ :code => e.error_code,
48
+ :message => e.error_message
49
+ end
50
+
51
+ @app.call(env)
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,18 @@
1
+ module VagrantPlugins
2
+ module NiftyCloud
3
+ module Action
4
+ # This can be used with "Call" built-in to check if the machine
5
+ # is created and branch in the middleware.
6
+ class IsCreated
7
+ def initialize(app, env)
8
+ @app = app
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,16 @@
1
+ module VagrantPlugins
2
+ module NiftyCloud
3
+ module Action
4
+ class MessageAlreadyCreated
5
+ def initialize(app, env)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ env[:ui].info(I18n.t("vagrant_niftycloud.already_created"))
11
+ @app.call(env)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ module VagrantPlugins
2
+ module NiftyCloud
3
+ module Action
4
+ class MessageNotCreated
5
+ def initialize(app, env)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ env[:ui].info(I18n.t("vagrant_niftycloud.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 NiftyCloud
3
+ module Action
4
+ class MessageWillNotDestroy
5
+ def initialize(app, env)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ env[:ui].info(I18n.t("vagrant_niftycloud.will_not_destroy", name: env[:machine].name))
11
+ @app.call(env)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,52 @@
1
+ require "log4r"
2
+
3
+ module VagrantPlugins
4
+ module NiftyCloud
5
+ module Action
6
+ # This action reads the SSH info for the machine and puts it into the
7
+ # `:machine_ssh_info` key in the environment.
8
+ class ReadSSHInfo
9
+ def initialize(app, env)
10
+ @app = app
11
+ @logger = Log4r::Logger.new("vagrant_niftycloud::action::read_ssh_info")
12
+ end
13
+
14
+ def call(env)
15
+ env[:machine_ssh_info] = read_ssh_info(env[:niftycloud_compute], env[:machine])
16
+
17
+ @app.call(env)
18
+ end
19
+
20
+ def read_ssh_info(niftycloud, machine)
21
+ return nil if machine.id.nil?
22
+
23
+ # Find the machine
24
+ # 例外の定義は以下参照
25
+ # http://cloud.nifty.com/api/sdk/rdoc/
26
+ begin
27
+ server = niftycloud.describe_instances(:instance_id => machine.id).reservationSet.item.first.instancesSet.item.first
28
+ # Read the DNS info
29
+ return {
30
+ :host => server.ipAddress,
31
+ :port => 22
32
+ }
33
+ rescue NIFTY::ConfigurationError => e
34
+ raise VagrantPlugins::NiftyCloud::Errors::NiftyCloudConfigurationError,
35
+ :message => e.message
36
+ rescue NIFTY::ArgumentError => e
37
+ raise VagrantPlugins::NiftyCloud::Errors::NiftyCloudArgumentError,
38
+ :message => e.message
39
+ rescue NIFTY::ResponseFormatError => e
40
+ raise VagrantPlugins::NiftyCloud::Errors::NiftyCloudResponseFormatError,
41
+ :message => e.message
42
+ rescue NIFTY::ResponseError => e
43
+ # The machine can't be found
44
+ @logger.info("Machine couldn't be found, assuming it got destroyed.")
45
+ machine.id = nil
46
+ return nil
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,56 @@
1
+ require "log4r"
2
+
3
+ module VagrantPlugins
4
+ module NiftyCloud
5
+ module Action
6
+ # This action reads the state of the machine and puts it in the
7
+ # `:machine_state_id` key in the environment.
8
+ class ReadState
9
+ def initialize(app, env)
10
+ @app = app
11
+ @logger = Log4r::Logger.new("vagrant_niftycloud::action::read_state")
12
+ end
13
+
14
+ def call(env)
15
+ env[:machine_state_id] = read_state(env[:niftycloud_compute], env[:machine])
16
+
17
+ @app.call(env)
18
+ end
19
+
20
+ def read_state(niftycloud, machine)
21
+ return :not_created if machine.id.nil?
22
+
23
+ # Find the machine
24
+ # 例外の定義は以下参照
25
+ # http://cloud.nifty.com/api/sdk/rdoc/
26
+ begin
27
+ server = niftycloud.describe_instances(:instance_id => machine.id).reservationSet.item.first.instancesSet.item.first
28
+
29
+ state = server.instanceState.name
30
+ case state
31
+ when 'suspending'
32
+ @logger.info("Machine not found or terminated, assuming it got destroyed.")
33
+ machine.id = nil
34
+ return :not_created
35
+ else
36
+ @logger.debug("Server State #{state.to_sym}")
37
+ return state.to_sym
38
+ end
39
+ rescue NIFTY::ConfigurationError => e
40
+ raise VagrantPlugins::NiftyCloud::Errors::NiftyCloudConfigurationError,
41
+ :message => e.message
42
+ rescue NIFTY::ArgumentError => e
43
+ raise VagrantPlugins::NiftyCloud::Errors::NiftyCloudArgumentError,
44
+ :message => e.message
45
+ rescue NIFTY::ResponseFormatError => e
46
+ raise VagrantPlugins::NiftyCloud::Errors::NiftyCloudResponseFormatError,
47
+ :message => e.message
48
+ rescue NIFTY::ResponseError => e
49
+ machine.id = nil
50
+ return :not_created
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,56 @@
1
+ # -*- coding: utf-8 -*-
2
+ require "log4r"
3
+
4
+ module VagrantPlugins
5
+ module NiftyCloud
6
+ module Action
7
+ # This resume the running instance.
8
+ class ResumeInstance
9
+ def initialize(app, env)
10
+ @app = app
11
+ @logger = Log4r::Logger.new("vagrant_niftycloud::action::resume_instance")
12
+ end
13
+
14
+ def call(env)
15
+
16
+ # 例外の定義は以下参照
17
+ # http://cloud.nifty.com/api/sdk/rdoc/
18
+ begin
19
+ env[:ui].info(I18n.t("vagrant_niftycloud.resuming"))
20
+
21
+ # 起動直後等、resume処理できないステータスの場合一旦待つ
22
+ server = env[:niftycloud_compute].describe_instances(:instance_id => env[:machine].id).reservationSet.item.first.instancesSet.item.first
23
+ while server.instanceState.name == 'pending'
24
+ sleep 5
25
+ server = env[:niftycloud_compute].describe_instances(:instance_id => env[:machine].id).reservationSet.item.first.instancesSet.item.first
26
+ end
27
+
28
+ if server.instanceState.name != 'running'
29
+ env[:niftycloud_compute].start_instances(:instance_id => env[:machine].id)
30
+ while server.instanceState.name != 'running'
31
+ sleep 5
32
+ server = env[:niftycloud_compute].describe_instances(:instance_id => env[:machine].id).reservationSet.item.first.instancesSet.item.first
33
+ end
34
+ end
35
+
36
+ @app.call(env)
37
+ rescue NIFTY::ConfigurationError => e
38
+ raise VagrantPlugins::NiftyCloud::Errors::NiftyCloudConfigurationError,
39
+ :message => e.message
40
+ rescue NIFTY::ArgumentError => e
41
+ raise VagrantPlugins::NiftyCloud::Errors::NiftyCloudArgumentError,
42
+ :message => e.message
43
+ rescue NIFTY::ResponseFormatError => e
44
+ raise VagrantPlugins::NiftyCloud::Errors::NiftyCloudResponseFormatError,
45
+ :message => e.message
46
+ rescue NIFTY::ResponseError => e
47
+ ui.error("Could not locate server '#{env[:machine].id}'. Please verify it was provisioned in the current zone.")
48
+ raise VagrantPlugins::NiftyCloud::Errors::NiftyCloudResponseError,
49
+ :code => e.error_code,
50
+ :message => e.error_message
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,131 @@
1
+ # -*- coding: utf-8 -*-
2
+ require "log4r"
3
+ require 'vagrant/util/retryable'
4
+
5
+ module VagrantPlugins
6
+ module NiftyCloud
7
+ module Action
8
+ # This runs the configured instance.
9
+ class RunInstance
10
+ include Vagrant::Util::Retryable
11
+
12
+ def initialize(app, env)
13
+ @app = app
14
+ @logger = Log4r::Logger.new("vagrant_niftycloud::action::run_instance")
15
+ end
16
+
17
+ def call(env)
18
+ # Initialize metrics if they haven't been
19
+ env[:metrics] ||= {}
20
+
21
+ # Get the zone we're going to booting up in
22
+ zone = env[:machine].provider_config.zone
23
+
24
+ # Get the configs
25
+ # TODO 開発フェーズなのでinstance_idを固定にしている
26
+ # Vagrantfileで設定できるようにすべきか、その場合Vagrantfileを共有している環境では同じIDでサーバ立てるとエラーになることを考慮する
27
+ instance_id = 'test2'
28
+ zone_config = env[:machine].provider_config.get_zone_config(zone)
29
+ image_id = zone_config.image_id
30
+ zone = zone_config.zone
31
+ instance_type = zone_config.instance_type
32
+ key_name = zone_config.key_name,
33
+ firewall = zone_config.firewall,
34
+ user_data = zone_config.user_data
35
+
36
+ # Launch!
37
+ env[:ui].info(I18n.t("vagrant_niftycloud.launching_instance"))
38
+ env[:ui].info(" -- Server Type: #{instance_type}")
39
+ env[:ui].info(" -- ImageId: #{image_id}")
40
+ env[:ui].info(" -- Zone: #{zone}") if zone
41
+ env[:ui].info(" -- Key Name: #{key_name}") if key_name
42
+ env[:ui].info(" -- User Data: yes") if user_data
43
+ env[:ui].info(" -- Firewall: #{firewall.inspect}") if !firewall.empty?
44
+ env[:ui].info(" -- User Data: #{user_data}") if user_data
45
+
46
+ options = {
47
+ :instance_id => instance_id,
48
+ :availability_zone => zone,
49
+ :instance_type => instance_type,
50
+ :image_id => image_id,
51
+ :key_name => zone_config.key_name,
52
+ :password => 'password',
53
+ :user_data => user_data,
54
+ :accounting_type => 2, #従量課金
55
+ :disable_api_termination => false #APIから即terminate可
56
+ }
57
+
58
+ if !firewall.empty?
59
+ options[:security_group] = firewall
60
+ end
61
+
62
+ # 例外の定義は以下参照
63
+ # http://cloud.nifty.com/api/sdk/rdoc/
64
+ begin
65
+ # インスタンス立ち上げ開始
66
+ server = env[:niftycloud_compute].run_instances(options).instancesSet.item.first
67
+ rescue NIFTY::ConfigurationError => e
68
+ raise VagrantPlugins::NiftyCloud::Errors::NiftyCloudConfigurationError,
69
+ :message => e.message
70
+ rescue NIFTY::ArgumentError => e
71
+ raise VagrantPlugins::NiftyCloud::Errors::NiftyCloudArgumentError,
72
+ :message => e.message
73
+ rescue NIFTY::ResponseFormatError => e
74
+ raise VagrantPlugins::NiftyCloud::Errors::NiftyCloudResponseFormatError,
75
+ :message => e.message
76
+ rescue NIFTY::ResponseError => e
77
+ raise VagrantPlugins::NiftyCloud::Errors::NiftyCloudResponseError,
78
+ :code => e.error_code,
79
+ :message => e.error_message
80
+ end
81
+
82
+ # リトライ回数。サーバステータスがrunningになるまで5秒のintervalでdescribe_instancesを実行するので
83
+ # タイムアウト秒数/5を上限回数とする
84
+ tries = zone_config.instance_ready_timeout / 5
85
+ count = 0
86
+ retryable(:on => Errors::InstanceReadyTimeout, :tries => tries) do
87
+ env[:ui].info(I18n.t("vagrant_niftycloud.waiting_for_ready"))
88
+ while server.instanceState.name != 'running'
89
+ next if env[:interrupted]
90
+
91
+ count += 1
92
+ sleep 5
93
+ server = env[:niftycloud_compute].describe_instances(:instance_id => instance_id).reservationSet.item.first.instancesSet.item.first
94
+ if count > tries
95
+ # Delete the instance
96
+ terminate(env)
97
+ # Notify the user
98
+ raise Errors::InstanceReadyTimeout, timeout: zone_config.instance_ready_timeout
99
+ end
100
+ end
101
+ end
102
+
103
+ # Immediately save the ID since it is created at this point.
104
+ env[:machine].id = instance_id
105
+
106
+ # Terminate the instance if we were interrupted
107
+ terminate(env) if env[:interrupted]
108
+
109
+ @app.call(env)
110
+ end
111
+
112
+ def recover(env)
113
+ return if env["vagrant.error"].is_a?(Vagrant::Errors::VagrantError)
114
+
115
+ if env[:machine].provider.state.id != :not_created
116
+ # Undo the import
117
+ terminate(env)
118
+ end
119
+ end
120
+
121
+ def terminate(env)
122
+ destroy_env = env.dup
123
+ destroy_env.delete(:interrupted)
124
+ destroy_env[:config_validate] = false
125
+ destroy_env[:force_confirm_destroy] = true
126
+ env[:action_runner].run(Action.action_destroy, destroy_env)
127
+ end
128
+ end
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,56 @@
1
+ # -*- coding: utf-8 -*-
2
+ require "log4r"
3
+
4
+ module VagrantPlugins
5
+ module NiftyCloud
6
+ module Action
7
+ # This suspend the running instance.
8
+ class SuspendInstance
9
+ def initialize(app, env)
10
+ @app = app
11
+ @logger = Log4r::Logger.new("vagrant_niftycloud::action::suspend_instance")
12
+ end
13
+
14
+ def call(env)
15
+
16
+ # 例外の定義は以下参照
17
+ # http://cloud.nifty.com/api/sdk/rdoc/
18
+ begin
19
+ env[:ui].info(I18n.t("vagrant_niftycloud.suspending"))
20
+
21
+ # 起動直後等、stop処理できないステータスの場合一旦待つ
22
+ server = env[:niftycloud_compute].describe_instances(:instance_id => env[:machine].id).reservationSet.item.first.instancesSet.item.first
23
+ while server.instanceState.name == 'pending'
24
+ sleep 5
25
+ server = env[:niftycloud_compute].describe_instances(:instance_id => env[:machine].id).reservationSet.item.first.instancesSet.item.first
26
+ end
27
+
28
+ if server.instanceState.name != 'stopped'
29
+ env[:niftycloud_compute].stop_instances(:instance_id => env[:machine].id, :force => false)
30
+ while server.instanceState.name != 'stopped'
31
+ sleep 5
32
+ server = env[:niftycloud_compute].describe_instances(:instance_id => env[:machine].id).reservationSet.item.first.instancesSet.item.first
33
+ end
34
+ end
35
+
36
+ @app.call(env)
37
+ rescue NIFTY::ConfigurationError => e
38
+ raise VagrantPlugins::NiftyCloud::Errors::NiftyCloudConfigurationError,
39
+ :message => e.message
40
+ rescue NIFTY::ArgumentError => e
41
+ raise VagrantPlugins::NiftyCloud::Errors::NiftyCloudArgumentError,
42
+ :message => e.message
43
+ rescue NIFTY::ResponseFormatError => e
44
+ raise VagrantPlugins::NiftyCloud::Errors::NiftyCloudResponseFormatError,
45
+ :message => e.message
46
+ rescue NIFTY::ResponseError => e
47
+ raise VagrantPlugins::NiftyCloud::Errors::NiftyCloudResponseError,
48
+ :code => e.error_code,
49
+ :message => e.error_message
50
+ ui.error("Could not locate server '#{env[:machine].id}'. Please verify it was provisioned in the current zone.")
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end