Workaround to make extensions variable work with MyPLC 5.0
[bootmanager.git] / source / steps / GetAndUpdateNodeDetails.py
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2003 Intel Corporation
4 # All rights reserved.
5 #
6 # Copyright (c) 2004-2006 The Trustees of Princeton University
7 # All rights reserved.
8
9 import sys, traceback
10 import string
11
12 from Exceptions import *
13 import BootAPI
14 import ModelOptions
15
16 def Run( vars, log ):
17     """
18
19     Contact PLC and get the attributes for this node. Also, parse in
20     options from the node model strong.
21
22     Also, update any node network settings at PLC, minus the ip address,
23     so, upload the mac (if node_id was in conf file), gateway, network,
24     broadcast, netmask, dns1/2, and the hostname/domainname.
25
26     Expect the following keys to be set:
27     SKIP_HARDWARE_REQUIREMENT_CHECK     Whether or not we should skip hardware
28                                         requirement checks
29                                         
30     The following keys are set/updated:
31     WAS_NODE_ID_IN_CONF                 Set to 1 if the node id was in the conf file
32     WAS_NODE_KEY_IN_CONF                Set to 1 if the node key was in the conf file
33     BOOT_STATE                          The current node boot state
34     NODE_MODEL                          The user specified model of this node
35     NODE_MODEL_OPTIONS                  The options extracted from the user specified
36                                                 model of this node 
37     SKIP_HARDWARE_REQUIREMENT_CHECK     Whether or not we should skip hardware
38                                                 requirement checks
39     NODE_SESSION                        The session value returned from BootGetNodeDetails
40     INTERFACES                          The network interfaces associated with this node
41     INTERFACE_SETTINGS                  A dictionary of the values of the interface settings
42     
43     Return 1 if able to contact PLC and get node info.
44     Raise a BootManagerException if anything fails.
45     """
46
47     log.write( "\n\nStep: Retrieving details of node from PLC.\n" )
48
49     # make sure we have the variables we need
50     try:
51         SKIP_HARDWARE_REQUIREMENT_CHECK= vars["SKIP_HARDWARE_REQUIREMENT_CHECK"]
52         if SKIP_HARDWARE_REQUIREMENT_CHECK == "":
53             raise ValueError, "SKIP_HARDWARE_REQUIREMENT_CHECK"
54
55         INTERFACE_SETTINGS= vars["INTERFACE_SETTINGS"]
56         if INTERFACE_SETTINGS == "":
57             raise ValueError, "INTERFACE_SETTINGS"
58
59         WAS_NODE_ID_IN_CONF= vars["WAS_NODE_ID_IN_CONF"]
60         if WAS_NODE_ID_IN_CONF == "":
61             raise ValueError, "WAS_NODE_ID_IN_CONF"
62
63         WAS_NODE_KEY_IN_CONF= vars["WAS_NODE_KEY_IN_CONF"]
64         if WAS_NODE_KEY_IN_CONF == "":
65             raise ValueError, "WAS_NODE_KEY_IN_CONF"
66
67     except KeyError, var:
68         raise BootManagerException, "Missing variable in vars: %s\n" % var
69     except ValueError, var:
70         raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
71
72     node_details= BootAPI.call_api_function( vars, "GetNodes", 
73                                              (vars['NODE_ID'], 
74                                               ['boot_state', 'nodegroup_ids', 'interface_ids', 'model', 'site_id']))[0]
75
76     vars['BOOT_STATE']= node_details['boot_state']
77     vars['RUN_LEVEL']= node_details['boot_state']
78     vars['NODE_MODEL']= string.strip(node_details['model'])
79     vars['SITE_ID'] = node_details['site_id'] 
80     log.write( "Successfully retrieved node record.\n" )
81     log.write( "Current boot state: %s\n" % vars['BOOT_STATE'] )
82     log.write( "Node make/model: %s\n" % vars['NODE_MODEL'] )
83     
84     # parse in the model options from the node_model string
85     model= vars['NODE_MODEL']
86     options= ModelOptions.Get(model)
87     vars['NODE_MODEL_OPTIONS']=options
88
89     # Check if we should skip hardware requirement check
90     if options & ModelOptions.MINHW:
91         vars['SKIP_HARDWARE_REQUIREMENT_CHECK']=1
92         log.write( "node model indicates override to hardware requirements.\n" )
93
94     # this contains all the node networks, for now, we are only concerned
95     # in the primary network
96     interfaces= BootAPI.call_api_function( vars, "GetInterfaces", (node_details['interface_ids'],))
97     got_primary= 0
98     for network in interfaces:
99         if network['is_primary'] == 1:
100             log.write( "Primary network as returned from PLC: %s\n" % str(network) )
101             got_primary= 1
102             break
103
104     if not got_primary:
105         raise BootManagerException, "Node did not have a primary network."
106
107     vars['INTERFACES']= interfaces
108     
109     # call getNodeFlavour and store in VARS['node_flavour']
110     try:
111         node_flavour = BootAPI.call_api_function(vars, "GetNodeFlavour", (vars['NODE_ID'], ) )
112         nodefamily = node_flavour['nodefamily']
113         extensions = node_flavour['extensions']
114         plain = node_flavour['plain']
115     except:
116         exc_type, exc_value, exc_traceback = sys.exc_info()
117         lines=traceback.format_exception(exc_type,exc_value,exc_traceback)
118         for line in lines: log.write(line)
119         raise BootManagerException ("Could not call GetNodeFlavour - need PLCAPI-5.2")
120     
121     flavour_keys = [
122             'virt',# 'vs' or 'lxc'
123             'nodefamily',# the basename for downloading nodeimage
124             'extensions',# extensions to be applied on top of the base nodeimage
125             'plain'# false if compressed image, true if not
126             ]
127
128     # MyPLC 5.0 workaround
129     if (vars['extensions']==''):
130         vars['extensions']=[]
131
132     for k in flavour_keys:
133         # Support MyPLC <5.2
134         if (not vars.has_key(k)):
135             vars[k] = node_flavour[k]
136
137     log.write ("NodeFlavour as returned from PLC: %s\n"%node_flavour)
138
139     return 1