cloudblocks 0.0.15 → 0.0.17

Sign up to get free protection for your applications and to get access to all the features.
data/bin/cb-agent CHANGED
@@ -2,13 +2,14 @@
2
2
 
3
3
  require 'yaml'
4
4
  require File.expand_path("../../lib/cloud-quartz", __FILE__)
5
+ require File.expand_path("../../lib/client_auth", __FILE__)
5
6
  require 'optparse'
6
7
  require 'socket'
7
8
  require 'logger'
8
9
  require 'highline/import'
9
10
  begin
10
11
  gem 'eventmachine', '~>1.0.0.beta.4'
11
- gem 'faye', '~>0.8.0'
12
+ gem 'faye', '~>0.8.3'
12
13
  gem 'highline', '~>1.6.11'
13
14
 
14
15
  require 'eventmachine'
@@ -48,7 +49,7 @@ def start
48
49
  end
49
50
  end
50
51
 
51
- check_version
52
+ #check_version
52
53
  load_plugins
53
54
  rescue => exc
54
55
  @log.error exc.message
@@ -56,14 +57,14 @@ def start
56
57
  end
57
58
 
58
59
  begin
59
- @log.info @quartz.status(1, @version, plugin_meta_data)
60
+ @log.info @quartz.init(@version, plugin_meta_data)
60
61
  rescue Exception => e
61
62
  message = e.message
62
63
  if message =~ /Couldn't find Agent with uid =/
63
64
  @log.warn "This agent is no longer registered at the server. The old registration details have been removed from this agent. Please re-run the agent to re-register it."
64
65
  puts "This agent is no longer registered at the server. The old registration details have been removed from this agent. Please re-run the agent to re-register it."
65
66
  puts @config_full
66
- File.delete(@config_full)
67
+ File.delete(@config_full)
67
68
  else
68
69
  @log.error e.message
69
70
  end
@@ -99,9 +100,9 @@ def stop
99
100
  @log.info "Attempting to stop the CloudBlocks Agent"
100
101
  puts "Attempting to stop the CloudBlocks Agent..."
101
102
 
102
- begin
103
- @quartz.status(2, @version, plugin_meta_data)
104
- EM.stop
103
+ begin
104
+ @quartz.status(2)
105
+ EM.stop
105
106
  rescue
106
107
  end
107
108
 
@@ -118,11 +119,11 @@ def stop
118
119
  end
119
120
  end
120
121
 
121
- def register
122
+ def register(stack_uid = nil)
122
123
  puts "Registering the CloudBlocks Agent with #{@url} and API Key #{@api_key}..."
123
124
  os_name = RUBY_PLATFORM
124
125
  os_id = os_name.include?('darwin') ? 5 : 1
125
- agent = { :agent_type_id => os_id, :agent_name => Socket.gethostname, :agent_timezone => Time.new.zone, :extra => os_name}
126
+ agent = { :agent_type_id => os_id, :agent_name => Socket.gethostname, :agent_timezone => Time.new.zone, :extra => os_name, :stack_uid => stack_uid }
126
127
 
127
128
  result = @quartz.register(agent)
128
129
  if result['ok']
@@ -246,43 +247,50 @@ def run_plugin(plugin, message)
246
247
  end
247
248
  end
248
249
 
249
- def check_version
250
+ #def check_version
251
+ # begin
252
+ # result = @quartz.check_version
253
+ # if result['ok']
254
+ # latest = result['latest']
255
+ # @log.warn 'A newer version of the CloudBlocks Agent is available. Please update the cloudblocks gem. See http://help.cloudblocks.co for more info' if latest > @version
256
+ # end
257
+ # rescue => exc
258
+ # warn "Cannot connect to the CloudBlocks server"
259
+ # exit -1
260
+ # end
261
+ #end
262
+
263
+ def pulsate
264
+ @log.debug "Pulsate"
250
265
  begin
251
- result = @quartz.check_version
252
- if result['ok']
253
- latest = result['latest']
254
- @log.warn 'A newer version of the CloudBlocks Agent is available. Please update the cloudblocks gem. See http://help.cloudblocks.co for more info' if latest > @version
255
- end
266
+ @quartz.pulse
256
267
  rescue => exc
257
- warn "Cannot connect to the CloudBlocks server"
258
- exit -1
268
+ @log.error "Failed to pulsate due to #{exc.message}"
259
269
  end
260
270
  end
261
271
 
262
272
  def run
263
273
  EM.run{
264
- Signal.trap('INT') { stop }
265
- Signal.trap('TERM'){ stop }
274
+ Signal.trap('INT') { stop }
275
+ Signal.trap('TERM'){ stop }
266
276
 
267
277
  # pulse
278
+ pulsate
268
279
  EM.add_periodic_timer 60 do # 1 minute
269
- @log.debug "Pulseate"
270
- begin
271
- @quartz.pulse
272
- rescue => exc
273
- @log.error "Failed to pulsate due to #{exc.message}"
274
- end
280
+ pulsate
275
281
  end
276
282
 
277
- if @realtime
278
- @log.info "Listening to realtime nofitifications from /quartz/agents/#{@agent_id} on #{@faye_url}"
279
- client = Faye::Client.new(@faye_url)
280
- client.subscribe("/quartz/agents/#{@agent_id}") do |message|
281
- @log.info "Got realtime notice for a new job #{message}"
282
- get_job
283
+ if @realtime
284
+ @log.info "Listening to realtime nofitifications from /quartz/agents/#{@agent_id} on #{@faye_url}"
285
+
286
+ client = Faye::Client.new(@faye_url)
287
+ client.add_extension(ClientAuth.new(@api_key, @secret_key))
288
+ client.subscribe("/user/#{@api_key}/quartz/agents/#{@agent_id}") do |message|
289
+ @log.info "Got realtime notice for a new job #{message}"
290
+ get_job
283
291
  end
284
- else
285
- @log.info "Checking for new jobs every 5 seconds"
292
+ else
293
+ @log.info "Checking for new jobs every 5 seconds"
286
294
  EM.add_periodic_timer 5 do
287
295
  get_job
288
296
  end
@@ -304,10 +312,10 @@ end
304
312
  def pid_process_running?(pid)
305
313
  begin
306
314
  pid_number = pid.to_i
307
- Process.getpgid(pid_number)
308
- true
315
+ Process.getpgid(pid_number)
316
+ true
309
317
  rescue Errno::ESRCH
310
- false
318
+ false
311
319
  end
312
320
  end
313
321
 
@@ -329,7 +337,7 @@ Dir.mkdir(@cb_tmp_dir) if !File.exists?(@cb_tmp_dir)
329
337
  @log_full = File.join(@cb_tmp_dir, @log_file)
330
338
 
331
339
  @api_key = ''
332
- @faye_url = 'https://sockets.cloudblocks.co/push/'
340
+ @faye_url = 'https://sockets.cloudblocks.co/push'
333
341
  commands = %w[register unregister start stop]
334
342
 
335
343
  @plugins = {}
@@ -341,13 +349,13 @@ command = nil
341
349
  @realtime = true
342
350
  OptionParser.new do |opts|
343
351
  opts.banner = <<-EOF
344
- CloudBlocks Agent. v#{str_version} (c) 2012 CloudBlocks
345
- For more information please visit http://cloudblocks.co
352
+ CloudBlocks Agent. v#{str_version} (c) 2012 CloudBlocks
353
+ For more information please visit http://cloudblocks.co
346
354
 
347
- Usage: cb-agent [register|unregister|start|stop] [options]
355
+ Usage: cb-agent [register|unregister|start|stop] [options]
348
356
 
349
- Options:
350
- EOF
357
+ Options:
358
+ EOF
351
359
 
352
360
  opts.on('--url URL', 'Server URL') do |server_url|
353
361
  @url = server_url
@@ -376,7 +384,6 @@ EOF
376
384
 
377
385
  opts.on('--sockets SOCKETS', 'Sockets URL') do |v|
378
386
  @faye_url = v
379
- @faye_url = "#{@faye_url}/" if @faye_url[-1] != '/'
380
387
  end
381
388
 
382
389
  opts.on('--api-key APIKEY', 'API key') do |v|
@@ -387,6 +394,10 @@ EOF
387
394
  @secret_key = v
388
395
  end
389
396
 
397
+ opts.on('--stack STACKIUD', 'Stack id') do |v|
398
+ @stack_uid = v
399
+ end
400
+
390
401
  opts.on('-r', '--realtime', 'Enable realtime notifications (default)') do |v|
391
402
  @realtime = true
392
403
  end
@@ -399,11 +410,11 @@ EOF
399
410
  puts opts
400
411
  puts <<-EOF
401
412
 
402
- Commands:
403
- register Register the CloudBlocks Agent
404
- start Starts the CloudBlocks Agent as a deamon
405
- stop Stops the CloudBlocks Agent daemon
406
- unregister Unregisters the CloudBlocks Agent
413
+ Commands:
414
+ register Register the CloudBlocks Agent
415
+ start Starts the CloudBlocks Agent as a deamon
416
+ stop Stops the CloudBlocks Agent daemon
417
+ unregister Unregisters the CloudBlocks Agent
407
418
 
408
419
  EOF
409
420
  exit 0
@@ -447,7 +458,7 @@ end
447
458
  # no agent id?
448
459
  if @agent_id.empty?
449
460
  @quartz = CloudQuartz.new(:api_key => @api_key, :url => @url, :secret_key => @secret_key)
450
- register
461
+ register(@stack_uid)
451
462
  @quartz = CloudQuartz.new(:api_key => @api_key, :url => @url, :agent_id => @agent_id, :secret_key => @secret_key)
452
463
  start
453
464
  end
data/lib/cloud-quartz.rb CHANGED
@@ -14,44 +14,41 @@ class CloudQuartz
14
14
  end
15
15
 
16
16
  def get_job
17
- process(self.class.get("/queue/#{@agent_id}.json", { :headers => http_headers } ))
17
+ process(self.class.get("/queue/#{@agent_id}.json", { :headers => ClientAuth.build_headers(@api_key, @secret_key) } ))
18
18
  end
19
19
 
20
20
  def register(agent)
21
- process(self.class.post('/agent.json', { :headers => http_headers.merge({'Content-Type' => 'application/json'}), :body => agent.to_json }))
21
+ process(self.class.post('/agent.json', { :headers => ClientAuth.build_headers(@api_key, @secret_key).merge({'Content-Type' => 'application/json'}), :body => agent.to_json }))
22
22
  end
23
23
 
24
24
  def unregister(agent)
25
- process(self.class.delete("/agent/#{@agent_id}.json", :headers => http_headers))
25
+ process(self.class.delete("/agent/#{@agent_id}.json", :headers => ClientAuth.build_headers(@api_key, @secret_key)))
26
26
  end
27
27
 
28
- def check_version
29
- self.class.get("/agent/version", :headers => http_headers)
30
- end
28
+ #TODO: Is this deprecated now?
29
+ #def check_version
30
+ # self.class.get("/agent/version", :headers => ClientAuth.build_headers(@api_key, @secret_key))
31
+ #end
31
32
 
32
33
  def post_results(job_id, data)
33
- process(self.class.post("/job/#{job_id}/complete.json", { :headers => http_headers.merge({'Content-Type' => 'application/json'}), :body => data.to_json } ))
34
+ process(self.class.post("/job/#{job_id}/complete.json", { :headers => ClientAuth.build_headers(@api_key, @secret_key).merge({'Content-Type' => 'application/json'}), :body => data.to_json } ))
34
35
  end
35
36
 
36
37
  def pulse
37
- process(self.class.get("/agent/#{@agent_id}/pulse.json", { :headers => http_headers } ))
38
+ process(self.class.get("/agent/#{@agent_id}/pulse.json", { :headers => ClientAuth.build_headers(@api_key, @secret_key) } ))
38
39
  end
39
40
 
40
- def status(stat, version, plugins)
41
- data = { :status => stat, :version => version, :plugins => plugins }
42
- process(self.class.post("/agent/#{@agent_id}/status.json", { :headers => http_headers.merge({'Content-Type' => 'application/json'}), :body => data.to_json }))
41
+ def status(stat)
42
+ data = { :status => stat }
43
+ process(self.class.post("/agent/#{@agent_id}/status.json", { :headers => ClientAuth.build_headers(@api_key, @secret_key).merge({'Content-Type' => 'application/json'}), :body => data.to_json }))
43
44
  end
44
45
 
45
- private
46
-
47
- def http_headers
48
- time = Time.now.utc.to_i
49
- { 'api_key' => @api_key, 'hash' => signature(time), 'time' => time.to_s }
46
+ def init(version, plugins)
47
+ data = { :version => version, :plugins => plugins }
48
+ process(self.class.post("/agent/#{@agent_id}/initialize.json", { :headers => ClientAuth.build_headers(@api_key, @secret_key).merge({'Content-Type' => 'application/json'}), :body => data.to_json }))
50
49
  end
51
50
 
52
- def signature(time)
53
- Digest::SHA1.hexdigest("#{@api_key}#{@secret_key}#{time}").downcase
54
- end
51
+ private
55
52
 
56
53
  def process(response)
57
54
  if response.code != 200
@@ -0,0 +1,74 @@
1
+ require File.join(File.dirname(__FILE__), 'quartz_plugin')
2
+ require 'tempfile'
3
+ require 'fileutils'
4
+
5
+ class Backup < QuartzPlugin
6
+
7
+ @@version_major = 0
8
+ @@version_minor = 0
9
+ @@version_revision = 1
10
+
11
+ def info
12
+ { :uid => "64cec894b98b43ce9e6047a9284a4b7d", :name => "Backup", :version => get_version }
13
+ end
14
+
15
+ def run(message)
16
+
17
+ pl = payload(message)
18
+
19
+ job_name = pl['job_name']
20
+ backup_script = "#Script generated by Cloud66 job '#{job_name}'\n\n" + pl['script']
21
+ script_name = pl['script_name']
22
+
23
+ random_file = Tempfile.new('cloud66').path + '.rb'
24
+ File.open(random_file, 'w') do |f|
25
+ f.puts backup_script
26
+ end
27
+
28
+ backup_root_directory = File.expand_path('~/.cloud66/backup')
29
+ FileUtils.mkdir_p(backup_root_directory)
30
+
31
+ command = "backup perform --trigger #{script_name} --root-path #{backup_root_directory} --config_file #{random_file} 2>&1"
32
+ @log.info "Shell command '#{command}'"
33
+
34
+ begin
35
+ result = run_shell("#{command}")
36
+ data = result[:message]
37
+
38
+ #regex to identify all the "error" lines
39
+ errorRegex = Regexp.new('(?<line>\[\d{4}\/\d{2}\/\d{2}\s\d{2}\:\d{2}\:\d{2}\]\[\\e\[31merror\\e\[0m\].*?)(\\n|$)')
40
+ errors = data.scan(errorRegex)
41
+
42
+ if errors.size > 0
43
+
44
+ #log all the errors
45
+ @log.info errors
46
+
47
+ #get rid of the backtrace
48
+ removeRegex = Regexp.new('(Backtrace\:|\d{2}\:\d{2}\]\[\\e\[31merror\\e\[0m\]\s*\/)')
49
+
50
+ #subregex to get rid of nasty timestamp and [\e[31merror\e[0m]
51
+ replaceRegex = Regexp.new('^.*\[\\e\[31merror\\e\[0m\]')
52
+
53
+ errorResult = []
54
+ errors.each do |errorArr|
55
+ error = errorArr[0]
56
+ errorResult << error.gsub(replaceRegex,'').chomp unless error =~ removeRegex
57
+ end
58
+
59
+ run_result(false, errorResult.join("\n"))
60
+ else
61
+ if (result[:ok])
62
+ run_result(true, "Backup completed successfully!")
63
+ else
64
+ run_result(true, "An error occurred!")
65
+ end
66
+ end
67
+
68
+ rescue => ex
69
+ run_result(false, "Failed to execute backup due to #{ex}")
70
+ ensure
71
+ File.delete random_file if File.exists? random_file
72
+ end
73
+ end
74
+ end
@@ -21,7 +21,7 @@ class S3Backup < QuartzPlugin
21
21
  @secret_key = pl['secret_key']
22
22
  @bucket = pl['bucket']
23
23
  @region = pl['region']
24
- @remote_path = pl['remote_path']
24
+ @remote_path = pl['remote_path']
25
25
  @local_pattern = pl['local_pattern']
26
26
  @keep = pl['keep'].empty? ? 0 : pl['keep'].to_i
27
27
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cloudblocks
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.15
4
+ version: 0.0.17
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-10 00:00:00.000000000 Z
12
+ date: 2012-08-14 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: httparty
16
- requirement: !ruby/object:Gem::Requirement
16
+ requirement: &70279506935040 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,15 +21,10 @@ dependencies:
21
21
  version: 0.8.1
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ! '>='
28
- - !ruby/object:Gem::Version
29
- version: 0.8.1
24
+ version_requirements: *70279506935040
30
25
  - !ruby/object:Gem::Dependency
31
26
  name: json
32
- requirement: !ruby/object:Gem::Requirement
27
+ requirement: &70279506933860 !ruby/object:Gem::Requirement
33
28
  none: false
34
29
  requirements:
35
30
  - - ! '>='
@@ -37,15 +32,10 @@ dependencies:
37
32
  version: 1.6.3
38
33
  type: :runtime
39
34
  prerelease: false
40
- version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
- requirements:
43
- - - ! '>='
44
- - !ruby/object:Gem::Version
45
- version: 1.6.3
35
+ version_requirements: *70279506933860
46
36
  - !ruby/object:Gem::Dependency
47
37
  name: eventmachine
48
- requirement: !ruby/object:Gem::Requirement
38
+ requirement: &70279506933120 !ruby/object:Gem::Requirement
49
39
  none: false
50
40
  requirements:
51
41
  - - ! '>='
@@ -53,15 +43,10 @@ dependencies:
53
43
  version: 0.12.10
54
44
  type: :runtime
55
45
  prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
- requirements:
59
- - - ! '>='
60
- - !ruby/object:Gem::Version
61
- version: 0.12.10
46
+ version_requirements: *70279506933120
62
47
  - !ruby/object:Gem::Dependency
63
48
  name: eventmachine
64
- requirement: !ruby/object:Gem::Requirement
49
+ requirement: &70279506932280 !ruby/object:Gem::Requirement
65
50
  none: false
66
51
  requirements:
67
52
  - - ~>
@@ -69,15 +54,10 @@ dependencies:
69
54
  version: 1.0.0.beta.4
70
55
  type: :runtime
71
56
  prerelease: false
72
- version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
- requirements:
75
- - - ~>
76
- - !ruby/object:Gem::Version
77
- version: 1.0.0.beta.4
57
+ version_requirements: *70279506932280
78
58
  - !ruby/object:Gem::Dependency
79
59
  name: faye
80
- requirement: !ruby/object:Gem::Requirement
60
+ requirement: &70279506931560 !ruby/object:Gem::Requirement
81
61
  none: false
82
62
  requirements:
83
63
  - - ! '>='
@@ -85,15 +65,10 @@ dependencies:
85
65
  version: 0.8.0
86
66
  type: :runtime
87
67
  prerelease: false
88
- version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
- requirements:
91
- - - ! '>='
92
- - !ruby/object:Gem::Version
93
- version: 0.8.0
68
+ version_requirements: *70279506931560
94
69
  - !ruby/object:Gem::Dependency
95
70
  name: open4
96
- requirement: !ruby/object:Gem::Requirement
71
+ requirement: &70279506930800 !ruby/object:Gem::Requirement
97
72
  none: false
98
73
  requirements:
99
74
  - - ! '>='
@@ -101,53 +76,49 @@ dependencies:
101
76
  version: 1.3.0
102
77
  type: :runtime
103
78
  prerelease: false
104
- version_requirements: !ruby/object:Gem::Requirement
105
- none: false
106
- requirements:
107
- - - ! '>='
108
- - !ruby/object:Gem::Version
109
- version: 1.3.0
79
+ version_requirements: *70279506930800
110
80
  - !ruby/object:Gem::Dependency
111
81
  name: fog
112
- requirement: !ruby/object:Gem::Requirement
82
+ requirement: &70279506913940 !ruby/object:Gem::Requirement
113
83
  none: false
114
84
  requirements:
115
- - - ! '>='
85
+ - - ~>
116
86
  - !ruby/object:Gem::Version
117
- version: 1.1.2
87
+ version: 1.4.0
118
88
  type: :runtime
119
89
  prerelease: false
120
- version_requirements: !ruby/object:Gem::Requirement
121
- none: false
122
- requirements:
123
- - - ! '>='
124
- - !ruby/object:Gem::Version
125
- version: 1.1.2
90
+ version_requirements: *70279506913940
126
91
  - !ruby/object:Gem::Dependency
127
- name: highline
128
- requirement: !ruby/object:Gem::Requirement
92
+ name: cloud66-backup
93
+ requirement: &70279506913160 !ruby/object:Gem::Requirement
129
94
  none: false
130
95
  requirements:
131
96
  - - ~>
132
97
  - !ruby/object:Gem::Version
133
- version: 1.6.11
98
+ version: 3.0.25
134
99
  type: :runtime
135
100
  prerelease: false
136
- version_requirements: !ruby/object:Gem::Requirement
101
+ version_requirements: *70279506913160
102
+ - !ruby/object:Gem::Dependency
103
+ name: highline
104
+ requirement: &70279506912320 !ruby/object:Gem::Requirement
137
105
  none: false
138
106
  requirements:
139
107
  - - ~>
140
108
  - !ruby/object:Gem::Version
141
109
  version: 1.6.11
110
+ type: :runtime
111
+ prerelease: false
112
+ version_requirements: *70279506912320
142
113
  description: See http://cloudblocks.co for more info
143
- email: hello@thecloudblocks.com
114
+ email: hello@cloudblocks.co
144
115
  executables:
145
116
  - cb-agent
146
117
  extensions: []
147
118
  extra_rdoc_files: []
148
119
  files:
149
- - lib/config-chief.rb
150
120
  - lib/cloud-quartz.rb
121
+ - lib/plugins/backup.rb
151
122
  - lib/plugins/broken.rb
152
123
  - lib/plugins/file_rotate.rb
153
124
  - lib/plugins/log_rotate.rb
@@ -181,7 +152,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
181
152
  version: '0'
182
153
  requirements: []
183
154
  rubyforge_project:
184
- rubygems_version: 1.8.24
155
+ rubygems_version: 1.8.15
185
156
  signing_key:
186
157
  specification_version: 3
187
158
  summary: CloudBlocks Gem and Agent
data/lib/config-chief.rb DELETED
@@ -1,100 +0,0 @@
1
- require 'HTTParty'
2
- require 'socket'
3
- require 'json'
4
-
5
- class ConfigChief
6
- include HTTParty
7
-
8
- @api_key = ""
9
- @workspace = ""
10
-
11
- def initialize(options = {})
12
- @api_key = options[:api_key]
13
- @workspace = options[:workspace]
14
- self.class.base_uri options[:url] || 'https://api.cloudblocks.co'
15
- end
16
-
17
- def get_by_key(key, options = {}, params = {})
18
- opts = {
19
- :default_value => nil,
20
- :full_obj => false
21
- }.merge(options)
22
-
23
- optionals = {}
24
- params.each do |k, v|
25
- optionals["cc-#{k}"] = v
26
- end
27
-
28
- headers = { :headers => http_headers.merge(optionals) }
29
-
30
- result = self.class.get("/workspaces/#{@workspace}/value.json", headers.merge(:query => { :query => key } ))
31
-
32
- if result.response.code.to_i == 200
33
- retrieved = result.parsed_response
34
- return retrieved if opts[:full_obj]
35
- return retrieved['parsed_value']
36
- end
37
-
38
- return opts[:default_value]
39
- end
40
-
41
- def get_by_id(id, options = {}, params = {})
42
- opts = {
43
- :default_value => nil,
44
- :full_obj => false
45
- }.merge(options)
46
-
47
- optionals = {}
48
- params.each do |k, v|
49
- optionals["cc-#{k}"] = v
50
- end
51
-
52
- headers = { :headers => http_headers.merge(optionals) }
53
-
54
- result = self.class.get("/workspaces/#{@workspace}/config_keys/#{id}.json", headers )
55
-
56
- if result.response.code.to_i == 200
57
- retrieved = result.parsed_response
58
- return retrieved if opts[:full_obj]
59
- return retrieved['parsed_value']
60
- end
61
-
62
- return opts[:default_value]
63
- end
64
-
65
- def workspaces
66
- self.class.get("/workspaces.json", { :headers => http_headers } )
67
- end
68
-
69
- def config_items(query = '*')
70
- headers = { :headers => http_headers }
71
- self.class.get("/workspaces/#{@workspace}/config_keys.json", headers.merge(:query => { :query => query } ))
72
- end
73
-
74
- def set_value(key, value)
75
- headers = { :headers => http_headers.merge({'Content-Type' => 'application/json'}) }
76
- self.class.post("/workspaces/#{@workspace}/config_keys.json", headers.merge(:body => { :key => key, :value => value }.to_json))
77
- end
78
-
79
- def register_node(options = {})
80
- headers = { :headers => http_headers.merge({'Content-Type' => 'application/json'}) }
81
- self.class.post("/workspaces/#{@workspace}/node.json", headers.merge(:body => options.to_json))
82
- end
83
-
84
- def unregister_node(node_uid)
85
- headers = { :headers => http_headers }
86
- self.class.delete("/workspaces/#{@workspace}/node/#{node_uid}.json", headers)
87
- end
88
-
89
- def update_node_status(node_uid, status)
90
- headers = { :headers => http_headers.merge({'Content-Type' => 'application/json'}) }
91
- self.class.post("/workspaces/#{@workspace}/node/#{node_uid}/status.json", headers.merge({ :body => { :status => status}.to_json}))
92
- end
93
-
94
- private
95
-
96
- def http_headers
97
- { 'api_key' => @api_key, 'ConfigChief-Node' => Socket.gethostname }
98
- end
99
-
100
- end