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