make mac optional in template and fix yaml checks
[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 with open(sys.argv[1]) as f:
24   yaml = yaml.safe_load(f)
25
26 def create_rc_genesis(source, target_suffix):
27   env = jinja2.Environment()
28   env.trim_blocks = True
29   env.lstrip_blocks = True
30   
31   with open(source) as fd:
32     template = env.from_string(fd.read())
33   data = template.render(yaml=yaml)
34   target_file = yaml['genesis']['name']+target_suffix
35   fd2 = open(target_file,'w')
36   fd2.write(data)
37   fd2.write("\n")
38   fd2.close()
39   print '{0} -> {1}'.format(source, target_file)
40
41 def create_rc_masters(source, target_suffix):
42   env = jinja2.Environment()
43   env.trim_blocks = True
44   env.lstrip_blocks = True
45
46   if 'masters' in yaml and type(yaml['masters']) is list:
47     for master in yaml['masters']:
48       with open(source) as fd:
49         template = env.from_string(fd.read())
50       data = template.render(yaml=master)
51       target_file = "server-config/"+master['name']+target_suffix
52       print target_file
53       if os.path.exists(target_file):
54         print 'rc file exists maynot be new node'
55         continue
56       if not os.path.exists(os.path.dirname(target_file)):
57         os.makedirs(os.path.dirname(target_file))
58       fd2 = open(target_file,'w')
59       fd2.write(data)
60       fd2.write("\n")
61       fd2.close()
62       print '{0} -> {1}'.format(source, target_file)
63       command = '/opt/akraino/tools/apply_dellxml.sh --rc {0} --template dell_r740_g14_uefi_base.xml.template --no-confirm'.format(target_file)
64       print 'command: {0}'.format(command)
65       os.system(command)
66
67 def create_rc_workers(source, target_suffix):
68   env = jinja2.Environment()
69   env.trim_blocks = True
70   env.lstrip_blocks = True
71
72   if 'workers' in yaml and type(yaml['workers']) is list:
73     for worker in yaml['workers']:
74       with open(source) as fd:
75         template = env.from_string(fd.read())
76       data = template.render(yaml=worker)
77       target_file = "server-config/"+worker['name']+target_suffix
78       print target_file
79       if os.path.exists(target_file):
80         print 'rc file exists maynot be new node'
81         continue
82       if not os.path.exists(os.path.dirname(target_file)):
83         os.makedirs(os.path.dirname(target_file))
84       fd2 = open(target_file,'w')
85       fd2.write(data)
86       fd2.write("\n")
87       fd2.close()
88       print '{0} -> {1}'.format(source, target_file)
89       command = '/opt/akraino/tools/apply_dellxml.sh --rc {0} --template dell_r740_g14_uefi_base.xml.template --no-confirm'.format(target_file)
90       print 'command: {0}'.format(command)
91       os.system(command)
92
93 if len(sys.argv) != 2:
94   print 'usage: update_bios_settings.py <yaml>'
95   sys.exit(1)
96
97 #create_rc_genesis("tools/j2/serverrc.j2", "rc")
98 create_rc_masters("tools/j2/serverrc_raid.j2", "rc.raid")
99 create_rc_workers("tools/j2/serverrc_raid.j2", "rc.raid")
100