Skip to content

Commit e3a04fc

Browse files
committed
Set virtual environment programmatically in 8r-1c-in-band-isis;
Minor fixes
1 parent 410bd1c commit e3a04fc

File tree

2 files changed

+43
-13
lines changed

2 files changed

+43
-13
lines changed

nets/8r-1c-in-band-isis/.venv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/root/.mininet-venv

nets/8r-1c-in-band-isis/isis8d.py

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,29 @@
11
#!/usr/bin/python
22

3-
from argparse import ArgumentParser
43
import os
4+
5+
# Activate virtual environment
6+
# This must be executed only if this file has been executed as a
7+
# script (instead of a module)
8+
if __name__ == '__main__':
9+
# Check if .venv file exists
10+
if os.path.exists('.venv'):
11+
with open('.venv', 'r') as venv_file:
12+
# Get virtualenv path from .venv file
13+
venv_path = venv_file.read()
14+
# Get path of the activation script
15+
venv_path = os.path.join(venv_path, 'bin/activate_this.py')
16+
if not os.path.exists(venv_path):
17+
print('Virtual environment path specified in .venv '
18+
'points to an invalid path\n')
19+
exit(-2)
20+
with open(venv_path) as f:
21+
# Read the activation script
22+
code = compile(f.read(), venv_path, 'exec')
23+
# Execute the activation script to activate the venv
24+
exec(code, {'__file__': venv_path})
25+
26+
from argparse import ArgumentParser
527
import shutil
628
from dotenv import load_dotenv
729
from mininet.topo import Topo
@@ -19,17 +41,20 @@
1941
PRIVDIR = '/var/priv'
2042

2143
START_NODE_MANAGERS = False
22-
NODE_MANAGER_PATH = ''
2344

2445
# Load environment variables from .env file
2546
load_dotenv()
2647

2748
# Get node manager path
28-
NODE_MANAGER_PATH = os.getenv('NODE_MANAGER_PATH')
29-
NODE_MANAGER_PATH.join(NODE_MANAGER_PATH, 'srv6_manager.py')
49+
NODE_MANAGER_PATH = os.getenv('NODE_MANAGER_PATH', None)
50+
if NODE_MANAGER_PATH is not None:
51+
NODE_MANAGER_PATH = os.path.join(NODE_MANAGER_PATH,
52+
'srv6_manager.py')
3053
# Get gRPC server port
31-
NODE_MANAGER_GRPC_PORT = os.getenv('NODE_MANAGER_GRPC_PORT')
32-
NODE_MANAGER_GRPC_PORT.join(NODE_MANAGER_GRPC_PORT, 'srv6_manager.py')
54+
NODE_MANAGER_GRPC_PORT = os.getenv('NODE_MANAGER_GRPC_PORT', None)
55+
if NODE_MANAGER_GRPC_PORT:
56+
NODE_MANAGER_GRPC_PORT = os.path.join(NODE_MANAGER_GRPC_PORT,
57+
'srv6_manager.py')
3358

3459

3560
class BaseNode(Host):
@@ -108,7 +133,7 @@ def config(self, **kwargs):
108133
# Start node managers
109134
if START_NODE_MANAGERS:
110135
self.cmd('python %s --grpc_port %s'
111-
% (NODE_MANAGER_PATH % NODE_MANAGER_GRPC_PORT))
136+
% (NODE_MANAGER_PATH, NODE_MANAGER_GRPC_PORT))
112137

113138

114139
# the add_link function creates a link and assigns the interface names
@@ -264,17 +289,21 @@ def parse_arguments():
264289
# Define whether to start node manager on routers or not
265290
START_NODE_MANAGERS = args.start_node_managers
266291
if START_NODE_MANAGERS:
267-
if NODE_MANAGER_PATH:
292+
if NODE_MANAGER_PATH is None:
293+
print('Error: --start-node-managers requires NODE_MANAGER_PATH '
294+
'variable')
295+
print('NODE_MANAGER_PATH variable not set in .env file\n')
296+
exit(-2)
297+
if not os.path.exists(NODE_MANAGER_PATH):
268298
print('Error: --start-node-managers requires NODE_MANAGER_PATH '
269299
'variable')
270-
print('NODE_MANAGER_PATH variable is not set in .env file or '
271-
'the variable points to a non existing folder')
300+
print('NODE_MANAGER_PATH defined in .env file '
301+
'points to a non existing folder\n')
272302
exit(-2)
273-
if NODE_MANAGER_GRPC_PORT:
303+
if NODE_MANAGER_GRPC_PORT is None:
274304
print('Error: --start-node-managers requires '
275305
'NODE_MANAGER_GRPC_PORT variable')
276-
print('NODE_MANAGER_GRPC_PORT variable is not set in .env file or '
277-
'the variable points to a non existing folder')
306+
print('NODE_MANAGER_GRPC_PORT variable not set in .env file\n')
278307
exit(-2)
279308
# Tell mininet to print useful information
280309
setLogLevel('info')

0 commit comments

Comments
 (0)