Top Level Namespace

Defined Under Namespace

Modules: RightApi Classes: RightScaleCLI

Instance Method Summary collapse

Instance Method Details

#ask_passObject

Author

Chris Fordham (<[email protected]>)

Copyright

Copyright © 2013 Chris Fordham

License

Apache License, Version 2.0

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.



17
18
19
20
21
22
23
# File 'lib/ask_pass.rb', line 17

def ask_pass
  require 'io/console'
  print 'RightScale Password: '
  password = STDIN.noecho(&:gets).strip
  puts
  password
end

#concurrent_resource_launch(resources) ⇒ Object



322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/deployment_cat.rb', line 322

def concurrent_resource_launch(resources) 
  puts "Creating concurrent launch code"

  str = ""

  resources.each do |r|
    str += "  @@global_" + r + " = @" + r + "\n"
  end

  str += "  concurrent do \n"
  resources.each do |r|
    str += "    provision(@@global_" + r + ")\n" 
  end
  str += "  end \n"

  resources.each do |r|
    str += "  @" + r + " = @@global_" + r + "\n"
  end

  str
end

#deployment_inputs_to_catObject



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/deployment_cat.rb', line 344

def deployment_inputs_to_cat() 
  puts "Creating deployment-level inputs"

  str = "\n\n"

  inputs = @dep.raw["inputs"].select{ |i| i["value"] != "blank"}
  if inputs.size > 0
    str += "  $inp = {\n"
    inputs.each_with_index do |input, i|
      str += "    '" + input["name"] + "':'" + input["value"] + "'"
      str += "," if i != inputs.size - 1
      str += "\n"
    end
    str += "  } \n"
    str += "  @@deployment.multi_update_inputs(inputs: $inp) \n"
  else
    str += "  # No deployment level inputs found \n"
  end
  str
end

#deployment_to_cat_file(client, deployment_id, deployment_inputs, concurrent) ⇒ Object



365
366
367
368
369
370
371
372
373
374
375
376
377
378
# File 'lib/deployment_cat.rb', line 365

def deployment_to_cat_file( client, deployment_id, deployment_inputs, concurrent )

  # Get and show the deployment name
  dep = client.deployments(:id=>deployment_id).show
  puts "Exporting Deployment: " + dep.name

  # Output to a file named after the deployment (cleaned up for Linux filenames)
  File.open(dep.name.gsub(/[^\w\s_-]+/, '')+'.cat.rb','w') do |f|

    f.puts deployment_to_cat_string( client, deployment_id, deployment_inputs, concurrent)

  end

end

#deployment_to_cat_string(client, deployment_id, deployment_inputs, concurrent) ⇒ Object



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/deployment_cat.rb', line 238

def deployment_to_cat_string(client, deployment_id, deployment_inputs, concurrent)

  # Get and show the deployment name
  @dep = client.deployments(:id=>deployment_id).show(:view=>"inputs_2_0")
  @deployment_inputs = deployment_inputs
  resources = []

  cat = ''
  cat += 
    
  # Output the metadata of this CloudApp
  cat += "name '"+@dep.name.gsub(/\'/,"\\\\'")+"'\n"
  cat += "rs_ca_ver 20131202\n"

  desc = @dep.description.gsub(/\'/,"\\\\'")
  desc = @dep.name.gsub(/\'/,"\\\\'") if desc == '' 
  cat += "short_description '"+desc+"'\n"  

  # For each Server in the deployment (regardless of its status)
  servers = @dep.servers.index
  scount = 0
  servers.each do |s|
    rname = "server_"+(scount+=1).to_s
    cat += server_to_cat(s, rname)
    resources << rname
  end

  serverarrays = @dep.server_arrays.index
  scount = 0
  serverarrays.each do |sa|
    rname = "server_array_"+(scount+=1).to_s
    cat += server_array_to_cat(sa, rname)
    resources << rname
  end

  # Iterate through all clouds to get instances in the deployment
  instances = []
  client.clouds.index.each do |c|
    inst = c.instances.index(:filter=>["deployment_href=="+@dep.href],:view=>'full')
    instances += inst
  end

  # Delete instances with a parent (parent means they're from a Server or ServerArray)
  instances.delete_if { |i| i.raw["links"].detect{ |l| l["rel"] == "parent" } }
  scount = 0
  instances.each do |i|
    rname = "instance_" + (scount+=1).to_s
    cat += instance_to_cat(i, rname)
    resources << rname
  end

  cat += launch_operation(concurrent, resources) if @deployment_inputs || concurrent

  cat
end

#instance_details_to_cat(ni) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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
172
173
174
175
176
177
178
179
180
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
# File 'lib/deployment_cat.rb', line 122

def instance_details_to_cat( ni )

  str = ""
  str += "  cloud '"+ni.cloud.show.name.gsub(/\'/,"\\\\'")+"'\n"
  #str += "  cloud_href '"+ni.cloud.show.href+"'\n"
  cloud_href = ni.cloud.show.href

  # Check to see if there is a datacenter link to export
  if !ni.raw["links"].detect{ |l| l["rel"] == "datacenter" && l["inherited_source"] == nil}.nil?
    begin
      str += "  datacenter '"+ni.datacenter.show.name.gsub(/\'/,"\\\\'")+"'\n"
    rescue
      str += "  # datacenter ** NOT ABLE TO EXPORT **\n"
    end
    # str += "  datacenter_href '"+ni.datacenter.show.href+"'\n"
  end

  # Check to see if there is a image link to export
  if !ni.raw["links"].detect{ |l| l["rel"] == "image" && l["inherited_source"] == nil}.nil?
    str += "  image '"+ni.image.show.name.gsub(/\'/,"\\\\'")+"'\n"
    # str += "  image_href '"+ni.image.show.href+"'\n"
  end

  # Check to see if there is an instance type link to export
  if !ni.raw["links"].detect{ |l| l["rel"] == "instance_type" && l["inherited_source"] == nil}.nil?
    str += "  instance_type '"+ni.instance_type.show.name.gsub(/\'/,"\\\\'")+"'\n"
    # str += "  instance_type_href '"+ni.instance_type.show.href+"'\n"
  end 

  # Check to see if there is an kernel type link to export
  if !ni.raw["links"].detect{ |l| l["rel"] == "kernel_image" && l["inherited_source"] == nil}.nil?
    str += "  kernel_image '"+ni.kernel_image.show.name.gsub(/\'/,"\\\\'")+"'\n"
    # str += "  kernel_image_href '"+ni.kernel_image.show.href+"'\n"
  end 

  # Check to see if there is an multi cloud image link to export
  if !ni.raw["links"].detect{ |l| l["rel"] == "multi_cloud_image" && l["inherited_source"] == nil}.nil?
    str += "  multi_cloud_image find('"+ni.multi_cloud_image.show.name.gsub(/\'/,"\\\\'")+"', revision: " + ni.multi_cloud_image.show.revision.to_s + ")\n"
    # str += "  multi_cloud_image_href '"+ni.multi_cloud_image.show.href+"'\n"
  end 

  # Check to see if there is an ramdisk image link to export
  if !ni.raw["links"].detect{ |l| l["rel"] == "ramdisk_image" && l["inherited_source"] == nil}.nil?
    str += "  ramdisk_image '"+ni.ramdisk_image.show.name.gsub(/\'/,"\\\\'")+"'\n"
    # str += "  ramdisk_image_href '"+ni.ramdisk_image.show.href+"'\n"
  end 

  # Check to see if there is an ssh key link to export
  if !ni.raw["links"].detect{ |l| l["rel"] == "ssh_key" }.nil?
    begin
      str += "  ssh_key '"+ni.ssh_key.show.resource_uid.gsub(/\'/,"\\\\'")+"'\n"
    rescue
      str += "  # ssh_key ** NOT ABLE TO EXPORT **\n"
    end
    # str += "  ssh_key_href '"+ni.ssh_key.show.href+"'\n"
  end 

  # Export the user_data if it's not blank
  if !ni.user_data.nil? && ni.user_data != ''
    str += "  user_data '"+ni.user_data.gsub(/\'/,"\\\\'")+"'\n"
  end

  # Subnets and security groups aren't proper links in right_api_client, so instead
  #  just use the href values for these
  # If we have problems getting any subnet, just ignore them all and print an error
  if !ni.raw["subnets"].nil? && ni.raw["subnets"].size > 0
    begin
      substr = "  subnets "
      ni.raw["subnets"].each_with_index do |sn, i|
        snr = @client.resource(sn["href"])

        # If the name is nil, use the resource_uid and network href
        if !snr.name
            substr += "find(resource_uid: '" + snr.resource_uid + "', network_href: '" + snr.network.href + "')"
        # Not all subnets have networks to check, so if not, just use the name
        elsif !snr.raw["links"].detect{ |l| l["rel"] == "network" }.nil?
          # Check to see if more than one subnet with this name exists in the cloud. If so, use find with the network_href
          if snr.network.show.cloud.show.subnets.index(:filter=>["name==#{snr.name}"]).length == 1
            substr += "'" + snr.name + "'"
          else
            substr += "find('" + snr.name + "', network_href: '" + snr.network.href + "')"
          end
        else
          substr += "'" + snr.name + "'"
        end

        substr += ", " if i != ni.raw["subnets"].size - 1
      end
      str += substr + "\n"
    rescue
      str += "  # subnets ** NOT ABLE TO EXPORT **\n"
    end
  end

  # Subnets and security groups aren't proper links in right_api_client, so instead
  #  just use the href values for these
  if !ni.raw["security_groups"].nil?
    str += "  security_groups "
    ni.raw["security_groups"].each_with_index do |sn, i|
      sgr = @client.resource(sn["href"])

      # Check to see if more than one SG with this name exists in the cloud. If so, use find with the network_href
      if sgr.cloud.show.security_groups.index(:filter=>["name==#{sgr.name}"]).length == 1
        str += "'" + sgr.name + "'"
      else
        str += "find('" + sgr.name + "', network_href: '" + sgr.network.href + "')"
      end

      str += ", " if i != ni.raw["security_groups"].size - 1
    end
    str += "\n"
  end

  str
end

#instance_to_cat(i, rname) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/deployment_cat.rb', line 78

def instance_to_cat( i, rname )

  puts "  Instance: " + i.name

  str = ""
  # Some of the basic resource information
  str += "resource '"+rname+"', type: 'instance' do\n"
  str += "  name '"+i.name.gsub(/\'/,"\\\\'")+"'\n"
  if !i.description.nil? 
    str += "  description '"+i.description.gsub(/\'/,"\\\\'")+"'\n"
  end

  str += instance_details_to_cat(i)

  str += "end\n"
  str
end

#launch_operation(concurrent, resources) ⇒ Object



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/deployment_cat.rb', line 294

def launch_operation( concurrent, resources )
  str = ""

  str += "operation 'launch' do \n"
  str += "  description 'Launch the application' \n"
  str += "  definition 'generated_launch' \n"
  str += "end \n"

  rlist = ""
  resources.each_with_index do |r, i|
    rlist += "@" + r
    rlist += ", " if i != resources.size - 1
  end 

  str += "define generated_launch("
  str += rlist if concurrent
  str += ") "
  str += " return #{rlist} " if concurrent
  str += " do \n"

  str += deployment_inputs_to_cat if @deployment_inputs
  str += concurrent_resource_launch(resources) if concurrent

  str += "end \n"
  str

end

#server_array_to_cat(sa, rname) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/deployment_cat.rb', line 2

def server_array_to_cat( sa, rname )

  puts "  ServerArray: " + sa.name
  str = ""

  # Some of the basic resource information
  str += "resource '"+rname+"', type: 'server_array' do\n"
  str += "  name '"+sa.name.gsub(/\'/,"\\\\'")+"'\n"
  if !sa.description.nil? 
    str += "  description '"+sa.description.gsub(/\'/,"\\\\'")+"'\n"
  end

  # Get the instance information and the ServerTemplate details
  ni = sa.show.next_instance.show(:view=>"full")

  str += instance_details_to_cat(ni)
  str += server_template_details_to_cat(ni)

  if sa.raw["optimized"]
    str += "  optimized '#{sa.optimized}'\n"
  end
  
  if sa.raw["state"]
    str += "  state '"+sa.state+"'\n"
  end

  if sa.raw["array_type"]
    str += "  array_type '"+sa.array_type+"'\n"
  end
  
  str += "  elasticity_params do {\n"
  str += "    'bounds' => {\n"
  str += "      'min_count'            => "+sa.elasticity_params["bounds"]["min_count"]+",\n"
  str += "      'max_count'            => "+sa.elasticity_params["bounds"]["max_count"]+"\n"
  str += "    },\n"
  str += "    'pacing' => {\n"
  str += "      'resize_calm_time'     => "+sa.elasticity_params["pacing"]["resize_calm_time"]+",\n"
  str += "      'resize_down_by'       => "+sa.elasticity_params["pacing"]["resize_down_by"]+",\n"
  str += "      'resize_up_by'         => "+sa.elasticity_params["pacing"]["resize_up_by"]+"\n"
  str += "    },\n"
  str += "    'alert_specific_params' => {\n"
  str += "      'decision_threshold'   => "+sa.elasticity_params["alert_specific_params"]["decision_threshold"]+",\n"
  str += "      'voters_tag_predicate' => '"+sa.elasticity_params["alert_specific_params"]["voters_tag_predicate"]+"'\n"
  str += "    }\n"
  str += "  } end\n"

  str += "end\n"
  str
end

#server_template_details_to_cat(ni) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/deployment_cat.rb', line 96

def server_template_details_to_cat( ni )

  st = ni.server_template.show(:view=>"inputs_2_0")

  str = ""
  # Output the server template information
  str += "  server_template find('"+st.name.gsub(/\'/,"\\\\'")+"', revision: "+st.revision.to_s()+")\n"
  # str += "  server_template_href '"+st.href+"'\n"

  # For each input, check to see if this input is in the ServerTemplate with the same value
  #  If so, skip it, since it appears to be inherited anyway
  inputs = ni.inputs.index(:view=>"inputs_2_0")
  str += "  inputs do {\n"
  inputs = inputs.sort_by {|a| a.name.downcase}
  inputs.each do |i|
    if st.raw["inputs"].find{ |sti| sti["name"] == i.name && sti["value"] == i.value }.nil? && 
       ((@deployment_inputs && @dep.raw["inputs"].find{ |sti| sti["name"] == i.name && sti["value"] == i.value }.nil?) || !@deployment_inputs)
      str += "    '"+i.name+"' => '"+i.value.gsub(/\'/,"\\\\'")+"',\n" if i.value != "blank"
    end
  end 
  str += "  } end\n"

  str

end

#server_to_cat(s, rname) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/deployment_cat.rb', line 52

def server_to_cat( s, rname )

  puts "  Server: " + s.name

  str = ""
  # Some of the basic resource information
  str += "resource '"+rname+"', type: 'server' do\n"
  str += "  name '"+s.name.gsub(/\'/,"\\\\'")+"'\n"
  if !s.description.nil? 
    str += "  description '"+s.description.gsub(/\'/,"\\\\'")+"'\n"
  end

  # Get the instance information and the ServerTemplate details
  ni = s.show.next_instance.show(:view=>"full")

  str += instance_details_to_cat(ni)
  str += server_template_details_to_cat(ni)

  if s.raw["optimized"]
    str += "  optimized '"+s.optimized+"'\n"
  end
  
  str += "end\n"
  str
end

#yesnoObject

Author

Chris Fordham (<[email protected]>)

Copyright

Copyright © 2013 Chris Fordham

License

Apache License, Version 2.0

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/yesno.rb', line 17

def yesno
  begin
    system('stty raw -echo')
    str = STDIN.getc
  ensure
    system('stty -raw echo')
  end
  { 'y' => true, 'n' => false }.fetch(str.downcase) do
    fail 'Invalid response. Please enter y/n.'
  end
end