updates to attempt to support trying to run traditional services in some manner within the context of a docker node

This commit is contained in:
Blake Harnden 2019-06-28 12:58:08 -07:00
parent 5971950523
commit 6ab8368f1c
3 changed files with 68 additions and 14 deletions

View file

@ -1364,7 +1364,7 @@ class Session(object):
# TODO: PyCoreNode is not the type to check # TODO: PyCoreNode is not the type to check
if isinstance(node, CoreNodeBase) and not nodeutils.is_node(node, NodeTypes.RJ45): if isinstance(node, CoreNodeBase) and not nodeutils.is_node(node, NodeTypes.RJ45):
# add a control interface if configured # add a control interface if configured
logging.info("booting node: %s", node.name) logging.info("booting node(%s): %s", node.name, node.services)
self.add_remove_control_interface(node=node, remove=False) self.add_remove_control_interface(node=node, remove=False)
result = pool.apply_async(self.services.boot_services, (node,)) result = pool.apply_async(self.services.boot_services, (node,))
results.append(result) results.append(result)

View file

@ -35,6 +35,7 @@ class DockerClient(object):
def run_cmd(self, cmd): def run_cmd(self, cmd):
if isinstance(cmd, list): if isinstance(cmd, list):
cmd = " ".join(cmd) cmd = " ".join(cmd)
logging.info("docker cmd: %s", cmd)
return utils.cmd_output("docker exec -it {name} {cmd}".format( return utils.cmd_output("docker exec -it {name} {cmd}".format(
name=self.name, name=self.name,
cmd=cmd cmd=cmd
@ -43,19 +44,31 @@ class DockerClient(object):
def ns_cmd(self, cmd): def ns_cmd(self, cmd):
if isinstance(cmd, list): if isinstance(cmd, list):
cmd = " ".join(cmd) cmd = " ".join(cmd)
logging.info("ns cmd: %s", cmd)
return utils.cmd_output("nsenter -t {pid} -m -u -i -p -n {cmd}".format( return utils.cmd_output("nsenter -t {pid} -m -u -i -p -n {cmd}".format(
pid=self.pid, pid=self.pid,
cmd=cmd cmd=cmd
)) ))
def get_pid(self): def get_pid(self):
status, output = utils.cmd_output("docker inspect -f '{{{{.State.Pid}}}}' {name}".format(name=self.name)) args = "docker inspect -f '{{{{.State.Pid}}}}' {name}".format(name=self.name)
status, output = utils.cmd_output(args)
if status: if status:
raise CoreCommandError(status, output) raise CoreCommandError(status, args, output)
self.pid = output self.pid = output
logging.debug("node(%s) pid: %s", self.name, self.pid) logging.debug("node(%s) pid: %s", self.name, self.pid)
return output return output
def copy_file(self, source, destination):
args = "docker cp {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): def getaddr(self, ifname, rescan=False):
""" """
Get address for interface on node. Get address for interface on node.
@ -139,6 +152,7 @@ class DockerNode(CoreNode):
with self.lock: with self.lock:
if self.up: if self.up:
raise ValueError("starting a node that is already up") raise ValueError("starting a node that is already up")
self.makenodedir()
self.pid = self.client.create_container("ubuntu:ifconfig") self.pid = self.client.create_container("ubuntu:ifconfig")
self.up = True self.up = True
@ -166,7 +180,9 @@ class DockerNode(CoreNode):
:return: exit status for command :return: exit status for command
:rtype: int :rtype: int
""" """
status, _ = self.client.ns_cmd(args) status, output = self.client.ns_cmd(args)
if status:
raise CoreCommandError(status, args, output)
return status return status
def cmd_output(self, args): def cmd_output(self, args):
@ -190,7 +206,7 @@ class DockerNode(CoreNode):
""" """
status, output = self.client.ns_cmd(args) status, output = self.client.ns_cmd(args)
if status: if status:
raise CoreCommandError(status, output) raise CoreCommandError(status, args, output)
return output return output
def termcmdstring(self, sh="/bin/sh"): def termcmdstring(self, sh="/bin/sh"):
@ -209,7 +225,11 @@ class DockerNode(CoreNode):
:param str path: path to create :param str path: path to create
:return: nothing :return: nothing
""" """
pass logging.info("creating node dir: %s", path)
args = "mkdir -p {path}".format(path=path)
status, output = self.client.run_cmd(args)
if status:
raise CoreCommandError(status, args, output)
def mount(self, source, target): def mount(self, source, target):
""" """
@ -220,4 +240,35 @@ class DockerNode(CoreNode):
:return: nothing :return: nothing
:raises CoreCommandError: when a non-zero exit status occurs :raises CoreCommandError: when a non-zero exit status occurs
""" """
pass logging.info("mounting source(%s) target(%s)", source, target)
raise Exception("you found a docker 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 docker node")

View file

@ -7,25 +7,28 @@ from core.emulator.enumerations import NodeTypes, EventTypes
if __name__ == "__main__": if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
coreemu = CoreEmu() coreemu = CoreEmu()
session = coreemu.create_session() session = coreemu.create_session()
session.set_state(EventTypes.CONFIGURATION_STATE) session.set_state(EventTypes.CONFIGURATION_STATE)
# create nodes and interfaces
try: try:
# create nodes one
prefixes = IpPrefixes(ip4_prefix="10.83.0.0/16") prefixes = IpPrefixes(ip4_prefix="10.83.0.0/16")
node_one = session.add_node(_type=NodeTypes.DOCKER) node_one = session.add_node(_type=NodeTypes.DOCKER)
node_two = session.add_node() session.services.add_services(node_one, node_one.type, ["SSH"])
logging.info("docker node(%s): %s", node_one.name, node_one.services)
interface_one = prefixes.create_interface(node_one) interface_one = prefixes.create_interface(node_one)
# create nodes two
node_two = session.add_node()
interface_two = prefixes.create_interface(node_two) interface_two = prefixes.create_interface(node_two)
# add link # add link
input("press key to continue")
session.add_link(node_one.id, node_two.id, interface_one, interface_two) session.add_link(node_one.id, node_two.id, interface_one, interface_two)
print(node_one.cmd_output("ifconfig"))
print(node_two.cmd_output("ifconfig")) # instantiate
input("press key to continue") logging.info("INSTANTIATE")
logging.info("docker node(%s): %s", node_one.name, node_one.services)
session.instantiate() session.instantiate()
finally: finally:
input("continue to shutdown") input("continue to shutdown")