right_api_helper 1.1.0 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +4 -0
- data/lib/right_api_helper/api15.rb +4 -2
- data/lib/right_api_helper/exception.rb +30 -0
- data/lib/right_api_helper/provisioner.rb +18 -15
- data/lib/right_api_helper/version.rb +1 -1
- data/lib/right_api_helper.rb +1 -0
- data/test/provisioner_test.rb +39 -0
- metadata +4 -1
data/Rakefile
CHANGED
@@ -144,20 +144,22 @@ module RightApiHelper
|
|
144
144
|
end
|
145
145
|
|
146
146
|
if name
|
147
|
+
@log.info "ServerTemplate name detected."
|
147
148
|
# find ServerTemplate by name
|
148
149
|
st_list = list_resources(:server_templates, :by_name, name)
|
149
150
|
revisions = st_list.map { |st| st.revision }
|
150
151
|
|
151
152
|
# check for duplicate revisions
|
152
153
|
duplicates = (revisions.size != revisions.uniq.size)
|
153
|
-
raise "ERROR: Duplicate ServerTemplate with the name of '#{name}' detected " +
|
154
|
+
raise RightScaleError, "ERROR: Duplicate ServerTemplate with the name of '#{name}' detected " +
|
154
155
|
"in account -- there can be only one. Please fix via the RightScale dashboard and retry." if duplicates
|
155
156
|
|
156
157
|
# always use latest revision
|
157
158
|
latest_rev = revisions.sort.last
|
158
159
|
server_template = st_list.select { |st| st.revision == latest_rev}.first
|
159
|
-
raise "ERROR: Unable to find ServerTemplate with the name of '#{name}' found " unless server_template
|
160
|
+
raise RightScaleError, "ERROR: Unable to find ServerTemplate with the name of '#{name}' found " unless server_template
|
160
161
|
else
|
162
|
+
@log.info "Looking up ServerTemplate by ID."
|
161
163
|
# find ServerTemplate by id
|
162
164
|
server_template = @client.server_templates.index(:id => id)
|
163
165
|
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
#
|
2
|
+
# Author: cary@rightscale.com
|
3
|
+
# Copyright 2014 RightScale, Inc.
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
#
|
17
|
+
|
18
|
+
module RightApiHelper
|
19
|
+
class RightScaleError < StandardError
|
20
|
+
end
|
21
|
+
|
22
|
+
class MultipleMatchesFound < RightScaleError
|
23
|
+
def initialize(resource, key, value)
|
24
|
+
key = key.to_s.delete("by_") # remove the 'by_' prefix
|
25
|
+
msg = "More than one #{resource} with the #{key} of '#{value}'. " +
|
26
|
+
"Please resolve via the RightScale dashboard and retry."
|
27
|
+
super msg
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -74,11 +74,11 @@ module RightApiHelper
|
|
74
74
|
end
|
75
75
|
|
76
76
|
def connection_url
|
77
|
-
raise "No server provisioned. No connection URL available." unless @server
|
77
|
+
raise RightScaleError, "No server provisioned. No connection URL available." unless @server
|
78
78
|
unless @data_request_url
|
79
79
|
user_data = @server.current_instance.show(:view => "full").user_data
|
80
80
|
@data_request_url = @api_shim.data_request_url(user_data)
|
81
|
-
@
|
81
|
+
@log.debug "Data Request URL: #{@data_request_url}"
|
82
82
|
end
|
83
83
|
@data_request_url
|
84
84
|
end
|
@@ -100,8 +100,8 @@ module RightApiHelper
|
|
100
100
|
#
|
101
101
|
# @raise {RightApiProvisionException} if anything
|
102
102
|
# goes wrong
|
103
|
-
def provision(
|
104
|
-
|
103
|
+
def provision(server_name = "default",
|
104
|
+
server_template = nil,
|
105
105
|
cloud_name = "ec2",
|
106
106
|
deployment_name = "default",
|
107
107
|
inputs = nil,
|
@@ -122,30 +122,33 @@ module RightApiHelper
|
|
122
122
|
# Verify ssh key uuid, if required by cloud
|
123
123
|
if @api_shim.requires_ssh_keys?(@cloud)
|
124
124
|
@ssh_key = @api_shim.find_ssh_key_by_uuid_or_first(@cloud, ssh_key_uuid)
|
125
|
-
raise "ERROR: cannot find an ssh_key named: #{ssh_key_uuid}" unless @ssh_key
|
125
|
+
raise RightScaleError, "ERROR: cannot find an ssh_key named: #{ssh_key_uuid}" unless @ssh_key
|
126
126
|
end
|
127
127
|
|
128
128
|
# Verify security group, if required by cloud
|
129
129
|
if @api_shim.requires_security_groups?(@cloud)
|
130
130
|
@sec_groups = []
|
131
|
+
if !security_groups.nil? && !security_groups.is_a?(Array)
|
132
|
+
raise RightScaleError, "security_groups must be an array. You passed a #{security_groups.class}"
|
133
|
+
end
|
131
134
|
security_groups ||= ["default"]
|
132
135
|
security_groups.each do |name|
|
133
136
|
group = @api_shim.find_security_group_by_name(@cloud, name)
|
134
|
-
raise "ERROR: cannot find an security group named: #{name}" unless group
|
137
|
+
raise RightScaleError, "ERROR: cannot find an security group named: #{name}" unless group
|
135
138
|
@sec_groups << group
|
136
139
|
end
|
137
140
|
end
|
138
141
|
|
139
142
|
# check for existing deployment and server in RightScale account
|
140
143
|
@deployment = @api_shim.find_deployment_by_name(deployment_name)
|
141
|
-
@
|
144
|
+
@log.info "Deployment '#{deployment_name}' #{@deployment ? "found." : "not found."}"
|
142
145
|
@server = @api_shim.find_server_by_name(server_name) if @deployment
|
143
|
-
@
|
146
|
+
@log.info "Server '#{server_name}' #{@server ? "found." : "not found."}"
|
144
147
|
|
145
148
|
if @server
|
146
149
|
# verify existing server is on the cloud we are requesting, if not fail.
|
147
150
|
actual_cloud_name = @api_shim.server_cloud_name(@server)
|
148
|
-
raise "ERROR: the server is in the '#{actual_cloud_name}' cloud, " +
|
151
|
+
raise RightScaleError, "ERROR: the server is in the '#{actual_cloud_name}' cloud, " +
|
149
152
|
"and not in the requested '#{cloud_name}' cloud.\n" +
|
150
153
|
"Please delete the server or pick and new server name." if cloud_name != actual_cloud_name
|
151
154
|
end
|
@@ -153,8 +156,8 @@ module RightApiHelper
|
|
153
156
|
unless @deployment && @server
|
154
157
|
# we need to create a server, can we find the servertemplate?
|
155
158
|
begin
|
156
|
-
@servertemplate = @
|
157
|
-
rescue
|
159
|
+
@servertemplate = @api_shim.find_servertemplate(server_template)
|
160
|
+
rescue RightScaleError
|
158
161
|
raise RightScaleError, "ERROR: cannot find ServerTemplate '#{server_template}'. Did you import it?\n" +
|
159
162
|
"Visit http://bit.ly/VnOiA7 for more info.\n\n"
|
160
163
|
end
|
@@ -177,12 +180,12 @@ module RightApiHelper
|
|
177
180
|
# create deployment and server as needed
|
178
181
|
unless @deployment
|
179
182
|
@deployment = @api_shim.create_deployment(deployment_name)
|
180
|
-
@
|
183
|
+
@log.info "Created deployment."
|
181
184
|
end
|
182
185
|
|
183
186
|
unless @server
|
184
187
|
@server = @api_shim.create_server(@deployment, @servertemplate, @mci, @cloud, server_name, @ssh_key, @sec_groups)
|
185
|
-
@
|
188
|
+
@log.info "Created server."
|
186
189
|
end
|
187
190
|
|
188
191
|
unless @api_shim.is_provisioned?(@server)
|
@@ -195,7 +198,7 @@ module RightApiHelper
|
|
195
198
|
end
|
196
199
|
|
197
200
|
# launch server
|
198
|
-
@
|
201
|
+
@log.info "Launching server..."
|
199
202
|
@server = @api_shim.launch_server(@server, inputs)
|
200
203
|
@api_shim.set_bad_states(BAD_STATES_UP)
|
201
204
|
@api_shim.server_wait_for_state(@server, "booting", 30)
|
@@ -215,7 +218,7 @@ module RightApiHelper
|
|
215
218
|
def server_info
|
216
219
|
info = @api_shim.server_info(@server)
|
217
220
|
while info.private_ip_addresses.empty?
|
218
|
-
@
|
221
|
+
@log.info "Waiting for cloud to provide IP address..."
|
219
222
|
sleep 30
|
220
223
|
info = @api_shim.server_info(@server)
|
221
224
|
end
|
data/lib/right_api_helper.rb
CHANGED
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'right_api_helper'
|
2
|
+
require 'yaml'
|
3
|
+
|
4
|
+
describe RightApiHelper::Provisioner do
|
5
|
+
|
6
|
+
before(:all) do
|
7
|
+
@user_secrets = YAML.load(File.read(File.join(ENV['HOME'], '.right_api_client', 'login.yml')))
|
8
|
+
@config = {
|
9
|
+
:servertemplate => 328222001, # "Base ServerTemplate for Linux Alpha (v14.0.0)"
|
10
|
+
:deployment_name => "CP: right_api_provision testbed -- deleteme!",
|
11
|
+
:server_name => "CP: Test: API Provisioned Server",
|
12
|
+
:cloud_name => "EC2 us-east-1"
|
13
|
+
}
|
14
|
+
end
|
15
|
+
|
16
|
+
it "provisions a server" do
|
17
|
+
puts "Provisioning server in '#{@user_secrets[:account_id]}' account..."
|
18
|
+
|
19
|
+
right_api_client = RightApiHelper::Session.new.create_client_from_file("~/.right_api_client/login.yml")
|
20
|
+
|
21
|
+
# initialize rightscale provisioner
|
22
|
+
@rightscale =
|
23
|
+
RightApiHelper::Provisioner.new(right_api_client)
|
24
|
+
|
25
|
+
# provision a RightScale managed server from a ServerTemplate
|
26
|
+
@rightscale.provision(@config[:server_name],
|
27
|
+
@config[:servertemplate],
|
28
|
+
@config[:cloud_name],
|
29
|
+
@config[:deployment_name],
|
30
|
+
@config[:server_inputs],
|
31
|
+
@config[:multi_cloud_image_name],
|
32
|
+
nil, # ssh_key_id not yet supported
|
33
|
+
["default"]) # secgroup_id not yet supported
|
34
|
+
|
35
|
+
@rightscale.wait_for_operational()
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: right_api_helper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -178,6 +178,7 @@ files:
|
|
178
178
|
- lib/right_api_helper/base.rb
|
179
179
|
- lib/right_api_helper/cache.rb
|
180
180
|
- lib/right_api_helper/deployments.rb
|
181
|
+
- lib/right_api_helper/exception.rb
|
181
182
|
- lib/right_api_helper/instances.rb
|
182
183
|
- lib/right_api_helper/provisioner.rb
|
183
184
|
- lib/right_api_helper/scripts/deployments_creator.rb
|
@@ -193,6 +194,7 @@ files:
|
|
193
194
|
- spec/scripts/deployments_creator_spec.rb
|
194
195
|
- spec/session_spec.rb
|
195
196
|
- spec/spec_helper.rb
|
197
|
+
- test/provisioner_test.rb
|
196
198
|
homepage: ''
|
197
199
|
licenses:
|
198
200
|
- MIT
|
@@ -228,4 +230,5 @@ test_files:
|
|
228
230
|
- spec/scripts/deployments_creator_spec.rb
|
229
231
|
- spec/session_spec.rb
|
230
232
|
- spec/spec_helper.rb
|
233
|
+
- test/provisioner_test.rb
|
231
234
|
has_rdoc:
|