ncc-api 1.1.2 → 1.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a94804c0a647446cdfd140587f6337c806a4b133
4
- data.tar.gz: 59c2c2f47c8bc855674828a4185302c7ae51c561
3
+ metadata.gz: ff7b4ce8e75c66fa4ec2faaf1d703d5f918f7721
4
+ data.tar.gz: d6d4a5e717d9d60373561a5bb0c65b663981d439
5
5
  SHA512:
6
- metadata.gz: 11ed2a94d010101eaa53a465941414bde1c78a64a62c0cb84f8c3d567fb6ce12fad68e32b9da44a481517ee229b670b7f39ef57de2de0e1033e007e214262a01
7
- data.tar.gz: 9ded9650c9df907a13241f196ff43e4963485f900249e034df2c745ff62d104c2555bc92b4de72ad31ba7b76565372a090b35149b572d1f209293cbfd7637829
6
+ metadata.gz: 886099d41691e9de1d418bd2ddcec075b1a1ee9b8b6e288523020a55bff02faf0d9534add86a58f16de42e01f6f39aa3bddf6044710266781fe22ff6ee291af3
7
+ data.tar.gz: ab9c60986b5177f960889fa0802e38daafbec3b41418f2995e4cfd9d9bfcd6000410f8d32ecf4fb1f2db00db87ce2c22bc76b2e7e3dd8a0d4fa3660ac5cfd209
@@ -22,13 +22,10 @@ require 'sinatra'
22
22
  require 'fog'
23
23
  require 'rack/logger'
24
24
 
25
-
26
- configure :development do
27
- set :logging, Logger::DEBUG
25
+ def ncc
26
+ $ncc ||= NCC.new(nil, :logger => logger)
28
27
  end
29
28
 
30
- $ncc = NCC.new
31
-
32
29
  def error_message(status, err, object=nil)
33
30
  status_message = case status
34
31
  when 400
@@ -56,6 +53,7 @@ def error_message(status, err, object=nil)
56
53
  end
57
54
  body = (params.has_key?('pretty') ? (JSON.pretty_generate(data) +
58
55
  "\n") : data.to_json)
56
+ logger.error "#{status_message} (#{err.class}) #{err.message}: #{err.backtrace.join("\n")}"
59
57
  [status, { "content-type" => "application/json" }, body]
60
58
  end
61
59
 
@@ -82,7 +80,7 @@ end
82
80
 
83
81
  get '/ncc_api' do
84
82
  respond 200 do
85
- $ncc.config['services'].to_hash.merge({ 'v2api' => $ncc.api_url })
83
+ ncc.config['services'].to_hash.merge({ 'v2api' => ncc.api_url })
86
84
  end
87
85
  end
88
86
 
@@ -98,23 +96,23 @@ get '/ncc_api/v2' do
98
96
  end
99
97
 
100
98
  get '/ncc_api/v2/clouds' do
101
- respond(200) { $ncc.clouds }
99
+ respond(200) { ncc.clouds }
102
100
  end
103
101
 
104
102
  get '/ncc_api/v2/sizes' do
105
- respond(200) { $ncc.sizes }
103
+ respond(200) { ncc.sizes }
106
104
  end
107
105
 
108
106
  get '/ncc_api/v2/images' do
109
- respond(200) { $ncc.images }
107
+ respond(200) { ncc.images }
110
108
  end
111
109
 
112
110
  get '/ncc_api/v2/sizes/:size_id' do |size_id|
113
- respond(200) { $ncc.sizes(size_id) }
111
+ respond(200) { ncc.sizes(size_id) }
114
112
  end
115
113
 
116
114
  get '/ncc_api/v2/images/:image_id' do |image_id|
117
- respond(200) { $ncc.images(image_id) }
115
+ respond(200) { ncc.images(image_id) }
118
116
  end
119
117
 
120
118
  get '/ncc_api/v2/clouds/:cloud' do |cloud|
@@ -122,30 +120,30 @@ get '/ncc_api/v2/clouds/:cloud' do |cloud|
122
120
  {
123
121
  'name' => cloud,
124
122
  'status' => 'ok',
125
- 'provider' => $ncc.clouds(cloud).provider,
126
- 'service' => $ncc.clouds(cloud).fog.class.to_s
123
+ 'provider' => ncc.clouds(cloud).provider,
124
+ 'service' => ncc.clouds(cloud).fog.class.to_s
127
125
  }
128
126
  end
129
127
  end
130
128
 
131
129
  get '/ncc_api/v2/clouds/:cloud/sizes' do |cloud|
132
- respond(200) { $ncc.clouds(cloud).sizes }
130
+ respond(200) { ncc.clouds(cloud).sizes }
133
131
  end
134
132
 
135
133
  get '/ncc_api/v2/clouds/:cloud/sizes/:size_id' do |cloud, size_id|
136
- respond(200) { $ncc.clouds(cloud).sizes(size_id) }
134
+ respond(200) { ncc.clouds(cloud).sizes(size_id) }
137
135
  end
138
136
 
139
137
  get '/ncc_api/v2/clouds/:cloud/images' do |cloud|
140
- respond(200) { $ncc.clouds(cloud).images }
138
+ respond(200) { ncc.clouds(cloud).images }
141
139
  end
142
140
 
143
141
  get '/ncc_api/v2/clouds/:cloud/images/:image_id' do |cloud, image_id|
144
- respond(200) { $ncc.clouds(cloud).images(image_id) }
142
+ respond(200) { ncc.clouds(cloud).images(image_id) }
145
143
  end
146
144
 
147
145
  get '/ncc_api/v2/clouds/:cloud/instances' do |cloud|
148
- respond(200) { $ncc.clouds(cloud).instances.map { |i| i.to_hash } }
146
+ respond(200) { ncc.clouds(cloud).instances.map { |i| i.to_hash } }
149
147
  end
150
148
 
151
149
 
@@ -153,13 +151,13 @@ get '/ncc_api/v2/clouds/:cloud/instances/:instance_id/console_log' do |cloud,
153
151
  instance_id|
154
152
  respond(200, 'content-type' => 'text/plain') do
155
153
  # TODO influence last-modified with console log timestamp
156
- $ncc.clouds(cloud).console_log(instance_id)['output']
154
+ ncc.clouds(cloud).console_log(instance_id)['output']
157
155
  end
158
156
  end
159
157
 
160
158
  get '/ncc_api/v2/clouds/:cloud/instances/:instance_id/console' do |cloud,
161
159
  instance_id|
162
- respond(200) { $ncc.clouds(cloud).console(instance_id) }
160
+ respond(200) { ncc.clouds(cloud).console(instance_id) }
163
161
  end
164
162
 
165
163
  post '/ncc_api/v2/clouds/:cloud/instances' do |cloud|
@@ -168,7 +166,7 @@ post '/ncc_api/v2/clouds/:cloud/instances' do |cloud|
168
166
  request.body.rewind
169
167
  instance_spec = JSON.parse(request.body.read)
170
168
  instance_req = instance_spec
171
- $ncc.clouds(cloud).create_instance(instance_req)
169
+ ncc.clouds(cloud).create_instance(instance_req)
172
170
  rescue JSON::ParserError => e
173
171
  raise NCC::Error::Client, "Error parsing request: #{e.message}"
174
172
  end
@@ -177,7 +175,7 @@ end
177
175
 
178
176
  get '/ncc_api/v2/clouds/:cloud/instances/:instance_id' do |cloud, instance_id|
179
177
  respond(200) do
180
- $ncc.clouds(cloud).instances(instance_id).to_hash
178
+ ncc.clouds(cloud).instances(instance_id).to_hash
181
179
  end
182
180
  end
183
181
 
@@ -185,14 +183,14 @@ end
185
183
  delete '/ncc_api/v2/clouds/:cloud/instances/:instance_id' do |cloud,
186
184
  instance_id|
187
185
  respond 204 do
188
- $ncc.clouds(cloud).delete(instance_id)
186
+ ncc.clouds(cloud).delete(instance_id)
189
187
  nil
190
188
  end
191
189
  end
192
190
 
193
191
  put '/ncc_api/v2/clouds/:cloud/instances/:instance_id' do |cloud, instance_id|
194
192
  respond 202 do
195
- instance = $ncc.clouds(cloud).instances(instance_id)
193
+ instance = ncc.clouds(cloud).instances(instance_id)
196
194
  begin
197
195
  request.body.rewind
198
196
  update_spec = JSON.parse(request.body.read)
@@ -205,7 +203,7 @@ put '/ncc_api/v2/clouds/:cloud/instances/:instance_id' do |cloud, instance_id|
205
203
  when 'status'
206
204
  if value == 'reboot'
207
205
  actions << lambda { instance.status = 'reboot' }
208
- $ncc.clouds(cloud).reboot(instance.id)
206
+ ncc.clouds(cloud).reboot(instance.id)
209
207
  else
210
208
  raise NCC::Error::Client,
211
209
  "Cannot update to status #{value.inspect}"
data/lib/ncc.rb CHANGED
@@ -26,10 +26,11 @@ require 'ncc/version'
26
26
 
27
27
  class NCC::Configurator
28
28
 
29
- attr_accessor :config_path
29
+ attr_accessor :config_path, :logger
30
30
 
31
31
  def initialize
32
32
  @config_path = ['/etc/ncc-api']
33
+ @logger = nil
33
34
  end
34
35
 
35
36
  end
@@ -44,22 +45,29 @@ class NCC
44
45
  end
45
46
 
46
47
  def initialize(config_path=nil, opt={})
47
- @logger = opt[:logger] if opt.has_key? :logger
48
+ @logger = opt[:logger] || @@global_config.logger
48
49
  config_path ||= @@global_config.config_path
49
50
  config_path = [config_path] unless config_path.respond_to? :unshift
50
51
  config_path.unshift(File.join(ENV['NCCAPI_HOME'], 'etc')) if
51
52
  ENV['NCCAPI_HOME']
53
+ info "Loading configuration from: #{config_path.inspect}"
52
54
  @config = NCC::Config.new(config_path, :logger => @logger)
53
55
  @inventory = NOMS::CMDB.new(@config)
54
56
  @clouds = { }
55
57
  end
56
58
 
57
59
  def debug(msg)
58
- if @logger.respond_to? :debug
60
+ if @logger and @logger.respond_to? :debug
59
61
  @logger.debug "#{me}: #{msg}"
60
62
  end
61
63
  end
62
64
 
65
+ def info(msg)
66
+ if @logger and @logger.respond_to? :info
67
+ @logger.info "#{me}: #{msg}"
68
+ end
69
+ end
70
+
63
71
  def me
64
72
  self.class
65
73
  end
@@ -105,8 +105,12 @@ class NCC::Config
105
105
  end
106
106
 
107
107
  def debug(msg)
108
- if opt(:logger) and opt(:logger).respond_to? :debug
109
- opt(:logger).debug "#<#{me}>: #{msg}"
108
+ # This produces a *lot* of debug logging, thus it's best to be
109
+ # able to use something else to control its log level.
110
+ if ENV.has_key? 'NCC_CONFIG_DEBUG' and not ENV['NCC_CONFIG_DEBUG'].empty?
111
+ if opt(:logger) and opt(:logger).respond_to? :debug
112
+ opt(:logger).debug "#<#{me}>: #{msg}"
113
+ end
110
114
  end
111
115
  end
112
116
 
@@ -15,9 +15,6 @@
15
15
  # limitations under the License.
16
16
  # */
17
17
 
18
- # TODO:
19
- # Align desired logger interface with Ruby Logger class standard
20
-
21
18
  require 'rubygems'
22
19
  require 'fog'
23
20
  require 'uuidtools'
@@ -90,8 +87,8 @@ class NCC::Connection
90
87
  end
91
88
 
92
89
  def notice(msg)
93
- if @logger and @logger.respond_to? :notice
94
- @logger.notice "#{self} #{msg}"
90
+ if @logger and @logger.respond_to? :info
91
+ @logger.info "#{self} #{msg}"
95
92
  end
96
93
  end
97
94
 
@@ -101,11 +98,24 @@ class NCC::Connection
101
98
  end
102
99
  end
103
100
 
101
+ def error(msg)
102
+ if @logger and @logger.respond_to? :error
103
+ @logger.error "#{self} #{msg}"
104
+ end
105
+ end
106
+
107
+ def fatal(msg)
108
+ if @logger and @logger.respond_to? :fatal
109
+ @logger.error "#{self} #{msg}"
110
+ end
111
+ end
112
+
104
113
  def connection_params
105
114
  []
106
115
  end
107
116
 
108
117
  def do_connect
118
+ info "Connecting to #{provider} cloud #{@cloud}"
109
119
  cloud_config = @cfg[:clouds][@cloud]
110
120
  pnames = connection_params + ['provider']
111
121
  params = Hash[cloud_config.to_hash(*pnames).map do |k, v|
@@ -564,9 +574,9 @@ class NCC::Connection
564
574
  end
565
575
 
566
576
  def communication_error(message)
567
- message = "Error communicating with #{provider} cloud #{@cloud}: " + message unless
577
+ raise NCC::Error::Cloud,
578
+ "Error communicating with #{provider} cloud #{@cloud}: " + message unless
568
579
  /Error .*communicating with/.match(message)
569
- raise NCC::Error::Cloud, message
570
580
  end
571
581
 
572
582
  def delete(instance_id)
@@ -1,3 +1,3 @@
1
1
  class NCC
2
- VERSION = '1.1.2'
2
+ VERSION = '1.2.2'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ncc-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
4
+ version: 1.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Brinkley
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-23 00:00:00.000000000 Z
11
+ date: 2015-01-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fog