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