Class: OpenStudio::Extension::RunnerConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/openstudio/extension/runner_config.rb

Constant Summary collapse

FILENAME =
'runner.conf'.freeze
DEFAULT_NPROC =
Parallel.processor_count - 1

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dirname) ⇒ RunnerConfig

Class to store configuration of the runner options.

@param [String] dirname Directory where runner.conf file is stored, typically the root of the extension.


22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/openstudio/extension/runner_config.rb', line 22

def initialize(dirname)
  # read in the runner config from file, if it exists, otherwise inform the user to run
  # the rake task
  @dirname = dirname

  check_file = File.join(@dirname, FILENAME)
  if File.exist? check_file
    @data = JSON.parse(File.read(check_file), symbolize_names: true)
  else
    raise "There is no runner.conf in directory #{@dirname}. Run `rake openstudio:runner:init`"
  end
end

Class Method Details

.default_config(dirname) ⇒ Object

Return the default runner configuration



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/openstudio/extension/runner_config.rb', line 64

def self.default_config(dirname)
  return {
    file_version: '0.1.0',
    max_datapoints: 1E9.to_i,
    num_parallel: DEFAULT_NPROC,
    run_simulations: true,
    verbose: false,
    gemfile_path: '',
    bundle_install_path: get_local_bundle_config_path(dirname),
  }
end

.get_local_bundle_config_path(dirname) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/openstudio/extension/runner_config.rb', line 48

def self.get_local_bundle_config_path(dirname)
  # Bundler.settings reads from pwd, and I can't test with this...
  bundler_settings = Bundler::Settings.new(File.join(dirname, '.bundle'))

  if (bundle_install_path = bundler_settings.path.explicit_path)
    puts "Defaulting bundle_install_path to bundle's local config value: '#{bundle_install_path}'"
  else
    puts "Defaulting to .bundle/install (and ignoring system wide: Bundler.configured_bundle_path.base_path=#{bundler_settings.path.base_path})"
    bundle_install_path = File.join(dirname, '.bundle/install/')
  end

  return bundle_install_path
end

.init(dirname) ⇒ Object

Save a templated runner configuration to the specified directory. Note that this will override any config that has been created

@param [String] dirname Directory where runner.conf file is stored, typically the root of the extension.


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/openstudio/extension/runner_config.rb', line 81

def self.init(dirname)
  runner_conf_file = File.join(dirname, FILENAME)
  if File.exist?(runner_conf_file)
    puts "runner.conf already exists, saving a runner.conf.bak, previous config:"
    puts File.read(runner_conf_file)
    FileUtils.cp(runner_conf_file, "#{FILENAME}.bak")
  end

  config = default_config(dirname)
  File.open(runner_conf_file, 'w') do |f|
    f << JSON.pretty_generate(config)
  end

  return config
end

Instance Method Details

#add_config_option(name, value) ⇒ Object

Add runner configuration that may be used in other extensions

@param [String] name, Name of the new config option


39
40
41
42
43
44
45
46
# File 'lib/openstudio/extension/runner_config.rb', line 39

def add_config_option(name, value)
  if @data.key? name.to_sym
    raise "Runner config already has the named option of #{name}"
  end

  # TODO: do we need to verify that name is allowed to be a key?
  @data[name.to_sym] = value
end

#optionsObject

Return the options as hash



121
122
123
# File 'lib/openstudio/extension/runner_config.rb', line 121

def options
  return @data
end

#saveObject

Save the updated config options, if they have changed. Changes will typically only occur if calling add_config option



100
101
102
103
104
# File 'lib/openstudio/extension/runner_config.rb', line 100

def save
  File.open(File.join(@dirname, FILENAME), 'w') do |f|
    f << JSON.pretty_generate(@data)
  end
end

#update_config(key, new_value) ⇒ Object

Update a runner config value

Parameters:

  • key, (String)

    The name of the key to update

  • new_value, (Variant)

    The new value to set the ‘key` to.



111
112
113
114
115
116
117
# File 'lib/openstudio/extension/runner_config.rb', line 111

def update_config(key, new_value)
  if @data.key? key.to_sym
    @data[key.to_sym] = new_value
  else
    raise "Could not find key '#{key}' to update in RunnerConfig."
  end
end