a121264978d0bd4cf42aa288eec1161bee2ceea9
[plcapi.git] / PLC / Methods / GenerateNodeConfFile.py
1 # $Id$
2 import random
3 import base64
4
5 from PLC.Faults import *
6 from PLC.Method import Method
7 from PLC.Parameter import Parameter, Mixed
8 from PLC.Nodes import Node, Nodes
9 from PLC.Interfaces import Interface, Interfaces
10 from PLC.Auth import Auth
11
12 class GenerateNodeConfFile(Method):
13     """
14     Creates a new node configuration file if all network settings are
15     present. This function will generate a new node key for the
16     specified node, effectively invalidating any old configuration
17     files.
18
19     Non-admins can only generate files for nodes at their sites.
20
21     Returns the contents of the file if successful, faults otherwise.
22     """
23
24     roles = ['admin', 'pi', 'tech']
25
26     accepts = [
27         Auth(),
28         Mixed(Node.fields['node_id'],
29               Node.fields['hostname']),
30         Parameter(bool, "True if you want to regenerate node key")
31         ]
32
33     returns = Parameter(str, "Node configuration file")
34
35     def call(self, auth, node_id_or_hostname, regenerate_node_key = True):
36         # Get node information
37         nodes = Nodes(self.api, [node_id_or_hostname])
38         if not nodes:
39             raise PLCInvalidArgument, "No such node"
40         node = nodes[0]
41
42         if node['peer_id'] is not None:
43             raise PLCInvalidArgument, "Not a local node"
44
45         # If we are not an admin, make sure that the caller is a
46         # member of the site at which the node is located.
47         if 'admin' not in self.caller['roles']:
48             if node['site_id'] not in self.caller['site_ids']:
49                 raise PLCPermissionDenied, "Not allowed to generate a configuration file for that node"
50
51         # Get interfaces for this node
52         primary = None
53         interfaces = Interfaces(self.api, node['interface_ids'])
54         for interface in interfaces:
55             if interface['is_primary']:
56                 primary = interface
57                 break
58         if primary is None:
59             raise PLCInvalidArgument, "No primary network configured"
60
61         # Split hostname into host and domain parts
62         parts = node['hostname'].split(".", 1)
63         if len(parts) < 2:
64             raise PLCInvalidArgument, "Node hostname is invalid"
65         host = parts[0]
66         domain = parts[1]
67
68         if regenerate_node_key:
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("=", "")
75             # Save it
76             node.sync()
77
78         # Generate node configuration file suitable for BootCD
79         file = ""
80
81         file += 'NODE_ID="%d"\n' % node['node_id']
82         file += 'NODE_KEY="%s"\n' % node['key']
83
84         if primary['mac']:
85             file += 'NET_DEVICE="%s"\n' % primary['mac'].lower()
86
87         file += 'IP_METHOD="%s"\n' % primary['method']
88
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 "")
97
98         file += 'HOST_NAME="%s"\n' % host
99         file += 'DOMAIN_NAME="%s"\n' % domain
100
101         for interface in interfaces:
102             if interface['method'] == 'ipmi':
103                 file += 'IPMI_ADDRESS="%s"\n' % interface['ip']
104                 if interface['mac']:
105                     file += 'IPMI_MAC="%s"\n' % interface['mac'].lower()
106                 break
107
108         return file