This repository has been archived by the owner on Feb 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdocker-generator.py
71 lines (59 loc) · 2.38 KB
/
docker-generator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/python
# coding: utf-8 -*-
#
# Copyright 2021 Arista Networks - Thomas Grimonet
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http: //www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an 'AS IS' BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from __future__ import (absolute_import, division,
print_function, unicode_literals)
import os
import sys
import yaml
import jinja2
class DockerTemplate():
def __init__(self, config_file, docker_template_path):
with open(config_file) as file:
self.config = yaml.load(file, Loader=yaml.FullLoader)
# Load template file
templateLoader = jinja2.FileSystemLoader(searchpath="./")
templateEnv = jinja2.Environment(loader=templateLoader)
self.template = templateEnv.get_template(docker_template_path)
def get_versions(self):
versions = list()
if 'versions' in self.config:
for v in self.config['versions']:
versions.append(v)
return versions
def build_folders(self):
for version in self.get_versions():
print(" - version: {}".format(version))
self.create_output_folder(foldername=str(version))
def create_output_folder(self, foldername):
if not os.path.exists("./" + foldername):
os.makedirs("./" + foldername)
def render(self, docker_file='Dockerfile'):
for version in self.get_versions():
self.__render_j2(destination_folder=str(version), version=version)
def __render_j2(self, destination_folder, version, filename='Dockerfile'):
dockerfile = self.template.render(version=version)
# to save the results
with open("./"+destination_folder + "/" + filename, "w") as fh:
fh.write(dockerfile)
if __name__ == '__main__':
print("Init")
avd_base = DockerTemplate(config_file='config.yml', docker_template_path='_template/Dockerfile.j2')
avd_base.build_folders()
avd_base.render()
print("Done")
sys.exit(0)