-
Notifications
You must be signed in to change notification settings - Fork 160
/
common.py
163 lines (123 loc) · 4.52 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
153
154
155
156
157
158
159
160
161
162
163
# 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
# SCION
from lib.packet.scion_addr import ISD_AS
from topology.net import AddressProxy
COMMON_DIR = 'endhost'
SCION_SERVICE_NAMES = (
"BeaconService",
"CertificateService",
"BorderRouters",
"PathService",
"DiscoveryService",
)
DOCKER_USR_VOL = ['/etc/passwd:/etc/passwd:ro', '/etc/group:/etc/group:ro']
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 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 docker_image(args, image):
if args.docker_registry:
image = '%s/%s' % (args.docker_registry, image)
else:
image = 'scion_%s' % image
if args.image_tag:
image = '%s:%s' % (image, args.image_tag)
return image
def docker_host(in_docker, docker, addr=None):
if in_docker:
# If in-docker we need to know the DOCKER0 IP
addr = os.getenv('DOCKER0', None)
if not addr:
print('DOCKER0 env variable required! Exiting!')
sys.exit(1)
elif docker or not addr:
# Using docker topology or there is no default addr,
# we directly get the DOCKER0 IP
addr = docker_ip()
return addr
def docker_ip():
return subprocess.check_output(['tools/docker-ip']).decode("utf-8").strip()
def remote_nets(networks, topo_id):
rem_nets = []
for key in networks:
if 'sig' in key and topo_id.file_fmt() not in key:
rem_nets.append(str(networks[key][0]['net']))
return ','.join(rem_nets)
def sciond_name(topo_id):
return 'sd%s' % topo_id.file_fmt()
def sciond_svc_name(topo_id):
return 'scion_%s' % sciond_name(topo_id)
def json_default(o):
if isinstance(o, AddressProxy):
return str(o.ip)
raise TypeError