daemon: refactored session.session_dir to session.directory

This commit is contained in:
Blake Harnden 2021-03-19 16:56:54 -07:00
parent 1c970bbe00
commit a2148c6923
8 changed files with 32 additions and 34 deletions

View file

@ -222,7 +222,7 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer):
# clear previous state and setup for creation
session.clear()
session.session_dir.mkdir(exist_ok=True)
session.directory.mkdir(exist_ok=True)
session.set_state(EventTypes.CONFIGURATION_STATE)
# location
@ -372,7 +372,7 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer):
state=session.state.value,
nodes=session.get_node_count(),
file=session_file,
dir=str(session.session_dir),
dir=str(session.directory),
)
sessions.append(session_summary)
return core_pb2.GetSessionsResponse(sessions=sessions)
@ -428,7 +428,7 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer):
state = EventTypes(request.state)
session.set_state(state)
if state == EventTypes.INSTANTIATION_STATE:
session.session_dir.mkdir(exist_ok=True)
session.directory.mkdir(exist_ok=True)
session.instantiate()
elif state == EventTypes.SHUTDOWN_STATE:
session.shutdown()
@ -575,7 +575,7 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer):
state=session.state.value,
nodes=nodes,
links=links,
dir=str(session.session_dir),
dir=str(session.directory),
user=session.user,
default_services=default_services,
location=location,

View file

@ -398,7 +398,7 @@ class EmaneManager(ModelManager):
return self.ifaces_to_nems.get(iface)
def write_nem(self, iface: CoreInterface, nem_id: int) -> None:
path = self.session.session_dir / "emane_nems"
path = self.session.directory / "emane_nems"
try:
with path.open("a") as f:
f.write(f"{iface.node.name} {iface.name} {nem_id}\n")
@ -539,10 +539,10 @@ class EmaneManager(ModelManager):
return
dev = self.get_config("eventservicedevice")
emanexml.create_event_service_xml(group, port, dev, self.session.session_dir)
emanexml.create_event_service_xml(group, port, dev, self.session.directory)
self.session.distributed.execute(
lambda x: emanexml.create_event_service_xml(
group, port, dev, self.session.session_dir, x
group, port, dev, self.session.directory, x
)
)
@ -596,10 +596,10 @@ class EmaneManager(ModelManager):
node.cmd(args)
logging.info("node(%s) emane daemon running: %s", node.name, args)
else:
log_file = self.session.session_dir / f"{node.name}-emane.log"
platform_xml = self.session.session_dir / f"{node.name}-platform.xml"
log_file = self.session.directory / f"{node.name}-emane.log"
platform_xml = self.session.directory / f"{node.name}-platform.xml"
args = f"{emanecmd} -f {log_file} {platform_xml}"
node.host_cmd(args, cwd=self.session.session_dir)
node.host_cmd(args, cwd=self.session.directory)
logging.info("node(%s) host emane daemon running: %s", node.name, args)
def install_iface(self, iface: CoreInterface) -> None:

View file

@ -145,7 +145,7 @@ class DistributedController:
f"command({requirement})"
)
self.servers[name] = server
cmd = f"mkdir -p {self.session.session_dir}"
cmd = f"mkdir -p {self.session.directory}"
server.remote_cmd(cmd)
def execute(self, func: Callable[[DistributedServer], None]) -> None:
@ -174,7 +174,7 @@ class DistributedController:
# remove all remote session directories
for name in self.servers:
server = self.servers[name]
cmd = f"rm -rf {self.session.session_dir}"
cmd = f"rm -rf {self.session.directory}"
server.remote_cmd(cmd)
# clear tunnels
self.tunnels.clear()

View file

@ -103,9 +103,9 @@ class Session:
self.id: int = _id
# define and create session directory when desired
self.session_dir: Path = Path(tempfile.gettempdir()) / f"pycore.{self.id}"
self.directory: Path = Path(tempfile.gettempdir()) / f"pycore.{self.id}"
if mkdir:
self.session_dir.mkdir()
self.directory.mkdir()
self.name: Optional[str] = None
self.file_path: Optional[Path] = None
@ -777,7 +777,7 @@ class Session:
# remove this sessions working directory
preserve = self.options.get_config("preservedir") == "1"
if not preserve:
shutil.rmtree(self.session_dir, ignore_errors=True)
shutil.rmtree(self.directory, ignore_errors=True)
def broadcast_event(self, event_data: EventData) -> None:
"""
@ -876,7 +876,7 @@ class Session:
:param state: state to write to file
:return: nothing
"""
state_file = self.session_dir / "state"
state_file = self.directory / "state"
try:
with state_file.open("w") as f:
f.write(f"{state.value} {state.name}\n")
@ -904,8 +904,8 @@ class Session:
"""
file_name, data = hook
logging.info("running hook %s", file_name)
file_path = self.session_dir / file_name
log_path = self.session_dir / f"{file_name}.log"
file_path = self.directory / file_name
log_path = self.directory / f"{file_name}.log"
try:
with file_path.open("w") as f:
f.write(data)
@ -916,7 +916,7 @@ class Session:
stdout=f,
stderr=subprocess.STDOUT,
close_fds=True,
cwd=self.session_dir,
cwd=self.directory,
env=self.get_environment(),
)
except (IOError, subprocess.CalledProcessError):
@ -982,7 +982,7 @@ class Session:
# create session deployed xml
xml_writer = corexml.CoreXmlWriter(self)
corexmldeployment.CoreXmlDeployment(self, xml_writer.scenario)
xml_file_path = self.session_dir / "session-deployed.xml"
xml_file_path = self.directory / "session-deployed.xml"
xml_writer.write(xml_file_path)
def get_environment(self, state: bool = True) -> Dict[str, str]:
@ -998,7 +998,7 @@ class Session:
env["CORE_PYTHON"] = sys.executable
env["SESSION"] = str(self.id)
env["SESSION_SHORT"] = self.short_session_id()
env["SESSION_DIR"] = str(self.session_dir)
env["SESSION_DIR"] = str(self.directory)
env["SESSION_NAME"] = str(self.name)
env["SESSION_FILENAME"] = str(self.file_path)
env["SESSION_USER"] = str(self.user)
@ -1009,7 +1009,7 @@ class Session:
# /home/user/.core/environment
# /tmp/pycore.<session id>/environment
core_env_path = constants.CORE_CONF_DIR / "environment"
session_env_path = self.session_dir / "environment"
session_env_path = self.directory / "environment"
if self.user:
user_home_path = Path(f"~{self.user}").expanduser()
user_env1 = user_home_path / ".core" / "environment"
@ -1036,7 +1036,7 @@ class Session:
logging.error("thumbnail file to set does not exist: %s", thumb_file)
self.thumbnail = None
return
dst_path = self.session_dir / thumb_file.name
dst_path = self.directory / thumb_file.name
shutil.copy(thumb_file, dst_path)
self.thumbnail = dst_path
@ -1051,10 +1051,10 @@ class Session:
if user:
try:
uid = pwd.getpwnam(user).pw_uid
gid = self.session_dir.stat().st_gid
os.chown(self.session_dir, uid, gid)
gid = self.directory.stat().st_gid
os.chown(self.directory, uid, gid)
except IOError:
logging.exception("failed to set permission on %s", self.session_dir)
logging.exception("failed to set permission on %s", self.directory)
self.user = user
def create_node(
@ -1137,7 +1137,7 @@ class Session:
Write nodes to a 'nodes' file in the session dir.
The 'nodes' file lists: number, name, api-type, class-type
"""
file_path = self.session_dir / "nodes"
file_path = self.directory / "nodes"
try:
with self.nodes_lock:
with file_path.open("w") as f:

View file

@ -1154,6 +1154,4 @@ class Ns2ScriptedMobility(WayPointMobility):
filename = Path(filename)
filename = self.findfile(filename)
args = f"{BASH} {filename} {typestr}"
utils.cmd(
args, cwd=self.session.session_dir, env=self.session.get_environment()
)
utils.cmd(args, cwd=self.session.directory, env=self.session.get_environment())

View file

@ -363,7 +363,7 @@ class CoreNodeBase(NodeBase):
:return: nothing
"""
if self.nodedir is None:
self.nodedir = self.session.session_dir / f"{self.name}.conf"
self.nodedir = self.session.directory / f"{self.name}.conf"
self.host_cmd(f"mkdir -p {self.nodedir}")
self.tmpnodedir = True
else:
@ -490,7 +490,7 @@ class CoreNode(CoreNodeBase):
"""
super().__init__(session, _id, name, server)
self.nodedir: Optional[Path] = nodedir
self.ctrlchnlname: Path = self.session.session_dir / self.name
self.ctrlchnlname: Path = self.session.directory / self.name
self.client: Optional[VnodeClient] = None
self.pid: Optional[int] = None
self.lock: RLock = RLock()

View file

@ -285,7 +285,7 @@ def expand_corepath(
if session is not None:
pathname = pathname.replace("~", f"/home/{session.user}")
pathname = pathname.replace("%SESSION%", str(session.id))
pathname = pathname.replace("%SESSION_DIR%", str(session.session_dir))
pathname = pathname.replace("%SESSION_DIR%", str(session.directory))
pathname = pathname.replace("%SESSION_USER%", session.user)
if node is not None:
pathname = pathname.replace("%NODE%", str(node.id))

View file

@ -95,7 +95,7 @@ def create_node_file(
if isinstance(node, CoreNode):
file_path = node.nodedir / file_name
else:
file_path = node.session.session_dir / file_name
file_path = node.session.directory / file_name
create_file(xml_element, doc_name, file_path, node.server)