bootstrap/template.py: generate Vagrantfile just once
[metze/samba/wip.git] / bootstrap / template.py
1 #!/usr/bin/env python3
2
3 # Copyright (C) Catalyst.Net Ltd 2019
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 """
19 Manage dependencies and bootstrap environments for Samba.
20
21 CLI script to render bootstrap.sh/Dockerfile/Vagrantfile.
22
23 Author: Joe Guo <joeg@catalyst.net.nz>
24 """
25
26 import io
27 import os
28 import logging
29 import argparse
30 from config import DISTS, VAGRANTFILE, OUT
31
32 logging.basicConfig(level='INFO')
33 log = logging.getLogger(__file__)
34
35
36 def render(dists):
37     """Render files for all dists"""
38     for dist, config in dists.items():
39         home = config['home']
40         os.makedirs(home, exist_ok=True)
41         for key in ['bootstrap.sh', 'locale.sh', 'packages.yml', 'Dockerfile']:
42             path = os.path.join(home, key)
43             log.info('%s: render "%s" to %s', dist, key, path)
44             with io.open(path, mode='wt', encoding='utf8') as fp:
45                 fp.write(config[key])
46             if path.endswith('.sh'):
47                 os.chmod(path, 0o755)
48
49     key = 'Vagrantfile'
50     path = os.path.join(OUT, key)
51     log.info('%s: render "%s" to %s', dist, key, path)
52     with io.open(path, mode='wt', encoding='utf8') as fp:
53         fp.write(VAGRANTFILE)
54
55
56 def main():
57     parser = argparse.ArgumentParser(
58         formatter_class=argparse.ArgumentDefaultsHelpFormatter,
59         description=('Render templates with samba dependencies '
60                      'to bootstrap multiple distributions.'))
61
62     parser.add_argument(
63         '-r', '--render', action='store_true', help='Render templates')
64
65     args = parser.parse_args()
66
67     if args.render:
68         render(DISTS)
69     else:
70         parser.print_help()
71
72
73 if __name__ == '__main__':
74     main()