Class: Arnold::NodeManager

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

Instance Method Summary collapse

Constructor Details

#initializeNodeManager

Returns a new instance of NodeManager.



9
10
11
12
13
14
15
16
# File 'lib/arnold/node_manager.rb', line 9

def initialize
  @datadir = "#{$CONFIG[:datadir]}/arnold"

  # make sure our data directories exist
  makedir "#{@datadir}"
  makedir "#{@datadir}/macaddr/"
  makedir "#{@datadir}/name/"
end

Instance Method Details

#load(guid) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/arnold/node_manager.rb', line 18

def load(guid)
  begin
    data = YAML.load_file("#{@datadir}/#{guid}.yaml")
    return Arnold::Node.new(guid, data['name'], data['macaddr'], data['parameters'], data['classes'])
  rescue Exception
    puts "Invalid node! #{data.to_yaml}"
    return Arnold::Node.new(guid)
  end
end

#loadallObject



28
29
30
31
32
33
34
# File 'lib/arnold/node_manager.rb', line 28

def loadall
  nodes=[]
  Dir.glob("#{@datadir}/*.yaml").each do |file|
    nodes << load(File.basename(file, '.yaml'))
  end
  nodes
end

#write(node) ⇒ Object



36
37
38
39
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
# File 'lib/arnold/node_manager.rb', line 36

def write(node)
  raise "Must have a name or mac address!" if (node.name.nil? and node.macaddr.nil?)

  if node.guid.nil?
    raise "Node name exists: please try again" if File.exists? "#{@datadir}/name/#{node.name}.yaml"
    raise "MAC address exists: please try again" if File.exists? "#{@datadir}/macaddr/#{node.macaddr}.yaml"
    node.guid = makeguid()
  else
    raise "Invalid Node" unless File.exists? "#{@datadir}/#{node.guid}.yaml"
  end

  data = {
    'parameters' => node.parameters,
    'classes'    => node.classes,
  }
  data['name']    = node.name if node.name
  data['macaddr'] = node.macaddr  if node.macaddr

  # duplicate the parameters hash. This allows hiera() calls to work as expected.
  # Principle of least surprise, ya know.
  data.merge! node.parameters

  File.open("#{@datadir}/#{node.guid}.yaml", 'w') do |file|
    file.write("###########################################################\n")
    file.write("### This file is managed by Arnold: the provisionator.  ###\n")
    file.write("# Any manual modifications will be gleefully overwritten. #\n")
    file.write("###########################################################\n")
    file.write(data.to_yaml)
  end

  make_link(node.guid, node.macaddr, :macaddr)
  make_link(node.guid, node.name, :name)
  remove_stale_symlinks("#{@datadir}/macaddr/")
  remove_stale_symlinks("#{@datadir}/name/")

  return node.guid
end