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