2020-04-18 16:24:26 +01:00
|
|
|
"""
|
|
|
|
Example for scripting a standalone distributed LXD session that does not interact
|
|
|
|
with the GUI.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import argparse
|
2019-10-17 17:09:03 +01:00
|
|
|
import logging
|
|
|
|
|
|
|
|
from core.emulator.coreemu import CoreEmu
|
2022-05-25 18:51:42 +01:00
|
|
|
from core.emulator.data import IpPrefixes
|
2020-05-21 06:14:03 +01:00
|
|
|
from core.emulator.enumerations import EventTypes
|
|
|
|
from core.nodes.lxd import LxcNode
|
2019-10-17 17:09:03 +01:00
|
|
|
|
|
|
|
|
2020-04-18 16:24:26 +01:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2019-10-21 19:36:59 +01:00
|
|
|
def main(args):
|
2019-10-17 17:09:03 +01:00
|
|
|
# ip generator for example
|
|
|
|
prefixes = IpPrefixes(ip4_prefix="10.83.0.0/16")
|
|
|
|
|
|
|
|
# create emulator instance for creating sessions and utility methods
|
2019-10-21 19:36:59 +01:00
|
|
|
coreemu = CoreEmu({"distributed_address": args.address})
|
2019-10-17 17:09:03 +01:00
|
|
|
session = coreemu.create_session()
|
|
|
|
|
|
|
|
# initialize distributed
|
|
|
|
server_name = "core2"
|
2019-10-21 19:36:59 +01:00
|
|
|
session.distributed.add_server(server_name, args.server)
|
2019-10-17 17:09:03 +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
|
2022-05-25 18:51:42 +01:00
|
|
|
options = LxcNode.create_options()
|
|
|
|
options.image = "ubuntu:18.04"
|
2020-06-13 00:52:41 +01:00
|
|
|
node1 = session.add_node(LxcNode, options=options)
|
2019-10-23 04:50:01 +01:00
|
|
|
options.server = server_name
|
2020-06-13 00:52:41 +01:00
|
|
|
node2 = session.add_node(LxcNode, options=options)
|
2019-10-17 17:09:03 +01:00
|
|
|
|
|
|
|
# create node interfaces and link
|
2020-06-16 17:30:16 +01:00
|
|
|
interface1_data = prefixes.create_iface(node1)
|
|
|
|
interface2_data = prefixes.create_iface(node2)
|
2020-06-13 00:52:41 +01:00
|
|
|
session.add_link(node1.id, node2.id, interface1_data, interface2_data)
|
2019-10-17 17:09:03 +01:00
|
|
|
|
|
|
|
# instantiate session
|
|
|
|
session.instantiate()
|
|
|
|
|
|
|
|
# pause script for verification
|
2019-10-21 19:36:59 +01:00
|
|
|
input("press enter for shutdown")
|
2019-10-17 17:09:03 +01:00
|
|
|
|
|
|
|
# shutdown session
|
|
|
|
coreemu.shutdown()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
logging.basicConfig(level=logging.INFO)
|
2020-04-18 16:24:26 +01:00
|
|
|
args = parse(__file__)
|
2019-10-21 19:36:59 +01:00
|
|
|
main(args)
|