Class: Gogetit::GogetMAAS

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conf, logger) ⇒ GogetMAAS

Returns a new instance of GogetMAAS.



7
8
9
10
11
12
13
14
# File 'lib/maas.rb', line 7

def initialize(conf, logger)
  @config = conf
  @conn = Maas::Client::MaasClient.new(
      config[:maas][:key],
      config[:maas][:url]
    )
  @logger = logger
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



5
6
7
# File 'lib/maas.rb', line 5

def config
  @config
end

#connObject (readonly)

Returns the value of attribute conn.



5
6
7
# File 'lib/maas.rb', line 5

def conn
  @conn
end

#domainObject (readonly)

Returns the value of attribute domain.



5
6
7
# File 'lib/maas.rb', line 5

def domain
  @domain
end

#loggerObject (readonly)

Returns the value of attribute logger.



5
6
7
# File 'lib/maas.rb', line 5

def logger
  @logger
end

Instance Method Details

#change_hostname(system_id, hostname) ⇒ Object



114
115
116
117
# File 'lib/maas.rb', line 114

def change_hostname(system_id, hostname)
  logger.info("Calling <#{__method__.to_s}>")
  conn.request(:put, ['machines', system_id], { 'hostname' => hostname })
end

#delete_dns_record(name) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/maas.rb', line 67

def delete_dns_record(name)
  logger.info("Calling <#{__method__.to_s}>")
  id = nil
  conn.request(:get, ['dnsresources']).each do |item|
    if item['fqdn'] == name + '.' + get_domain
      id = item['id']
    end
  end

  if ! id.nil?
    conn.request(:delete, ['dnsresources', id.to_s])
  else
    logger.warn('No such record found.')
  end
end

#dnsresource_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
# File 'lib/maas.rb', line 30

def dnsresource_exists?(name)
  logger.info("Calling <#{__method__.to_s}>")
  conn.request(:get, ['dnsresources']).each do |item|
    return true if item['fqdn'] == name + '.' + get_domain
  end
  false
end

#domain_name_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/maas.rb', line 38

def domain_name_exists?(name)
  return true if dnsresource_exists?(name) or machine_exists?(name)
end

#get_domainObject



16
17
18
19
20
# File 'lib/maas.rb', line 16

def get_domain
  return @domain if @domain
  logger.info("Calling <#{__method__.to_s}>")
  @domain = conn.request(:get, ['domains'])[0]['name']
end

#get_machine_state(system_id) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/maas.rb', line 106

def get_machine_state(system_id)
  logger.info("Calling <#{__method__.to_s}>")
  conn.request(:get, ['machines']).each do |m|
    return m['status_name'] if m['system_id'] == system_id
  end
  false
end

#get_system_id(name) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/maas.rb', line 91

def get_system_id(name)
  logger.info("Calling <#{__method__.to_s}>")
  conn.request(:get, ['machines']).each do |m|
    return m['system_id'] if m['hostname'] == name
  end
  nil
end

#ipaddresses(op = nil, params = nil) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/maas.rb', line 42

def ipaddresses(op = nil, params = nil)
  case op
  when nil
    conn.request(:get, ['ipaddresses'])
  when 'reserve'
    # sample = {
    #   'subnet' => '10.1.2.0/24',
    #   'ip' => '10.1.2.8',
    #   'hostname' => 'hostname',
    #   'mac' => 'blahblah'
    # }
    default_param = { 'op' => op }
    conn.request(:post, ['ipaddresses'], default_param.merge!(params))
  when 'release'
    # Gogetit.maas.ipaddresses('release', {'ip' => '10.1.2.8'})
    # sample = {
    #   'ip' => '10.1.2.8',
    #   'hostname' => 'hostname',
    #   'mac' => 'blahblah'
    # }
    default_param = { 'op' => op }
    conn.request(:post, ['ipaddresses'], default_param.merge!(params))
  end
end

#machine_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
26
27
28
# File 'lib/maas.rb', line 22

def machine_exists?(name)
  logger.info("Calling <#{__method__.to_s}>")
  conn.request(:get, ['machines']).each do |m|
    return true if m['hostname'] == name
  end
  false
end

#refresh_podsObject



83
84
85
86
87
88
89
# File 'lib/maas.rb', line 83

def refresh_pods
  logger.info("Calling <#{__method__.to_s}>")
  pod_id = conn.request(:get, ['pods'])
  pod_id.each do |pod|
    conn.request(:post, ['pods', pod['id']], { 'op' => 'refresh' } )
  end
end

#wait_until_state(system_id, state) ⇒ Object



99
100
101
102
103
104
# File 'lib/maas.rb', line 99

def wait_until_state(system_id, state)
  logger.info("Calling <#{__method__.to_s}> for being #{state}")
  until conn.request(:get, ['machines', system_id])['status_name'] == state
    sleep 3
  end
end