Class: VagrantPlugins::Dotvm::ConfigParser

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-dotvm/configparser.rb

Instance Method Summary collapse

Constructor Details

#initialize(vars = {}) ⇒ ConfigParser

Returns a new instance of ConfigParser.



5
6
7
# File 'lib/vagrant-dotvm/configparser.rb', line 5

def initialize(vars = {})
  @vars = vars
end

Instance Method Details

#parse(yaml) ⇒ Object



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
# File 'lib/vagrant-dotvm/configparser.rb', line 72

def parse(yaml)
  config = {
    :machines => []
  }

  yaml['machines'].each do |machine|
    item = parse_machine(machine)

    machine['networks'] and machine['networks'].each do |net|
      item[:networks] << self.parse_net(net)
    end

    machine['provision'] and machine['provision'].each do |prv|
      item[:provision] << self.parse_provision(prv)
    end

    machine['shared_folders'] and machine['shared_folders'].each do |folder|
      item[:folders] << self.parse_folder(folder)
    end

    machine['groups'] and machine['groups'].each do |group|
      item[:groups] << group
    end

    config[:machines] << item
  end

  return config
end

#parse_folder(folder) ⇒ Object



64
65
66
67
68
69
# File 'lib/vagrant-dotvm/configparser.rb', line 64

def parse_folder(folder)
  return {
    :host  => self.replace_vars(folder['host']),
    :guest => folder['guest'],
  }
end

#parse_machine(machine) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/vagrant-dotvm/configparser.rb', line 24

def parse_machine(machine)
  return {
    :nick	      => machine['nick'],
    :name	      => machine['name'],
    :box        => machine['box'],
    :memory     => machine['memory'],
    :cpus       => machine['cpus'],
    :cpucap     => machine['cpucap'],
    :primary    => machine['primary'] ||= false,
    :networks   => [],
    :provision  => [],
    :folders    => [],
    :groups     => [],
  }
end

#parse_net(net) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/vagrant-dotvm/configparser.rb', line 41

def parse_net(net)
  return {
    :net  => net['net'],
    :type => net['type'],
    :ip   => net['ip'],
    :mask => net['mask'],
  }
end

#parse_provision(prv) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/vagrant-dotvm/configparser.rb', line 51

def parse_provision(prv)
  return {
    :type		  => prv['type'],
    :source         => self.replace_vars(prv['source']),
    :destination    => prv['destination'],
    :path		  => self.replace_vars(prv['path']),
    :module_path    => self.replace_vars(prv['module_path']),
    :manifests_path => self.replace_vars(prv['manifests_path']),
    :manifest_file  => prv['manifest_file'],
  }
end

#replace_vars(value) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/vagrant-dotvm/configparser.rb', line 10

def replace_vars(value)
  if value
    value = value.dup()
    
    @vars.each do |key, val|
      pattern = '%' + key + '%'
      value.gsub! pattern, val
    end
  end
    
  return value
end