This commit was manufactured by cvs2svn to create branch
[plcapi.git] / PLC / Methods / AdmGenerateNodeConfFile.py
1 import random
2 import base64
3
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
10
11 class AdmGenerateNodeConfFile(Method):
12     """
13     Deprecated. Functionality can be implemented with GetNodes,
14     GetNodeNetworks, and UpdateNode.
15
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
19     files.
20
21     Non-admins can only generate files for nodes at their sites.
22
23     Returns the contents of the file if successful, faults otherwise.
24     """
25
26     status = "deprecated"
27
28     roles = ['admin', 'pi', 'tech']
29
30     accepts = [
31         Auth(),
32         Mixed(Node.fields['node_id'],
33               Node.fields['hostname'])
34         ]
35
36     returns = Parameter(str, "Node configuration file")
37
38     def call(self, auth, node_id_or_hostname):
39         # Get node information
40         nodes = Nodes(self.api, [node_id_or_hostname])
41         if not nodes:
42             raise PLCInvalidArgument, "No such node"
43         node = nodes[0]
44
45         if node['peer_id'] is not None:
46             raise PLCInvalidArgument, "Not a local node"
47
48         # If we are not an admin, make sure that the caller is a
49         # member of the site at which the node is located.
50         if 'admin' not in self.caller['roles']:
51             if node['site_id'] not in self.caller['site_ids']:
52                 raise PLCPermissionDenied, "Not allowed to generate a configuration file for that node"
53
54         # Get node networks for this node
55         primary = None
56         nodenetworks = NodeNetworks(self.api, node['nodenetwork_ids'])
57         for nodenetwork in nodenetworks:
58             if nodenetwork['is_primary']:
59                 primary = nodenetwork
60                 break
61         if primary is None:
62             raise PLCInvalidArgument, "No primary network configured"
63
64         # Split hostname into host and domain parts
65         parts = node['hostname'].split(".", 1)
66         if len(parts) < 2:
67             raise PLCInvalidArgument, "Node hostname is invalid"
68         host = parts[0]
69         domain = parts[1]
70
71         # Generate 32 random bytes
72         bytes = random.sample(xrange(0, 256), 32)
73         # Base64 encode their string representation
74         node['key'] = base64.b64encode("".join(map(chr, bytes)))
75         # XXX Boot Manager cannot handle = in the key
76         node['key'] = node['key'].replace("=", "")
77         # Save it
78         node.sync()
79
80         # Generate node configuration file suitable for BootCD
81         file = ""
82
83         file += 'NODE_ID="%d"\n' % node['node_id']
84         file += 'NODE_KEY="%s"\n' % node['key']
85
86         if primary['mac']:
87             file += 'NET_DEVICE="%s"\n' % primary['mac'].lower()
88
89         file += 'IP_METHOD="%s"\n' % primary['method']
90
91         if primary['method'] == 'static':
92             file += 'IP_ADDRESS="%s"\n' % primary['ip']
93             file += 'IP_GATEWAY="%s"\n' % primary['gateway']
94             file += 'IP_NETMASK="%s"\n' % primary['netmask']
95             file += 'IP_NETADDR="%s"\n' % primary['network']
96             file += 'IP_BROADCASTADDR="%s"\n' % primary['broadcast']
97             file += 'IP_DNS1="%s"\n' % primary['dns1']
98             file += 'IP_DNS2="%s"\n' % (primary['dns2'] or "")
99
100         file += 'HOST_NAME="%s"\n' % host
101         file += 'DOMAIN_NAME="%s"\n' % domain
102
103         for nodenetwork in nodenetworks:
104             if nodenetwork['method'] == 'ipmi':
105                 file += 'IPMI_ADDRESS="%s"\n' % nodenetwork['ip']
106                 if nodenetwork['mac']:
107                     file += 'IPMI_MAC="%s"\n' % nodenetwork['mac'].lower()
108                 break
109
110         return file