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