add hpgen10 support and fix redfish path
[yaml_builds.git] / scripts / update_bios_settings.py
1 opyright (c) 2018 AT&T Intellectual Property. All rights reserved.        #
2 #                                                                            #
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may    #
4 # not use this file except in compliance with the License.                   #
5 #                                                                            #
6 # You may obtain a copy of the License at                                    #
7 #       http://www.apache.org/licenses/LICENSE-2.0                           #
8 #                                                                            #
9 # Unless required by applicable law or agreed to in writing, software        #
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT  #
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.           #
12 # See the License for the specific language governing permissions and        #
13 # limitations under the License.                                             #
14 ##############################################################################
15
16 import os
17 import sys
18 import yaml
19 import jinja2
20 import subprocess
21
22 def create_node_rcfile(nodes, defaults, j2template, rcfile_suffix):
23   env = jinja2.Environment()
24   env.trim_blocks = True
25   env.lstrip_blocks = True
26   
27   with open(j2template) as fd:
28     template = env.from_string(fd.read())
29   
30   if type(nodes) is list:
31     for node in nodes:
32       newnode = dict( defaults.items() + node.items() )
33       data = template.render(yaml=newnode)
34       rcfile = "server-config/"+newnode['name']+rcfile_suffix
35       print rcfile
36       if os.path.exists(rcfile):
37         print 'warning: rc file {} exists. maynot be new node. overwriting file'.format(rcfile)
38         #continue
39       if not os.path.exists(os.path.dirname(rcfile)):
40         os.makedirs(os.path.dirname(rcfile),0600)
41       fd2 = open(rcfile,'w')
42       fd2.write(data)
43       fd2.write("\n")
44       fd2.close()
45       print '{0} -> {1}'.format(j2template, rcfile)
46       command = ""
47       if newnode['vendor'] == "DELL":
48         command = '/opt/akraino/redfish/apply_dellxml.sh --rc {0} --template {1} --no-confirm'.format(rcfile, newnode["bios_template"])
49       if newnode['vendor'] == "HP" or newnode['vendor'] == "HPE":
50         command = '/opt/akraino/redfish/apply_hpejson.sh --rc {0} --template {1} --no-confirm'.format(rcfile, newnode["bios_template"])
51       if command:
52         print 'command: {0}'.format(command)
53         os.system(command)
54
55 ### MAIN ###
56 if len(sys.argv) != 2:
57   print 'usage: update_bios_settings.py <yaml>'
58   sys.exit(1)
59
60 with open(sys.argv[1]) as f:
61   siteyaml = yaml.safe_load(f)
62
63 # create set of defaults based on top level ipmi_admin and hardware key/value pairs
64 defaults = dict( siteyaml["ipmi_admin"].items() + siteyaml["hardware"].items() )
65
66 # add keys for backward compatibility
67 defaults = dict( [('oob_user',siteyaml['ipmi_admin']['username'])]       + defaults.items())
68 defaults = dict( [('oob_password',siteyaml['ipmi_admin']['password'])]   + defaults.items())
69 defaults = dict( [('oem',siteyaml['hardware']['vendor'])]                + defaults.items())
70
71 print 'Using defaults:'
72 for line in yaml.dump(defaults,default_flow_style=False).split('\n'):
73     print '    {}'.format(line)
74
75 if 'masters' in siteyaml:
76     create_node_rcfile(siteyaml["masters"], defaults, "tools/j2/serverrc_raid.j2", "rc.raid")
77
78 if 'workers' in siteyaml:
79     create_node_rcfile(siteyaml["workers"], defaults, "tools/j2/serverrc_raid.j2", "rc.raid")
80