bug fixes
[plcapi.git] / PLC / Methods / GetNodeFlavour.py
1 from PLC.Method import Method
2 from PLC.Auth import Auth
3 from PLC.Faults import *
4 from PLC.Parameter import *
5 from PLC.Nodes import Node, Nodes
6
7 class GetNodeFlavour(Method):
8     """
9     Returns detailed information on a given node's flavour, i.e. its
10     base installation.
11
12     This depends on the global PLC settings in the PLC_FLAVOUR area,
13     optionnally overridden by any of the following tags if set on that node:
14
15     'arch', 'pldistro', 'fcdistro',
16     'deployment', 'extensions',
17     """
18
19     roles = ['admin', 'user', 'node']
20
21     accepts = [
22         Auth(),
23         Mixed(Node.fields['node_id'],
24               Node.fields['hostname']),
25         ]
26
27     returns = {
28         'nodefamily' : Parameter (str, "the nodefamily this node should be based upon"),
29         'fcdistro': Parameter (str, "the fcdistro this node should be based upon"),
30         'extensions' : [ Parameter (str, "extensions to add to the base install") ],
31         'plain' : Parameter (bool, "use plain bootstrapfs image if set (for tests)" ) ,
32         }
33
34
35     ########## nodefamily
36     def nodefamily (self, auth, node_id, fcdistro, arch):
37
38         # the deployment tag, if set, wins
39         # xxx Thierry: this probably is wrong; we need fcdistro to be set anyway
40         # for generating the proper yum config....
41         deployment = GetNodeDeployment (self.api,self.caller).call(auth,node_id)
42         if deployment: return deployment
43
44         pldistro = GetNodePldistro (self.api,self.caller).call(auth, node_id)
45         if not pldistro:
46             pldistro = self.api.config.PLC_FLAVOUR_NODE_PLDISTRO
47             SetNodePldistro(self.api,self.caller).call(auth,node_id,pldistro)
48
49         # xxx would make sense to check the corresponding bootstrapfs is available
50         return "%s-%s-%s"%(pldistro,fcdistro,arch)
51
52     def extensions (self, auth, node_id, fcdistro, arch):
53         try:
54             return [ "%s-%s-%s"%(e,fcdistro,arch) for e in GetNodeExtensions(self.api,self.caller).call(auth,node_id).split() ]
55         except:
56             return []
57
58     def plain (self, auth, node_id):
59         return not not GetNodePlainBootstrapfs(self.api,self.caller).call(auth,node_id)
60
61     def call(self, auth, node_id_or_name):
62         # Get node information
63         nodes = Nodes(self.api, [node_id_or_name])
64         if not nodes:
65             raise PLCInvalidArgument, "No such node %r"%node_id_or_name
66         node = nodes[0]
67         node_id = node['node_id']
68
69         arch = GetNodeArch (self.api,self.caller).call(auth,node_id)
70         # if not set, use the global default and tag the node, in case the global default changes later on
71         if not arch:
72             arch = self.api.config.PLC_FLAVOUR_NODE_ARCH
73             SetNodeArch (self.api,self.caller).call(auth,node_id,arch)
74
75         fcdistro = GetNodeFcdistro (self.api,self.caller).call(auth, node_id)
76         if not fcdistro:
77             fcdistro = self.api.config.PLC_FLAVOUR_NODE_FCDISTRO
78             SetNodeFcdistro (self.api,self.caller).call (auth, node_id, fcdistro)
79
80         # xxx could use some sanity checking, and could provide fallbacks
81         return { 'nodefamily' : self.nodefamily(auth,node_id, fcdistro, arch),
82                  'fcdistro' : fcdistro,
83                  'extensions' : self.extensions(auth,node_id, fcdistro, arch),
84                  'plain' : self.plain(auth,node_id),
85                  }