Class: VagrantPlugins::Dotvm::ConfigParser

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

Constant Summary collapse

DEFAULT_NET =
'private_network'
DEFAULT_NETMASK =
'255.255.255.0'

Instance Method Summary collapse

Constructor Details

#initialize(vars = {}) ⇒ ConfigParser

Returns a new instance of ConfigParser.



8
9
10
# File 'lib/vagrant-dotvm/configparser.rb', line 8

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

Instance Method Details

#coalesce(*args) ⇒ Object



143
144
145
146
147
148
# File 'lib/vagrant-dotvm/configparser.rb', line 143

def coalesce(*args)
  args.each do |val|
    next if val.nil?
    return val
  end
end

#parse(yaml) ⇒ Object



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
134
135
136
137
138
139
140
# File 'lib/vagrant-dotvm/configparser.rb', line 108

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

    machine['authorized_keys'].to_a.each do |key|
      item[:authorized_keys] << self.parse_authorized_key(key)
    end

    config[:machines] << item
  end

  return self.replace_vars(config)
end

#parse_authorized_key(key) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/vagrant-dotvm/configparser.rb', line 99

def parse_authorized_key(key)
  return {
    :type => key['type'],
    :path => key['path'],
    :key => key['key'],
  }
end

#parse_folder(folder) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/vagrant-dotvm/configparser.rb', line 90

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

#parse_machine(machine) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/vagrant-dotvm/configparser.rb', line 40

def parse_machine(machine)
  return {
    :nick	      => machine['nick'],
    :name	      => machine['name'],
    :box        => machine['box'],
    :memory     => machine['memory'],
    :cpus       => machine['cpus'],
    :cpucap     => machine['cpucap'],
    :primary    => self.coalesce(machine['primary'], false),
    :natnet     => machine['natnet'],
    :networks   => [],
    :provision  => [],
    :folders    => [
      {
        :host => '%project.host%',
        :guest => '%project.guest%',
        :disabled => false,
      }
    ],
    :groups     => [],
    :authorized_keys => [],
  }
end

#parse_net(net) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/vagrant-dotvm/configparser.rb', line 65

def parse_net(net)
  return {
    :net  => self.coalesce(net['net'], DEFAULT_NET),
    :type => net['type'],
    :ip   => net['ip'],
    :mask => self.coalesce(net['mask'], net['netmask'], DEFAULT_NETMASK),
    :interface => net['interface'],
  }
end

#parse_provision(prv) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/vagrant-dotvm/configparser.rb', line 76

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

#replace_vars(value) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/vagrant-dotvm/configparser.rb', line 13

def replace_vars(value)
  if value.kind_of?(Hash)
    result = {}
    value.each do |key, val|
      result[key] = self.replace_vars(val)
    end
  elsif value.kind_of?(Array)
    result = value.map do |val|
      self.replace_vars(val)
    end
  elsif value.kind_of?(String)
    result = value.dup

    @vars.each do |k, v|
      pattern = '%' + k + '%'
      result.gsub! pattern, v
    end
  elsif !value.respond_to?(:duplicable) or !value.duplicable?
    result = value
  else
    result = value.dup
  end

  return result
end