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