class Construqt::Hosts

Public Class Methods

new(region) click to toggle source
# File lib/construqt/hosts.rb, line 6
def initialize(region)
  @region = region
  @hosts = {}
  @default_pwd = SecureRandom.urlsafe_base64(24)
end

Public Instance Methods

add(host_name, cfg, &block) click to toggle source
# File lib/construqt/hosts.rb, line 35
def add(host_name, cfg, &block)
  (host_name, host) = Construqt::Tags.add(host_name) { |name| add_internal(name, cfg) { |h| block.call(h) } }
  host
end
add_internal(name, cfg, &block) click to toggle source
# File lib/construqt/hosts.rb, line 40
def add_internal(name, cfg, &block)
  #binding.pry
  throw "id is not allowed" if cfg['id']
  throw "configip is not allowed" if cfg['configip']
  throw "Host with the name #{name} exisits" if @hosts[name]
  cfg['interfaces'] = {}
  cfg['id'] ||=nil
  cfg['configip'] ||=nil

  cfg['name'] = name
  cfg['dns_server'] ||= false
  cfg['result'] = nil
  cfg['shadow'] ||= nil
  cfg['flavour'] = Flavour.find(cfg['flavour'] || 'ubuntu')
  #         cfg['clazz'] = cfg['flavour'].clazz("host")
  throw "flavour #{cfg['flavour']} for host #{name} not found" unless cfg['flavour']
  cfg['region'] = @region
  host = cfg['flavour'].create_host(name, cfg)
  block.call(host)
  throw "host attribute id is required" unless host.id.kind_of? HostId
  throw "host attribute configip is required" unless host.configip.kind_of? HostId

  if (host.id.first_ipv4! && !host.id.first_ipv4!.dhcpv4?) ||
      (host.id.first_ipv6! && !host.id.first_ipv6!.dhcpv6?)
    adr = nil
    if host.id.first_ipv4!
      adr = (adr || region.network.addresses.create).add_ip(host.id.first_ipv4.first_ipv4.to_s).set_name(host.name)
    end

    if host.id.first_ipv6!
      adr = (adr || region.network.addresses.create).add_ip(host.id.first_ipv6.first_ipv6.to_s).set_name(host.name)
    end

    adr = region.network.addresses.create unless adr
    adr.host = host if adr
  end

  @hosts[name] = host
end
build_config(hosts = nil) click to toggle source
# File lib/construqt/hosts.rb, line 86
def build_config(hosts = nil)
  (hosts || @hosts.values).each do |host|
    host.build_config(host, nil)
  end
end
commit(hosts = nil) click to toggle source
# File lib/construqt/hosts.rb, line 92
def commit(hosts = nil)
  (hosts || @hosts.values).each { |h| h.commit }
  Flavour.call_aspects("completed", nil, nil)
end
default_password() click to toggle source
# File lib/construqt/hosts.rb, line 20
def default_password
  @default_pwd
end
del(name) click to toggle source
# File lib/construqt/hosts.rb, line 28
def del(name)
  host = @hosts[name]
  return nil unless host
  @hosts.delete(name)
  host
end
find(name) click to toggle source
# File lib/construqt/hosts.rb, line 80
def find(name)
  ret = @hosts[name]
  throw "host not found #{name}" unless ret
  ret
end
get_hosts() click to toggle source
# File lib/construqt/hosts.rb, line 24
def get_hosts()
  @hosts.values
end
region() click to toggle source
# File lib/construqt/hosts.rb, line 12
def region
  @region
end
set_default_password(pwd) click to toggle source
# File lib/construqt/hosts.rb, line 16
def set_default_password(pwd)
  @default_pwd = pwd
end