6 from PLC.Faults import *
7 from PLC.Method import Method
8 from PLC.Parameter import Parameter, Mixed
9 from PLC.Nodes import Node, Nodes
10 from PLC.Interfaces import Interface, Interfaces
11 from PLC.Auth import Auth
13 class GenerateNodeConfFile(Method):
15 Creates a new node configuration file if all network settings are
16 present. This function will generate a new node key for the
17 specified node, effectively invalidating any old configuration
20 Non-admins can only generate files for nodes at their sites.
22 Returns the contents of the file if successful, faults otherwise.
25 roles = ['admin', 'pi', 'tech']
29 Mixed(Node.fields['node_id'],
30 Node.fields['hostname']),
31 Parameter(bool, "True if you want to regenerate node key")
34 returns = Parameter(str, "Node configuration file")
36 def call(self, auth, node_id_or_hostname, regenerate_node_key = True):
37 # Get node information
38 nodes = Nodes(self.api, [node_id_or_hostname])
40 raise PLCInvalidArgument, "No such node"
43 if node['peer_id'] is not None:
44 raise PLCInvalidArgument, "Not a local node"
46 # If we are not an admin, make sure that the caller is a
47 # member of the site at which the node is located.
48 if 'admin' not in self.caller['roles']:
49 if node['site_id'] not in self.caller['site_ids']:
50 raise PLCPermissionDenied, "Not allowed to generate a configuration file for that node"
52 # Get interfaces for this node
54 interfaces = Interfaces(self.api, node['interface_ids'])
55 for interface in interfaces:
56 if interface['is_primary']:
60 raise PLCInvalidArgument, "No primary network configured"
62 # Split hostname into host and domain parts
63 parts = node['hostname'].split(".", 1)
65 raise PLCInvalidArgument, "Node hostname is invalid"
69 if regenerate_node_key:
70 # Generate 32 random bytes
71 bytes = random.sample(xrange(0, 256), 32)
72 # Base64 encode their string representation
73 node['key'] = base64.b64encode("".join(map(chr, bytes)))
74 # XXX Boot Manager cannot handle = in the key
75 node['key'] = node['key'].replace("=", "")
79 # Generate node configuration file suitable for BootCD
82 file += 'NODE_ID="%d"\n' % node['node_id']
83 file += 'NODE_KEY="%s"\n' % node['key']
86 file += 'NET_DEVICE="%s"\n' % primary['mac'].lower()
88 file += 'IP_METHOD="%s"\n' % primary['method']
90 if primary['method'] == 'static':
91 file += 'IP_ADDRESS="%s"\n' % primary['ip']
92 file += 'IP_GATEWAY="%s"\n' % primary['gateway']
93 file += 'IP_NETMASK="%s"\n' % primary['netmask']
94 file += 'IP_NETADDR="%s"\n' % primary['network']
95 file += 'IP_BROADCASTADDR="%s"\n' % primary['broadcast']
96 file += 'IP_DNS1="%s"\n' % primary['dns1']
97 file += 'IP_DNS2="%s"\n' % (primary['dns2'] or "")
99 file += 'HOST_NAME="%s"\n' % host
100 file += 'DOMAIN_NAME="%s"\n' % domain
102 for interface in interfaces:
103 if interface['method'] == 'ipmi':
104 file += 'IPMI_ADDRESS="%s"\n' % interface['ip']
106 file += 'IPMI_MAC="%s"\n' % interface['mac'].lower()