Class: Mt::Wall::DSL::HostBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/mt/wall/dsl/host_builder.rb

Overview

Block context for the ‘host` verb. Collects one or more addresses or CIDR subnets (and optional group memberships) and produces a Model::AddressObject.

host "web" do
  address "10.0.0.5"
  address "10.0.1.0/24"
  member_of "web-prod", "web-wordpress"
end

“host” here means a named address object (an address-list), not necessarily a single machine: a host may hold several addresses and/or subnets.

‘member_of` is sugar for declaring membership from the host side. It does NOT live on the AddressObject: the named host is folded into each referenced Group’s members (membership is single-sourced in Model::Group). A referenced group that was not declared elsewhere is created on demand.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, comment: nil) ⇒ HostBuilder

Returns a new instance of HostBuilder.



26
27
28
29
30
31
# File 'lib/mt/wall/dsl/host_builder.rb', line 26

def initialize(name, comment: nil)
  @name = name
  @comment = comment
  @addresses = []
  @memberships = []
end

Instance Attribute Details

#membershipsArray<String> (readonly)

Group names this host should be folded into. Read by RootBuilder after the block runs, to extend each Group’s members.

Returns:

  • (Array<String>)


51
52
53
# File 'lib/mt/wall/dsl/host_builder.rb', line 51

def memberships
  @memberships
end

Instance Method Details

#address(value) ⇒ void

This method returns an undefined value.

Parameters:

  • value (String)

    an IPv4/IPv6 address or CIDR subnet (family is inferred via IPAddr; an unparseable value is a fail-fast ConfigurationError)



37
38
39
# File 'lib/mt/wall/dsl/host_builder.rb', line 37

def address(value)
  @addresses << Validators.validate_address!(value)
end

#member_of(*group_names) ⇒ void

This method returns an undefined value.

Declare that this host belongs to one or more groups (by name).

Parameters:

  • group_names (Array<String>)


44
45
46
# File 'lib/mt/wall/dsl/host_builder.rb', line 44

def member_of(*group_names)
  group_names.each { |name| @memberships << Validators.validate_group_token!(name) }
end

#to_objectModel::AddressObject

Materialize the collected addresses into a Model::AddressObject.



55
56
57
58
59
60
# File 'lib/mt/wall/dsl/host_builder.rb', line 55

def to_object
  raise ConfigurationError, "host #{@name.inspect} has no addresses; declare at least one" if @addresses.empty?

  Model::AddressObject.new(name: Validators.validate_name!(@name, label: "host"),
                           addresses: @addresses, comment: @comment)
end