vagrant-cloudcenter 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,137 @@
1
+ require "log4r"
2
+ require "json"
3
+
4
+ require 'vagrant/util/retryable'
5
+
6
+ require 'vagrant-cloudcenter/util/timer'
7
+
8
+ module VagrantPlugins
9
+ module Cloudcenter
10
+ module Action
11
+ # This starts a stopped instance.
12
+ class StartInstance
13
+ include Vagrant::Util::Retryable
14
+
15
+ def initialize(app, env)
16
+ @app = app
17
+ @logger = Log4r::Logger.new("cloudcenter::action::start_instance")
18
+ end
19
+
20
+ def call(env)
21
+ if !File.exists?(env[:machine].provider_config.deployment_config)
22
+ puts "Missing deployment_config file"
23
+ exit
24
+ end
25
+
26
+ countdown = 24
27
+
28
+ if !env[:machine_name]
29
+ deployment_config = JSON.parse(File.read(env[:machine].provider_config.deployment_config))
30
+ env[:machine_name] = deployment_config["name"]
31
+ end
32
+
33
+ access_key = env[:machine].provider_config.access_key
34
+ host_ip = env[:machine].provider_config.host_ip
35
+ username = env[:machine].provider_config.username
36
+
37
+ begin
38
+ encoded = URI.encode("https://#{username}:#{access_key}@#{host_ip}/v2/jobs?search=[deploymentEntity.name,fle,#{env[:machine_name]}]");
39
+
40
+
41
+
42
+ response = JSON.parse(RestClient::Request.execute(
43
+ :method => :get,
44
+ :url => encoded,
45
+ :verify_ssl => false,
46
+ :accept => "json",
47
+ :headers => {"Content-Type" => "application/json"}
48
+
49
+ ));
50
+ if !response["jobs"].empty?
51
+ jobID = response["jobs"][0]["id"]
52
+ end
53
+
54
+ rescue => e
55
+ error = JSON.parse(e.response)
56
+ code = error["errors"][0]["code"]
57
+
58
+ puts "\n Error code: #{error['errors'][0]['code']}\n"
59
+ puts "\n #{error['errors'][0]['message']}\n\n"
60
+
61
+ exit
62
+ end
63
+
64
+ if !jobID.nil?
65
+ begin
66
+ encoded = URI.encode("https://#{username}:#{access_key}@#{host_ip}/v2/jobs/#{jobID}");
67
+
68
+ payload = { "action" => "RESUME" }
69
+
70
+ payload = JSON.generate(payload)
71
+
72
+ response = JSON.parse(RestClient::Request.execute(
73
+ :method => :put,
74
+ :url => encoded,
75
+ :verify_ssl => false,
76
+ :accept => "json",
77
+ :payload => payload,
78
+ :headers => {"Content-Type" => "application/json"}
79
+ ));
80
+
81
+ rescue => e
82
+
83
+ error = JSON.parse(e.response)
84
+ code = error["errors"][0]["code"]
85
+
86
+ puts "\n Error code: #{error['errors'][0]['code']}\n"
87
+ puts "\n #{error['errors'][0]['message']}\n\n"
88
+
89
+ exit
90
+ end
91
+
92
+ while (countdown > 0 )
93
+
94
+ countdown -= 1
95
+
96
+ begin
97
+ encoded = URI.encode("https://#{username}:#{access_key}@#{host_ip}/v2/jobs/#{jobID}");
98
+
99
+ response = JSON.parse(RestClient::Request.execute(
100
+ :method => :get,
101
+ :url => encoded,
102
+ :verify_ssl => false,
103
+ :accept => "json"
104
+
105
+ ))
106
+ rescue => e
107
+ error = JSON.parse(e.response)
108
+ code = error["errors"][0]["code"]
109
+
110
+ puts "\n Error code: #{error['errors'][0]['code']}\n"
111
+ puts "\n #{error['errors'][0]['message']}\n\n"
112
+
113
+ exit
114
+ end
115
+
116
+ if response["deploymentEntity"]["attributes"]["status"] == "Deployed"
117
+ env[:state] = :stopped
118
+ env[:ui].info(I18n.t("cloudcenter.ready"))
119
+ break
120
+ else
121
+ env[:ui].info(I18n.t("cloudcenter.starting"))
122
+ end
123
+
124
+ sleep 20
125
+
126
+ end
127
+
128
+
129
+ end
130
+
131
+
132
+ @app.call(env)
133
+ end
134
+ end
135
+ end
136
+ end
137
+ end
@@ -0,0 +1,129 @@
1
+ require "log4r"
2
+
3
+ module VagrantPlugins
4
+ module Cloudcenter
5
+ module Action
6
+ # This stops the running instance.
7
+ class StopInstance
8
+
9
+ def initialize(app, env)
10
+ @app = app
11
+ @logger = Log4r::Logger.new("cloudcenter::action::stop_instance")
12
+ end
13
+
14
+ def call(env)
15
+ countdown = 24
16
+
17
+ if !File.exists?(env[:machine].provider_config.deployment_config)
18
+ puts "Missing deployment_config file"
19
+ exit
20
+ end
21
+
22
+ if !env[:machine_name]
23
+ deployment_config = JSON.parse(File.read(env[:machine].provider_config.deployment_config))
24
+ env[:machine_name] = deployment_config["name"]
25
+ end
26
+
27
+ access_key = env[:machine].provider_config.access_key
28
+ host_ip = env[:machine].provider_config.host_ip
29
+ username = env[:machine].provider_config.username
30
+
31
+ begin
32
+ encoded = URI.encode("https://#{username}:#{access_key}@#{host_ip}/v2/jobs?search=[deploymentEntity.name,fle,#{env[:machine_name]}]");
33
+
34
+
35
+
36
+ response = JSON.parse(RestClient::Request.execute(
37
+ :method => :get,
38
+ :url => encoded,
39
+ :verify_ssl => false,
40
+ :accept => "json",
41
+ :headers => {"Content-Type" => "application/json"}
42
+
43
+ ));
44
+ if !response["jobs"].empty?
45
+ jobID = response["jobs"][0]["id"]
46
+ end
47
+
48
+ rescue => e
49
+ error = JSON.parse(e.response)
50
+ code = error["errors"][0]["code"]
51
+
52
+ puts "\n Error code: #{error['errors'][0]['code']}\n"
53
+ puts "\n #{error['errors'][0]['message']}\n\n"
54
+
55
+ exit
56
+ end
57
+
58
+ if !jobID.nil?
59
+ begin
60
+ encoded = URI.encode("https://#{username}:#{access_key}@#{host_ip}/v2/jobs/#{jobID}");
61
+
62
+ payload = { "action" => "SUSPEND" }
63
+
64
+ payload = JSON.generate(payload)
65
+
66
+ response = JSON.parse(RestClient::Request.execute(
67
+ :method => :put,
68
+ :url => encoded,
69
+ :verify_ssl => false,
70
+ :accept => "json",
71
+ :payload => payload,
72
+ :headers => {"Content-Type" => "application/json"}
73
+ ));
74
+
75
+ rescue => e
76
+ error = JSON.parse(e.response)
77
+ code = error["errors"][0]["code"]
78
+
79
+ puts "\n Error code: #{error['errors'][0]['code']}\n"
80
+ puts "\n #{error['errors'][0]['message']}\n\n"
81
+
82
+ exit
83
+ end
84
+
85
+ while (countdown > 0 )
86
+
87
+ countdown -= 1
88
+
89
+ begin
90
+ encoded = URI.encode("https://#{username}:#{access_key}@#{host_ip}/v2/jobs/#{jobID}");
91
+
92
+ response = JSON.parse(RestClient::Request.execute(
93
+ :method => :get,
94
+ :url => encoded,
95
+ :verify_ssl => false,
96
+ :accept => "json"
97
+
98
+ ))
99
+ rescue => e
100
+ error = JSON.parse(e.response)
101
+ code = error["errors"][0]["code"]
102
+
103
+ puts "\n Error code: #{error['errors'][0]['code']}\n"
104
+ puts "\n #{error['errors'][0]['message']}\n\n"
105
+
106
+ exit
107
+ end
108
+
109
+ if response["deploymentEntity"]["attributes"]["status"] == "Suspended"
110
+ env[:state] = :stopped
111
+ env[:ui].info(I18n.t("cloudcenter.stopped"))
112
+ break
113
+ else
114
+ env[:ui].info(I18n.t("cloudcenter.stopping"))
115
+ end
116
+
117
+ sleep 20
118
+
119
+ end
120
+
121
+
122
+ end
123
+
124
+ @app.call(env)
125
+ end
126
+ end
127
+ end
128
+ end
129
+ end
@@ -0,0 +1,131 @@
1
+ require "log4r"
2
+
3
+ require 'vagrant/util/retryable'
4
+
5
+ require 'vagrant-cloudcenter/util/timer'
6
+
7
+ module VagrantPlugins
8
+ module Cloudcenter
9
+ module Action
10
+ # This starts a stopped instance.
11
+ class TerminateInstance
12
+ include Vagrant::Util::Retryable
13
+
14
+ def initialize(app, env)
15
+ @app = app
16
+ @logger = Log4r::Logger.new("cloudcenter::action::start_instance")
17
+ end
18
+
19
+ def call(env)
20
+
21
+ if !File.exists?(env[:machine].provider_config.deployment_config)
22
+ puts "Missing deployment_config file"
23
+ exit
24
+ end
25
+
26
+ countdown = 24
27
+
28
+ if !env[:machine_name]
29
+ deployment_config = JSON.parse(File.read(env[:machine].provider_config.deployment_config))
30
+ env[:machine_name] = deployment_config["name"]
31
+ end
32
+
33
+ access_key = env[:machine].provider_config.access_key
34
+ host_ip = env[:machine].provider_config.host_ip
35
+ username = env[:machine].provider_config.username
36
+
37
+ begin
38
+ encoded = URI.encode("https://#{username}:#{access_key}@#{host_ip}/v2/jobs?search=[deploymentEntity.name,fle,#{env[:machine_name]}]");
39
+
40
+ response = JSON.parse(RestClient::Request.execute(
41
+ :method => :get,
42
+ :url => encoded,
43
+ :verify_ssl => false,
44
+ :accept => "json",
45
+ :headers => {"Content-Type" => "application/json"}
46
+
47
+ ));
48
+ if !response["jobs"].empty?
49
+ jobID = response["jobs"][0]["id"]
50
+ end
51
+
52
+ rescue => e
53
+ error = JSON.parse(e.response)
54
+ code = error["errors"][0]["code"]
55
+
56
+ puts "\n Error code: #{error['errors'][0]['code']}\n"
57
+ puts "\n #{error['errors'][0]['message']}\n\n"
58
+
59
+ exit
60
+ end
61
+
62
+ if !jobID.nil?
63
+ begin
64
+ encoded = URI.encode("https://#{username}:#{access_key}@#{host_ip}/v2/jobs/#{jobID}");
65
+
66
+ response = JSON.parse(RestClient::Request.execute(
67
+ :method => :delete,
68
+ :url => encoded,
69
+ :verify_ssl => false,
70
+ :accept => "json"
71
+ ));
72
+
73
+ rescue => e
74
+ error = JSON.parse(e.response)
75
+ code = error["errors"][0]["code"]
76
+
77
+ puts "\n Error code: #{error['errors'][0]['code']}\n"
78
+ puts "\n #{error['errors'][0]['message']}\n\n"
79
+
80
+ exit
81
+
82
+ end
83
+
84
+ while (countdown > 0 )
85
+
86
+ countdown -= 1
87
+
88
+ begin
89
+ encoded = URI.encode("https://#{username}:#{access_key}@#{host_ip}/v2/jobs?search=[deploymentEntity.name,fle,#{env[:machine_name]}]");
90
+
91
+ response = JSON.parse(RestClient::Request.execute(
92
+ :method => :get,
93
+ :url => encoded,
94
+ :verify_ssl => false,
95
+ :accept => "json",
96
+ :headers => {"Content-Type" => "application/json"}
97
+ ));
98
+
99
+ rescue => e
100
+ error = JSON.parse(e.response)
101
+ code = error["errors"][0]["code"]
102
+
103
+ puts "\n Error code: #{error['errors'][0]['code']}\n"
104
+ puts "\n #{error['errors'][0]['message']}\n\n"
105
+
106
+ exit
107
+
108
+ end
109
+
110
+ if response["jobs"].empty?
111
+ env[:state] = nil?
112
+ env[:ui].info(I18n.t("cloudcenter.terminated"))
113
+ break
114
+ else
115
+ env[:ui].info(I18n.t("cloudcenter.terminating"))
116
+ end
117
+
118
+ sleep 20
119
+
120
+ end
121
+
122
+
123
+ end
124
+
125
+
126
+ @app.call(env)
127
+ end
128
+ end
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,229 @@
1
+ require "pathname"
2
+
3
+ require "vagrant/action/builder"
4
+ require_relative "action/deploy"
5
+ require_relative "action/read_state"
6
+
7
+ module VagrantPlugins
8
+ module Cloudcenter
9
+ module Action
10
+ # Include the built-in modules so we can use them as top-level things.
11
+ include Vagrant::Action::Builtin
12
+
13
+ action_root = Pathname.new(File.expand_path("../action", __FILE__))
14
+ autoload :Deploy, action_root.join("deploy")
15
+ autoload :IsCreated, action_root.join("is_created")
16
+ autoload :IsStopped, action_root.join("is_stopped")
17
+ autoload :MessageAlreadyCreated, action_root.join("message_already_created")
18
+ autoload :MessageNotCreated, action_root.join("message_not_created")
19
+ autoload :PackageInstance, action_root.join("package_instance")
20
+ autoload :ReadSSHInfo, action_root.join("read_ssh_info")
21
+ autoload :ReadState, action_root.join("read_state")
22
+ autoload :RunInstance, action_root.join("run_instance")
23
+ autoload :StartInstance, action_root.join("start_instance")
24
+ autoload :StopInstance, action_root.join("stop_instance")
25
+ autoload :TerminateInstance, action_root.join("terminate_instance")
26
+ autoload :TimedProvision, action_root.join("timed_provision") # some plugins now expect this action to exist
27
+ autoload :WaitForState, action_root.join("wait_for_state")
28
+ autoload :WarnNetworks, action_root.join("warn_networks")
29
+
30
+ def self.action_prepare_boot
31
+ Vagrant::Action::Builder.new.tap do |b|
32
+ b.use Provision
33
+ b.use SyncedFolders
34
+ end
35
+ end
36
+
37
+ # This action is called to destroy the remote machine.
38
+ def self.action_destroy
39
+ Vagrant::Action::Builder.new.tap do |b|
40
+ b.use Call, DestroyConfirm do |env, b1|
41
+ if env[:result]
42
+ b1.use ConfigValidate
43
+ b1.use Call, IsCreated do |env, b2|
44
+ if env[:result] != :created
45
+ env[:ui].info(I18n.t("cloudcenter.not_created"))
46
+ else
47
+ b2.use TerminateInstance
48
+ end
49
+ end
50
+ else
51
+ env[:ui].info(I18n.t("cloudcenter.will_not_destroy", name: env[:machine].name))
52
+ end
53
+
54
+ end
55
+ end
56
+ end
57
+
58
+ # This action is called to halt the remote machine.
59
+ def self.action_halt
60
+ Vagrant::Action::Builder.new.tap do |b|
61
+ b.use ConfigValidate
62
+ b.use Call, IsCreated do |env, b1|
63
+ if env[:result] != :created
64
+ env[:ui].info(I18n.t("cloudcenter.not_created"))
65
+ else
66
+ b1.use Call, IsStopped do |env2, b2|
67
+ if env2[:result] != :stopped
68
+ env2[:ui].info(I18n.t("cloudcenter.stopping"))
69
+ b2.use StopInstance
70
+ else
71
+ env2[:ui].info(I18n.t("cloudcenter.already_stopped"))
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
78
+
79
+ # This action is called to resume the remote machine.
80
+ def self.action_resume
81
+ Vagrant::Action::Builder.new.tap do |b|
82
+ b.use ConfigValidate
83
+ b.use Call, IsCreated do |env, b1|
84
+ if env[:result] != :created
85
+ env[:ui].info(I18n.t("cloudcenter.not_created"))
86
+ else
87
+ b1.use Call, IsStopped do |env2, b2|
88
+ if env2[:result] == :stopped
89
+ env2[:ui].info(I18n.t("cloudcenter.starting"))
90
+ b2.use action_prepare_boot
91
+ b2.use StartInstance
92
+ else
93
+ env2[:ui].info(I18n.t("cloudcenter.already_running"))
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
99
+ end
100
+
101
+
102
+ # This action is called to read the SSH info of the machine. The
103
+ # resulting state is expected to be put into the `:machine_ssh_info`
104
+ # key.
105
+ def self.action_read_ssh_info
106
+ Vagrant::Action::Builder.new.tap do |b|
107
+ b.use ConfigValidate
108
+ b.use ReadSSHInfo
109
+ end
110
+ end
111
+
112
+ # This action is called to SSH into the machine.
113
+ def self.action_ssh
114
+ Vagrant::Action::Builder.new.tap do |b|
115
+ b.use ConfigValidate
116
+
117
+ b.use Call, IsCreated do |env, b2|
118
+
119
+ if env[:result] != :created
120
+ env[:ui].info(I18n.t("cloudcenter.not_created"))
121
+ else
122
+ b2.use ReadSSHInfo
123
+ b2.use SSHExec
124
+ end
125
+
126
+ end
127
+ end
128
+ end
129
+
130
+ # This action is called to read the state of the machine. The
131
+ # resulting state is expected to be put into the `:machine_state_id`
132
+ # key.
133
+ def self.action_read_state
134
+ Vagrant::Action::Builder.new.tap do |b|
135
+ b.use ConfigValidate
136
+ b.use ReadState
137
+ end
138
+ end
139
+
140
+ # This action is called to bring the box up from nothing.
141
+ def self.action_up
142
+ Vagrant::Action::Builder.new.tap do |b|
143
+ b.use HandleBox
144
+ b.use ConfigValidate
145
+ b.use BoxCheckOutdated
146
+ b.use ReadState
147
+ b.use Call, IsCreated do |env1, b1|
148
+ if env1[:result] == :created
149
+ b1.use Call, IsStopped do |env2, b2|
150
+ if env2[:result] == :stopped
151
+ env2[:ui].info(I18n.t("cloudcenter.starting"))
152
+ b2.use action_prepare_boot
153
+ b2.use StartInstance
154
+ else
155
+ env2[:ui].info(I18n.t("cloudcenter.already_running"))
156
+ end
157
+ end
158
+ else
159
+ env1[:ui].info(I18n.t("cloudcenter.deploying"))
160
+ b1.use action_prepare_boot
161
+ b1.use Deploy
162
+ end
163
+ end
164
+
165
+ end
166
+ end
167
+
168
+ def self.action_sync
169
+ Vagrant::Action::Builder.new.tap do |b|
170
+ b.use ConfigValidate
171
+ b.use Call, IsCreated do |env, b1|
172
+ if env[:result] != :created
173
+ env[:ui].info(I18n.t("cloudcenter.not_created"))
174
+ else
175
+ b1.use ReadSSHInfo
176
+ b1.use SyncedFolders
177
+ end
178
+
179
+ end
180
+ end
181
+ end
182
+
183
+
184
+ # This action is called when `vagrant provision` is called.
185
+ def self.action_provision
186
+ Vagrant::Action::Builder.new.tap do |b|
187
+ b.use ConfigValidate
188
+ b.use Call, IsCreated do |env, b2|
189
+ if env[:result] != :created
190
+ env[:ui].info(I18n.t("cloudcenter.not_created"))
191
+ else
192
+ b2.use Provision
193
+ end
194
+
195
+
196
+ end
197
+ end
198
+ end
199
+
200
+ # The current implementation uses the CloudCenter Jobs API which starts/stops the entire job instead of an individual VM which requires
201
+ # knowledge of the VM ID. There is no reboot/reload option for the jobs API so instead we will stop the job and then start the job
202
+ def self.action_reload
203
+ Vagrant::Action::Builder.new.tap do |b|
204
+ b.use ConfigValidate
205
+ b.use Call, IsCreated do |env1, b1|
206
+ if env1[:result] == :created
207
+ b1.use Call, IsStopped do |env2, b2|
208
+ if env2[:result] == :stopped
209
+ env2[:ui].info(I18n.t("cloudcenter.already_stopped"))
210
+ env2[:ui].info(I18n.t("cloudcenter.starting"))
211
+ b2.use action_prepare_boot
212
+ b2.use StartInstance
213
+ else
214
+ b2.use StopInstance
215
+ b2.use action_prepare_boot
216
+ b2.use StartInstance
217
+ end
218
+ end
219
+ else
220
+ env[:ui].info(I18n.t("cloudcenter.not_created"))
221
+ end
222
+ end
223
+
224
+ end
225
+ end
226
+
227
+ end
228
+ end
229
+ end
@@ -0,0 +1,74 @@
1
+ module VagrantPlugins
2
+ module Cloudcenter
3
+ module Command
4
+ class App < Vagrant.plugin("2", :command)
5
+ def self.synopsis
6
+ "Retrieve application details"
7
+ end
8
+
9
+ def execute
10
+
11
+ RestClient.log = 'stdout'
12
+ # Get the rest API key for authentication
13
+
14
+
15
+ host_ip = ENV['host_ip']
16
+ access_key = ENV['access_key']
17
+ username = ENV['username']
18
+
19
+ options = {}
20
+ options[:force] = false
21
+
22
+ opts = OptionParser.new do |o|
23
+ o.banner = "Usage: vagrant cloudcenter app [application-id]"
24
+ o.separator ""
25
+
26
+ end
27
+
28
+ # Parse the options
29
+ argv = parse_options(opts)
30
+
31
+ puts argv[0]
32
+
33
+ begin
34
+
35
+ if argv[0] && argv[0].match(/\A\d+\z/)
36
+
37
+ encoded = URI.encode("https://#{username}:#{access_key}@#{host_ip}/v1/apps/#{argv[0]}");
38
+
39
+ catalog = JSON.parse(RestClient::Request.execute(
40
+ :method => :get,
41
+ :url => encoded,
42
+ :verify_ssl => false,
43
+ :content_type => "json",
44
+ :accept => "json"
45
+ ));
46
+ puts JSON.pretty_generate(catalog)
47
+
48
+ end
49
+ rescue => e
50
+
51
+ if e.inspect == "Timed out connecting to server"
52
+ puts "\n#ConnectionError - Unable to connnect to CloudCenter Manager \n"
53
+ exit
54
+ else
55
+ error = JSON.parse(e.response)
56
+ code = error["errors"][0]["code"]
57
+
58
+ puts "\n Error code: #{error['errors'][0]['code']}\n"
59
+ puts "\n #{error['errors'][0]['message']}\n\n"
60
+
61
+ exit
62
+ end
63
+ end
64
+
65
+
66
+ 0
67
+
68
+
69
+
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end