Class: RightApiHelper::API15
- Inherits:
-
Base
- Object
- Base
- RightApiHelper::API15
show all
- Defined in:
- lib/right_api_helper/api15.rb
Instance Method Summary
collapse
-
#create_deployment(name) ⇒ Object
-
#create_server(deployment, server_template, mci, cloud, name, ssh_key = nil, groups = nil) ⇒ Object
-
#data_request_url(userdata) ⇒ Object
-
#delete_server(name) ⇒ Object
-
#destroy_deployment(deployment) ⇒ Object
-
#find_cloud_by_name(name) ⇒ Object
- returns
-
String if cloud is found, nil if not found.
-
#find_deployment_by_name(name) ⇒ Object
-
#find_mci_by_name(name) ⇒ Object
-
#find_security_group_by_id(cloud, security_group_uid) ⇒ Object
-
#find_security_group_by_name(cloud, security_group_name) ⇒ Object
-
#find_server_by_name(name) ⇒ Object
-
#find_servertemplate(name_or_id) ⇒ Object
-
#find_ssh_key_by_uuid_or_first(cloud, ssh_uuid = nil) ⇒ Object
-
#is_bad?(state) ⇒ Boolean
-
#is_provisioned?(server) ⇒ Boolean
-
#launch_server(server, inputs = { :name => "text:dummy"}) ⇒ Object
@param(Hash) inputs Hash input name/value pairs i.e.
-
#list_clouds(filter_by = nil, filter_value = nil) ⇒ Object
-
#list_deployments(filter_by = nil, filter_value = nil) ⇒ Object
-
#list_multi_cloud_images(server_template, filter_by, filter_value) ⇒ Object
-
#list_security_groups(cloud, filter_by, filter_value) ⇒ Object
-
#list_servers(filter_by, filter_value) ⇒ Object
-
#list_servertemplates(filter_by, filter_value) ⇒ Object
-
#requires_security_groups?(cloud) ⇒ Boolean
If the cloud reports security groups then we assume it requires them to launch servers.
-
#requires_ssh_keys?(cloud) ⇒ Boolean
If the cloud reports ssh keys, then we assume it requires them to launch servers.
-
#server_cloud_name(server) ⇒ Object
-
#server_info(server) ⇒ Object
-
#server_ready?(server) ⇒ Boolean
-
#server_wait_for_state(server, target_state, delay = 10) ⇒ Object
-
#set_bad_states(list_array) ⇒ Object
-
#set_server_inputs(server, inputs) ⇒ Object
Only use this before you launch the server.
-
#terminate_server(server) ⇒ Object
-
#user_data(server) ⇒ Object
Methods inherited from Base
#api_url, #initialize, #log_level, #logger
Instance Method Details
#create_deployment(name) ⇒ Object
173
174
175
|
# File 'lib/right_api_helper/api15.rb', line 173
def create_deployment(name)
@client.deployments.create(:deployment => { :name => name, :decription => "Created by the Vagrant"})
end
|
#create_server(deployment, server_template, mci, cloud, name, ssh_key = nil, groups = nil) ⇒ Object
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
# File 'lib/right_api_helper/api15.rb', line 181
def create_server(deployment, server_template, mci, cloud, name, ssh_key = nil, groups = nil)
unless st_href = server_template.show.href
raise "ERROR: ServerTemplate parameter not initialized properly"
end
unless mci.nil?
unless mci_href = mci.show.href
raise "ERROR: Multi Cloud Image parameter not initialized properly"
end
end
unless d_href = deployment.show.href
raise "ERROR: Deployment parameter not initialized properly"
end
unless c_href = cloud.show.href
raise "ERROR: Deployment parameter not initialized properly"
end
if ssh_key
unless ssh_key_href = ssh_key.show.href
raise "ERROR: ssh_key parameter not initialized properly"
end
end
security_group_hrefs = nil
if groups
security_group_hrefs = []
groups.each do |group|
unless group_href = group.show.href
raise "ERROR: ssh_key parameter not initialized properly"
end
security_group_hrefs << group_href
end
end
instance_hash = {
:cloud_href => c_href,
:server_template_href => st_href
}
instance_hash[:ssh_key_href] = ssh_key_href if ssh_key
instance_hash[:security_group_hrefs] = security_group_hrefs if security_group_hrefs
instance_hash[:multi_cloud_image_href] = mci_href unless mci_href.nil?
server =
@client.servers.create({
:server => {
:name => name,
:decription => "Created by the right_provision_api",
:deployment_href => d_href,
:instance => instance_hash
}
})
end
|
#data_request_url(userdata) ⇒ Object
70
71
72
73
74
75
76
77
|
# File 'lib/right_api_helper/api15.rb', line 70
def data_request_url(userdata)
data_hash = {}
entry = userdata.split('&').select { |entry| entry =~ /RS_rn_auth/i }
raise "ERROR: user data token not found. " +
"Does your MCI have a provides:rs_agent_type=right_link tag?" unless entry
token = entry.first.split('=')[1]
"#{api_url}/servers/data_injection_payload/#{token}"
end
|
#delete_server(name) ⇒ Object
79
80
81
82
83
84
85
86
87
|
# File 'lib/right_api_helper/api15.rb', line 79
def delete_server(name)
server = find_server_by_name(name)
begin
server.terminate
server_wait_for_state(server, "terminated")
rescue
end
server.destroy
end
|
#destroy_deployment(deployment) ⇒ Object
177
178
179
|
# File 'lib/right_api_helper/api15.rb', line 177
def destroy_deployment(deployment)
deployment.destroy
end
|
#find_cloud_by_name(name) ⇒ Object
- returns
-
String if cloud is found, nil if not found
130
131
132
|
# File 'lib/right_api_helper/api15.rb', line 130
def find_cloud_by_name(name)
find_resource(:clouds, :by_name, name)
end
|
#find_deployment_by_name(name) ⇒ Object
125
126
127
|
# File 'lib/right_api_helper/api15.rb', line 125
def find_deployment_by_name(name)
find_resource(:deployments, :by_name, name)
end
|
#find_mci_by_name(name) ⇒ Object
134
135
136
|
# File 'lib/right_api_helper/api15.rb', line 134
def find_mci_by_name(name)
find_resource(:multi_cloud_images, :by_name, name)
end
|
#find_security_group_by_id(cloud, security_group_uid) ⇒ Object
117
118
119
|
# File 'lib/right_api_helper/api15.rb', line 117
def find_security_group_by_id(cloud, security_group_uid)
find_cloud_resource(cloud, :security_groups, :by_resource_uid, security_group_uid)
end
|
#find_security_group_by_name(cloud, security_group_name) ⇒ Object
113
114
115
|
# File 'lib/right_api_helper/api15.rb', line 113
def find_security_group_by_name(cloud, security_group_name)
find_cloud_resource(cloud, :security_groups, :by_name, security_group_name)
end
|
#find_server_by_name(name) ⇒ Object
121
122
123
|
# File 'lib/right_api_helper/api15.rb', line 121
def find_server_by_name(name)
find_resource(:servers, :by_name, name)
end
|
#find_servertemplate(name_or_id) ⇒ Object
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
# File 'lib/right_api_helper/api15.rb', line 138
def find_servertemplate(name_or_id)
server_template = nil; id = nil; name = nil
begin
id = Integer(name_or_id)
rescue Exception => e
name = name_or_id
end
if name
@log.debug "ServerTemplate name detected."
st_list = list_resources(:server_templates, :by_name, name)
revisions = st_list.map { |st| st.revision }
duplicates = (revisions.size != revisions.uniq.size)
raise RightScaleError, "ERROR: Duplicate ServerTemplate with the name of '#{name}' detected " +
"in account -- there can be only one. Please fix via the RightScale dashboard and retry." if duplicates
latest_rev = revisions.sort.last
server_template = st_list.select { |st| st.revision == latest_rev}.first
raise RightScaleError, "ERROR: Unable to find ServerTemplate with the name of '#{name}' found " unless server_template
else
@log.debug "Looking up ServerTemplate by ID."
server_template = @client.server_templates.index(:id => id)
end
server_template
end
|
#find_ssh_key_by_uuid_or_first(cloud, ssh_uuid = nil) ⇒ Object
Find SSH key
EC2 and Eucalyptus require an SSH key to launch a server. RightScale manages SSH keys for each user so just grabbing the first one is fine, however older configurations might relay on specific keys. You will need to grab the resource UUID from the RightScale dashboard for the key that you want to use.
42
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/right_api_helper/api15.rb', line 42
def find_ssh_key_by_uuid_or_first(cloud, ssh_uuid = nil)
ssh_key = nil
if ssh_uuid
sshkey = find_cloud_resource(cloud, :ssh_keys, :by_resource_uid, ssh_uuid)
else
keys = cloud.show.ssh_keys
ssh_key = keys.index.first if keys
end
ssh_key
end
|
#is_bad?(state) ⇒ Boolean
277
278
279
280
|
# File 'lib/right_api_helper/api15.rb', line 277
def is_bad?(state)
@bad_states ||= []
@bad_states.select{|s| state =~ /#{s}/}.size > 0
end
|
#is_provisioned?(server) ⇒ Boolean
242
243
244
|
# File 'lib/right_api_helper/api15.rb', line 242
def is_provisioned?(server)
server.show.api_methods.include?(:current_instance)
end
|
#launch_server(server, inputs = { :name => "text:dummy"}) ⇒ Object
@param(Hash) inputs Hash input name/value pairs i.e. { :name => “text:dummy”}
247
248
249
250
251
252
|
# File 'lib/right_api_helper/api15.rb', line 247
def launch_server(server, inputs = { :name => "text:dummy"})
server_name = server.show.name
server.launch(inputs)
find_server_by_name(server_name)
end
|
#list_clouds(filter_by = nil, filter_value = nil) ⇒ Object
97
98
99
|
# File 'lib/right_api_helper/api15.rb', line 97
def list_clouds(filter_by=nil, filter_value=nil)
list_resources(:clouds, filter_by, filter_value)
end
|
#list_deployments(filter_by = nil, filter_value = nil) ⇒ Object
93
94
95
|
# File 'lib/right_api_helper/api15.rb', line 93
def list_deployments(filter_by=nil, filter_value=nil)
list_resources(:deployments, filter_by, filter_value)
end
|
#list_multi_cloud_images(server_template, filter_by, filter_value) ⇒ Object
109
110
111
|
# File 'lib/right_api_helper/api15.rb', line 109
def list_multi_cloud_images(server_template, filter_by, filter_value)
list_subresources(server_template, :multi_cloud_images, filter_by, filter_value)
end
|
#list_security_groups(cloud, filter_by, filter_value) ⇒ Object
105
106
107
|
# File 'lib/right_api_helper/api15.rb', line 105
def list_security_groups(cloud, filter_by, filter_value)
list_subresources(cloud, :security_groups, filter_by, filter_value)
end
|
#list_servers(filter_by, filter_value) ⇒ Object
89
90
91
|
# File 'lib/right_api_helper/api15.rb', line 89
def list_servers(filter_by, filter_value)
list_resources(:servers, filter_by, filter_value)
end
|
#list_servertemplates(filter_by, filter_value) ⇒ Object
101
102
103
|
# File 'lib/right_api_helper/api15.rb', line 101
def list_servertemplates(filter_by, filter_value)
list_resources(:server_templates, filter_by, filter_value)
end
|
#requires_security_groups?(cloud) ⇒ Boolean
If the cloud reports security groups then we assume it requires them to launch servers.
57
58
59
60
61
62
63
64
|
# File 'lib/right_api_helper/api15.rb', line 57
def requires_security_groups?(cloud)
begin
cloud.show.security_groups
true
rescue RightApi::ApiError => e
false
end
end
|
#requires_ssh_keys?(cloud) ⇒ Boolean
If the cloud reports ssh keys, then we assume it requires them to launch servers.
26
27
28
29
30
31
32
33
|
# File 'lib/right_api_helper/api15.rb', line 26
def requires_ssh_keys?(cloud)
begin
cloud.show.ssh_keys
true
rescue RightApi::ApiError => e
false
end
end
|
#server_cloud_name(server) ⇒ Object
286
287
288
289
290
|
# File 'lib/right_api_helper/api15.rb', line 286
def server_cloud_name(server)
instance = instance_from_server(server)
cloud = cloud_from_instance(instance)
cloud.show.name
end
|
#server_info(server) ⇒ Object
292
293
294
|
# File 'lib/right_api_helper/api15.rb', line 292
def server_info(server)
server.show.current_instance.show(:view => 'extended')
end
|
#server_ready?(server) ⇒ Boolean
282
283
284
|
# File 'lib/right_api_helper/api15.rb', line 282
def server_ready?(server)
server_state(server) == "operational"
end
|
#server_wait_for_state(server, target_state, delay = 10) ⇒ Object
263
264
265
266
267
268
269
270
271
|
# File 'lib/right_api_helper/api15.rb', line 263
def server_wait_for_state(server, target_state, delay = 10)
current_state = server_state(server)
while current_state != target_state
raise "Unexpected sever state: #{current_state}" if is_bad?(current_state)
puts "Server #{current_state}. Waiting for instance to be in #{target_state} state..."
sleep delay
current_state = server_state(server)
end
end
|
#set_bad_states(list_array) ⇒ Object
273
274
275
|
# File 'lib/right_api_helper/api15.rb', line 273
def set_bad_states(list_array)
@bad_states = list_array
end
|
Only use this before you launch the server
259
260
261
|
# File 'lib/right_api_helper/api15.rb', line 259
def set_server_inputs(server, inputs)
server.show.next_instance.show.inputs.multi_update({"inputs" => inputs})
end
|
#terminate_server(server) ⇒ Object
254
255
256
|
# File 'lib/right_api_helper/api15.rb', line 254
def terminate_server(server)
server.terminate
end
|
#user_data(server) ⇒ Object
66
67
68
|
# File 'lib/right_api_helper/api15.rb', line 66
def user_data(server)
@user_data ||= server.show.current_instance(:view=>"extended").show.user_data
end
|