core-extra/daemon/examples/python/distributed_ptp.py

68 lines
1.9 KiB
Python
Raw Normal View History

"""
Example for scripting a standalone distributed peer to peer session that does not
interact with the GUI.
"""
import argparse
2019-10-09 05:17:15 +01:00
import logging
from core.emulator.coreemu import CoreEmu
from core.emulator.emudata import IpPrefixes, NodeOptions
from core.emulator.enumerations import EventTypes
def parse(name):
parser = argparse.ArgumentParser(description=f"Run {name} example")
parser.add_argument(
"-a",
"--address",
help="local address that distributed servers will use for gre tunneling",
)
parser.add_argument(
"-s", "--server", help="distributed server to use for creating nodes"
)
options = parser.parse_args()
return options
def main(args):
2019-10-09 05:17:15 +01:00
# ip generator for example
prefixes = IpPrefixes(ip4_prefix="10.83.0.0/16")
# create emulator instance for creating sessions and utility methods
coreemu = CoreEmu({"distributed_address": args.address})
2019-10-09 05:17:15 +01:00
session = coreemu.create_session()
# initialize distributed
server_name = "core2"
session.distributed.add_server(server_name, args.server)
2019-10-09 05:17:15 +01:00
# must be in configuration state for nodes to start, when using "node_add" below
session.set_state(EventTypes.CONFIGURATION_STATE)
# create local node, switch, and remote nodes
options = NodeOptions()
node_one = session.add_node(options=options)
options.server = server_name
node_two = session.add_node(options=options)
2019-10-09 05:17:15 +01:00
# create node interfaces and link
interface_one = prefixes.create_interface(node_one)
interface_two = prefixes.create_interface(node_two)
session.add_link(node_one.id, node_two.id, interface_one, interface_two)
# instantiate session
session.instantiate()
# pause script for verification
input("press enter for shutdown")
2019-10-09 05:17:15 +01:00
# shutdown session
coreemu.shutdown()
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
args = parse(__file__)
main(args)