omf_rc 6.0.0.pre.8 → 6.0.0.pre.9

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.
Files changed (43) hide show
  1. data/bin/install_omf_rc +79 -0
  2. data/bin/omf_rc +52 -36
  3. data/config/config.yml +8 -0
  4. data/init/debian +51 -0
  5. data/init/fedora +54 -0
  6. data/init/run_omf_rc.sh +62 -0
  7. data/init/ubuntu +12 -0
  8. data/lib/omf_rc/omf_error.rb +5 -5
  9. data/lib/omf_rc/resource_factory.rb +7 -11
  10. data/lib/omf_rc/resource_proxy/abstract_resource.rb +327 -228
  11. data/lib/omf_rc/resource_proxy/application.rb +61 -56
  12. data/lib/omf_rc/resource_proxy/net.rb +2 -2
  13. data/lib/omf_rc/resource_proxy/node.rb +11 -2
  14. data/lib/omf_rc/resource_proxy/virtual_machine.rb +1 -1
  15. data/lib/omf_rc/resource_proxy/wlan.rb +2 -0
  16. data/lib/omf_rc/resource_proxy_dsl.rb +22 -1
  17. data/lib/omf_rc/util/common_tools.rb +2 -4
  18. data/lib/omf_rc/util/hostapd.rb +4 -3
  19. data/lib/omf_rc/util/ip.rb +8 -5
  20. data/lib/omf_rc/util/iw.rb +18 -8
  21. data/lib/omf_rc/util/sysfs.rb +14 -0
  22. data/lib/omf_rc/util/vmbuilder.rb +1 -1
  23. data/lib/omf_rc/util/wpa.rb +4 -3
  24. data/lib/omf_rc/version.rb +1 -1
  25. data/lib/omf_rc.rb +3 -1
  26. data/omf_rc.gemspec +4 -2
  27. data/test/omf_rc/message_process_error_spec.rb +3 -3
  28. data/test/omf_rc/resource_factory_spec.rb +14 -7
  29. data/test/omf_rc/resource_proxy/abstract_resource_spec.rb +47 -21
  30. data/test/omf_rc/resource_proxy/application_spec.rb +156 -119
  31. data/test/omf_rc/resource_proxy/mock_spec.rb +6 -1
  32. data/test/omf_rc/resource_proxy/node_spec.rb +32 -12
  33. data/test/omf_rc/resource_proxy_dsl_spec.rb +31 -19
  34. data/test/omf_rc/util/common_tools_spec.rb +8 -11
  35. data/test/omf_rc/util/ip_spec.rb +7 -1
  36. data/test/omf_rc/util/iw_spec.rb +18 -13
  37. data/test/omf_rc/util/mock_spec.rb +6 -1
  38. data/test/omf_rc/util/mod_spec.rb +17 -10
  39. data/test/test_helper.rb +3 -0
  40. metadata +51 -48
  41. data/config/omf_rc.yml +0 -70
  42. data/lib/omf_rc/resource_proxy/openflow_slice.rb +0 -79
  43. data/lib/omf_rc/resource_proxy/openflow_slice_factory.rb +0 -71
@@ -0,0 +1,79 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ abort "Please use Ruby 1.9.3 or higher" if RUBY_VERSION < "1.9.3"
4
+
5
+ require 'optparse'
6
+ require 'fileutils'
7
+
8
+ if Process.uid!=0
9
+ abort "You have to be root to install the OMF RC startup script and config file.
10
+ You also need to have the omf_rc gem installed as root and have RVM installed as root (if you don't use system ruby)."
11
+ end
12
+
13
+ options = {}
14
+ optparse = OptionParser.new do |opts|
15
+ opts.banner = "Usage: #{File.basename($PROGRAM_NAME)} [options]"
16
+ opts.on("-i", "--initscript", "Install init script to run the OMF RC on boot (as root)") do |i|
17
+ options[:init] = i
18
+ end
19
+ opts.on("-c", "--configfile", "Install config file template in /etc/omf_rc/config.yml") do |c|
20
+ options[:config] = c
21
+ end
22
+ opts.on_tail("-h", "--help", "Show this message") do
23
+ puts opts
24
+ exit
25
+ end
26
+ end
27
+
28
+ optparse.parse!
29
+ if options.empty?
30
+ puts optparse
31
+ exit
32
+ end
33
+
34
+ spec = Gem::Specification.find_by_name("omf_rc")
35
+ gem_root = spec.gem_dir
36
+
37
+ if options[:config]
38
+ puts "Copying configuration file..."
39
+ FileUtils.mkdir_p "/etc/omf_rc"
40
+ FileUtils.cp "#{gem_root}/config/config.yml", "/etc/omf_rc/config.yml"
41
+ FileUtils.chmod 0644, "/etc/omf_rc/config.yml"
42
+ puts "done."
43
+ end
44
+
45
+ if options[:init]
46
+ puts "Detecting operating system..."
47
+
48
+ if !File.exist?("/etc/issue")
49
+ abort "Unknown or unsupported operating system. Cannot install init script."
50
+ end
51
+
52
+ os=`awk 'NR==1 {print $1}' /etc/issue`.chomp
53
+
54
+ if os.nil?
55
+ abort "Unknown or unsupported operating system. Cannot install init script."
56
+ end
57
+
58
+ puts "'#{os}' found"
59
+
60
+ FileUtils.cp "#{gem_root}/init/run_omf_rc.sh", "/usr/local/bin/run_omf_rc.sh"
61
+
62
+ case os
63
+ when "Ubuntu"
64
+ FileUtils.cp "#{gem_root}/init/ubuntu", "/etc/init/omf_rc.conf"
65
+ puts "Upstart script for Ubuntu has been installed. Execute 'start omf_rc' to run the daemon."
66
+ when "Debian"
67
+ FileUtils.cp "#{gem_root}/init/debian", "/etc/init.d/omf_rc"
68
+ `update-rc.d omf_rc defaults`
69
+ puts "Init script for Debian has been installed. Execute '/etc/init.d/omf_rc start' to run the daemon."
70
+ when "Fedora"
71
+ FileUtils.cp "#{gem_root}/init/fedora", "/etc/init.d/omf_rc"
72
+ `chkconfig --add omf_rc`
73
+ # older Fedora's don't use systemd
74
+ `systemctl --system daemon-reload` if File.exist?("/bin/systemctl")
75
+ puts "Init script for Fedora has been installed. Execute '/etc/init.d/omf_rc start' to run the daemon."
76
+ else
77
+ abort "OS '#{os}' is not supported. Cannot install init script."
78
+ end
79
+ end
data/bin/omf_rc CHANGED
@@ -1,43 +1,42 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require "optparse"
3
+ abort "Please use Ruby 1.9.3 or higher" if RUBY_VERSION < "1.9.3"
4
+
5
+ require 'optparse'
6
+ require 'erb'
7
+ require 'socket'
8
+
4
9
  require 'omf_rc'
5
10
  require 'omf_rc/resource_factory'
6
- $stdout.sync = true
7
-
8
- options = {
9
- uid: `hostname`.chomp
10
- }
11
11
 
12
+ $stdout.sync = true
13
+ options = {}
12
14
  executable_name = File.basename($PROGRAM_NAME)
13
15
  oml_enabled = false
16
+ gem_version = Gem::Specification.find_by_name('omf_rc').version.to_s
14
17
 
15
18
  begin
16
19
  oml_enabled = OML4R::init(ARGV, :appName => executable_name) do |opts|
17
20
  opts.banner = "usage: #{executable_name} [options]"
18
21
 
19
- opts.on("-u USER", "Username") do |user|
20
- options[:user] = user
22
+ opts.on("-c CONFIGFILE", "Configuration File") do |file|
23
+ options[:configfile] = file
21
24
  end
22
25
 
23
- opts.on("-p PASSWORD", "Password") do |password|
24
- options[:password] = password
26
+ opts.on("-a ADVANCED_CONFIGFILE", "Advanced Configuration File") do |file|
27
+ options[:advanced_configfile] = file
25
28
  end
26
29
 
27
- opts.on("-s SERVER", "PubSub server") do |server|
28
- options[:server] = server
30
+ opts.on("-u URI", "Communication URI (xmpp://user:password@domain)") do |uri|
31
+ options[:uri] = uri
29
32
  end
30
33
 
31
- opts.on("-t TOPIC", "PubSub topic to create, also becomes the uid of the resource, default to hostname") do |topic|
32
- options[:uid] = topic
34
+ opts.on("-e ENVIRONMENT", "Environment (development, production ...)") do |environment|
35
+ options[:environment] = environment
33
36
  end
34
37
 
35
- opts.on("-d", "--debug", "Debug mode") do
36
- options[:debug] = true
37
- end
38
-
39
- opts.on("-l LOG_FILE_DIR", "Write log file to this folder") do |file_dir|
40
- options[:log_file_dir] = file_dir
38
+ opts.on("-i UID", "UID of the resource, also becomes the pubsub topic of the resource, default to hostname") do |uid|
39
+ options[:uid] = uid
41
40
  end
42
41
  end
43
42
  rescue => e
@@ -54,30 +53,47 @@ rescue => e
54
53
  end
55
54
  end
56
55
 
56
+ if !options[:configfile].nil?
57
+ cfg_options = YAML.load(ERB.new(File.read(options[:configfile])).result)
58
+ options = cfg_options.merge(options)
59
+ end
60
+
61
+ options[:uid] ||=`hostname`.chomp
62
+
57
63
  OmfCommon::Measure.enable if oml_enabled
58
64
 
59
- unless options[:server] && options[:user] && options[:password]
60
- puts "Error: Missing parameters to connect to a PubSub Server (see --help)"
61
- exit(1)
65
+ options[:environment] ||= :development
66
+
67
+ if options[:uri]
68
+ common_options = { communication: { url: options[:uri] } }
69
+ else
70
+ common_options = {}
62
71
  end
63
72
 
64
- Logging.logger.root.level = :debug if options[:debug]
65
- Blather.logger = logger
73
+ if !options[:advanced_configfile].nil?
74
+ a_cfg_options = (YAML.load_file(options[:advanced_configfile]))
75
+ common_options = a_cfg_options.merge(common_options)
76
+ end
66
77
 
67
- if options[:log_file_dir] && File.exist?(options[:log_file_dir])
68
- Logging.logger.root.add_appenders(
69
- Logging.appenders.file(
70
- "#{options[:log_file_dir]}/omf_rc.log",
71
- :layout => Logging.layouts.pattern(:date_pattern => '%F %T %z',
72
- :pattern => '[%d] %-5l %c: %m\n')))
78
+ unless common_options[:communication] && common_options[:communication][:url]
79
+ puts "Error: Missing parameters to connect to a PubSub Server (see --help)"
80
+ exit(1)
73
81
  end
74
82
 
83
+ resource_options = {
84
+ uid: options[:uid]
85
+ }
86
+
75
87
  OmfRc::ResourceFactory.load_default_resource_proxies
76
88
 
77
- EM.run do
78
- node = OmfRc::ResourceFactory.new(:node, options)
79
- node.connect
89
+ OmfCommon.init(options[:environment].to_sym, common_options) do |el|
90
+ info "Starting OMF Resource Controller version '#{gem_version}'"
80
91
 
81
- trap(:INT) { node.disconnect }
82
- trap(:TERM) { node.disconnect }
92
+ OmfCommon.comm.on_connected do |comm|
93
+ info "Connected as #{comm.jid}" if comm.jid
94
+ res = OmfRc::ResourceFactory.create(:node, resource_options)
95
+
96
+ comm.on_interrupted { res.disconnect }
97
+ end
83
98
  end
99
+ info "Stopping OMF Resource Controller version '#{gem_version}'"
data/config/config.yml ADDED
@@ -0,0 +1,8 @@
1
+ ---
2
+ # default topic name is this machine's hostname
3
+ # default xmpp user name/password is "hostname-pid"
4
+ # this is to ensure that every RC has its own topic and XMPP account
5
+ :uid: <%= Socket.gethostname %>
6
+ :uri: xmpp://<%= "#{Socket.gethostname}-#{Process.pid}" %>:<%= "#{Socket.gethostname}-#{Process.pid}" %>@localhost
7
+ :environment: production
8
+ :debug: false
data/init/debian ADDED
@@ -0,0 +1,51 @@
1
+ #! /bin/sh
2
+ ### BEGIN INIT INFO
3
+ # Provides: omf-rc
4
+ # Required-Start: $remote_fs $syslog
5
+ # Required-Stop: $remote_fs $syslog
6
+ # Default-Start: 2 3 4 5
7
+ # Default-Stop: 0 1 6
8
+ # Short-Description: Start daemon at boot time
9
+ # Description: Enable service provided by daemon.
10
+ ### END INIT INFO
11
+
12
+ DAEMON=/usr/local/bin/run_omf_rc.sh
13
+ [ -x "$DAEMON" ] || exit 0
14
+ NAME=omf_rc
15
+ PIDFILE=/var/run/$NAME.pid
16
+ . /lib/init/vars.sh
17
+ . /lib/lsb/init-functions
18
+
19
+ start(){
20
+ echo -n "Starting OMF Resource Controller: $NAME"
21
+ start-stop-daemon --start --quiet --background --pidfile $PIDFILE --make-pidfile --exec $DAEMON
22
+ echo "."
23
+ }
24
+
25
+ stop(){
26
+ echo -n "Stopping OMF Resource Controller: $NAME"
27
+ start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE
28
+ echo "."
29
+ }
30
+
31
+ case "$1" in
32
+ start)
33
+ start
34
+ ;;
35
+ stop)
36
+ stop
37
+ ;;
38
+ restart|force-reload)
39
+ stop
40
+ start
41
+ ;;
42
+ status)
43
+ status_of_proc -p $PIDFILE "$DAEMON" "$NAME" && exit 0 || exit $?
44
+ ;;
45
+ *)
46
+ echo "Usage: $0 {start|stop|restart|force-reload|status}" >&2
47
+ exit 3
48
+ ;;
49
+ esac
50
+
51
+ :
data/init/fedora ADDED
@@ -0,0 +1,54 @@
1
+ #! /bin/sh
2
+ ### BEGIN INIT INFO
3
+ # Provides: omf-rc
4
+ # Required-Start: $remote_fs $syslog
5
+ # Required-Stop: $remote_fs $syslog
6
+ # Default-Start: 2 3 4 5
7
+ # Default-Stop: 0 1 6
8
+ # Short-Description: Start daemon at boot time
9
+ # Description: Enable service provided by daemon.
10
+ ### END INIT INFO
11
+
12
+ DAEMON=/usr/local/bin/run_omf_rc.sh
13
+ [ -x "$DAEMON" ] || exit 0
14
+ NAME=omf_rc
15
+ PIDFILE=/var/run/$NAME.pid
16
+ . /etc/rc.d/init.d/functions
17
+
18
+ start(){
19
+ echo -n "Starting OMF Resource Controller: $NAME"
20
+ $DAEMON >& /var/log/$NAME.daemon.log &
21
+ echo $! > $PIDFILE
22
+ echo "."
23
+ }
24
+
25
+ stop(){
26
+ echo -n "Stopping OMF Resource Controller: $NAME"
27
+ if [ -f $PIDFILE ]; then
28
+ kill `cat $PIDFILE`
29
+ rm $PIDFILE
30
+ sleep 2
31
+ else
32
+ echo -e "\nNo pidfile found. Is $NAME running?"
33
+ fi
34
+ echo "."
35
+ }
36
+
37
+ case "$1" in
38
+ start)
39
+ start
40
+ ;;
41
+ stop)
42
+ stop
43
+ ;;
44
+ restart|force-reload)
45
+ stop
46
+ start
47
+ ;;
48
+ *)
49
+ echo "Usage: $0 {start|stop|restart|force-reload}" >&2
50
+ exit 3
51
+ ;;
52
+ esac
53
+
54
+ :
@@ -0,0 +1,62 @@
1
+ #!/bin/bash
2
+
3
+ # Startup wrapper for the OMF6 RC
4
+ # detects system-wide RVM installations & Ruby from distro packages
5
+ # and runs OMF6 RC
6
+
7
+ # system-wide RVM must be installed using
8
+ # '\curl -L https://get.rvm.io | sudo bash -s stable'
9
+
10
+ die() { echo "ERROR: $@" 1>&2 ; exit 1; }
11
+
12
+ RUBY_VER="ruby-1.9.3-p286"
13
+ RUBY_BIN_SUFFIX=""
14
+
15
+ if [ `id -u` != "0" ]; then
16
+ die "This script is intended to be run as 'root'"
17
+ fi
18
+
19
+ if [ -e /etc/profile.d/rvm.sh ]; then
20
+ # use RVM if installed
21
+ echo "System-wide RVM installation detected"
22
+ source /etc/profile.d/rvm.sh
23
+ if [[ $? != 0 ]] ; then
24
+ die "Failed to initialize RVM environment"
25
+ fi
26
+ rvm use $RUBY_VER@omf > /dev/null
27
+ if [[ $? != 0 ]] ; then
28
+ die "$RUBY_VER with gemset 'omf' is not installed in your RVM"
29
+ fi
30
+ ruby -v | grep 1.9.3 > /dev/null
31
+ if [[ $? != 0 ]] ; then
32
+ die "Could not run Ruby 1.9.3"
33
+ fi
34
+ gem list | grep omf_rc > /dev/null
35
+ if [[ $? != 0 ]] ; then
36
+ die "The omf_rc gem is not installed in the 'omf' gemset"
37
+ fi
38
+ else
39
+ # check for distro ruby when no RVM was found
40
+ echo "No system-wide RVM installation detected"
41
+ ruby -v | grep 1.9.3 > /dev/null
42
+ if [[ $? != 0 ]] ; then
43
+ ruby1.9.3 -v | grep 1.9.3 > /dev/null
44
+ if [[ $? != 0 ]] ; then
45
+ die "Could not run system Ruby 1.9.3. No useable Ruby installation found."
46
+ fi
47
+ RUBY_BIN_SUFFIX="1.9.3"
48
+ fi
49
+ echo "Ruby 1.9.3 found"
50
+ gem$RUBY_BIN_SUFFIX list | grep omf_rc > /dev/null
51
+ if [[ $? != 0 ]] ; then
52
+ die "The omf_rc gem is not installed"
53
+ fi
54
+ fi
55
+
56
+ RC=`which omf_rc`
57
+ if [[ $? != 0 ]] ; then
58
+ die "could not find omf_rc executable"
59
+ fi
60
+
61
+ echo "Running OMF6 RC"
62
+ exec /usr/bin/env ruby$RUBY_BIN_SUFFIX $RC -c /etc/omf_rc/config.yml $@
data/init/ubuntu ADDED
@@ -0,0 +1,12 @@
1
+ description "OMF6 Resource Controller"
2
+ author "NICTA <omf-user@lists.nicta.com.au>"
3
+
4
+ start on (filesystem and net-device-up IFACE=lo)
5
+ stop on runlevel [!2345]
6
+
7
+ env DAEMON=/usr/local/bin/run_omf_rc.sh
8
+
9
+ respawn
10
+ respawn limit 10 5
11
+
12
+ exec $DAEMON
@@ -1,11 +1,11 @@
1
- # Error during message processing, include message related information context_id and inform_to, for publishing errors to pubsub server
1
+ # Error during message processing, include message related information cid and replyto, for publishing errors to pubsub server
2
2
  #
3
3
  class OmfRc::MessageProcessError < StandardError
4
- attr_reader :context_id, :inform_to
4
+ attr_reader :cid, :replyto
5
5
 
6
- def initialize(context_id, inform_to, msg = nil)
7
- @context_id = context_id
8
- @inform_to = inform_to
6
+ def initialize(cid, replyto, msg = nil)
7
+ @cid = cid
8
+ @replyto = replyto
9
9
  super(msg)
10
10
  end
11
11
  end
@@ -9,27 +9,21 @@ class OmfRc::ResourceFactory
9
9
  # List of registered resource proxies
10
10
  @@proxy_list = Hashie::Mash.new
11
11
 
12
- # By default, we use xmpp dsl, which based on blather
13
- DEFAULT_OPTS = {
14
- dsl: 'xmpp'
15
- }
16
-
17
12
  class << self
18
13
  # Factory method to initiate new resource proxy
19
14
  #
20
15
  # @param (see OmfRc::ResourceProxy::AbstractResource#initialize)
21
16
  #
22
17
  # @see OmfRc::ResourceProxy::AbstractResource
23
- def new(type, opts = nil, comm = nil, &block)
18
+ def create(type, opts = {}, creation_opts = {}, &creation_callback)
24
19
  unless @@proxy_list.include?(type)
25
20
  raise ArgumentError, "Resource type not found: #{type.to_s}" unless @@proxy_list.include?(type)
26
21
  end
27
- type = type.to_s
28
- opts = opts ? DEFAULT_OPTS.merge(opts) : DEFAULT_OPTS
29
22
  # Create a new instance of abstract resource
30
- resource = OmfRc::ResourceProxy::AbstractResource.new(type, opts, comm)
23
+ resource = OmfRc::ResourceProxy::AbstractResource.new(type, opts, creation_opts, &creation_callback)
31
24
  # Then extend this instance with relevant module identified by type
32
- resource.extend("OmfRc::ResourceProxy::#{type.camelize}".constantize)
25
+ emodule = @@proxy_list[type].proxy_module || "OmfRc::ResourceProxy::#{type.camelize}".constantize
26
+ resource.extend(emodule)
33
27
  # Initiate property hash
34
28
  resource.methods.each do |m|
35
29
  resource.__send__(m) if m =~ /def_property_(.+)/
@@ -39,6 +33,8 @@ class OmfRc::ResourceFactory
39
33
  resource
40
34
  end
41
35
 
36
+ alias :new :create
37
+
42
38
  # Return the proxy list
43
39
  def proxy_list
44
40
  @@proxy_list
@@ -61,7 +57,7 @@ class OmfRc::ResourceFactory
61
57
  end
62
58
  end
63
59
 
64
- def load_addtional_resource_proxies(folder)
60
+ def load_additional_resource_proxies(folder)
65
61
  Dir["#{folder}/*.rb"].each do |file|
66
62
  require "#{folder}/#{File.basename(file).gsub(/\.rb/, '')}"
67
63
  end