rs-api-tools 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,91 @@
1
+ #!/usr/bin/ruby
2
+
3
+ # rs-create-alert-spec <alert spec params>
4
+
5
+ require 'rubygems'
6
+ require 'getoptlong'
7
+ require 'json'
8
+ require 'rest_connection'
9
+ require 'active_support' #for to_xml()
10
+ require 'xmlsimple'
11
+
12
+ def usage
13
+ puts("#{$0} [--xml] [--json]")
14
+ puts "Example:"
15
+ puts "rs-create-alert-spec --name 'foobar alert' --file 'processes-java/ps_count' --variable 'processes' --condition '>' --threshold '25.0' --escalation-name 'critical' --duration 60 --subject-type ServerTemplate --action escalate"
16
+ exit
17
+ end
18
+
19
+ def usage_exit
20
+ usage; exit
21
+ end
22
+
23
+ json = false
24
+ xml = false
25
+ verbose = false
26
+ alert_spec_params = Hash.new
27
+
28
+ usage_exit if ARGV.length < 18 # (9 required options, but this should not be checked so arbitrarily)
29
+
30
+ opts = GetoptLong.new(
31
+ [ '--name', '-n', GetoptLong::REQUIRED_ARGUMENT ],
32
+ [ '--file', '-f', GetoptLong::REQUIRED_ARGUMENT ],
33
+ [ '--variable', '-v', GetoptLong::REQUIRED_ARGUMENT ],
34
+ [ '--condition', '-c', GetoptLong::REQUIRED_ARGUMENT ],
35
+ [ '--threshold', '-t', GetoptLong::REQUIRED_ARGUMENT ],
36
+ [ '--escalation-name', '-e', GetoptLong::REQUIRED_ARGUMENT ],
37
+ [ '--duration', '-d', GetoptLong::REQUIRED_ARGUMENT ],
38
+ [ '--description', '-D', GetoptLong::OPTIONAL_ARGUMENT ],
39
+ [ '--subject-type', '-s', GetoptLong::REQUIRED_ARGUMENT ],
40
+ [ '--subject-href', '-S', GetoptLong::OPTIONAL_ARGUMENT ],
41
+ [ '--action', '-a', GetoptLong::REQUIRED_ARGUMENT ],
42
+ [ '--vote-tag', GetoptLong::OPTIONAL_ARGUMENT ],
43
+ [ '--vote-type', GetoptLong::OPTIONAL_ARGUMENT ],
44
+ [ '--xml', '-x', GetoptLong::OPTIONAL_ARGUMENT ],
45
+ [ '--json', '-j', GetoptLong::OPTIONAL_ARGUMENT ],
46
+ [ '--verbose', '-V', GetoptLong::OPTIONAL_ARGUMENT ],
47
+ [ '--help', '-h', GetoptLong::NO_ARGUMENT ]
48
+ )
49
+
50
+ opts.each do |opt, arg|
51
+ case opt
52
+ when '--help'
53
+ usage; exit
54
+ when '--name'
55
+ alert_spec_params['name'] = arg
56
+ when '--file'
57
+ alert_spec_params['file'] = arg
58
+ when '--variable'
59
+ alert_spec_params['variable'] = arg
60
+ when '--condition'
61
+ alert_spec_params['condition'] = arg
62
+ when '--threshold'
63
+ alert_spec_params['threshold'] = arg
64
+ when '--duration'
65
+ alert_spec_params['duration'] = arg
66
+ when '--description'
67
+ alert_spec_params['description'] = arg
68
+ when '--subject-type'
69
+ alert_spec_params['subject_type'] = arg
70
+ when '--subject-href'
71
+ alert_spec_params['subject_href'] = arg
72
+ when '--action'
73
+ alert_spec_params['action'] = arg
74
+ when '--vote-tag'
75
+ alert_spec_params['vote_tag'] = arg
76
+ when '--vote-type'
77
+ alert_spec_params['vote_tpe'] = arg
78
+ when '--json'
79
+ json = true
80
+ when '--verbose'
81
+ verbose = true
82
+ when '--xml'
83
+ xml = true
84
+ end
85
+ end
86
+
87
+ puts alert_spec_params.to_yaml if verbose
88
+
89
+ #AlertSpec.attach(alert_spec_params)
90
+
91
+ puts 'This command is unfinished. Please contrib via pull request.'
@@ -4,10 +4,6 @@
4
4
 
5
5
  require 'rubygems'
6
6
  require 'getoptlong'
7
- require 'json'
8
- require 'rest_connection'
9
- require 'active_support' #for to_xml()
10
- require 'xmlsimple'
11
7
 
12
8
  def usage
13
9
  puts("#{$0} [--xml] [--json]")
@@ -31,11 +27,15 @@ opts.each do |opt, arg|
31
27
  end
32
28
  end
33
29
 
30
+ require 'json'
31
+ require 'rest_connection'
32
+
34
33
  alerts = AlertSpec.find_all()
35
34
 
36
35
  if json
37
36
  puts alerts.to_json
38
37
  elsif xml
38
+ require 'xmlsimple'
39
39
  puts JSON.parse(alerts.to_json).to_xml(:root => 'deployments', :skip_instruct => true)
40
40
  else
41
41
  puts alerts.to_yaml
@@ -4,9 +4,6 @@
4
4
 
5
5
  require 'rubygems'
6
6
  require 'getoptlong'
7
- require 'json'
8
- require 'rest_connection'
9
- require 'active_support' #for to_xml()
10
7
 
11
8
  def usage
12
9
  puts("#{$0} [--xml] [--json]")
@@ -30,6 +27,9 @@ opts.each do |opt, arg|
30
27
  end
31
28
  end
32
29
 
30
+ require 'json'
31
+ require 'rest_connection'
32
+
33
33
  ebs_snapshots = Ec2EbsSnapshot.find_all()
34
34
 
35
35
  if json
@@ -1,41 +1,67 @@
1
1
  #!/usr/bin/ruby
2
2
 
3
- # rs-describe-ec2-ebs-volumes
3
+ # rs-describe-ec2-ebs-volumes [--cloud <cloud_id>] [--json] [--xml] [--help]
4
4
 
5
5
  require 'rubygems'
6
6
  require 'getoptlong'
7
- require 'json'
8
- require 'rest_connection'
9
- require 'active_support' #for to_xml()
10
- require 'xmlsimple'
11
7
 
12
8
  def usage
13
- puts("#{$0} [--xml] [--json]")
9
+ puts("#{$0} [--cloud] [--help] [--xml] [--json]")
14
10
  exit
15
11
  end
16
12
 
17
13
  opts = GetoptLong.new(
14
+ [ '--cloud', '-c', GetoptLong::OPTIONAL_ARGUMENT ],
18
15
  [ '--xml', '-x', GetoptLong::OPTIONAL_ARGUMENT ],
19
- [ '--json', '-j', GetoptLong::OPTIONAL_ARGUMENT ]
16
+ [ '--json', '-j', GetoptLong::OPTIONAL_ARGUMENT ],
17
+ [ '--help', '-h', GetoptLong::OPTIONAL_ARGUMENT ]
20
18
  )
21
19
 
22
20
  json = false
23
21
  xml = false
22
+ cloud_id = false
24
23
 
25
24
  opts.each do |opt, arg|
26
25
  case opt
26
+ when '--cloud'
27
+ cloud_id = arg
27
28
  when '--json'
28
29
  json = true
29
30
  when '--xml'
30
31
  xml = true
32
+ when '--help'
33
+ usage; exit
31
34
  end
32
35
  end
33
36
 
34
- ebs_volumes = Ec2EbsVolume.find_all()
37
+ # RightScale (public) cloud IDs
38
+ rs_clouds = Array.new
39
+ rs_clouds.push({:cloud_id => 1, :name => "AWS US-East", :region => "us-east-1"})
40
+ rs_clouds.push({:cloud_id => 2, :name => "AWS EU", :region => "eu-west-1"})
41
+ rs_clouds.push({:cloud_id => 3, :name => "AWS US-West", :region => "us-west-1"})
42
+ rs_clouds.push({:cloud_id => 4, :name => "AWS AP-Singapore", :region => "ap-southeast-1"})
43
+ rs_clouds.push({:cloud_id => 5, :name => "AWS AP-Tokyo", :region => "ap-northeast-1"})
44
+ rs_clouds.push({:cloud_id => 6, :name => "AWS AP-Oregon", :region => "us-west-2"})
45
+ rs_clouds.push({:cloud_id => 7, :name => "AWS SA-Sao Paulo", :region => "sa-east-1"})
46
+
47
+ require 'rest_connection'
48
+
49
+ ebs_volumes = Array.new
50
+
51
+ if cloud_id
52
+ ebs_volumes += Ec2EbsVolume.find_by_cloud_id(cloud_id)
53
+ else
54
+ rs_clouds.each { |cloud|
55
+ puts "Fetching from #{cloud[:name]} (#{cloud[:region]})."
56
+ ebs_volumes += Ec2EbsVolume.find_by_cloud_id(cloud[:cloud_id])
57
+ }
58
+ end
35
59
 
36
60
  if json
61
+ require 'json'
37
62
  puts ebs_volumes.to_json
38
63
  elsif xml
64
+ require 'active_support' #for to_xml()
39
65
  puts JSON.parse(ebs_volumes.to_json).to_xml(:root => 'ec2-ebs-volumes', :skip_instruct => true)
40
66
  else
41
67
  puts ebs_volumes.to_yaml
@@ -0,0 +1,49 @@
1
+ #!/usr/bin/ruby
2
+
3
+ # rs-describe-key
4
+
5
+ puts 'This operation is not currently permitted with the RightScale API (will return HTTP code: 401: Permission denied).'
6
+ exit 1
7
+
8
+ require 'rubygems'
9
+ require 'getoptlong'
10
+
11
+ def usage
12
+ puts("#{$0} --id <ec2_ssh_key_id> [--xml] [--json]")
13
+ exit
14
+ end
15
+
16
+ opts = GetoptLong.new(
17
+ [ '--id', '-i', GetoptLong::REQUIRED_ARGUMENT ],
18
+ [ '--xml', '-x', GetoptLong::OPTIONAL_ARGUMENT ],
19
+ [ '--json', '-j', GetoptLong::OPTIONAL_ARGUMENT ]
20
+ )
21
+
22
+ key_id = nil
23
+ json = false
24
+ xml = false
25
+
26
+ opts.each do |opt, arg|
27
+ case opt
28
+ when '--id'
29
+ key_id = arg
30
+ when '--json'
31
+ json = true
32
+ when '--xml'
33
+ xml = true
34
+ end
35
+ end
36
+
37
+ require 'json'
38
+ require 'rest_connection'
39
+
40
+ key = Ec2SshKey.find(key_id)
41
+
42
+ if json
43
+ puts key.to_json
44
+ elsif xml
45
+ require 'xmlsimple'
46
+ puts JSON.parse(key.to_json).to_xml(:root => 'ec2_ssh_key', :skip_instruct => true)
47
+ else
48
+ puts key.to_yaml
49
+ end
@@ -4,13 +4,9 @@
4
4
 
5
5
  require 'rubygems'
6
6
  require 'getoptlong'
7
- require 'json'
8
- require 'rest_connection'
9
- require 'active_support' #for to_xml()
10
- require 'xmlsimple'
11
7
 
12
8
  def usage
13
- puts("usage: rs-describe-server --settings [current] [--nickname <server_nickname> | --id <server_id>]")
9
+ puts("usage: rs-describe-server --settings [current] [--name <server_nickname> | --id <server_id>]")
14
10
  puts(" [[--verbose]]")
15
11
  end
16
12
 
@@ -21,15 +17,15 @@ end
21
17
  def help
22
18
  usage
23
19
  puts ''
24
- puts 'Reboots a RightScale server.'
20
+ puts 'Describes a RightScale server.'
25
21
  puts ''
26
- puts "examples: rs-reboot-server --name 'Sandbox'"
22
+ puts "examples: rs-describe-server --id 836587 --settings current"
27
23
  puts ''
28
24
  exit
29
25
  end
30
26
 
31
27
  opts = GetoptLong.new(
32
- [ '--nickname', '-n', GetoptLong::OPTIONAL_ARGUMENT ],
28
+ [ '--name', '-n', GetoptLong::OPTIONAL_ARGUMENT ],
33
29
  [ '--settings', '-s', GetoptLong::OPTIONAL_ARGUMENT ],
34
30
  [ '--id', '-i', GetoptLong::OPTIONAL_ARGUMENT ],
35
31
  [ '--xml', '-x', GetoptLong::OPTIONAL_ARGUMENT ],
@@ -42,10 +38,11 @@ settings = false
42
38
  json = false
43
39
  xml = false
44
40
  verbose = false
41
+ server = false
45
42
 
46
43
  opts.each do |opt, arg|
47
44
  case opt
48
- when '--nickname'
45
+ when '--name'
49
46
  server_name = arg
50
47
  when '--settings'
51
48
  settings = arg
@@ -63,35 +60,42 @@ end
63
60
 
64
61
  usage_exit if !(server_name || server_id)
65
62
 
63
+ require 'json'
64
+ require 'rest_connection'
65
+
66
66
  # get server
67
67
  if server_name
68
68
  puts "Finding server: '%#{server_name}%'"
69
69
  server = Server.find(:first) { |s| s.nickname =~ /#{server_name}/ }
70
- #puts "Found server, '#{server.nickname}'."
71
70
  elsif server_id
72
71
  server = Server.find(server_id.to_i)
73
72
  else
74
73
  usage_exit
75
74
  end
76
75
 
76
+ if server
77
+ puts "Found server, '#{server.nickname}'."
78
+ else
79
+ puts "No server found!"
80
+ end
81
+
77
82
  if settings
78
- if settings == 'next'
79
- server_settings = server.settings
80
- elsif settings == 'current'
81
- server.reload_current
82
- server_settings = server.settings
83
- end
84
- output = server_settings
83
+ if settings == 'next'
84
+ server_settings = server.settings
85
+ elsif settings == 'current'
86
+ server.reload_current
87
+ server_settings = server.settings
88
+ end
89
+ api_result = server_settings
85
90
  else
86
- output = server
91
+ api_result = server
87
92
  end
88
93
 
89
94
  if json
90
- puts output.to_json
95
+ puts api_result.to_json
91
96
  elsif xml
92
- puts JSON.parse(output.to_json).to_xml(:root => 'servers', :skip_instruct => true)
97
+ require 'xmlsimple'
98
+ puts JSON.parse(api_result.to_json).to_xml(:root => 'servers', :skip_instruct => true)
93
99
  else
94
- puts output.to_yaml
95
- end
96
-
97
-
100
+ puts api_result.to_yaml
101
+ end
@@ -1,13 +1,13 @@
1
1
  #!/usr/bin/ruby
2
2
 
3
- # rs-describe-servers
3
+ # rs-describe-servers [--filter <filter>]
4
+
5
+ # e.g. rs-describe-servers
6
+ # rs-describe-servers --filter "ip_address=44.1.2.3" # server-side filter
7
+ # rs-describe-servers --filter "state=operational" # client-side filter
4
8
 
5
9
  require 'rubygems'
6
10
  require 'getoptlong'
7
- require 'json'
8
- require 'rest_connection'
9
- require 'active_support' #for to_xml()
10
- require 'xmlsimple'
11
11
 
12
12
  def usage
13
13
  puts("#{$0} [--xml] [--json]")
@@ -15,28 +15,81 @@ def usage
15
15
  end
16
16
 
17
17
  opts = GetoptLong.new(
18
+ [ '--filter', '-f', GetoptLong::OPTIONAL_ARGUMENT ],
19
+ [ '--current', '-c', GetoptLong::OPTIONAL_ARGUMENT ],
20
+ [ '--field', '-F', GetoptLong::OPTIONAL_ARGUMENT ],
21
+ [ '--raw', '-r', GetoptLong::OPTIONAL_ARGUMENT ],
18
22
  [ '--xml', '-x', GetoptLong::OPTIONAL_ARGUMENT ],
19
23
  [ '--json', '-j', GetoptLong::OPTIONAL_ARGUMENT ]
20
24
  )
21
25
 
26
+ filter = false
22
27
  json = false
23
28
  xml = false
29
+ raw = false
30
+ field = false
24
31
 
25
32
  opts.each do |opt, arg|
26
33
  case opt
34
+ when '--filter'
35
+ filter = arg.split('=')
27
36
  when '--json'
28
37
  json = true
29
38
  when '--xml'
30
39
  xml = true
31
- end
40
+ when '--raw'
41
+ raw = true
42
+ when '--field'
43
+ field = arg
44
+ end
32
45
  end
33
46
 
34
- servers = Server.find_all()
47
+ filters = [ 'aws_id', 'created_at', 'deployment_href', 'ip_address', 'nickname', 'private_ip_address', 'updated_at' ]
48
+
49
+ require 'rest_connection'
50
+ require 'json'
51
+
52
+ if filter
53
+ if filters.include? filter[0]
54
+ # server-side filter
55
+ servers = Server.find_with_filter(filter[0] => filter[1])
56
+ else
57
+ # client-side filter
58
+ servers = Server.find(:all) {|server| server.params[filter[0]] == filter[1]}
59
+ end
60
+ else
61
+ servers = Server.find_all()
62
+ end
35
63
 
36
64
  if json
37
- puts servers.to_json
65
+ servers_formatted = Array.new
66
+ servers.each do |server|
67
+ servers_formatted.push(server.params)
68
+ end
69
+ puts JSON.pretty_generate(servers_formatted)
38
70
  elsif xml
39
- puts JSON.parse(servers.to_json).to_xml(:root => 'servers', :skip_instruct => true)
71
+ require "active_support/core_ext"
72
+ puts "<servers>"
73
+ servers.each do |server|
74
+ puts JSON.parse(server.params.to_json).to_xml(:root => 'server', :skip_instruct => true)
75
+ end
76
+ puts "</servers>"
77
+ elsif raw
78
+ servers.each do |server|
79
+ #print server.state
80
+ if server.state == ('operational'||'stranded'||'booting'||'decommissioning')
81
+ #puts server.state
82
+ server.reload_as_current
83
+ #puts server.settings.to_yaml
84
+ #server.reload_as_current
85
+ #puts server.to_yaml
86
+ if field
87
+ puts server.settings[field]
88
+ else
89
+ puts server
90
+ end
91
+ end
92
+ end
40
93
  else
41
- puts servers.to_yaml
94
+ puts servers.to_yaml # output yaml by default for readability
42
95
  end
@@ -0,0 +1,115 @@
1
+ #!/usr/bin/ruby
2
+
3
+ # rs-fetch-rightscripts
4
+
5
+ require 'rubygems'
6
+ require 'getoptlong'
7
+
8
+ def usage
9
+ puts("rs-fetch-rightscripts [ --id <server_template_id> | --name <server_template_name>] [ OPTIONS ]")
10
+ puts("e.g. rs-fetch-rightscripts --name 'RightScale Linux Server RL 5.8' -v --xml")
11
+ exit
12
+ end
13
+
14
+ opts = GetoptLong.new(
15
+ [ '--id', '-i', GetoptLong::OPTIONAL_ARGUMENT ],
16
+ [ '--name', '-n', GetoptLong::OPTIONAL_ARGUMENT ],
17
+ [ '--verbose', '-v', GetoptLong::OPTIONAL_ARGUMENT ],
18
+ [ '--json', '-j', GetoptLong::OPTIONAL_ARGUMENT ],
19
+ [ '--xml', '-x', GetoptLong::OPTIONAL_ARGUMENT ]
20
+ )
21
+
22
+ name = false
23
+ id = false
24
+ show_help = false
25
+ verbose = false
26
+ xml = false
27
+ json = false
28
+ server_template = false
29
+
30
+ opts.each do |opt, arg|
31
+ case opt
32
+ when '--name'
33
+ name = arg
34
+ when '--id'
35
+ id = arg
36
+ when '--verbose'
37
+ verbose = true
38
+ when '--json'
39
+ json = true
40
+ require 'json'
41
+ when '--xml'
42
+ xml = true
43
+ require 'xmlsimple'
44
+ end
45
+ end
46
+
47
+ usage if !(name || id)
48
+
49
+ def sanitize_filename(filename)
50
+ filename.strip do |name|
51
+ # NOTE: File.basename doesn't work right with Windows paths on Unix
52
+ # get only the filename, not the whole path
53
+ name.gsub! /^.*(\\|\/)/, ''
54
+
55
+ # Finally, replace all non alphanumeric, underscore
56
+ # or periods with underscore
57
+ # name.gsub! /[^\w\.\-]/, '_'
58
+ # Basically strip out the non-ascii alphabets too
59
+ # and replace with x.
60
+ # You don't want all _ :)
61
+ name.gsub!(/[^0-9A-Za-z.\-]/, 'x')
62
+ end
63
+ end
64
+
65
+ require 'rest_connection'
66
+
67
+ # get servertemplate
68
+ if name
69
+ puts "Finding ServerTemplate: '%#{name}%'." if verbose
70
+ server_template = ServerTemplate.find(:first) { |s| s.nickname =~ /#{name}/ }
71
+ elsif id
72
+ puts "Fetching ServerTemplate, ID: #{id}." if verbose
73
+ server_template = ServerTemplate.find(id.to_i)
74
+ end
75
+
76
+ if server_template
77
+ puts "Located ServerTemplate, '#{server_template.nickname}'." if verbose
78
+ puts server_template.to_yaml if verbose
79
+ else
80
+ puts "No ServerTemplate found."
81
+ exit 1
82
+ end
83
+
84
+ # get executables (RightScripts or Chef recipes) of the ServerTemplate
85
+ executables = server_template.fetch_executables
86
+
87
+ if json
88
+ # TODO: proper processing
89
+ executables.each do |exec|
90
+ puts exec.to_s.to_json
91
+ end
92
+ elsif xml
93
+ require 'cgi'
94
+ puts "<executables>"
95
+ executables.each do |exec|
96
+ puts " <executable>"
97
+ puts " <position>#{exec.position}</position>"
98
+ puts " <name>#{exec.name}</name>"
99
+ puts " <recipe>#{! exec.recipe.to_s.empty? ? exec.recipe : 'n/a'}</recipe>"
100
+ puts " <href>#{exec.href}</href>"
101
+ puts " <phase>#{exec.apply}</phase>"
102
+ if exec.right_script?
103
+ puts " <description>#{exec.right_script.description}</description>"
104
+ puts " <created_at>#{exec.right_script.created_at}</created_at>"
105
+ puts " <updated_at>#{exec.right_script.updated_at}</updated_at>"
106
+ puts " <version>#{exec.right_script.version == 0 ? 'HEAD' : exec.right_script.version}</version>"
107
+ puts " <filename>#{sanitize_filename(exec.name).gsub(' ', '_')}</filename>"
108
+ puts " <script>#{CGI.escapeHTML(exec.right_script['script'])}</script>"
109
+ end
110
+ puts " </executable>"
111
+ end
112
+ puts "</executables>"
113
+ else
114
+ puts executables.to_yaml
115
+ end
@@ -1,126 +1,3 @@
1
- #!/usr/bin/ruby
1
+ #!/bin/sh
2
2
 
3
- # rs-fetch-rightscripts
4
-
5
- require 'rubygems'
6
- require 'getoptlong'
7
- require 'json'
8
- require 'rest_connection'
9
- require 'activesupport' #for to_xml()
10
- require 'xmlsimple'
11
-
12
- def usage
13
- puts("rs-fetch-rightscripts [ --id <server_template_id> | --name <server_template_name>] [ OPTIONS ]")
14
- puts("e.g. rs-fetch-rightscripts --name 'RightScale Linux Server RL 5.7 -v --xml'")
15
- exit
16
- end
17
-
18
- opts = GetoptLong.new(
19
- [ '--id', '-i', GetoptLong::OPTIONAL_ARGUMENT ],
20
- [ '--name', '-n', GetoptLong::OPTIONAL_ARGUMENT ],
21
- [ '--verbose', '-v', GetoptLong::OPTIONAL_ARGUMENT ],
22
- [ '--json', '-j', GetoptLong::OPTIONAL_ARGUMENT ],
23
- [ '--xml', '-x', GetoptLong::OPTIONAL_ARGUMENT ],
24
- )
25
-
26
- name = false
27
- id = false
28
- show_help = false
29
- verbose = false
30
- xml = false
31
- json = false
32
-
33
- opts.each do |opt, arg|
34
- case opt
35
- when '--name'
36
- name = arg
37
- when '--id'
38
- id = arg
39
- when '--verbose'
40
- verbose = true
41
- when '--json'
42
- json = true
43
- when '--xml'
44
- xml = true
45
- end
46
- end
47
-
48
- usage if !(name || id)
49
-
50
- def sanitize_filename(filename)
51
- filename.strip do |name|
52
- # NOTE: File.basename doesn't work right with Windows paths on Unix
53
- # get only the filename, not the whole path
54
- name.gsub! /^.*(\\|\/)/, ''
55
-
56
- # Finally, replace all non alphanumeric, underscore
57
- # or periods with underscore
58
- # name.gsub! /[^\w\.\-]/, '_'
59
- # Basically strip out the non-ascii alphabets too
60
- # and replace with x.
61
- # You don't want all _ :)
62
- name.gsub!(/[^0-9A-Za-z.\-]/, 'x')
63
- end
64
- end
65
-
66
- # Awesome truncate
67
- # First regex truncates to the length, plus the rest of that word, if any.
68
- # Second regex removes any trailing whitespace or punctuation (except ;).
69
- # Unlike the regular truncate method, this avoids the problem with cutting
70
- # in the middle of an entity ex.: truncate("this &amp; that",9) => "this &am..."
71
- # though it will not be the exact length.
72
- def awesome_truncate(text, length = 30, truncate_string = "...")
73
- return if text.nil?
74
- l = length - truncate_string.mb_chars.length
75
- text.mb_chars.length > length ? text[/\A.{#{l}}\w*\;?/m][/.*[\w\;]/m] + truncate_string : text
76
- end
77
-
78
- server_template = false
79
-
80
- # get servertemplate
81
- if name
82
- puts "Finding ServerTemplate: '%#{name}%'." unless ! verbose
83
- server_template = ServerTemplate.find(:first) { |s| s.nickname =~ /#{name}/ }
84
- elsif id
85
- puts "Fetching ServerTemplate; ID: #{id}." unless ! verbose
86
- server_template = ServerTemplate.find(id.to_i)
87
- end
88
-
89
- if server_template
90
- puts "Located ServerTemplate, '#{server_template.nickname}'." unless ! verbose
91
- puts server_template.to_yaml unless ! verbose
92
- else
93
- puts "No ServerTemplate found."
94
- exit 1
95
- end
96
-
97
- # get executables (RightScripts) of template
98
- executables = server_template.fetch_executables
99
-
100
- if json
101
- puts executables.to_json
102
- elsif xml
103
- #puts JSON.parse(executables.to_json).to_xml(:root => 'right_scripts', :skip_instruct => true)
104
- executables.each do |exec|
105
- puts "<right_script>"
106
- puts " <position>#{exec.position}</position>"
107
- puts " <name>#{exec.name}</name>"
108
- puts " <recipe>#{! exec.recipe.to_s.empty? ? exec.recipe : 'n/a'}</recipe>"
109
- puts " <href>#{exec.href}</href>"
110
- puts " <description>#{exec.right_script.description}</description>"
111
- puts " <created_at>#{exec.right_script.created_at}</created_at>"
112
- puts " <updated_at>#{exec.right_script.updated_at}</updated_at>"
113
- puts " <version>#{exec.right_script.version == 0 ? 'HEAD' : exec.right_script.version}</version>"
114
- puts " <phase>#{exec.apply}</phase>"
115
- puts " <filename>#{sanitize_filename(exec.name).gsub(' ', '_')}</filename>"
116
- puts " <script>#{awesome_truncate(exec.right_script.script)}</script>"
117
- puts "</right_script>"
118
- end
119
- # puts "--- #{exec.position} #{! exec.recipe.to_s.empty? ? exec.recipe : '*'} #{exec.name} #{exec.right_script.version == 0 ? 'HEAD' : 'rev'+exec.right_script.version.to_s} #{exec.description} #{exec.href} #{exec.right_script.created_at} #{exec.right_script.updated_at} #{exec.apply}"
120
- # puts "- #{sanitize_filename(exec.name)}: "
121
- # puts "#{awesome_truncate(exec.right_script.script)}"
122
- # puts '---'
123
- # puts
124
- else
125
- puts executables.to_yaml
126
- end
3
+ rs-fetch-executables "$@"
@@ -0,0 +1,77 @@
1
+ #!/usr/bin/ruby
2
+
3
+ # rs-lock-server
4
+
5
+ require 'rubygems'
6
+ require 'getoptlong'
7
+
8
+ def usage
9
+ puts("usage: rs-lock-server [--name <server_name> | --id <server_id>]")
10
+ puts(" [[--verbose]]")
11
+ end
12
+
13
+ def usage_exit
14
+ usage; exit
15
+ end
16
+
17
+ def help
18
+ usage
19
+ puts ''
20
+ puts 'Locks a RightScale server.'
21
+ puts ''
22
+ puts "examples: rs-lock-server --name 'Back to earth'"
23
+ puts ''
24
+ exit
25
+ end
26
+
27
+ opts = GetoptLong.new(
28
+ [ '--name', '-n', GetoptLong::OPTIONAL_ARGUMENT ],
29
+ [ '--id', '-i', GetoptLong::OPTIONAL_ARGUMENT ],
30
+ [ '--xml', '-x', GetoptLong::OPTIONAL_ARGUMENT ],
31
+ [ '--json', '-j', GetoptLong::OPTIONAL_ARGUMENT ],
32
+ [ '--help', '-h', GetoptLong::OPTIONAL_ARGUMENT ],
33
+ [ '--verbose', '-v', GetoptLong::OPTIONAL_ARGUMENT ]
34
+ )
35
+
36
+ server_name = false
37
+ server_id = false
38
+ show_help = false
39
+ verbose = false
40
+
41
+ opts.each do |opt, arg|
42
+ case opt
43
+ when '--name'
44
+ server_name = arg
45
+ when '--id'
46
+ server_id = arg
47
+ when '--help'
48
+ show_help = true
49
+ when '--verbose'
50
+ verbose = true
51
+ end
52
+ end
53
+
54
+ help if show_help
55
+
56
+ require 'json'
57
+ require 'rest_connection'
58
+
59
+ usage_exit if !(server_name || server_id)
60
+
61
+ # get server
62
+ if server_name
63
+ puts "Finding server: '%#{server_name}%'"
64
+ server = Server.find(:first) { |s| s.nickname =~ /#{server_name}/ }
65
+ puts "Found server, '#{server.nickname}'."
66
+ puts server.to_yaml if verbose
67
+ elsif server_id
68
+ server = Server.find(server_id.to_i)
69
+ else
70
+ usage_exit
71
+ end
72
+
73
+ # lock server
74
+ puts "Locking server, '#{server.nickname}'."
75
+ server.reload_as_current
76
+ server.settings["locked"] = 'true'
77
+ server.save
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/ruby
2
+
3
+ # rs-query-cloud
4
+
5
+ require 'rubygems'
6
+ require 'getoptlong'
7
+ require 'json'
8
+ require 'rest_connection'
9
+
10
+ def usage
11
+ puts("#{$0} [--cloud]")
12
+ exit
13
+ end
14
+
15
+ opts = GetoptLong.new(
16
+ [ '--cloud', '-c', GetoptLong::REQUIRED_ARGUMENT ]
17
+ )
18
+
19
+ cloud_id = false
20
+
21
+ opts.each do |opt, arg|
22
+ case opt
23
+ when '--cloud'
24
+ cloud_id = arg
25
+ end
26
+ end
27
+
data/bin/rs-run-recipe ADDED
@@ -0,0 +1,80 @@
1
+ #!/usr/bin/ruby
2
+
3
+ # rs-run-recipe
4
+
5
+ require 'rubygems'
6
+ require 'getoptlong'
7
+
8
+ def usage
9
+ puts("usage: rs-run-recipe [--name <recipe_name>]")
10
+ puts(" [--server-name <server_name> | --server-id <server_id>]")
11
+ end
12
+
13
+ def usage_exit
14
+ usage; exit
15
+ end
16
+
17
+ def help
18
+ usage
19
+ puts ''
20
+ puts 'Run a Chef recipe on a RightScale server.'
21
+ puts ''
22
+ puts "examples: rs-run-recipe --name 'helloworld::goodbye_world' --server 'Messy Sandbox'"
23
+ puts ''
24
+ exit
25
+ end
26
+
27
+ opts = GetoptLong.new(
28
+ [ '--name', '-n', GetoptLong::OPTIONAL_ARGUMENT ],
29
+ [ '--server-name', '-s', GetoptLong::OPTIONAL_ARGUMENT ],
30
+ [ '--server-id', '-S', GetoptLong::OPTIONAL_ARGUMENT ],
31
+ [ '--xml', '-x', GetoptLong::OPTIONAL_ARGUMENT ],
32
+ [ '--json', '-j', GetoptLong::OPTIONAL_ARGUMENT ],
33
+ [ '--help', '-h', GetoptLong::OPTIONAL_ARGUMENT ],
34
+ [ '--verbose', '-v', GetoptLong::OPTIONAL_ARGUMENT ]
35
+ )
36
+
37
+ recipe_name = false
38
+ server_name = false
39
+ server_id = false
40
+ show_help = false
41
+ verbose = false
42
+
43
+ opts.each do |opt, arg|
44
+ case opt
45
+ when '--name'
46
+ recipe_name = arg
47
+ when '--server-name'
48
+ server_name = arg
49
+ when '--server-id'
50
+ server_id = arg
51
+ when '--help'
52
+ show_help = true
53
+ when '--verbose'
54
+ verbose = true
55
+ end
56
+ end
57
+
58
+ help if show_help
59
+
60
+ require 'json'
61
+ require 'rest_connection'
62
+
63
+ usage_exit if !(recipe_name)
64
+
65
+ # get server
66
+ if server_name
67
+ puts "Finding server: '%#{server_name}%'"
68
+ server = Server.find(:first) { |s| s.nickname =~ /#{server_name}/ }
69
+ puts "Found server, '#{server.nickname}'."
70
+ puts server.to_yaml if verbose
71
+ elsif server_id
72
+ server = Server.find(server_id.to_i)
73
+ else
74
+ usage_exit
75
+ end
76
+
77
+ puts "Running Chef Recipe, '#{recipe_name}' on server, '#{server.nickname}'."
78
+ recipe_executable = Executable.new(recipe_name)
79
+ state = server.run_executable(recipe_executable)
80
+ state.wait_for_completed
data/bin/rs-search-tags CHANGED
@@ -16,9 +16,12 @@ end
16
16
  def help
17
17
  usage
18
18
  puts ''
19
- puts 'Searches for machine tags.'
19
+ puts 'Searches for resources with a specific RightScale tag.'
20
20
  puts ''
21
21
  puts "examples: rs-search-tags --resource Ec2Instance \"foo:bar=foobar\""
22
+ puts " - Searches for all EC2 instances with the tag, \"foo:bar=foobar\""
23
+ puts " rs-search-tags --resource Server \"rs_agent_dev:log_level=DEBUG\""
24
+ puts " - Searches for all Servers (next instance) with the tag, \"rs_agent_dev:log_level=DEBUG\""
22
25
  puts ''
23
26
  exit
24
27
  end
@@ -32,6 +35,7 @@ opts = GetoptLong.new(
32
35
  [ '--resource', '-r', GetoptLong::REQUIRED_ARGUMENT ],
33
36
  [ '--xml', '-x', GetoptLong::OPTIONAL_ARGUMENT ],
34
37
  [ '--json', '-j', GetoptLong::OPTIONAL_ARGUMENT ],
38
+ [ '--help', '-h', GetoptLong::OPTIONAL_ARGUMENT ],
35
39
  [ '--verbose', '-v', GetoptLong::OPTIONAL_ARGUMENT ]
36
40
  )
37
41
 
@@ -45,6 +49,8 @@ opts.each do |opt, arg|
45
49
  xml = true
46
50
  when '--verbose'
47
51
  verbose = true
52
+ when '--help'
53
+ help; exit
48
54
  end
49
55
  end
50
56
 
@@ -56,13 +62,14 @@ require 'rest_connection'
56
62
 
57
63
  tags_search = Array.new
58
64
  tags_search.push(tag)
59
- #puts Gem.datadir('rest_connection')
60
65
 
61
66
  servers = Tag.search(resource, ["#{tag}"])
62
67
 
63
68
  if json
64
69
  puts servers.to_json
65
70
  elsif xml
71
+ require 'active_support'
72
+ #require 'active_support/core_ext'
66
73
  puts JSON.parse(servers.to_json).to_xml(:root => 'servers', :skip_instruct => true)
67
74
  else
68
75
  puts servers.to_yaml
data/bin/rs-tag-array ADDED
@@ -0,0 +1,39 @@
1
+ #!/usr/bin/ruby
2
+
3
+ # rs-tag-array
4
+
5
+ require 'rubygems'
6
+ require 'getoptlong'
7
+
8
+ def usage
9
+ puts("#{$0} --id <server_array_id> --tag <tag_foo>")
10
+ exit
11
+ end
12
+
13
+ # taggable resources
14
+ # 'Server', 'Ec2EbsSnapshot', 'Ec2EbsVolume', 'Ec2Image', 'Image', 'ServerArray', 'Ec2Instance', 'Instance', 'Deployment', 'ServerTemplate', 'Ec2ServerTemplate'
15
+
16
+ opts = GetoptLong.new(
17
+ [ '--id', '-i', GetoptLong::REQUIRED_ARGUMENT ],
18
+ [ '--tag', '-t', GetoptLong::REQUIRED_ARGUMENT ],
19
+ )
20
+
21
+ array_id = false
22
+ tag = false
23
+
24
+ opts.each do |opt, arg|
25
+ case opt
26
+ when '--id'
27
+ array_id = arg
28
+ when '--tag'
29
+ tag = true
30
+ end
31
+ end
32
+
33
+ usage unless (array_id and tag)
34
+
35
+ require 'rest_connection'
36
+
37
+ server_array = Ec2ServerArray.find('server_arrays/'+array_id)
38
+
39
+ Tag.set(server_array.href, tag)
@@ -62,9 +62,5 @@ if server
62
62
  server.terminate
63
63
  else
64
64
  array_instance = Server.find_with_filter('aws_id' => "#{instance_id}")[0]
65
- end
66
-
67
-
68
-
69
- Tag.search('Ec2Instance', "server:private_ip_0=10.128.74.253")
70
-
65
+ array_instance.terminate
66
+ end
@@ -0,0 +1,77 @@
1
+ #!/usr/bin/ruby
2
+
3
+ # rs-unlock-server
4
+
5
+ require 'rubygems'
6
+ require 'getoptlong'
7
+
8
+ def usage
9
+ puts("usage: rs-unlock-server [--name <server_name> | --id <server_id>]")
10
+ puts(" [[--verbose]]")
11
+ end
12
+
13
+ def usage_exit
14
+ usage; exit
15
+ end
16
+
17
+ def help
18
+ usage
19
+ puts ''
20
+ puts 'Unlocks a RightScale server.'
21
+ puts ''
22
+ puts "examples: rs-unlock-server --name 'Back to earth'"
23
+ puts ''
24
+ exit
25
+ end
26
+
27
+ opts = GetoptLong.new(
28
+ [ '--name', '-n', GetoptLong::OPTIONAL_ARGUMENT ],
29
+ [ '--id', '-i', GetoptLong::OPTIONAL_ARGUMENT ],
30
+ [ '--xml', '-x', GetoptLong::OPTIONAL_ARGUMENT ],
31
+ [ '--json', '-j', GetoptLong::OPTIONAL_ARGUMENT ],
32
+ [ '--help', '-h', GetoptLong::OPTIONAL_ARGUMENT ],
33
+ [ '--verbose', '-v', GetoptLong::OPTIONAL_ARGUMENT ]
34
+ )
35
+
36
+ server_name = false
37
+ server_id = false
38
+ show_help = false
39
+ verbose = false
40
+
41
+ opts.each do |opt, arg|
42
+ case opt
43
+ when '--name'
44
+ server_name = arg
45
+ when '--id'
46
+ server_id = arg
47
+ when '--help'
48
+ show_help = true
49
+ when '--verbose'
50
+ verbose = true
51
+ end
52
+ end
53
+
54
+ help if show_help
55
+
56
+ require 'json'
57
+ require 'rest_connection'
58
+
59
+ usage_exit if !(server_name || server_id)
60
+
61
+ # get server
62
+ if server_name
63
+ puts "Finding server: '%#{server_name}%'"
64
+ server = Server.find(:first) { |s| s.nickname =~ /#{server_name}/ }
65
+ puts "Found server, '#{server.nickname}'."
66
+ puts server.to_yaml if verbose
67
+ elsif server_id
68
+ server = Server.find(server_id.to_i)
69
+ else
70
+ usage_exit
71
+ end
72
+
73
+ # unlock server
74
+ puts "Unlocking server, '#{server.nickname}'."
75
+ #server.reload_as_current
76
+ server.settings["locked"] = 'false'
77
+ server.save
@@ -45,7 +45,7 @@ if nickname
45
45
  server_array = Ec2ServerArray.find(:first) { |s| s.nickname =~ /#{nickname}/ }
46
46
  puts server.to_yaml if verbose
47
47
  elsif id
48
- server = Server.find(server_id.to_i)
48
+ server = Ec2ServerArray.find(server_id.to_i)
49
49
  end
50
50
  puts "Found server array, '#{server_array.nickname}'."
51
51
 
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rs-api-tools
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
5
- prerelease:
4
+ prerelease: false
6
5
  segments:
7
6
  - 0
8
7
  - 0
9
- - 1
10
- version: 0.0.1
8
+ - 2
9
+ version: 0.0.2
11
10
  platform: ruby
12
11
  authors:
13
12
  - Chris Fordham
@@ -15,17 +14,16 @@ autorequire:
15
14
  bindir: bin
16
15
  cert_chain: []
17
16
 
18
- date: 2012-06-06 00:00:00 Z
17
+ date: 2013-06-12 00:00:00 -07:00
18
+ default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: json
22
22
  prerelease: false
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
- none: false
25
24
  requirements:
26
25
  - - ">="
27
26
  - !ruby/object:Gem::Version
28
- hash: 15
29
27
  segments:
30
28
  - 1
31
29
  - 4
@@ -33,7 +31,6 @@ dependencies:
33
31
  version: 1.4.4
34
32
  - - <=
35
33
  - !ruby/object:Gem::Version
36
- hash: 13
37
34
  segments:
38
35
  - 1
39
36
  - 6
@@ -45,11 +42,9 @@ dependencies:
45
42
  name: rest_connection
46
43
  prerelease: false
47
44
  requirement: &id002 !ruby/object:Gem::Requirement
48
- none: false
49
45
  requirements:
50
46
  - - ">="
51
47
  - !ruby/object:Gem::Version
52
- hash: 3
53
48
  segments:
54
49
  - 0
55
50
  version: "0"
@@ -58,10 +53,10 @@ dependencies:
58
53
  description: RightScale API Command Line Tools.
59
54
  email: chris@xhost.com.au
60
55
  executables:
61
- - .DS_Store
62
56
  - rs-clone-deployment
63
57
  - rs-clone-rightscript
64
58
  - rs-clone-server
59
+ - rs-create-alert-spec
65
60
  - rs-describe-alerts
66
61
  - rs-describe-attached-vols
67
62
  - rs-describe-deployment
@@ -69,6 +64,7 @@ executables:
69
64
  - rs-describe-ec2-ebs-snapshots
70
65
  - rs-describe-ec2-ebs-volumes
71
66
  - rs-describe-ec2-security-groups
67
+ - rs-describe-key
72
68
  - rs-describe-keys
73
69
  - rs-describe-multi-cloud-images
74
70
  - rs-describe-rightscript
@@ -79,6 +75,7 @@ executables:
79
75
  - rs-describe-server-self
80
76
  - rs-describe-server-templates
81
77
  - rs-describe-servers
78
+ - rs-fetch-executables
82
79
  - rs-fetch-rightscripts
83
80
  - rs-get-array-instances
84
81
  - rs-get-server-input
@@ -86,22 +83,27 @@ executables:
86
83
  - rs-get-server-sketchy-data
87
84
  - rs-launch-deployment
88
85
  - rs-launch-server
86
+ - rs-lock-server
89
87
  - rs-ping-active-servers
88
+ - rs-query-cloud
90
89
  - rs-reboot-deployment
91
90
  - rs-reboot-server
92
91
  - rs-relaunch-deployment
93
92
  - rs-relaunch-server
94
93
  - rs-run-all-boot-executables
94
+ - rs-run-recipe
95
95
  - rs-run-rightscript
96
96
  - rs-search-tags
97
97
  - rs-start-deployment
98
98
  - rs-start-server
99
99
  - rs-stop-deployment
100
100
  - rs-stop-server
101
+ - rs-tag-array
101
102
  - rs-terminate-all
102
103
  - rs-terminate-deployment
103
104
  - rs-terminate-self
104
105
  - rs-terminate-server
106
+ - rs-unlock-server
105
107
  - rs-update-input
106
108
  - rs-update-inputs
107
109
  - rs-update-server-array
@@ -109,54 +111,9 @@ extensions: []
109
111
 
110
112
  extra_rdoc_files: []
111
113
 
112
- files:
113
- - bin/.DS_Store
114
- - bin/rs-clone-deployment
115
- - bin/rs-clone-rightscript
116
- - bin/rs-clone-server
117
- - bin/rs-describe-alerts
118
- - bin/rs-describe-attached-vols
119
- - bin/rs-describe-deployment
120
- - bin/rs-describe-deployments
121
- - bin/rs-describe-ec2-ebs-snapshots
122
- - bin/rs-describe-ec2-ebs-volumes
123
- - bin/rs-describe-ec2-security-groups
124
- - bin/rs-describe-keys
125
- - bin/rs-describe-multi-cloud-images
126
- - bin/rs-describe-rightscript
127
- - bin/rs-describe-rightscripts
128
- - bin/rs-describe-server
129
- - bin/rs-describe-server-array
130
- - bin/rs-describe-server-arrays
131
- - bin/rs-describe-server-self
132
- - bin/rs-describe-server-templates
133
- - bin/rs-describe-servers
134
- - bin/rs-fetch-rightscripts
135
- - bin/rs-get-array-instances
136
- - bin/rs-get-server-input
137
- - bin/rs-get-server-monitoring
138
- - bin/rs-get-server-sketchy-data
139
- - bin/rs-launch-deployment
140
- - bin/rs-launch-server
141
- - bin/rs-ping-active-servers
142
- - bin/rs-reboot-deployment
143
- - bin/rs-reboot-server
144
- - bin/rs-relaunch-deployment
145
- - bin/rs-relaunch-server
146
- - bin/rs-run-all-boot-executables
147
- - bin/rs-run-rightscript
148
- - bin/rs-search-tags
149
- - bin/rs-start-deployment
150
- - bin/rs-start-server
151
- - bin/rs-stop-deployment
152
- - bin/rs-stop-server
153
- - bin/rs-terminate-all
154
- - bin/rs-terminate-deployment
155
- - bin/rs-terminate-self
156
- - bin/rs-terminate-server
157
- - bin/rs-update-input
158
- - bin/rs-update-inputs
159
- - bin/rs-update-server-array
114
+ files: []
115
+
116
+ has_rdoc: true
160
117
  homepage: https://github.com/flaccid/rs-api-tools
161
118
  licenses: []
162
119
 
@@ -166,27 +123,23 @@ rdoc_options: []
166
123
  require_paths:
167
124
  - lib
168
125
  required_ruby_version: !ruby/object:Gem::Requirement
169
- none: false
170
126
  requirements:
171
127
  - - ">="
172
128
  - !ruby/object:Gem::Version
173
- hash: 3
174
129
  segments:
175
130
  - 0
176
131
  version: "0"
177
132
  required_rubygems_version: !ruby/object:Gem::Requirement
178
- none: false
179
133
  requirements:
180
134
  - - ">="
181
135
  - !ruby/object:Gem::Version
182
- hash: 3
183
136
  segments:
184
137
  - 0
185
138
  version: "0"
186
139
  requirements: []
187
140
 
188
141
  rubyforge_project:
189
- rubygems_version: 1.8.10
142
+ rubygems_version: 1.3.6
190
143
  signing_key:
191
144
  specification_version: 3
192
145
  summary: rs-api-tools
data/bin/.DS_Store DELETED
Binary file