bonobo 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 17abe2387534346d865f43d8a8422ae7ecddccde
4
+ data.tar.gz: e2acb90fe4b744c4d08c55aefc648690e0ccd6b6
5
+ SHA512:
6
+ metadata.gz: cfcefb1d006b4483cf353f18d9433f318320d774845ed24c05af58caa411a8cf32143e103520dd5f993cf69eb3c59450723e7e99467781f1557837faab1b995a
7
+ data.tar.gz: 28e332acd3a32095a6eb7bf0c0d5c383dd9f2f0344709b7e237d43e49746f7a32a75d0b1937c8d6774b02b4a25a6382ba702f5aea5d4184a6a769c1ee4e8edfd
@@ -0,0 +1,97 @@
1
+ require 'getoptlong'
2
+ require 'logger'
3
+ require 'yaml'
4
+ require 'right_api_client'
5
+ require 'rest-client'
6
+
7
+ require 'pry'
8
+
9
+ require 'bonobo/connection'
10
+ require 'bonobo/log'
11
+
12
+ # this is our main bonobo class
13
+ class Bonobo
14
+ VERSION = '0.1.0'.freeze
15
+ attr_accessor :params, :filter
16
+
17
+ def initialize
18
+ # set up some nice defaults
19
+ @params = {}
20
+ @params[:tags] = []
21
+ @params[:arrays] = []
22
+ @params[:deployments] = []
23
+ @params[:inputs] = []
24
+ @params[:script] = ''
25
+
26
+ @filter = :none
27
+ end
28
+
29
+ def parse_command_line
30
+ opts = GetoptLong.new(
31
+ ['--tag', '-t', GetoptLong::REQUIRED_ARGUMENT],
32
+ ['--array', '-a', GetoptLong::REQUIRED_ARGUMENT],
33
+ ['--deployment', '-d', GetoptLong::REQUIRED_ARGUMENT],
34
+ ['--script', '-s', GetoptLong::REQUIRED_ARGUMENT],
35
+ ['--input', '-i', GetoptLong::REQUIRED_ARGUMENT],
36
+ ['--help', '-h', GetoptLong::NO_ARGUMENT],
37
+ ['--version', '-v', GetoptLong::NO_ARGUMENT],
38
+ ['--verbose', GetoptLong::NO_ARGUMENT]
39
+ )
40
+
41
+ opts.each do |opt, arg|
42
+ case opt
43
+ when '--tag', '-t'
44
+ @params[:tags] << arg
45
+ @filter = :tag
46
+ when '--array', '-a'
47
+ @params[:arrays] << arg
48
+ @filter = :array
49
+ when '--deployment', '-d'
50
+ @params[:deployments] << arg
51
+ @filter = :deployment
52
+ when '--script', '-s'
53
+ @params[:script] = arg
54
+ when '--input', '-i'
55
+ @params[:inputs] << arg
56
+ when '--help', '-h'
57
+ help
58
+ exit 0
59
+ when '--version', '-v'
60
+ Log.info VERSION
61
+ exit 0
62
+ when '--verbose'
63
+ Log.threshold = Logger::DEBUG
64
+ end
65
+ end
66
+
67
+ rescue GetoptLong::InvalidOption => ex
68
+ help
69
+ p ex
70
+ exit 1
71
+ end
72
+
73
+ def run
74
+ parse_command_line
75
+ # Connect to the api
76
+ @client = Bonobo::Connection.new
77
+
78
+ servers = find_objects(@filter)
79
+
80
+ servers.each do |s|
81
+ Log.info s['name']
82
+ end
83
+ end
84
+
85
+ def find_objects(filter)
86
+ case filter
87
+ when :tag
88
+ results = @client.by_tag(@params[:tags])
89
+ when :array
90
+ results = @client.by_array(@params[:arrays])
91
+ when :deployment
92
+ raise 'Not done yet'
93
+ end
94
+
95
+ results
96
+ end
97
+ end
@@ -0,0 +1,99 @@
1
+ class Bonobo
2
+ # this class handles all connection related tasks
3
+ class Connection
4
+ require 'singleton'
5
+ attr_accessor :client, :audit_url, :endpoint, :account_id
6
+
7
+ # We simply load up the creds file and set up account and api_url
8
+ def load_creds
9
+ creds = YAML.load_file("#{ENV['HOME']}/.rest_connection/rest_api_config.yaml")
10
+
11
+ # Extract specific details
12
+ creds[:account] = File.basename(creds[:api_url])
13
+ creds[:api_url] = 'https://' + URI.parse(creds[:api_url]).host
14
+
15
+ @account_id = creds[:account]
16
+ @endpoint = URI.parse(creds[:api_url]).host
17
+
18
+ creds
19
+ end
20
+
21
+ # here we log onto the api
22
+ def initialize
23
+ Log.debug 'Logging into Api 1.5 right_api_client'
24
+ creds = load_creds
25
+
26
+ @client = RightApi::Client.new(
27
+ email: creds[:user],
28
+ password: creds[:pass],
29
+ account_id: creds[:account],
30
+ api_url: creds[:api_url],
31
+ timeout: 60,
32
+ enable_retry: true
33
+ )
34
+
35
+ Log.info 'Connected to account: ' + @account_id
36
+ rescue
37
+ puts 'Error: connection couldnt be establishhed'
38
+ end
39
+
40
+ # wrapper to make any query to the 1.6 api
41
+ def api16_call(query)
42
+ url = 'https://' + @endpoint + query
43
+ response = RestClient::Request.execute(
44
+ method: :get,
45
+ url: url,
46
+ cookies: @client.cookies,
47
+ headers: { 'X-Api_Version' => '1.6',
48
+ 'X-Account' => @client.account_id }
49
+ )
50
+
51
+ result = JSON.parse(response)
52
+ Log.debug result.size.to_s + ' instances matched our query : ' + query
53
+ result
54
+ rescue RestClient::ExceptionWithResponse => err
55
+ p err.inspect
56
+ end
57
+
58
+ # wrapper to request all operational instances
59
+ def all_instances
60
+ all_instances = instances
61
+ all_instances
62
+ end
63
+
64
+ # wrapper to request a specific subset of instances
65
+ def instances(extra_filters = '')
66
+ filters_list = 'state=operational&' + extra_filters
67
+ filters = CGI.escape(filters_list)
68
+
69
+ query = '/api/instances?view=full&filter=' + filters
70
+
71
+ instances = api16_call(query)
72
+ instances
73
+ end
74
+
75
+ # this function filters out instances based on an array of tags
76
+ # tags should be an Array.
77
+ def by_tag(tags = [])
78
+ binding.pry
79
+ t = tags.join('&tag=')
80
+ filter = "tag=#{t}"
81
+ results = instances(filter)
82
+
83
+ Log.warn 'Tag query returned no results: ' + tags.join(' ') if results.empty?
84
+ results
85
+ end
86
+
87
+ # get server arrays
88
+ def by_array(arrays = [])
89
+ instances = all_instances
90
+ # select only array members
91
+ instances.select! do |i|
92
+ i['links']['incarnator']['kind'] == 'cm#server_array' &&
93
+ arrays.any? { |array| i['links']['incarnator']['name'].include?(array) }
94
+ end
95
+
96
+ instances
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,38 @@
1
+ class Bonobo
2
+ # setting up all logger related
3
+ class Log
4
+ @@logger = Logger.new(STDOUT)
5
+
6
+ # SeverityID, [DateTime #pid] SeverityLabel -- ProgName: message
7
+ @@logger.datetime_format = '%H:%M:%S'
8
+ @@logger.sev_threshold=Logger::INFO
9
+
10
+ def self.logger=(l)
11
+ @@logger = l
12
+ end
13
+
14
+ def self.threshold=(level)
15
+ @@logger.sev_threshold = level
16
+ end
17
+
18
+ def self.default_level(level)
19
+ @@logger.level = level
20
+ end
21
+
22
+ def self.debug(m)
23
+ @@logger.debug(m)
24
+ end
25
+
26
+ def self.info(m)
27
+ @@logger.info(m)
28
+ end
29
+
30
+ def self.warn(m)
31
+ @@logger.warn(m)
32
+ end
33
+
34
+ def self.error(m)
35
+ @@logger.error(m)
36
+ end
37
+ end
38
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bonobo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Marc Goujon
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2010-04-28 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A simple right_chimp
14
+ email: marc@rightscale.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/bonobo.rb
20
+ - lib/bonobo/connection.rb
21
+ - lib/bonobo/log.rb
22
+ homepage: http://rubygems.org/gems/bonobo
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.5.1
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: Im a bonobo!
46
+ test_files: []