4 from PLC.Faults import *
5 from PLC.Method import Method
6 from PLC.Parameter import Parameter, Mixed
7 from PLC.Nodes import Node, Nodes
8 from PLC.NodeNetworks import NodeNetwork, NodeNetworks
9 from PLC.Auth import Auth
11 class AdmGenerateNodeConfFile(Method):
13 Deprecated. Functionality can be implemented with GetNodes,
14 GetNodeNetworks, and UpdateNode.
16 Creates a new node configuration file if all network settings are
17 present. This function will generate a new node key for the
18 specified node, effectively invalidating any old configuration
21 Non-admins can only generate files for nodes at their sites.
23 Returns the contents of the file if successful, faults otherwise.
28 roles = ['admin', 'pi', 'tech']
32 Mixed(Node.fields['node_id'],
33 Node.fields['hostname'])
36 returns = Parameter(str, "Node configuration file")
38 def call(self, auth, node_id_or_hostname):
39 # Get node information
40 nodes = Nodes(self.api, [node_id_or_hostname])
42 raise PLCInvalidArgument, "No such node"
44 PLCCheckLocalNode(node,"AdmGenerateNodeConfFile")
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 node networks for this node
54 nodenetworks = NodeNetworks(self.api, node['nodenetwork_ids'])
55 for nodenetwork in nodenetworks:
56 if nodenetwork['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 # Generate 32 random bytes
70 bytes = random.sample(xrange(0, 256), 32)
71 # Base64 encode their string representation
72 node['key'] = base64.b64encode("".join(map(chr, bytes)))
73 # XXX Boot Manager cannot handle = in the key
74 node['key'] = node['key'].replace("=", "")
78 # Generate node configuration file suitable for BootCD
81 file += 'NODE_ID="%d"\n' % node['node_id']
82 file += 'NODE_KEY="%s"\n' % node['key']
85 file += 'NET_DEVICE="%s"\n' % primary['mac'].lower()
87 file += 'IP_METHOD="%s"\n' % primary['method']
89 if primary['method'] == 'static':
90 file += 'IP_ADDRESS="%s"\n' % primary['ip']
91 file += 'IP_GATEWAY="%s"\n' % primary['gateway']
92 file += 'IP_NETMASK="%s"\n' % primary['netmask']
93 file += 'IP_NETADDR="%s"\n' % primary['network']
94 file += 'IP_BROADCASTADDR="%s"\n' % primary['broadcast']
95 file += 'IP_DNS1="%s"\n' % primary['dns1']
96 file += 'IP_DNS2="%s"\n' % (primary['dns2'] or "")
98 file += 'HOST_NAME="%s"\n' % host
99 file += 'DOMAIN_NAME="%s"\n' % domain
101 for nodenetwork in nodenetworks:
102 if nodenetwork['method'] == 'ipmi':
103 file += 'IPMI_ADDRESS="%s"\n' % nodenetwork['ip']
104 if nodenetwork['mac']:
105 file += 'IPMI_MAC="%s"\n' % nodenetwork['mac'].lower()