Class: Vagrant::Hmurca::GeneralProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant/hmurca/config_parser.rb

Overview

Public: Processor for top level configuration.

Constant Summary collapse

ARITY =
{
  'domain'      => 1,
  'base-box'    => 1,
  'sync-folder' => 2,
  'use-nfs'     => 1
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGeneralProcessor

Returns a new instance of GeneralProcessor.



94
95
96
97
# File 'lib/vagrant/hmurca/config_parser.rb', line 94

def initialize
  @nodes        = []
  @sync_folders = []
end

Instance Attribute Details

#base_boxObject (readonly)

Returns the value of attribute base_box.



92
93
94
# File 'lib/vagrant/hmurca/config_parser.rb', line 92

def base_box
  @base_box
end

#domainObject (readonly)

Returns the value of attribute domain.



92
93
94
# File 'lib/vagrant/hmurca/config_parser.rb', line 92

def domain
  @domain
end

#nodesObject (readonly)

Returns the value of attribute nodes.



92
93
94
# File 'lib/vagrant/hmurca/config_parser.rb', line 92

def nodes
  @nodes
end

#sync_foldersObject (readonly)

Returns the value of attribute sync_folders.



92
93
94
# File 'lib/vagrant/hmurca/config_parser.rb', line 92

def sync_folders
  @sync_folders
end

#use_nfsObject (readonly)

Returns the value of attribute use_nfs.



92
93
94
# File 'lib/vagrant/hmurca/config_parser.rb', line 92

def use_nfs
  @use_nfs
end

Instance Method Details

#name_as_prefixObject

Public: Produces reverse domain name to use as a box prefix.



135
136
137
# File 'lib/vagrant/hmurca/config_parser.rb', line 135

def name_as_prefix
  return self.domain.split('.').reverse.join('.')
end

#parse_bool(v, line_no) ⇒ Object

Internal: Converts given string value to boolean. If value isn’t one of ‘yes’ or ‘no’, then it raises an error.

v - The String value to covnert. line_no - The Integer number of the current parsed line.

Returns true if value equals ‘yes’, false if ‘no’. Raises InvalidBooleanValueError if value can’t be converted.



188
189
190
191
192
193
194
195
196
197
# File 'lib/vagrant/hmurca/config_parser.rb', line 188

def parse_bool(v, line_no)
  case v
  when "yes"
    true
  when "no"
    false
  else
    raise InvalidBooleanValueError.new(v, line_no)
  end
end

#process(line_no, cmd, *args) ⇒ Object



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
# File 'lib/vagrant/hmurca/config_parser.rb', line 99

def process(line_no, cmd, *args)
  case cmd
  when 'domain'
    d = args.first
    raise InvalidDomainNameError.new(d, line_no) unless valid_domain?(d)
    @domain = d
  when 'base-box'
    b = args.first
    @base_box = b
  when 'sync-folder'
    host, guest = *args

    unless File.directory?(host)
      raise SyncedFolderNotFoundError.new(host, line_no)
    end

    @sync_folders << [host, guest]
  when 'use-nfs'
    @use_nfs = parse_bool(args.first, line_no)
  when 'node'
    if args.size > 1
      raise Booby::InvalidNumberOfParametersError.new(
        "0 or 1", args.size, cmd, line_no
        )
    end

    @nodes << (p = NodeGroupProcessor.new(line_no, *args))
    return p
  else
    return false
  end

  true
end

#valid_domain?(domain) ⇒ Boolean

Internal: Checks weather given domain name is valid or not.

domain - The String domain name to check.

Returns true if domain name is valid.

Returns:

  • (Boolean)


176
177
178
# File 'lib/vagrant/hmurca/config_parser.rb', line 176

def valid_domain?(domain)
  domain =~ /^[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/ix
end

#validate!Object

Internal: Validates post-parsing constraints, like presence checking, etc.

Returns itself.



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/vagrant/hmurca/config_parser.rb', line 142

def validate!
  raise NoDomainSpecifiedError unless self.domain

  # Validate each node separately...
  self.nodes.each(&:validate!)

  # Check for duplicated nodes...
  # XXX: Yes this way of checking might be slow, but who cares with max
  # 10 defined nodes?
  unique_nodes = {}
  self.nodes.each do |node|
    name = node.name
    raise NodeDuplicatedError.new(name) if unique_nodes[name]
    unique_nodes[name] = true
  end

  # Check for duplicated forward ports...
  # XXX: Same here in terms of efficiency.
  unique_ports = {}
  self.nodes.each do |node|
    node.forward_ports.keys.each do |port|
      raise NodeForwardPortDuplicatedError.new(port) if unique_ports[port]
      unique_ports[port] = true
    end
  end

  return self
end