-
Notifications
You must be signed in to change notification settings - Fork 6
/
yans.py
71 lines (57 loc) · 1.89 KB
/
yans.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''yans
Yet Another Network Simulator
Usage:
yans [-V] [-t --topo=<topo_path>] (up|stop|destroy)
yans [-V] [-t --topo=<topo_path>] console <node_name>
yans -h | --help
yans --version
Options:
-h --help Show this screen.
--version Show version.
-t --topo=<topo_path> Network topology YAML [default: ./topo.yaml].
-V --verbose Verbose mode
'''
from __future__ import unicode_literals, print_function
from docopt import docopt
import logging
import sys
from docker_command import destroy_links, create_nodes, create_links, ensure_docker_machine, destroy_nodes, bind_interface, attach_node
from topology import Topology, TopologySpecError
__version__ = "0.1.0"
__author__ = "Kenneth Jiang"
__license__ = "MIT"
def main():
'''Main entry point for the yans CLI.'''
args = docopt(__doc__, version=__version__)
ensure_docker_machine()
if args['--verbose']:
logging.getLogger().setLevel(logging.DEBUG)
topo_file = args['--topo']
try:
topo = Topology(topo_file)
except TopologySpecError as err:
sys.exit(err)
if args['up']:
create_links(topo.links)
create_nodes(topo.nodes)
for link in topo.links:
for interface in link.interfaces:
bind_interface(interface)
topo.draw()
print('To log into each node:')
for node in topo.nodes:
print('`$ yans -t ' + topo_file + ' console ' + node.name + '`')
if args['destroy']:
destroy_nodes(topo.nodes)
destroy_links(topo.links)
if args['console']:
node_name = args['<node_name>']
node = topo.node_by_name(node_name)
if node:
attach_node(node)
else:
sys.exit('Node named "' + node_name + '" is not found in ' + topo_file)
if __name__ == '__main__':
main()