-
Notifications
You must be signed in to change notification settings - Fork 9
/
__init__.py
190 lines (156 loc) · 6.41 KB
/
__init__.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
"""
MIT License
Copyright (c) 2021 Youssouf Drif
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import logging
import sys
import codecs
from typing import Dict
from cerberus import Validator
from ruamel import yaml
from ..model import Networker, Repository, Service
from ..utils.utils import HexInt, representer
class Scenario(object):
"""Generic Scenario Interface"""
validation_schema = None
scenario_type = "generic"
def __init__(self, definition: Dict) -> None:
self.name = definition['name']
self.definition = definition
self.networker = Networker()
self.repository = Repository()
def set_path(self, results_folder: str, containers_folder: str, configurations_folder: str, output_configuration_folder: str, scenario_folder: str):
"""
Set the path for the output folders
"""
self.repository.results_folder = results_folder
self.repository.containers_folder = containers_folder
self.repository.configurations_folder = configurations_folder
self.repository.output_configuration_folder = output_configuration_folder
self.repository.scenario_folder = scenario_folder
def prepare_scenario(self) -> None:
"""
Prepare all the requirements prior to the generation of the scenario topology
"""
logging.info(
f"[{self.name}] prepare_scenario function undefined, I QUIT !")
sys.exit(1)
def generate_networks(self) -> None:
"""
Generate the networks of the testbed
"""
logging.info(
f"[{self.name}] generate_networks function undefined, I QUIT !")
sys.exit(1)
def generate_topology(self) -> None:
"""
Create all the services and generate the network topology of the scenario
"""
logging.info(
f"[{self.name}] generate_topology function undefined, I QUIT !")
sys.exit(1)
def configure_services(self) -> None:
"""
Configure all the services
"""
for service in sorted(self.repository.services):
self.repository.services.get(service).configure(self.repository)
def configure_compose(self) -> None:
"""
Configure the services docker compose part
"""
for service in sorted(self.repository.services):
self.repository.services.get(
service).configure_compose(self.repository)
def configure_entrypoint(self) -> None:
"""
Configure the services entrypoint
"""
for service in sorted(self.repository.services):
self.repository.services.get(
service).configure_entrypoint(self.repository)
def write_configuration(self) -> None:
"""
Write the configuration files of each service
"""
for service in sorted(self.repository.services):
s = self.repository.services.get(service)
s.write_configuration(self.repository.output_configuration_folder)
def write_entrypoint(self) -> None:
"""
Write entrypoints for each service
"""
for service in sorted(self.repository.services):
s: Service = self.repository.services.get(service)
s.write_entrypoint(self.repository)
def write_compose(self) -> None:
"""
Write docker-compose.yaml file
"""
output = {
"version": "3.9",
"services": {},
"networks": {},
"volumes": {"dbdata": None}
}
for service in sorted(self.repository.services):
if self.repository.services[service].compose != None:
output["services"][service] = self.repository.services[service].compose.generate()
network_index: int = 0
for network in sorted(self.networker.networks_name):
output["networks"][network] = {
"name": network,
"driver": "bridge",
"ipam": {
"driver": "default",
"config": [
{
"subnet": str(
self.networker.networks_name.get(network))
}
]
},
"driver_opts":
{
"com.docker.network.bridge.name": f"network-{network_index}"
}
}
network_index += 1
path: str = f"{self.repository.scenario_folder}/{self.name}/docker-compose.yaml"
yam = yaml.YAML()
yam.indent(sequence=4, offset=2)
with codecs.open(path, "w", encoding="utf-8") as compose_file:
yam.dump(output, compose_file)
def run(self) -> None:
logging.info(f"[{self.name}] run function undefined, I QUIT !")
sys.exit(1)
@classmethod
def sanitize(cls, definition: Dict, name: str, scenario: object) -> Dict:
"""
Verify if the scenario definition is valid and sanitize accoding to the cerberus schema
"""
if cls.validation_schema == None:
definition['name'] = name
return Scenario(definition)
validator = Validator(cls.validation_schema)
if not validator.validate(definition, cls.validation_schema):
logging.error("Error in the scenario definition")
logging.error(validator.errors)
sys.exit(1)
definition['name'] = name
return scenario(definition)