initial lxd based node working from simple scripts

This commit is contained in:
Blake Harnden 2019-07-02 07:48:43 -07:00
parent 2397cd58ee
commit e83b38d96a
8 changed files with 483 additions and 3 deletions

View file

@ -82,6 +82,7 @@ class NodeTypes(Enum):
CONTROL_NET = 13 CONTROL_NET = 13
EMANE_NET = 14 EMANE_NET = 14
DOCKER = 15 DOCKER = 15
LXC = 16
class Rj45Models(Enum): class Rj45Models(Enum):

View file

@ -500,7 +500,7 @@ class Session(object):
# create node # create node
logging.info("creating node(%s) id(%s) name(%s) start(%s)", node_class.__name__, _id, name, start) logging.info("creating node(%s) id(%s) name(%s) start(%s)", node_class.__name__, _id, name, start)
if _type == NodeTypes.DOCKER: if _type in [NodeTypes.DOCKER, NodeTypes.LXC]:
node = self.create_node(cls=node_class, _id=_id, name=name, start=start, image=node_options.image) node = self.create_node(cls=node_class, _id=_id, name=name, start=start, image=node_options.image)
else: else:
node = self.create_node(cls=node_class, _id=_id, name=name, start=start) node = self.create_node(cls=node_class, _id=_id, name=name, start=start)
@ -514,7 +514,7 @@ class Session(object):
self.set_node_position(node, node_options) self.set_node_position(node, node_options)
# add services to default and physical nodes only # add services to default and physical nodes only
if _type in [NodeTypes.DEFAULT, NodeTypes.PHYSICAL, NodeTypes.DOCKER]: if _type in [NodeTypes.DEFAULT, NodeTypes.PHYSICAL, NodeTypes.DOCKER, NodeTypes.LXC]:
node.type = node_options.model node.type = node_options.model
logging.debug("set node type: %s", node.type) logging.debug("set node type: %s", node.type)
self.services.add_services(node, node.type, node_options.services) self.services.add_services(node, node.type, node_options.services)

336
daemon/core/nodes/lxd.py Normal file
View file

@ -0,0 +1,336 @@
import json
import logging
import os
import threading
import time
from core import utils, CoreCommandError
from core.emulator.enumerations import NodeTypes
from core.nodes.base import CoreNode
class LxdClient(object):
def __init__(self, name, image):
self.name = name
self.image = image
self.pid = None
self._addr = {}
def create_container(self):
utils.check_cmd("lxc launch {image} {name}".format(
name=self.name,
image=self.image
))
data = self._get_data()[0]
self.pid = data["state"]["pid"]
return self.pid
def _get_data(self):
args = "lxc list {name} --format json".format(name=self.name)
status, output = utils.cmd_output(args)
if status:
raise CoreCommandError(status, args, output)
return json.loads(output)
def _cmd_args(self, cmd):
return "lxc exec {name} -- {cmd}".format(
name=self.name,
cmd=cmd
)
def is_alive(self):
data = self._get_data()
if not data:
return False
data = data[0]
return data["state"]["status"] == "Running"
def stop_container(self):
utils.check_cmd("lxc delete --force {name}".format(
name=self.name
))
def run_cmd_output(self, cmd):
if isinstance(cmd, list):
cmd = " ".join(cmd)
args = self._cmd_args(cmd)
logging.info("lxc cmd output: %s", args)
return utils.cmd_output(args)
def run_cmd(self, cmd, wait=True):
if isinstance(cmd, list):
cmd = " ".join(cmd)
args = self._cmd_args(cmd)
logging.info("lxc cmd: %s", args)
return utils.cmd(args, wait)
def _ns_args(self, cmd):
return "nsenter -t {pid} -m -u -i -p -n {cmd}".format(
pid=self.pid,
cmd=cmd
)
def ns_cmd_output(self, cmd):
if isinstance(cmd, list):
cmd = " ".join(cmd)
args = self._ns_args(cmd)
logging.info("ns cmd: %s", args)
return utils.cmd_output(args)
def ns_cmd(self, cmd, wait=True):
if isinstance(cmd, list):
cmd = " ".join(cmd)
args = self._ns_args(cmd)
logging.info("ns cmd: %s", args)
return utils.cmd(args, wait)
def copy_file(self, source, destination):
if destination[0] != "/":
destination = os.path.join("/root/", destination)
args = "lxc file push {source} {name}/{destination}".format(
source=source,
name=self.name,
destination=destination
)
status, output = utils.cmd_output(args)
if status:
raise CoreCommandError(status, args, output)
def getaddr(self, ifname, rescan=False):
"""
Get address for interface on node.
:param str ifname: interface name to get address for
:param bool rescan: rescan flag
:return: interface information
:rtype: dict
"""
if ifname in self._addr and not rescan:
return self._addr[ifname]
interface = {"ether": [], "inet": [], "inet6": [], "inet6link": []}
args = ["ip", "addr", "show", "dev", ifname]
status, output = self.ns_cmd_output(args)
for line in output:
line = line.strip().split()
if line[0] == "link/ether":
interface["ether"].append(line[1])
elif line[0] == "inet":
interface["inet"].append(line[1])
elif line[0] == "inet6":
if line[3] == "global":
interface["inet6"].append(line[1])
elif line[3] == "link":
interface["inet6link"].append(line[1])
else:
logging.warning("unknown scope: %s" % line[3])
if status:
logging.warning("nonzero exist status (%s) for cmd: %s", status, args)
self._addr[ifname] = interface
return interface
class LxcNode(CoreNode):
apitype = NodeTypes.LXC.value
valid_address_types = {"inet", "inet6", "inet6link"}
def __init__(self, session, _id=None, name=None, nodedir=None, bootsh="boot.sh", start=True, image=None):
"""
Create a CoreNode instance.
:param core.emulator.session.Session session: core session instance
:param int _id: object id
:param str name: object name
:param str nodedir: node directory
:param str bootsh: boot shell to use
:param bool start: start flag
"""
super(CoreNode, self).__init__(session, _id, name, start=start)
self.nodedir = nodedir
self.ctrlchnlname = os.path.abspath(os.path.join(self.session.session_dir, self.name))
if image is None:
image = "ubuntu"
self.client = LxdClient(self.name, image)
self.pid = None
self.up = False
self.lock = threading.RLock()
self._mounts = []
self.bootsh = bootsh
if start:
self.startup()
def alive(self):
"""
Check if the node is alive.
:return: True if node is alive, False otherwise
:rtype: bool
"""
return self.client.is_alive()
def startup(self):
"""
Start a new namespace node by invoking the vnoded process that
allocates a new namespace. Bring up the loopback device and set
the hostname.
:return: nothing
"""
with self.lock:
if self.up:
raise ValueError("starting a node that is already up")
self.makenodedir()
self.pid = self.client.create_container()
self.up = True
def shutdown(self):
"""
Shutdown logic.
:return: nothing
"""
# nothing to do if node is not up
if not self.up:
return
with self.lock:
self._netif.clear()
self.client.stop_container()
self.up = False
def cmd(self, args, wait=True):
"""
Runs shell command on node, with option to not wait for a result.
:param list[str]|str args: command to run
:param bool wait: wait for command to exit, defaults to True
:return: exit status for command
:rtype: int
"""
# return self.client.ns_cmd(args, wait)
return self.client.run_cmd(args, wait)
def cmd_output(self, args):
"""
Runs shell command on node and get exit status and output.
:param list[str]|str args: command to run
:return: exit status and combined stdout and stderr
:rtype: tuple[int, str]
"""
# return self.client.ns_cmd_output(args)
return self.client.run_cmd_output(args)
def check_cmd(self, args):
"""
Runs shell command on node.
:param list[str]|str args: command to run
:return: combined stdout and stderr
:rtype: str
:raises CoreCommandError: when a non-zero exit status occurs
"""
# status, output = self.client.ns_cmd_output(args)
status, output = self.client.run_cmd_output(args)
if status:
raise CoreCommandError(status, args, output)
return output
def termcmdstring(self, sh="/bin/sh"):
"""
Create a terminal command string.
:param str sh: shell to execute command in
:return: str
"""
return ""
def privatedir(self, path):
"""
Create a private directory.
:param str path: path to create
:return: nothing
"""
logging.info("creating node dir: %s", path)
args = "mkdir -p {path}".format(path=path)
status, output = self.client.run_cmd_output(args)
if status:
raise CoreCommandError(status, args, output)
def mount(self, source, target):
"""
Create and mount a directory.
:param str source: source directory to mount
:param str target: target directory to create
:return: nothing
:raises CoreCommandError: when a non-zero exit status occurs
"""
logging.info("mounting source(%s) target(%s)", source, target)
raise Exception("you found a lxc node")
def nodefile(self, filename, contents, mode=0o644):
"""
Create a node file with a given mode.
:param str filename: name of file to create
:param contents: contents of file
:param int mode: mode for file
:return: nothing
"""
logging.info("node dir(%s) ctrlchannel(%s)", self.nodedir, self.ctrlchnlname)
logging.info("nodefile filename(%s) mode(%s)", filename, mode)
file_path = os.path.join(self.nodedir, filename)
with open(file_path, "w") as f:
os.chmod(f.name, mode)
f.write(contents)
self.client.copy_file(file_path, filename)
def nodefilecopy(self, filename, srcfilename, mode=None):
"""
Copy a file to a node, following symlinks and preserving metadata.
Change file mode if specified.
:param str filename: file name to copy file to
:param str srcfilename: file to copy
:param int mode: mode to copy to
:return: nothing
"""
logging.info("node file copy file(%s) source(%s) mode(%s)", filename, srcfilename, mode)
raise Exception("you found a lxc node")
def newnetif(self, net=None, addrlist=None, hwaddr=None, ifindex=None, ifname=None):
"""
Create a new network interface.
:param core.nodes.base.CoreNetworkBase net: network to associate with
:param list addrlist: addresses to add on the interface
:param core.nodes.ipaddress.MacAddress hwaddr: hardware address to set for interface
:param int ifindex: index of interface to create
:param str ifname: name for interface
:return: interface index
:rtype: int
"""
if not addrlist:
addrlist = []
with self.lock:
ifindex = self.newveth(ifindex=ifindex, ifname=ifname, net=net)
if net is not None:
self.attachnet(ifindex, net)
if hwaddr:
self.sethwaddr(ifindex, hwaddr)
# delay required for lxc nodes
time.sleep(0.5)
for address in utils.make_tuple(addrlist):
self.addaddr(ifindex, address)
self.ifup(ifindex)
return ifindex

View file

@ -3,6 +3,7 @@ Provides default node maps that can be used to run core with.
""" """
import core.nodes.base import core.nodes.base
import core.nodes.docker import core.nodes.docker
import core.nodes.lxd
import core.nodes.network import core.nodes.network
import core.nodes.physical import core.nodes.physical
from core.emane.nodes import EmaneNet from core.emane.nodes import EmaneNet
@ -27,5 +28,6 @@ NODES = {
NodeTypes.TAP_BRIDGE: GreTapBridge, NodeTypes.TAP_BRIDGE: GreTapBridge,
NodeTypes.PEER_TO_PEER: core.nodes.network.PtpNet, NodeTypes.PEER_TO_PEER: core.nodes.network.PtpNet,
NodeTypes.CONTROL_NET: core.nodes.network.CtrlNet, NodeTypes.CONTROL_NET: core.nodes.network.CtrlNet,
NodeTypes.DOCKER: core.nodes.docker.DockerNode NodeTypes.DOCKER: core.nodes.docker.DockerNode,
NodeTypes.LXC: core.nodes.lxd.LxcNode
} }

View file

@ -0,0 +1,29 @@
# LXD Support
Information on how LXD can be leveraged and included to create
nodes based on LXC containers and images to interface with
existing CORE nodes, when needed.
# Installation
```shell
sudo snap install lxd
```
# Configuration
Initialize LXD and say no to adding a default bridge.
```shell
sudo lxd init
```
# Tools and Versions Tested With
* LXD 3.14
* nsenter from util-linux 2.31.1
# Examples
This directory provides a few small examples creating LXC nodes
using LXD and linking them to themselves or with standard CORE nodes.

View file

@ -0,0 +1,34 @@
import logging
from core.emulator.coreemu import CoreEmu
from core.emulator.emudata import IpPrefixes, NodeOptions
from core.emulator.enumerations import NodeTypes, EventTypes
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
coreemu = CoreEmu()
session = coreemu.create_session()
session.set_state(EventTypes.CONFIGURATION_STATE)
try:
prefixes = IpPrefixes(ip4_prefix="10.83.0.0/16")
options = NodeOptions(image="ubuntu")
options.services = ["SSH"]
# create node one
node_one = session.add_node(_type=NodeTypes.LXC, node_options=options)
interface_one = prefixes.create_interface(node_one)
# create node two
node_two = session.add_node()
interface_two = prefixes.create_interface(node_two)
# add link
session.add_link(node_one.id, node_two.id, interface_one, interface_two)
# instantiate
session.instantiate()
finally:
input("continue to shutdown")
coreemu.shutdown()

View file

@ -0,0 +1,35 @@
import logging
from core.emulator.coreemu import CoreEmu
from core.emulator.emudata import IpPrefixes, NodeOptions
from core.emulator.enumerations import NodeTypes, EventTypes
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
coreemu = CoreEmu()
session = coreemu.create_session()
session.set_state(EventTypes.CONFIGURATION_STATE)
# create nodes and interfaces
try:
prefixes = IpPrefixes(ip4_prefix="10.83.0.0/16")
options = NodeOptions(image="ubuntu")
# create node one
node_one = session.add_node(_type=NodeTypes.LXC, node_options=options)
interface_one = prefixes.create_interface(node_one)
# create node two
node_two = session.add_node(_type=NodeTypes.LXC, node_options=options)
interface_two = prefixes.create_interface(node_two)
# add link
session.add_link(node_one.id, node_two.id, interface_one, interface_two)
# instantiate
session.instantiate()
finally:
input("continue to shutdown")
coreemu.shutdown()

View file

@ -0,0 +1,43 @@
import logging
from core.emulator.coreemu import CoreEmu
from core.emulator.emudata import IpPrefixes, NodeOptions
from core.emulator.enumerations import NodeTypes, EventTypes
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
coreemu = CoreEmu()
session = coreemu.create_session()
session.set_state(EventTypes.CONFIGURATION_STATE)
try:
prefixes = IpPrefixes(ip4_prefix="10.83.0.0/16")
options = NodeOptions(image="ubuntu")
# create switch
switch = session.add_node(_type=NodeTypes.SWITCH)
# node one
node_one = session.add_node(_type=NodeTypes.LXC, node_options=options)
interface_one = prefixes.create_interface(node_one)
# node two
node_two = session.add_node(_type=NodeTypes.LXC, node_options=options)
interface_two = prefixes.create_interface(node_two)
# node three
node_three = session.add_node()
interface_three = prefixes.create_interface(node_three)
# add links
session.add_link(node_one.id, switch.id, interface_one)
session.add_link(node_two.id, switch.id, interface_two)
session.add_link(node_three.id, switch.id, interface_three)
# instantiate
session.instantiate()
finally:
input("continue to shutdown")
coreemu.shutdown()