sfpagent 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of sfpagent might be problematic. Click here for more details.

data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  SFP Agent for Ruby
2
2
  ==================
3
3
  - Author: Herry (herry13@gmail.com)
4
- - Version: 0.1.1
4
+ - Version: 0.1.2
5
5
  - License: [BSD License](https://github.com/herry13/sfpagent/blob/master/LICENSE)
6
6
 
7
7
  A Ruby script and API of an SFP agent. The agent could be accessed through HTTP RESTful API.
data/lib/sfpagent.rb CHANGED
@@ -1,14 +1,14 @@
1
1
  # external dependencies
2
2
  require 'rubygems'
3
3
  require 'json'
4
- require 'sfp'
4
+ #require 'sfp'
5
5
 
6
6
  module Nuri
7
7
  end
8
8
 
9
9
  # internal dependencies
10
10
  libdir = File.expand_path(File.dirname(__FILE__))
11
-
11
+ require libdir + '/../../sfp-ruby/lib/sfp.rb'
12
12
  require libdir + '/sfpagent/net_helper.rb'
13
13
  require libdir + '/sfpagent/executor.rb'
14
14
  require libdir + '/sfpagent/runtime.rb'
@@ -30,6 +30,7 @@ module Sfp
30
30
  WEBrick::BasicLog::WARN)
31
31
 
32
32
  @@model_lock = Mutex.new
33
+ @@runtime_lock = Mutex.new
33
34
 
34
35
  def self.logger
35
36
  @@logger
@@ -176,7 +177,7 @@ module Sfp
176
177
  @@logger.info "There is no model in cache."
177
178
  else
178
179
  begin
179
- @@runtime = Sfp::Runtime.new(model)
180
+ @@runtime_lock.synchronize { @@runtime = Sfp::Runtime.new(model) }
180
181
  @@logger.info "Reloading the model in cache [OK]"
181
182
  rescue Exception => e
182
183
  @@logger.error "Reloading the model in cache [Failed] #{e}"
@@ -187,41 +188,46 @@ module Sfp
187
188
  # Return the current state of the model.
188
189
  #
189
190
  def self.get_state(as_sfp=true)
190
- return nil if !defined?(@@runtime) or @@runtime.nil?
191
- begin
192
- @@runtime.get_state if @@runtime.modules.nil?
193
- return @@runtime.get_state(as_sfp)
194
- rescue Exception => e
195
- @@logger.error "Get state [Failed] #{e}\n#{e.backtrace.join("\n")}"
196
- end
191
+ @@runtime_lock.synchronize {
192
+ return nil if !defined?(@@runtime) or @@runtime.nil?
193
+ begin
194
+ @@runtime.get_state if @@runtime.modules.nil?
195
+ return @@runtime.get_state(as_sfp)
196
+ rescue Exception => e
197
+ @@logger.error "Get state [Failed] #{e}\n#{e.backtrace.join("\n")}"
198
+ end
199
+ }
197
200
  false
198
201
  end
199
202
 
200
203
  def self.resolve(path, as_sfp=true)
201
- return Sfp::Undefined.new if !defined?(@@runtime) or @@runtime.nil? or @@runtime.modules.nil?
202
204
  begin
203
- path = path.simplify
204
- _, node, _ = path.split('.', 3)
205
- if @@runtime.modules.has_key?(node)
206
- # local resolve
207
- parent, attribute = path.pop_ref
208
- mod = @@runtime.modules.at?(parent)
209
- if mod.is_a?(Hash)
210
- mod[:_self].update_state
211
- state = mod[:_self].state
212
- return state[attribute] if state.has_key?(attribute)
213
- end
214
- else
215
- agents = get_agents
216
- if agents[node].is_a?(Hash)
217
- agent = agents[node]
218
- path = path[1, path.length-1].gsub /\./, '/'
219
- code, data = NetHelper.get_data(agent['sfpAddress'], agent['sfpPort'], "/state#{path}")
220
- if code.to_i == 200
221
- state = JSON[data]['state']
222
- return Sfp::Unknown.new if state == '<sfp::unknown>'
223
- return state if !state.is_a?(String) or state[0,15] != '<sfp::undefined'
205
+ @@runtime_lock.synchronize {
206
+ return Sfp::Undefined.new if !defined?(@@runtime) or @@runtime.nil? or @@runtime.modules.nil?
207
+ path = path.simplify
208
+ _, node, _ = path.split('.', 3)
209
+ if @@runtime.modules.has_key?(node)
210
+ # local resolve
211
+ parent, attribute = path.pop_ref
212
+ mod = @@runtime.modules.at?(parent)
213
+ if mod.is_a?(Hash)
214
+ mod[:_self].update_state
215
+ state = mod[:_self].state
216
+ return state[attribute] if state.has_key?(attribute)
224
217
  end
218
+ return Sfp::Undefined.new
219
+ end
220
+ }
221
+ agents = get_agents
222
+ if agents[node].is_a?(Hash)
223
+ # remote resolve
224
+ agent = agents[node]
225
+ path = path[1, path.length-1].gsub /\./, '/'
226
+ code, data = NetHelper.get_data(agent['sfpAddress'], agent['sfpPort'], "/state#{path}")
227
+ if code.to_i == 200
228
+ state = JSON[data]['state']
229
+ return Sfp::Unknown.new if state == '<sfp::unknown>'
230
+ return state if !state.is_a?(String) or state[0,15] != '<sfp::undefined'
225
231
  end
226
232
  end
227
233
  rescue Exception => e
@@ -1,6 +1,6 @@
1
- require 'etc'
2
- require 'fileutils'
3
-
1
+ #
2
+ # predefined methods: update_state, apply, reset, resolve
3
+ #
4
4
  module Sfp::Resource
5
5
  attr_accessor :parent
6
6
  attr_reader :state, :model
@@ -17,6 +17,10 @@ module Sfp::Resource
17
17
  @state = {}
18
18
  end
19
19
 
20
+ def apply(p={})
21
+ true
22
+ end
23
+
20
24
  def to_model
21
25
  @state = {}
22
26
  @model.each { |k,v| @state[k] = v }
data/sfpagent.gemspec CHANGED
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'sfpagent'
3
- s.version = '0.1.1'
4
- s.date = '2013-07-03'
3
+ s.version = '0.1.2'
4
+ s.date = '2013-08-05'
5
5
  s.summary = 'SFP Agent'
6
6
  s.description = 'A Ruby implementation of SFP agent.'
7
7
  s.authors = ['Herry']
@@ -16,5 +16,5 @@ Gem::Specification.new do |s|
16
16
  s.homepage = 'https://github.com/herry13/sfpagent'
17
17
  s.rubyforge_project = 'sfpagent'
18
18
 
19
- s.add_dependency 'sfp', '~> 0.3.0'
19
+ s.add_dependency 'sfp', '~> 0.3.6'
20
20
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sfpagent
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,19 +9,24 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-03 00:00:00.000000000 Z
12
+ date: 2013-08-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sfp
16
- requirement: &17002580 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: 0.3.0
21
+ version: 0.3.6
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *17002580
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 0.3.6
25
30
  description: A Ruby implementation of SFP agent.
26
31
  email: herry13@gmail.com
27
32
  executables:
@@ -62,7 +67,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
62
67
  version: '0'
63
68
  requirements: []
64
69
  rubyforge_project: sfpagent
65
- rubygems_version: 1.8.11
70
+ rubygems_version: 1.8.23
66
71
  signing_key:
67
72
  specification_version: 3
68
73
  summary: SFP Agent