monitor shipyard deploy to completion
[yaml_builds.git] / scripts / jcopy.py
1 #!/usr/bin/python
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 #  jcopy.py - Copy a file or files to a target directory, making
19 #    substitutions as needed from the values contained in a YAML file.
20 #    compatibile with python and python3
21 #
22 #  usage: jcopy.py <yaml_input> <template_dir_or_file> <out_dir>
23
24 import os.path
25 import jinja2
26 import sys
27 import yaml
28 import crypt
29 import netaddr
30 from base64 import b64encode
31
32 def cidr_netmask(value):
33   if '/' in str(value):
34     v = netaddr.IPNetwork(value)
35     result = v.netmask
36   else:
37     result = "ERROR"
38   return result
39
40 def cidr_subnet(value):
41   if '/' in str(value):
42     v = netaddr.IPNetwork(value)
43     result = v.network
44   else:
45     result = "ERROR"
46   return result
47
48 def crypt_sha512(value):
49   if not '$6$' in str(value):
50     if sys.hexversion < 0x3000000:
51       result = crypt.crypt(value, "$6$"+b64encode(os.urandom(16)))
52     else:
53       result = crypt.crypt(value, crypt.mksalt(crypt.METHOD_SHA512))
54   else:
55     result = value
56   return result
57
58 def usage(msg=None):
59   if not msg is None:
60     print(msg)
61   print( 'usage: jcopy.py <yaml> <template_dir_or_file> <out_dir>' )
62   sys.exit(1)
63
64 def expand_template( t, output_fname, fill=60 ):
65   print( '### {0: <{fill}} ==> {1}'.format(t.filename, output_fname, fill=fill) )
66   if not os.path.exists(os.path.dirname(output_fname)):
67     os.makedirs(os.path.dirname(output_fname))
68   t.stream(yaml=siteyaml).dump(output_fname)
69
70 if len(sys.argv) != 4:
71   usage('incorrect number of arguments')
72
73 yaml_input=sys.argv[1]
74 j2in_name=sys.argv[2]
75 yaml_out=sys.argv[3]
76
77 if not os.path.isfile(yaml_input):
78   usage('input yaml file {} does not exist'.format(yaml_input))
79
80 with open(yaml_input) as f:
81   siteyaml = yaml.safe_load(f)
82
83 if os.path.isfile(j2in_name):
84   j2_env = jinja2.Environment(loader=jinja2.FileSystemLoader(os.path.dirname(j2in_name)), trim_blocks=True, lstrip_blocks=True, keep_trailing_newline=True, undefined=jinja2.make_logging_undefined())
85   j2_env.filters['cidr_netmask'] = cidr_netmask
86   j2_env.filters['cidr_subnet'] = cidr_subnet
87   j2_env.filters['crypt_sha512'] = crypt_sha512
88   expand_template(j2_env.get_template(name=os.path.basename(j2in_name)),yaml_out,len(j2in_name))
89 else:
90   j2_env = jinja2.Environment(loader=jinja2.FileSystemLoader(j2in_name), trim_blocks=True, lstrip_blocks=True, keep_trailing_newline=True, undefined=jinja2.make_logging_undefined())
91   j2_env.filters['cidr_netmask'] = cidr_netmask
92   j2_env.filters['cidr_subnet'] = cidr_subnet
93   j2_env.filters['crypt_sha512'] = crypt_sha512
94   templates=j2_env.list_templates(extensions=('j2'))
95   fill=len(max(templates,key=len))+len(j2in_name)
96   for f in templates:
97     expand_template(j2_env.get_template(name=f), yaml_out+'/'+f.replace('.j2','.yaml'), fill)
98   print('%d files processed.' % len(templates))
99
100 sys.exit(0)
101
102 # sudo apt-get install python-jinja2 python-yaml
103 # sudo apt-get install python3-jinja2 python3-yaml