Class: OmfRc::Runner

Inherits:
Object
  • Object
show all
Includes:
Hashie
Defined in:
omf_rc/lib/omf_rc/runner.rb

Overview

Class to start an OMF RC. The configuration parameters can be set in decreasing priority through the command line, configuration file and default settings (see @def_opts).

Having said that, there is one exception and that relates to the 'oml' configuration which is stripped out first and handed to the OML4R library during command line parsing.

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Runner) initialize

Returns a new instance of Runner



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'omf_rc/lib/omf_rc/runner.rb', line 21

def initialize()
  @executable_name = File.basename($PROGRAM_NAME)
  @oml_enabled = false
  @instrument = false
  @gem_version = OmfCommon.version_of('omf_common')

  @node_id = Socket.gethostname

  @def_opts = Mash.new(
    environment: 'production',
    resources: [ { type: :node, uid: @node_id }],
    factories: {},
    communication: { url: "amqp://localhost" },
    logging: {},
    add_default_factories: true,
  )

  @gopts = Mash.new
  @opts = Mash.new

  @omlopts = {appName: @executable_name}
end

Instance Attribute Details

- (Object) copts

Returns the value of attribute copts



18
19
20
# File 'omf_rc/lib/omf_rc/runner.rb', line 18

def copts
  @copts
end

- (Object) def_opts

Returns the value of attribute def_opts



18
19
20
# File 'omf_rc/lib/omf_rc/runner.rb', line 18

def def_opts
  @def_opts
end

- (Object) gopts

Returns the value of attribute gopts



18
19
20
# File 'omf_rc/lib/omf_rc/runner.rb', line 18

def gopts
  @gopts
end

- (Object) opts

Returns the value of attribute opts



19
20
21
# File 'omf_rc/lib/omf_rc/runner.rb', line 19

def opts
  @opts
end

Instance Method Details

- (Object) oml_init



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'omf_rc/lib/omf_rc/runner.rb', line 151

def oml_init
  begin
    @omlopts[:afterParse] = lambda {|o| parse_config_files() }
    @oml_enabled = OML4R::init(ARGV, @omlopts) do |op|
      op.banner = "OMF Resource Controller version '#{@gem_version}'\n"
      op.banner += "Usage: #{@executable_name} [options]"

      op.on("-c CONFIGFILE", "Configuration File") do |file|
        @gopts[:config_file] = file
      end

      op.on("--log_config CONFIGFILE", "Logging Configuration File") do |file|
        @gopts[:logging_configfile] = file
      end

      op.on("-e ENVIRONMENT", "Environment (development, production ...) [#{@def_opts[:environment]}]") do |e|
        @gopts[:environment] = e
      end

      op.on("-i", "--instrument", "Turn on self instrumentation, OML parameters must be set!") do
        @instrument = true
      end

      op.on("-v", "--version", "Show version") do
        puts "OMF Resource Controller version '#{@gem_version}'"
        exit
      end

      op.on("-h", "--help", "Show this message") do
        puts op
        exit
      end
    end
  rescue OML4R::MissingArgumentException => e
    puts "Warning: #{e.message} to instrument this RC, so it will run without instrumentation. (see --oml-help)"
  rescue => e
    puts e.message
    #puts e.backtrace
    exit(1)
  end
end

- (Object) parse_config_files



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'omf_rc/lib/omf_rc/runner.rb', line 118

def parse_config_files()
  config_file = @gopts.delete(:config_file)

  if config_file.nil?
    puts "You must specify a config file"
    exit(1)
  else
    cfg_opts = Mash.new(OmfCommon.load_yaml(config_file, symbolize_keys: true, erb_process: true))

    @opts.merge!(@def_opts.merge(cfg_opts))

    # Legacy support uri & uid opt could also configure comm & resource
    cfg_opts.each do |k, v|
      case k.to_sym
      when :uri
        @opts[:communication][:url] = v
      when :uid
        @opts[:resources][0][:type] = :node
        @opts[:resources][0][:uid] = v
      when :debug
        @opts[:logging][:level] = 'debug' if v
      else
        @opts[k] = v
      end
    end

    @opts.merge!(@gopts)
    # Merge in place as OML may hold @omlopts
    in_place_oml_opts = (@opts[:oml] || @opts[:instrumentation] || Mash.new).to_hash(symbolize_keys: true)
    @omlopts.merge!(in_place_oml_opts)
  end
end

- (Object) run



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'omf_rc/lib/omf_rc/runner.rb', line 44

def run()
  oml_init() # calls parse_config_files()

  OmfCommon::Measure.enable if @oml_enabled && @instrument

  OmfCommon.init(@opts[:environment], @opts.to_hash) do |el|
    # Load a customised logging set up if provided
    OmfCommon.load_logging_config(@opts[:logging_configfile])

    info "Starting OMF Resource Controller version '#{@gem_version}'"

    Signal.trap("SIGINT") do
      # TODO: Should release resources first

      # Workaround to EM issue under ruby v2
      # https://github.com/eventmachine/eventmachine/issues/418
      el.after(0) do
        info "Stopping ..."
        OmfCommon.comm.disconnect
        el.stop
      end
    end

    # Load extensions
    if @opts[:add_default_factories] != false
      OmfRc::ResourceFactory.load_default_resource_proxies
    end
    if f = @opts[:factories]
      if (loads = f[:load])
        loads.each do |m|
          begin
            info "Try to load proxy module '#{m}'"
            require(m)
          rescue LoadError => e
            error e.message
          end
        end
      end
      if (inits = f[:defaults])
        pl = OmfRc::ResourceFactory.proxy_list
        inits.each do |m, v|
          unless p = pl[m]
            error "Can't find proxy '#{m}' to set defaults for"
          end
          info "Setting proxy defaults for '#{m}'"
          debug "Setting proxy defaults for '#{m}' to '#{v}'"
          OmfRc::ResourceProxy::AbstractResource.set_defaults(m, v)
        end
      end
    end

    OmfCommon.comm.on_connected do |comm|
      info "Connected using #{comm.conn_info}"

      rc_cert = OmfCommon.load_credentials(@opts[:credentials])

      @opts[:resources].each do |res_opts|
        rtype = res_opts.delete(:type)
        res_creation_opts = res_opts.delete(:creation_opts)
        res_creation_opts ||= res_opts.delete(:create_opts)
        res_creation_opts ||= {}
        res_opts[:certificate] = rc_cert
        begin
          OmfRc::ResourceFactory.create(rtype, res_opts, res_creation_opts)
        rescue => e
          error "#{e.message}\n#{e.backtrace.join("\n")}"
        end
      end

    end
  end
  info "Stopping OMF Resource Controller version '#{@gem_version}'"
end