Class: Construqt::Hosts

Inherits:
Object
  • Object
show all
Defined in:
lib/construqt/hosts.rb

Instance Method Summary collapse

Constructor Details

#initialize(region) ⇒ Hosts

Returns a new instance of Hosts.



6
7
8
9
10
# File 'lib/construqt/hosts.rb', line 6

def initialize(region)
  @region = region
  @hosts = {}
  @default_pwd = SecureRandom.urlsafe_base64(24)
end

Instance Method Details

#add(host_name, cfg, &block) ⇒ Object



35
36
37
38
# 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) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# 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) ⇒ Object



86
87
88
89
90
# 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) ⇒ Object



92
93
94
95
# 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_passwordObject



20
21
22
# File 'lib/construqt/hosts.rb', line 20

def default_password
  @default_pwd
end

#del(name) ⇒ Object



28
29
30
31
32
33
# File 'lib/construqt/hosts.rb', line 28

def del(name)
  host = @hosts[name]
  return nil unless host
  @hosts.delete(name)
  host
end

#find(name) ⇒ Object



80
81
82
83
84
# File 'lib/construqt/hosts.rb', line 80

def find(name)
  ret = @hosts[name]
  throw "host not found #{name}" unless ret
  ret
end

#get_hostsObject



24
25
26
# File 'lib/construqt/hosts.rb', line 24

def get_hosts()
  @hosts.values
end

#regionObject



12
13
14
# File 'lib/construqt/hosts.rb', line 12

def region
  @region
end

#set_default_password(pwd) ⇒ Object



16
17
18
# File 'lib/construqt/hosts.rb', line 16

def set_default_password(pwd)
  @default_pwd = pwd
end