Skip to content

Commit 41b52ff

Browse files
authored
Activate venv when the scripts are started
2 parents c22db81 + 7d324f7 commit 41b52ff

File tree

12 files changed

+175
-29
lines changed

12 files changed

+175
-29
lines changed

nets/3routers/.venv

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

nets/3routers/ospf3r.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
11
#!/usr/bin/python
22

33
import os
4+
5+
# Activate virtual environment if a venv path has been specified in .venv
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+
426
import shutil
527
from mininet.topo import Topo
628
from mininet.node import Host

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: 40 additions & 14 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 if a venv path has been specified in .venv
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,17 @@
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)
3355

3456

3557
class BaseNode(Host):
@@ -107,8 +129,8 @@ def config(self, **kwargs):
107129
BaseNode.config(self, **kwargs)
108130
# Start node managers
109131
if START_NODE_MANAGERS:
110-
self.cmd('python %s --grpc_port %s'
111-
% (NODE_MANAGER_PATH % NODE_MANAGER_GRPC_PORT))
132+
self.cmd('python %s --grpc-port %s &'
133+
% (NODE_MANAGER_PATH, NODE_MANAGER_GRPC_PORT))
112134

113135

114136
# the add_link function creates a link and assigns the interface names
@@ -264,17 +286,21 @@ def parse_arguments():
264286
# Define whether to start node manager on routers or not
265287
START_NODE_MANAGERS = args.start_node_managers
266288
if START_NODE_MANAGERS:
267-
if NODE_MANAGER_PATH:
289+
if NODE_MANAGER_PATH is None:
290+
print('Error: --start-node-managers requires NODE_MANAGER_PATH '
291+
'variable')
292+
print('NODE_MANAGER_PATH variable not set in .env file\n')
293+
exit(-2)
294+
if not os.path.exists(NODE_MANAGER_PATH):
268295
print('Error: --start-node-managers requires NODE_MANAGER_PATH '
269296
'variable')
270-
print('NODE_MANAGER_PATH variable is not set in .env file or '
271-
'the variable points to a non existing folder')
297+
print('NODE_MANAGER_PATH defined in .env file '
298+
'points to a non existing folder\n')
272299
exit(-2)
273-
if NODE_MANAGER_GRPC_PORT:
300+
if NODE_MANAGER_GRPC_PORT is None:
274301
print('Error: --start-node-managers requires '
275302
'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')
303+
print('NODE_MANAGER_GRPC_PORT variable not set in .env file\n')
278304
exit(-2)
279305
# Tell mininet to print useful information
280306
setLogLevel('info')

nets/8r-1c-out-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-out-band-isis/isis8d.py

Lines changed: 40 additions & 14 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 if a venv path has been specified in .venv
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,17 @@
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)
3355

3456

3557
class BaseNode(Host):
@@ -107,8 +129,8 @@ def config(self, **kwargs):
107129
BaseNode.config(self, **kwargs)
108130
# Start node managers
109131
if START_NODE_MANAGERS:
110-
self.cmd('python %s --grpc_port %s'
111-
% (NODE_MANAGER_PATH % NODE_MANAGER_GRPC_PORT))
132+
self.cmd('python %s --grpc-port %s &'
133+
% (NODE_MANAGER_PATH, NODE_MANAGER_GRPC_PORT))
112134

113135

114136
class Switch(OVSBridge):
@@ -313,17 +335,21 @@ def parse_arguments():
313335
# Define whether to start node manager on routers or not
314336
START_NODE_MANAGERS = args.start_node_managers
315337
if START_NODE_MANAGERS:
316-
if NODE_MANAGER_PATH:
338+
if NODE_MANAGER_PATH is None:
339+
print('Error: --start-node-managers requires NODE_MANAGER_PATH '
340+
'variable')
341+
print('NODE_MANAGER_PATH variable not set in .env file\n')
342+
exit(-2)
343+
if not os.path.exists(NODE_MANAGER_PATH):
317344
print('Error: --start-node-managers requires NODE_MANAGER_PATH '
318345
'variable')
319-
print('NODE_MANAGER_PATH variable is not set in .env file or '
320-
'the variable points to a non existing folder')
346+
print('NODE_MANAGER_PATH defined in .env file '
347+
'points to a non existing folder\n')
321348
exit(-2)
322-
if NODE_MANAGER_GRPC_PORT:
349+
if NODE_MANAGER_GRPC_PORT is None:
323350
print('Error: --start-node-managers requires '
324351
'NODE_MANAGER_GRPC_PORT variable')
325-
print('NODE_MANAGER_GRPC_PORT variable is not set in .env file or '
326-
'the variable points to a non existing folder')
352+
print('NODE_MANAGER_GRPC_PORT variable not set in .env file\n')
327353
exit(-2)
328354
# Tell mininet to print useful information
329355
setLogLevel('info')

nets/8r-1c-srv6-pm/.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-srv6-pm/isis8d.py

Lines changed: 23 additions & 1 deletion
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 if a venv path has been specified in .venv
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

nets/8routers-isis-ipv6/.venv

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

nets/8routers-isis-ipv6/isis8d.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
11
#!/usr/bin/python
22

33
import os
4+
5+
# Activate virtual environment if a venv path has been specified in .venv
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+
426
import shutil
527
from mininet.topo import Topo
628
from mininet.node import Host

0 commit comments

Comments
 (0)