Class: OmfEc::Runner

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRunner

Returns a new instance of Runner.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/omf_ec/runner.rb', line 14

def initialize
  @gem_version = OmfEc::VERSION
  @oml_enabled = false
  @executable_name = File.basename($PROGRAM_NAME)

  @oml_opts = {
    appName: @executable_name,
    afterParse: lambda { |o| parse_cmd_opts }
  }

  # Default configuration options
  @config_opts = Mash.new(
    environment: 'development',
    communication: { url: "amqp://localhost" },
  )

  @cmd_opts = Mash.new

  @argv = ARGV.dup
end

Instance Attribute Details

#oedl_pathObject (readonly)

Returns the value of attribute oedl_path.



12
13
14
# File 'lib/omf_ec/runner.rb', line 12

def oedl_path
  @oedl_path
end

Instance Method Details

#initObject



280
281
282
283
284
# File 'lib/omf_ec/runner.rb', line 280

def init
  oml_init
  setup_experiment
  #load_experiment
end

#load_experimentObject



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/omf_ec/runner.rb', line 232

def load_experiment
  begin
    OmfCommon.init(@config_opts.delete(:environment), @config_opts) do |el|
      setup_logging
      OmfCommon.comm.on_connected do |comm|
        info "OMF Experiment Controller #{OmfEc::VERSION} - Start"
        info "Connected using #{comm.conn_info}"
        info "Execute: #{@oedl_path}"
        info "Properties: #{OmfEc.experiment.cmdline_properties}"

        if @config_opts[:communication][:auth] && @config_opts[:communication][:auth][:authenticate]
          ec_cert = OmfCommon.load_credentials(
            root_cert_dir: @config_opts[:root_cert_dir],
            entity_cert: @config_opts[:cert],
            entity_key: @config_opts[:key]
          )

          ec_cert.resource_id = OmfCommon.comm.local_address
          OmfCommon::Auth::CertificateStore.instance.register(ec_cert)
        end

        OmfEc.experiment.("ec_version", "#{OmfEc::VERSION}")
        OmfEc.experiment.("exp_path", @oedl_path)
        OmfEc.experiment.("ec_pid", "#{Process.pid}")
        OmfEc.experiment.archive_oedl(@oedl_path)

        begin
          load @oedl_path
          OmfEc::Experiment.start
        rescue => e
          OmfEc.experiment.("state", "error")
          error e.message
          error e.backtrace.join("\n")
        end

        trap(:TERM) { OmfEc::Experiment.done }
        trap(:INT) { OmfEc::Experiment.done }
      end
    end
  rescue => e
    logger.fatal e.message
    logger.fatal e.backtrace.join("\n")
    puts "Experiment controller exits unexpectedly"
    puts e
    exit(1)
  end
end

#oml_initObject



35
36
37
38
39
40
41
42
43
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/omf_ec/runner.rb', line 35

def oml_init
  begin
    @oml_enabled = OML4R::init(ARGV, @oml_opts) do |op|
      op.banner = "OMF Experiment Controller version '#{@gem_version}'\n"
      op.banner += "Usage: #{@executable_name} [options] path_to_oedl_file [-- --experiment_property value]"

      op.on("-u", "--uri ADDRESS", "URI for communication layer [amqp://localhost]") do |uri|
        @cmd_opts[:uri] = uri
        remove_cmd_opts_from_argv("-u", "--uri", uri)
      end

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

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

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

      op.on("--root_cert_dir DIRECTORY", "Directory containing root certificates") do |dir|
        @cmd_opts[:root_cert_dir] = dir
        remove_cmd_opts_from_argv("--root_cert_dir", dir)
      end

      op.on("--cert CERTIFICATE", "Your certificate") do |cert|
        @cmd_opts[:cert] = cert
        remove_cmd_opts_from_argv("--cert", cert)
      end

      op.on("--key KEY", "Your private key") do |key|
        @cmd_opts[:key] = key
        remove_cmd_opts_from_argv("--key", key)
      end

      op.on("--name", "--experiment EXPERIMENT_NAME", "Experiment name") do |e_name|
        @cmd_opts[:experiment_name] = e_name
        remove_cmd_opts_from_argv("--name", "--experiment", e_name)
      end

      op.on("--slice SLICE_NAME", "Slice name [Deprecated]") do |slice|
        @cmd_opts[:slice] = slice
        remove_cmd_opts_from_argv("--slice", slice)
      end

      op.on("--oml_uri URI", "URI for the OML data collection of experiment applications") do |uri|
        @cmd_opts[:oml_uri] = uri
        remove_cmd_opts_from_argv("--oml_uri", uri)
      end

      op.on("--inst_oml_uri URI", "EC Instrumentation: OML URI to use") do |uri|
        @cmd_opts[:inst_oml_uri] = uri
        remove_cmd_opts_from_argv("--inst_oml_uri", uri)
      end

      op.on("--inst_oml_id ID", "EC Instrumentation: OML ID to use") do |id|
        @cmd_opts[:inst_oml_id] = id
        remove_cmd_opts_from_argv("--inst_oml_id", id)
      end

      op.on("--inst_oml_domain DOMAIN", "EC Instrumentation: OML Domain to use") do |domain|
        @cmd_opts[:inst_oml_domain] = domain
        remove_cmd_opts_from_argv("--inst_oml_domain", domain)
      end

      op.on("-g", "--show-graph", "Parse graph definition to construct graph information in log output") do
        @cmd_opts['show-graph'] = true
        remove_cmd_opts_from_argv("--show-graph")
      end

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

      op.on("-d", "--debug", "Debug mode (printing debug logging messages)") do
        @cmd_opts[:debug] = true
        remove_cmd_opts_from_argv("-d", "--debug")
      end

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

#parse_cmd_optsObject



135
136
137
138
139
140
141
142
143
# File 'lib/omf_ec/runner.rb', line 135

def parse_cmd_opts
  parse_config_file

  # uri in command line is short for communication/url
  uri = @cmd_opts.delete(:uri)
  @config_opts[:communication][:url] = uri if uri
  @config_opts[:communication][:auth] = { authenticate: true } if @cmd_opts[:cert]
  @config_opts.merge!(@cmd_opts)
end

#parse_config_fileObject



145
146
147
148
149
150
151
152
153
154
# File 'lib/omf_ec/runner.rb', line 145

def parse_config_file
  if (config_file = @cmd_opts.delete(:config_file))
    if File.exist?(config_file)
      @config_opts.merge!(OmfCommon.load_yaml(config_file))
    else
      puts "Config file '#{config_file}' doesn't exist"
      exit(1)
    end
  end
end

#remove_cmd_opts_from_argv(*args) ⇒ Object



156
157
158
# File 'lib/omf_ec/runner.rb', line 156

def remove_cmd_opts_from_argv(*args)
  args.each { |v| @argv.slice!(@argv.index(v)) if @argv.index(v) }
end

#runObject



286
287
288
# File 'lib/omf_ec/runner.rb', line 286

def run
  load_experiment
end

#setup_experimentObject



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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/omf_ec/runner.rb', line 160

def setup_experiment
  OmfEc.experiment.name = @config_opts[:experiment_name] if @config_opts[:experiment_name]
  OmfEc.experiment.oml_uri = @config_opts[:oml_uri] if @config_opts[:oml_uri]
  OmfEc.experiment.show_graph = @config_opts['show-graph']

  # Instrument EC
  if @config_opts[:inst_oml_uri] && @config_opts[:inst_oml_id] && @config_opts[:inst_oml_domain]
    instrument_ec = OML4R::init(nil, {
      collect: @config_opts[:inst_oml_uri],
      nodeID: @config_opts[:inst_oml_id],
      domain: @config_opts[:inst_oml_domain],
      appName: @executable_name
    })

    OmfCommon::Measure.enable if instrument_ec
  end

  remove_cmd_opts_from_argv("exec")

  index_of_dividing_hyphen = @argv.index("--")

  @argv[0..index_of_dividing_hyphen || -1].in_groups_of(2) do |arg_g|
    if arg_g[0] =~ /^--(.+)/ && !arg_g[1].nil?
      remove_cmd_opts_from_argv(*arg_g)
    end
  end

  @oedl_path = @argv[0] && File.expand_path(@argv[0])

  if @oedl_path.nil? || !File.exist?(@oedl_path)
    puts "Experiment script '#{@argv[0]}' not found"
    exit(1)
  end

  @argv.slice!(0)

  # User-provided command line values for Experiment Properties cannot be
  # set here as the properties have not been defined yet by the experiment.
  # Thus just pass them to the experiment, which will be responsible
  # for setting them later
  properties = {}
  if index_of_dividing_hyphen
    remove_cmd_opts_from_argv("--")
    exp_properties = @argv
    exp_properties.in_groups_of(2) do |p|
      unless p[0] =~ /^--(.+)/ && !p[1].nil?
        puts "Malformatted properties '#{exp_properties.join(' ')}'"
        exit(1)
      else
        properties[$1.to_sym] = p[1].ducktype
        remove_cmd_opts_from_argv(*p)
      end
    end
    OmfEc.experiment.cmdline_properties = properties
  end
end

#setup_loggingObject



217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/omf_ec/runner.rb', line 217

def setup_logging
  OmfCommon.load_logging_config(@config_opts[:logging_configfile])

  if @config_opts[:debug]
    Logging.logger.root.level = 'debug'
  else
    Logging.consolidate 'OmfCommon', 'OmfRc'
  end

  if OmfEc.experiment.oml_uri
    require 'oml4r/logging/oml4r_appender'
    Logging.logger.root.add_appenders(Logging.appenders.oml4r('oml4r', :appName => 'omf_ec', :domain => "#{OmfEc.experiment.id}", :collect => "#{OmfEc.experiment.oml_uri}"))
  end
end