Class: RightApiProvision::API15
- Inherits:
-
Object
- Object
- RightApiProvision::API15
- Defined in:
- lib/right_api_provision/api15.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
Instance Method Summary collapse
- #connection(email, password, account_id, api_url = nil) ⇒ Object
- #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_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
Find SSH key.
-
#initialize ⇒ API15
constructor
A new instance of API15.
- #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, filter_value) ⇒ Object
- #list_deployments(filter_by, filter_value) ⇒ 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
Constructor Details
#initialize ⇒ API15
Returns a new instance of API15.
24 25 26 |
# File 'lib/right_api_provision/api15.rb', line 24 def initialize require "right_api_client" end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
22 23 24 |
# File 'lib/right_api_provision/api15.rb', line 22 def client @client end |
Instance Method Details
#connection(email, password, account_id, api_url = nil) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/right_api_provision/api15.rb', line 28 def connection(email, password, account_id, api_url = nil) begin args = { :email => email, :password => password, :account_id => account_id } @url = api_url args[:api_url] = @url if @url @connection ||= RightApi::Client.new(args) #@logger = Logger.new(STDOUT) #@logger.level = Logger::DEBUG #@connection.log(@logger) @client = @connection rescue Exception => e args.delete(:password) # don't log password puts "ERROR: could not connect to RightScale API. Params: #{args.inspect}" puts e. puts e.backtrace raise e end end |
#create_deployment(name) ⇒ Object
191 192 193 |
# File 'lib/right_api_provision/api15.rb', line 191 def create_deployment(name) @connection.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
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 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/right_api_provision/api15.rb', line 199 def create_server(deployment, server_template, mci, cloud, name, ssh_key = nil, groups = nil) # check params 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 # Use the MCI if provided otherwise let the API choose the default MCI # in the ServerTemplate. instance_hash[:multi_cloud_image_href] = mci_href unless mci_href.nil? # create server in deployment using specfied ST server = @connection.servers.create({ :server => { :name => name, :decription => "Created by the right_provision_api", #TODO: pass this as a param :deployment_href => d_href, :instance => instance_hash } }) end |
#data_request_url(userdata) ⇒ Object
93 94 95 96 97 98 99 100 |
# File 'lib/right_api_provision/api15.rb', line 93 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] "#{@url}/servers/data_injection_payload/#{token}" end |
#delete_server(name) ⇒ Object
102 103 104 105 106 107 108 109 110 111 |
# File 'lib/right_api_provision/api15.rb', line 102 def delete_server(name) server = find_server_by_name(name) server.terminate begin server_wait_for_state(server, "terminated") rescue Exception => e end server.destroy end |
#destroy_deployment(deployment) ⇒ Object
195 196 197 |
# File 'lib/right_api_provision/api15.rb', line 195 def destroy_deployment(deployment) deployment.destroy end |
#find_cloud_by_name(name) ⇒ Object
- returns
-
String if cloud is found, nil if not found
150 151 152 |
# File 'lib/right_api_provision/api15.rb', line 150 def find_cloud_by_name(name) find_resource(:clouds, :by_name, name) end |
#find_deployment_by_name(name) ⇒ Object
145 146 147 |
# File 'lib/right_api_provision/api15.rb', line 145 def find_deployment_by_name(name) find_resource(:deployments, :by_name, name) end |
#find_mci_by_name(name) ⇒ Object
154 155 156 |
# File 'lib/right_api_provision/api15.rb', line 154 def find_mci_by_name(name) find_resource(:multi_cloud_images, :by_name, name) end |
#find_security_group_by_name(cloud, security_group_name) ⇒ Object
137 138 139 |
# File 'lib/right_api_provision/api15.rb', line 137 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
141 142 143 |
# File 'lib/right_api_provision/api15.rb', line 141 def find_server_by_name(name) find_resource(:servers, :by_name, name) end |
#find_servertemplate(name_or_id) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/right_api_provision/api15.rb', line 158 def find_servertemplate(name_or_id) server_template = nil; id = nil; name = nil # detect if user passed in a name or an id # there is probably a cleaner way to do this, but I am lazy ATM. begin id = Integer(name_or_id) rescue Exception => e name = name_or_id # Cannot be case to integer, assume a name was passed end if name # find ServerTemplate by name st_list = list_resources(:server_templates, :by_name, name) revisions = st_list.map { |st| st.revision } # check for duplicate revisions duplicates = (revisions.size != revisions.uniq.size) raise "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 # always use latest revision latest_rev = revisions.sort.last server_template = st_list.select { |st| st.revision == latest_rev}.first raise "ERROR: Unable to find ServerTemplate with the name of '#{name}' found " unless server_template else # find ServerTemplate by id server_template = @connection.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.
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/right_api_provision/api15.rb', line 65 def find_ssh_key_by_uuid_or_first(cloud, ssh_uuid = nil) ssh_key = nil if ssh_uuid # grab specific ssh key sshkey = find_resource(:ssh_keys, :by_resource_uid, uuid) else # grab first key found keys = cloud.show.ssh_keys ssh_key = keys.index.first if keys end ssh_key end |
#is_bad?(state) ⇒ Boolean
295 296 297 298 |
# File 'lib/right_api_provision/api15.rb', line 295 def is_bad?(state) @bad_states ||= [] @bad_states.select{|s| state =~ /#{s}/}.size > 0 end |
#is_provisioned?(server) ⇒ Boolean
260 261 262 |
# File 'lib/right_api_provision/api15.rb', line 260 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”}
265 266 267 268 269 270 |
# File 'lib/right_api_provision/api15.rb', line 265 def launch_server(server, inputs = { :name => "text:dummy"}) server_name = server.show.name server.launch(inputs) # TODO: parse inputs from Vagrantfile # XXX: need to create a new server object after launch -- why? API bug? find_server_by_name(server_name) end |
#list_clouds(filter_by, filter_value) ⇒ Object
121 122 123 |
# File 'lib/right_api_provision/api15.rb', line 121 def list_clouds(filter_by, filter_value) list_resources(:clouds, filter_by, filter_value) end |
#list_deployments(filter_by, filter_value) ⇒ Object
117 118 119 |
# File 'lib/right_api_provision/api15.rb', line 117 def list_deployments(filter_by, filter_value) list_resources(:deployments, filter_by, filter_value) end |
#list_multi_cloud_images(server_template, filter_by, filter_value) ⇒ Object
133 134 135 |
# File 'lib/right_api_provision/api15.rb', line 133 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
129 130 131 |
# File 'lib/right_api_provision/api15.rb', line 129 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
113 114 115 |
# File 'lib/right_api_provision/api15.rb', line 113 def list_servers(filter_by, filter_value) list_resources(:servers, filter_by, filter_value) end |
#list_servertemplates(filter_by, filter_value) ⇒ Object
125 126 127 |
# File 'lib/right_api_provision/api15.rb', line 125 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.
80 81 82 83 84 85 86 87 |
# File 'lib/right_api_provision/api15.rb', line 80 def requires_security_groups?(cloud) begin cloud.show.security_groups true rescue RightApi::ApiError => e false # assume cloud does not require them end end |
#requires_ssh_keys?(cloud) ⇒ Boolean
If the cloud reports ssh keys, then we assume it requires them to launch servers.
49 50 51 52 53 54 55 56 |
# File 'lib/right_api_provision/api15.rb', line 49 def requires_ssh_keys?(cloud) begin cloud.show.ssh_keys true rescue RightApi::ApiError => e false # assume cloud does not require them end end |
#server_cloud_name(server) ⇒ Object
304 305 306 307 308 |
# File 'lib/right_api_provision/api15.rb', line 304 def server_cloud_name(server) instance = instance_from_server(server) cloud = cloud_from_instance(instance) cloud.show.name end |
#server_info(server) ⇒ Object
310 311 312 |
# File 'lib/right_api_provision/api15.rb', line 310 def server_info(server) server.show.current_instance.show(:view => 'extended') end |
#server_ready?(server) ⇒ Boolean
300 301 302 |
# File 'lib/right_api_provision/api15.rb', line 300 def server_ready?(server) server_state(server) == "operational" end |
#server_wait_for_state(server, target_state, delay = 10) ⇒ Object
281 282 283 284 285 286 287 288 289 |
# File 'lib/right_api_provision/api15.rb', line 281 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
291 292 293 |
# File 'lib/right_api_provision/api15.rb', line 291 def set_bad_states(list_array) @bad_states = list_array end |
#set_server_inputs(server, inputs) ⇒ Object
Only use this before you launch the server
277 278 279 |
# File 'lib/right_api_provision/api15.rb', line 277 def set_server_inputs(server, inputs) server.show.next_instance.show.inputs.multi_update({"inputs" => inputs}) end |
#terminate_server(server) ⇒ Object
272 273 274 |
# File 'lib/right_api_provision/api15.rb', line 272 def terminate_server(server) server.terminate end |
#user_data(server) ⇒ Object
89 90 91 |
# File 'lib/right_api_provision/api15.rb', line 89 def user_data(server) @user_data ||= server.show.current_instance(:view=>"extended").show.user_data end |