diff --git a/daemon/core/emulator/session.py b/daemon/core/emulator/session.py index 246f0c0a..ef79dcf5 100644 --- a/daemon/core/emulator/session.py +++ b/daemon/core/emulator/session.py @@ -1364,7 +1364,7 @@ class Session(object): # TODO: PyCoreNode is not the type to check if isinstance(node, CoreNodeBase) and not nodeutils.is_node(node, NodeTypes.RJ45): # 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) result = pool.apply_async(self.services.boot_services, (node,)) results.append(result) diff --git a/daemon/core/nodes/docker.py b/daemon/core/nodes/docker.py index 74b9ea01..007c76fc 100644 --- a/daemon/core/nodes/docker.py +++ b/daemon/core/nodes/docker.py @@ -35,6 +35,7 @@ class DockerClient(object): def run_cmd(self, cmd): if isinstance(cmd, list): cmd = " ".join(cmd) + logging.info("docker cmd: %s", cmd) return utils.cmd_output("docker exec -it {name} {cmd}".format( name=self.name, cmd=cmd @@ -43,19 +44,31 @@ class DockerClient(object): def ns_cmd(self, cmd): if isinstance(cmd, list): cmd = " ".join(cmd) + logging.info("ns cmd: %s", cmd) return utils.cmd_output("nsenter -t {pid} -m -u -i -p -n {cmd}".format( pid=self.pid, cmd=cmd )) 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: - raise CoreCommandError(status, output) + raise CoreCommandError(status, args, output) self.pid = output logging.debug("node(%s) pid: %s", self.name, self.pid) 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): """ Get address for interface on node. @@ -139,6 +152,7 @@ class DockerNode(CoreNode): with self.lock: if self.up: raise ValueError("starting a node that is already up") + self.makenodedir() self.pid = self.client.create_container("ubuntu:ifconfig") self.up = True @@ -166,7 +180,9 @@ class DockerNode(CoreNode): :return: exit status for command :rtype: int """ - status, _ = self.client.ns_cmd(args) + status, output = self.client.ns_cmd(args) + if status: + raise CoreCommandError(status, args, output) return status def cmd_output(self, args): @@ -190,7 +206,7 @@ class DockerNode(CoreNode): """ status, output = self.client.ns_cmd(args) if status: - raise CoreCommandError(status, output) + raise CoreCommandError(status, args, output) return output def termcmdstring(self, sh="/bin/sh"): @@ -209,7 +225,11 @@ class DockerNode(CoreNode): :param str path: path to create :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): """ @@ -220,4 +240,35 @@ class DockerNode(CoreNode): :return: nothing :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") diff --git a/daemon/examples/docker/docker2core.py b/daemon/examples/docker/docker2core.py index 1b27039b..39491214 100644 --- a/daemon/examples/docker/docker2core.py +++ b/daemon/examples/docker/docker2core.py @@ -7,25 +7,28 @@ 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: + # create nodes one prefixes = IpPrefixes(ip4_prefix="10.83.0.0/16") 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) + + # create nodes two + node_two = session.add_node() interface_two = prefixes.create_interface(node_two) # add link - input("press key to continue") 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")) - input("press key to continue") + + # instantiate + logging.info("INSTANTIATE") + logging.info("docker node(%s): %s", node_one.name, node_one.services) session.instantiate() finally: input("continue to shutdown")