-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathcommon.py
152 lines (115 loc) · 4.06 KB
/
common.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
# Copyright 2018 ETH Zurich, Anapaya Systems
#
# 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.
# Stdlib
import os
import subprocess
import sys
# External packages
from external.ipaddress import ip_address
# SCION
from lib.packet.scion_addr import ISD_AS
COMMON_DIR = 'endhost'
SCION_SERVICE_NAMES = (
"BeaconService",
"CertificateService",
"BorderRouters",
"PathService",
"DiscoveryService",
)
DEFAULT_DOCKER_ZK_PORT = 2182
DEFAULT_ZK_PORT = 2181
class ArgsBase:
def __init__(self, args):
for k, v in vars(args).items():
setattr(self, k, v)
class ArgsTopoConfig(ArgsBase):
def __init__(self, args, topo_config):
"""
:param object args: Contains the passed command line arguments as named attributes.
:param dict topo_config: The parsed topology config.
"""
super().__init__(args)
self.config = topo_config
class ArgsTopoDicts(ArgsBase):
def __init__(self, args, topo_dicts, port_gen=None):
"""
:param object args: Contains the passed command line arguments as named attributes.
:param dict topo_dicts: The generated topo dicts from TopoGenerator.
:param PortGenerator port_gen: The port generator
"""
super().__init__(args)
self.topo_dicts = topo_dicts
self.port_gen = port_gen
class TopoID(ISD_AS):
def ISD(self):
return "ISD%s" % self.isd_str()
def AS(self):
return "AS%s" % self.as_str()
def AS_file(self):
return "AS%s" % self.as_file_fmt()
def file_fmt(self):
return "%s-%s" % (self.isd_str(), self.as_file_fmt())
def base_dir(self, out_dir):
return os.path.join(out_dir, self.ISD(), self.AS_file())
def __lt__(self, other):
return str(self) < str(other)
def __repr__(self):
return "<TopoID: %s>" % self
def _prom_addr_br(br_id, br_ele, port_gen):
"""Get the prometheus address for a border router"""
pub = _get_pub(br_ele['InternalAddrs'])
return "[%s]:%s" % (pub['PublicOverlay']['Addr'].ip, port_gen.register(br_id + "prom"))
def _prom_addr_infra(infra_id, infra_ele, port_gen):
"""Get the prometheus address for an infrastructure element."""
pub = _get_pub(infra_ele['Addrs'])
return "[%s]:%s" % (pub['Public']['Addr'].ip, port_gen.register(infra_id + "prom"))
def _get_pub(topo_addr):
pub = topo_addr.get('IPv6')
if pub is not None:
return pub
return topo_addr['IPv4']
def _get_pub_ip(topo_addr):
return _get_pub(topo_addr)["Public"]["Addr"].ip
def _get_l4_port(topo_addr):
return _get_pub(topo_addr)["Public"]["L4Port"]
def _srv_iter(topo_dicts, out_dir, common=False):
for topo_id, as_topo in topo_dicts.items():
base = topo_id.base_dir(out_dir)
for service in SCION_SERVICE_NAMES:
for elem in as_topo[service]:
yield topo_id, as_topo, os.path.join(base, elem)
if common:
yield topo_id, as_topo, os.path.join(base, COMMON_DIR)
def _gen_zk_entry(addr, port, in_docker, docker):
if in_docker:
port = DEFAULT_DOCKER_ZK_PORT
if not port:
port = DEFAULT_ZK_PORT
if in_docker:
addr = os.getenv('DOCKER0', None)
if not addr:
print('DOCKER0 env variable required! Exiting!')
sys.exit(1)
elif docker:
addr = _docker_ip()
elif not addr:
addr = _docker_ip()
else:
addr = str(ip_address(addr))
return {
'Addr': addr,
'L4Port': port
}
def _docker_ip():
return subprocess.check_output(['tools/docker-ip']).decode("utf-8").strip()