Class: Sistero::Config
- Inherits:
-
Object
- Object
- Sistero::Config
- Defined in:
- lib/sistero/config.rb
Instance Attribute Summary collapse
-
#defaults ⇒ Object
Returns the value of attribute defaults.
-
#vms ⇒ Object
Returns the value of attribute vms.
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ Config
constructor
A new instance of Config.
- #to_s ⇒ Object
- #vm(name) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Config
Returns a new instance of Config.
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 |
# File 'lib/sistero/config.rb', line 37 def initialize(opts = {}) # read defaults from config file @cfg_file_path = opts[:cfg_file_path] unless @cfg_file_path directory = opts[:directory] || '.' for n in 0..100 cfg_file_path = File.join(directory, CONFIG_FILE_NAME) if File.exists? cfg_file_path @cfg_file_path = cfg_file_path break end directory = File. '..', directory break if directory == '/' end @cfg_file_path ||= "#{ENV['HOME']}/.config/#{APP_NAME}" end @defaults = VM.new @vms = [] cfg = YAML.load_file @cfg_file_path postprocess_cfg cfg cfg['defaults'].each do |key, value| @defaults[key] = value end @vms = cfg['vms'].map do |vm_cfg| vm = VM.new *@defaults vm.name = nil vm_cfg.each do |key, value| vm[key] = value end unless vm.user_data.nil? user_data = vm.user_data.dup VM_KEYS.each do |key| value = vm[key] if value.is_a? String user_data.gsub! "\#{#{key}}", value end end vm.user_data = user_data end raise "every vm must have a name field" unless vm.name vm end end |
Instance Attribute Details
#defaults ⇒ Object
Returns the value of attribute defaults.
24 25 26 |
# File 'lib/sistero/config.rb', line 24 def defaults @defaults end |
#vms ⇒ Object
Returns the value of attribute vms.
24 25 26 |
# File 'lib/sistero/config.rb', line 24 def vms @vms end |
Instance Method Details
#to_s ⇒ Object
88 89 90 |
# File 'lib/sistero/config.rb', line 88 def to_s @vms.map(&:to_s).join "\n" end |
#vm(name) ⇒ Object
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/sistero/config.rb', line 26 def vm name name ||= @defaults['name'] raise "must set a default name or specify one" unless name # TODO: also handle wildcards vm = @vms.find do |vm| vm.name == name end raise "could not find vm for #{name}" unless vm vm end |