omf_common 6.0.2.pre.2 → 6.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. data/bin/file_broadcaster.rb +5 -0
  2. data/bin/file_receiver.rb +5 -0
  3. data/bin/omf_keygen +3 -3
  4. data/bin/omf_send_configure +114 -0
  5. data/bin/omf_send_request +19 -4
  6. data/example/engine_alt.rb +13 -7
  7. data/example/viz/garage_monitor.rb +69 -0
  8. data/example/viz/garage_viz.rb +52 -0
  9. data/example/viz/htdocs/image/garage.png +0 -0
  10. data/example/viz/htdocs/template/garage_banner.html +2 -0
  11. data/example/viz/layout.yaml +44 -0
  12. data/example/vm_alt.rb +5 -0
  13. data/lib/omf_common.rb +17 -8
  14. data/lib/omf_common/auth.rb +5 -0
  15. data/lib/omf_common/auth/certificate.rb +21 -2
  16. data/lib/omf_common/auth/certificate_store.rb +50 -20
  17. data/lib/omf_common/auth/ssh_pub_key_convert.rb +7 -0
  18. data/lib/omf_common/comm.rb +6 -1
  19. data/lib/omf_common/comm/amqp/amqp_communicator.rb +88 -12
  20. data/lib/omf_common/comm/amqp/amqp_file_transfer.rb +5 -0
  21. data/lib/omf_common/comm/amqp/amqp_topic.rb +37 -18
  22. data/lib/omf_common/comm/local/local_communicator.rb +5 -0
  23. data/lib/omf_common/comm/local/local_topic.rb +5 -0
  24. data/lib/omf_common/comm/topic.rb +32 -13
  25. data/lib/omf_common/comm/xmpp/communicator.rb +11 -1
  26. data/lib/omf_common/comm/xmpp/topic.rb +5 -0
  27. data/lib/omf_common/comm/xmpp/xmpp_mp.rb +5 -0
  28. data/lib/omf_common/command.rb +5 -0
  29. data/lib/omf_common/core_ext/string.rb +5 -0
  30. data/lib/omf_common/default_logging.rb +23 -5
  31. data/lib/omf_common/eventloop.rb +40 -23
  32. data/lib/omf_common/eventloop/em.rb +18 -5
  33. data/lib/omf_common/eventloop/local_evl.rb +18 -15
  34. data/lib/omf_common/exec_app.rb +44 -24
  35. data/lib/omf_common/key.rb +5 -0
  36. data/lib/omf_common/measure.rb +5 -0
  37. data/lib/omf_common/message.rb +5 -0
  38. data/lib/omf_common/message/json/json_message.rb +13 -5
  39. data/lib/omf_common/message/xml/message.rb +19 -4
  40. data/lib/omf_common/message/xml/relaxng_schema.rb +5 -0
  41. data/lib/omf_common/message/xml/topic_message.rb +5 -0
  42. data/lib/omf_common/version.rb +6 -1
  43. data/omf_common.gemspec +3 -2
  44. data/test/fixture/1st_level.pem +20 -0
  45. data/test/fixture/2nd_level.pem +19 -0
  46. data/test/fixture/3rd_level.pem +19 -0
  47. data/test/fixture/pubsub.rb +5 -0
  48. data/test/fixture/rc.pem +18 -0
  49. data/test/fixture/root.pem +17 -0
  50. data/test/omf_common/auth/certificate_spec.rb +27 -0
  51. data/test/omf_common/auth/certificate_store_spec.rb +58 -0
  52. data/test/omf_common/auth/ssh_pub_key_convert_spec.rb +5 -0
  53. data/test/omf_common/comm/topic_spec.rb +7 -1
  54. data/test/omf_common/comm/xmpp/communicator_spec.rb +5 -0
  55. data/test/omf_common/comm/xmpp/topic_spec.rb +5 -0
  56. data/test/omf_common/comm_spec.rb +5 -0
  57. data/test/omf_common/command_spec.rb +5 -0
  58. data/test/omf_common/core_ext/string_spec.rb +5 -0
  59. data/test/omf_common/message/xml/message_spec.rb +5 -0
  60. data/test/omf_common/message_spec.rb +8 -3
  61. data/test/test_helper.rb +5 -0
  62. metadata +48 -11
@@ -1,3 +1,8 @@
1
+ # Copyright (c) 2012 National ICT Australia Limited (NICTA).
2
+ # This software may be used and distributed solely under the terms of the MIT license (License).
3
+ # You should find a copy of the License in LICENSE.TXT or at http://opensource.org/licenses/MIT.
4
+ # By downloading or using this software you accept the terms and the liability disclaimer in the License.
5
+
1
6
  #
2
7
  DESCR = %{
3
8
  Broadcast a file to a topic group
data/bin/file_receiver.rb CHANGED
@@ -1,3 +1,8 @@
1
+ # Copyright (c) 2012 National ICT Australia Limited (NICTA).
2
+ # This software may be used and distributed solely under the terms of the MIT license (License).
3
+ # You should find a copy of the License in LICENSE.TXT or at http://opensource.org/licenses/MIT.
4
+ # By downloading or using this software you accept the terms and the liability disclaimer in the License.
5
+
1
6
  #
2
7
  DESCR = %{
3
8
  Receive a file sent to a topic group
data/bin/omf_keygen CHANGED
@@ -4,7 +4,7 @@ require 'omf_common'
4
4
 
5
5
  OmfCommon::Auth::CertificateStore.init
6
6
 
7
- root = OmfCommon::Auth::Certificate.create(nil, 'omf_ca', 'ca', 'omf')
7
+ root = OmfCommon::Auth::Certificate.create(nil, 'omf_ca', 'ca', 'omf', nil, Time.now, 3600 * 365 * 10)
8
8
 
9
9
  ec_key = OpenSSL::PKey::RSA.new(2048)
10
10
  rc_key = OpenSSL::PKey::RSA.new(2048)
@@ -12,8 +12,8 @@ rc_key = OpenSSL::PKey::RSA.new(2048)
12
12
  ec_pub = ec_key.public_key
13
13
  rc_pub = rc_key.public_key
14
14
 
15
- ec = root.create_for('ec', 'ec', 'controller', 'omf', 365, ec_pub)
16
- rc = root.create_for('rc', 'rc', 'controller', 'omf', 365, rc_pub)
15
+ ec = root.create_for('my_ec', 'ec', 'controller', 'omf', 3600 * 365 * 10, ec_pub)
16
+ rc = root.create_for('my_rc', 'rc', 'controller', 'omf', 3600 * 365 * 10, rc_pub)
17
17
 
18
18
  %w(root ec_key rc_key ec_pub rc_pub ec rc).each do |n|
19
19
  File.write("#{n}.pem", eval(n).to_pem)
@@ -0,0 +1,114 @@
1
+ #!/usr/bin/env ruby
2
+ BIN_DIR = File.dirname(File.symlink?(__FILE__) ? File.readlink(__FILE__) : __FILE__)
3
+ TOP_DIR = File.join(BIN_DIR, '..')
4
+ $: << File.join(TOP_DIR, 'lib')
5
+
6
+ DESCR = %{
7
+ Send a configure message to a specific resource (topic) and print out any replies.
8
+
9
+ Any additional command line arguments are interpreted as property:value and are
10
+ sent with the create message.
11
+ }
12
+
13
+ require 'omf_common'
14
+
15
+ OP_MODE = :development
16
+ $debug = false
17
+ $timeout = 10
18
+
19
+
20
+ opts = {
21
+ communication: {
22
+ #url: 'xmpp://srv.mytestbed.net'
23
+ },
24
+ eventloop: { type: :em},
25
+ logging: {
26
+ level: 'info'
27
+ }
28
+ }
29
+
30
+ resource_url = nil
31
+ resource_properties = {}
32
+
33
+ op = OptionParser.new
34
+ op.banner = "Usage: #{op.program_name} [options] property1:value1 property2:value2 ...\n#{DESCR}\n"
35
+ op.on '-r', '--resource-url URL', "URL of resource (e.g. xmpp://my.server.com/topic1)" do |url|
36
+ resource_url = url
37
+ end
38
+ op.on '-y', '--yaml YAML_FILE', "Read type and property from YAML file" do |yfile|
39
+ require 'yaml'
40
+ y = YAML.load_file(yfile)
41
+ unless o = y['configure']
42
+ puts "Expected top level 'configure', but found '#{y.keys.inspect}'"
43
+ abort
44
+ end
45
+ resource_properties = o['properties']
46
+ end
47
+ op.on '-d', '--debug', "Set log level to DEBUG" do
48
+ opts[:logging][:level] = 'debug'
49
+ $debug = true
50
+ end
51
+ op.on_tail('-h', "--help", "Show this message") { $stderr.puts op; exit }
52
+ rest = op.parse(ARGV) || []
53
+
54
+ unless resource_url || resource_type
55
+ $stderr.puts 'Missing --resource-url --type or'
56
+ $stderr.puts op
57
+ exit(-1)
58
+ end
59
+
60
+ r = resource_url.split('/')
61
+ resource = r.pop
62
+ opts[:communication][:url] = r.join('/')
63
+
64
+ key = nil
65
+ def err_exit
66
+ $stderr.puts("Options need to be of the 'key: value' type")
67
+ exit(-1)
68
+ end
69
+ rest.each do |s|
70
+ sa = s.split(':')
71
+ if sa.length == 2
72
+ err_exit if key
73
+ resource_properties[sa[0]] = sa[1]
74
+ else
75
+ if s.end_with?(':')
76
+ err_exit if key
77
+ key = s[0]
78
+ else
79
+ err_exit unless key
80
+ resource_properties[key] = s[0]
81
+ key = nil
82
+ end
83
+ end
84
+ end
85
+ err_exit if key
86
+
87
+ def print_prop(name, value, level = 0)
88
+ print " #{' ' * level}#{name}:"
89
+ if value.is_a? Hash
90
+ puts ''
91
+ value.each {|k, v| print_prop(k, v, level + 1)}
92
+ else
93
+ puts " #{value}"
94
+ end
95
+ end
96
+
97
+ OmfCommon.init(OP_MODE, opts) do |el|
98
+ OmfCommon.comm.on_connected do |comm|
99
+ comm.subscribe(resource) do |topic|
100
+ topic.configure(resource_properties) do |msg|
101
+ if (src_topic = msg.src.id) == topic.id
102
+ puts "#{topic.id}"
103
+ else
104
+ puts "#{src_topic} via #{topic.id}"
105
+ end
106
+ msg.each_property do |name, value|
107
+ print_prop(name, value)
108
+ end
109
+ puts "-----------------"
110
+ end
111
+ el.after($timeout) { el.stop } if $timeout > 0
112
+ end
113
+ end
114
+ end
data/bin/omf_send_request CHANGED
@@ -13,6 +13,7 @@ to those, otherwise all properties are requested.
13
13
  require 'omf_common'
14
14
 
15
15
  OP_MODE = :development
16
+ $timeout = 10
16
17
 
17
18
  opts = {
18
19
  communication: {
@@ -47,17 +48,31 @@ r = resource_url.split('/')
47
48
  resource = r.pop
48
49
  opts[:communication][:url] = r.join('/')
49
50
 
51
+ def print_prop(name, value, level = 0)
52
+ print " #{' ' * level}#{name}:"
53
+ if value.is_a? Hash
54
+ puts ''
55
+ value.each {|k, v| print_prop(k, v, level + 1)}
56
+ else
57
+ puts " #{value}"
58
+ end
59
+ end
60
+
50
61
  OmfCommon.init(OP_MODE, opts) do |el|
51
62
  OmfCommon.comm.on_connected do |comm|
52
63
  comm.subscribe(resource) do |topic|
53
64
  topic.request(req_properties) do |msg|
54
- dm = (opts[:logging][:level] == 'debug') ? " #{msg.inspect}" : ""
55
- puts "#{resource} <#{msg.type}(#{msg.itype})>#{dm}"
65
+ if (src_topic = msg.src.id) == topic.id
66
+ puts "#{topic.id}"
67
+ else
68
+ puts "#{src_topic} via #{topic.id}"
69
+ end
56
70
  msg.each_property do |name, value|
57
- puts " #{name}: #{value}"
71
+ print_prop(name, value)
58
72
  end
59
- puts "------"
73
+ puts "-----------------"
60
74
  end
75
+ el.after($timeout) { el.stop } if $timeout > 0
61
76
  end
62
77
  end
63
78
  end
@@ -1,15 +1,20 @@
1
+ # Copyright (c) 2012 National ICT Australia Limited (NICTA).
2
+ # This software may be used and distributed solely under the terms of the MIT license (License).
3
+ # You should find a copy of the License in LICENSE.TXT or at http://opensource.org/licenses/MIT.
4
+ # By downloading or using this software you accept the terms and the liability disclaimer in the License.
5
+
1
6
  # OMF_VERSIONS = 6.0
2
7
  require 'omf_common'
3
8
  require 'omf_common/auth/certificate'
4
9
 
5
- root_cert = OmfCommon::Auth::Certificate.create('sa', 'authority')
10
+ #root_cert = OmfCommon::Auth::Certificate.create(nil, 'sa', 'authority')
6
11
  opts = {
7
12
  communication: {
8
13
  auth: {
9
14
  #store: 'amqp://localhost',
10
- certs: [
11
- root_cert.to_pem_compact
12
- ]
15
+ # certs: [
16
+ # root_cert.to_pem_compact
17
+ # ]
13
18
  }
14
19
  }
15
20
  }
@@ -17,7 +22,7 @@ opts = {
17
22
 
18
23
  OmfCommon.init(:local, opts)
19
24
  # Create a certificate for this controller
20
- root_cert.create_for(:controller, :controller, OmfCommon.comm.local_address())
25
+ #root_cert.create_for(:controller, :controller, OmfCommon.comm.local_address())
21
26
 
22
27
 
23
28
  def create_engine(garage)
@@ -111,8 +116,9 @@ OmfCommon.eventloop.run do |el|
111
116
 
112
117
  # Create garage proxy
113
118
  load File.join(File.dirname(__FILE__), '..', '..', 'omf_rc', 'example', 'garage_controller.rb')
114
- garage_cert = root_cert.create_for(:garage1, :garage)
115
- garage_inst = OmfRc::ResourceFactory.create(:garage, uid: :garage_1, certificate: garage_cert)
119
+ #garage_cert = root_cert.create_for(:garage1, :garage)
120
+ #garage_inst = OmfRc::ResourceFactory.create(:garage, uid: :garage_1, certificate: garage_cert)
121
+ garage_inst = OmfRc::ResourceFactory.create(:garage, uid: :garage_1)
116
122
 
117
123
  # Get handle on existing entity
118
124
  comm.subscribe('garage_1') do |garage|
@@ -0,0 +1,69 @@
1
+ # Copyright (c) 2012 National ICT Australia Limited (NICTA).
2
+ # This software may be used and distributed solely under the terms of the MIT license (License).
3
+ # You should find a copy of the License in LICENSE.TXT or at http://opensource.org/licenses/MIT.
4
+ # By downloading or using this software you accept the terms and the liability disclaimer in the License.
5
+
6
+ require 'omf_oml/table'
7
+
8
+ class GarageMonitor < OMF::Common::LObject
9
+ OP_MODE = :development
10
+
11
+ def initialize(opts)
12
+ @observed_topics = {}
13
+
14
+ create_event_table
15
+ Thread.new do
16
+ begin
17
+ sleep 3
18
+ OmfCommon.init(OP_MODE, opts) do |el|
19
+ OmfCommon.comm.on_connected do |comm|
20
+ observe('garage1', comm)
21
+ end
22
+ end
23
+ rescue Exception => ex
24
+ puts "ERROR: #{ex}"
25
+ puts "\t#{ex.backtrace.join("\n\t")}"
26
+ end
27
+ end
28
+
29
+
30
+ end
31
+
32
+ def observe(tname, comm)
33
+ return if (tname.nil? || @observed_topics.key?(tname))
34
+
35
+ info "Observing '#{tname}'"
36
+ @observed_topics[tname] = true
37
+ comm.subscribe(tname) do |topic|
38
+ topic.on_message do |msg|
39
+ ts = Time.now.strftime('%H:%M:%S')
40
+ src = topic.id
41
+ type = "#{msg.type}(#{msg.itype})"
42
+ msg.each_property do |name, value|
43
+ @evt_table.add_row [ts, src, type, name, value]
44
+ end
45
+ if msg.itype == 'released' && src != :garage1
46
+ @engine_table.add_row [ts, src, -1, -1]
47
+ end
48
+
49
+ if (msg[:throttle] && msg[:rpm])
50
+ puts "RPM: #{msg[:rpm]}::#{msg[:rpm].class}"
51
+ @engine_table.add_row [ts, src, msg[:throttle], msg[:rpm]]
52
+ end
53
+ observe(msg[:res_id], comm)
54
+ end
55
+ end
56
+ end
57
+
58
+
59
+
60
+ def create_event_table(fake_events = true)
61
+ evt_schema = [:ts, :address, :type, :name, :value]
62
+ @evt_table = OMF::OML::OmlTable.new 'events', evt_schema, :max_size => 500
63
+ OMF::Web.register_datasource @evt_table
64
+
65
+ engine_schema = [:ts, :source, [:throttle, :int], [:rpm, :int]]
66
+ @engine_table = OMF::OML::OmlTable.new 'engine', engine_schema, :max_size => 20
67
+ OMF::Web.register_datasource @engine_table.indexed_by(:source), name: 'engine'
68
+ end
69
+ end
@@ -0,0 +1,52 @@
1
+ # Copyright (c) 2012 National ICT Australia Limited (NICTA).
2
+ # This software may be used and distributed solely under the terms of the MIT license (License).
3
+ # You should find a copy of the License in LICENSE.TXT or at http://opensource.org/licenses/MIT.
4
+ # By downloading or using this software you accept the terms and the liability disclaimer in the License.
5
+
6
+
7
+ require 'omf_common'
8
+ require 'omf_common/lobject'
9
+ # require 'yaml'
10
+ # require 'log4r'
11
+
12
+ OMF::Common::Loggable.init_log 'garage_monitor', searchPath: File.dirname(__FILE__)
13
+
14
+
15
+ omf_opts = {
16
+ communication: {
17
+ url: 'amqp://0.0.0.0',
18
+ auth: {}
19
+
20
+ },
21
+ eventloop: { type: :em},
22
+ logging: {
23
+ level: 'info'
24
+ }
25
+ }
26
+
27
+ # Configure the web server
28
+ #
29
+ opts = {
30
+ app_name: 'garage_monitor',
31
+ page_title: 'Garage',
32
+ layout: "#{File.dirname(__FILE__)}/layout.yaml",
33
+ footer_right: 'git:mytestbed/omf',
34
+ static_dirs_pre: ["#{File.dirname(__FILE__)}/htdocs"],
35
+ handlers: {
36
+ # delay connecting to databases to AFTER we may run as daemon
37
+ pre_rackup: lambda do
38
+ load("#{File.dirname(__FILE__)}/garage_monitor.rb")
39
+ GarageMonitor.new(omf_opts)
40
+ end,
41
+ pre_parse: lambda do |p|
42
+ p.separator ""
43
+ p.separator "GARAGE options:"
44
+ p.on '--comms-url URL', "URL to communication layer [#{omf_opts[:communication][:url]}]" do |url|
45
+ omf_opts[:communication][:url] = url
46
+ end
47
+ p.separator ""
48
+ end
49
+ }
50
+ }
51
+ require 'omf_web'
52
+ OMF::Web.start(opts)
Binary file
@@ -0,0 +1,2 @@
1
+
2
+ <img src="/resource/image/garage.png">
@@ -0,0 +1,44 @@
1
+
2
+
3
+ #
4
+ #
5
+
6
+ widget:
7
+ id: overview
8
+ name: Overview
9
+ top_level: true
10
+ priority: 900
11
+ type: layout/two_columns/50_50
12
+ chrome: false # don't show title
13
+ widgets:
14
+ left:
15
+ - type: moustache
16
+ text: '<img src="/resource/image/garage.png">'
17
+ context:
18
+
19
+ - type: data/gauge
20
+ title: Engine
21
+ data_source:
22
+ name: engine
23
+ dynamic: 1 # check once a second
24
+ mapping:
25
+ id: source
26
+ value: rpm
27
+ right:
28
+ - type: data/table2
29
+ title: Events
30
+ data_source:
31
+ name: events
32
+ dynamic: 1 # check once a second
33
+ # topts:
34
+ # enableCellNavigation: true
35
+ margin:
36
+ left: 5
37
+
38
+ - type: data/table2
39
+ title: Engine
40
+ data_source:
41
+ name: engine
42
+ dynamic: 1 # check once a second
43
+ margin:
44
+ left: 5
data/example/vm_alt.rb CHANGED
@@ -1,3 +1,8 @@
1
+ # Copyright (c) 2012 National ICT Australia Limited (NICTA).
2
+ # This software may be used and distributed solely under the terms of the MIT license (License).
3
+ # You should find a copy of the License in LICENSE.TXT or at http://opensource.org/licenses/MIT.
4
+ # By downloading or using this software you accept the terms and the liability disclaimer in the License.
5
+
1
6
 
2
7
  # Communication setup
3
8
  Comm.init(:xmpp)
data/lib/omf_common.rb CHANGED
@@ -1,3 +1,8 @@
1
+ # Copyright (c) 2012 National ICT Australia Limited (NICTA).
2
+ # This software may be used and distributed solely under the terms of the MIT license (License).
3
+ # You should find a copy of the License in LICENSE.TXT or at http://opensource.org/licenses/MIT.
4
+ # By downloading or using this software you accept the terms and the liability disclaimer in the License.
5
+
1
6
  require 'active_support/core_ext'
2
7
 
3
8
  require 'omf_common/default_logging'
@@ -12,6 +17,10 @@ require 'omf_common/eventloop'
12
17
 
13
18
  include OmfCommon::DefaultLogging
14
19
 
20
+ # Set the default encoding to UTF8
21
+ Encoding.default_external = Encoding::UTF_8
22
+ Encoding.default_internal = Encoding::UTF_8
23
+
15
24
  module OmfCommon
16
25
  DEFAULTS = {
17
26
  development: {
@@ -19,7 +28,7 @@ module OmfCommon
19
28
  type: 'em'
20
29
  },
21
30
  logging: {
22
- level: {
31
+ level: {
23
32
  default: 'debug'
24
33
  },
25
34
  appenders: {
@@ -36,7 +45,7 @@ module OmfCommon
36
45
  type: :em
37
46
  },
38
47
  logging: {
39
- level: {
48
+ level: {
40
49
  default: 'info'
41
50
  },
42
51
  appenders: {
@@ -62,7 +71,7 @@ module OmfCommon
62
71
  type: :em
63
72
  },
64
73
  logging: {
65
- level: {
74
+ level: {
66
75
  default: 'info'
67
76
  },
68
77
  appenders: {
@@ -82,7 +91,7 @@ module OmfCommon
82
91
  },
83
92
  eventloop: { type: :local},
84
93
  logging: {
85
- level: {
94
+ level: {
86
95
  default: 'debug'
87
96
  },
88
97
  appenders: {
@@ -106,7 +115,7 @@ module OmfCommon
106
115
  type: :em
107
116
  },
108
117
  logging: {
109
- level: {
118
+ level: {
110
119
  default: 'debug'
111
120
  },
112
121
  appenders: {
@@ -188,9 +197,9 @@ module OmfCommon
188
197
  # :remove_root ROOT_NAME: Remove the root node. Throw exception if not ROOT_NAME
189
198
  # :wait_for_readable SECS: Wait until the yaml file becomes readable. Check every SECS
190
199
  # :erb_process flag: Run the content of the loaded file through ERB first before YAML parsing
191
- # :erb_safe_level level: If safe_level is set to a non-nil value, ERB code will be run in a
200
+ # :erb_safe_level level: If safe_level is set to a non-nil value, ERB code will be run in a
192
201
  # separate thread with $SAFE set to the provided level.
193
- # :erb_binding binding: Optional binding given to ERB#result
202
+ # :erb_binding binding: Optional binding given to ERB#result
194
203
  #
195
204
  def self.load_yaml(file_name, opts = {})
196
205
  if path_opt = opts[:path]
@@ -262,7 +271,7 @@ module OmfCommon
262
271
  if name.to_s == 'default'
263
272
  logger.level = lvl.to_sym
264
273
  else
265
- Logging.logger[name.to_s].level = lvl.to_sym
274
+ Logging.logger[name.to_s].level = lvl.to_sym
266
275
  end
267
276
  end
268
277
  else