updated emane config files to be generated for remote servers, fixed services not using node remote server compatible commands

This commit is contained in:
Blake Harnden 2019-10-10 11:53:52 -07:00
parent a4b6b8be51
commit bc58693339
7 changed files with 184 additions and 39 deletions

View file

@ -1,12 +1,16 @@
import logging
import os
import threading
from tempfile import NamedTemporaryFile
from invoke import UnexpectedExit
from core.errors import CoreCommandError
LOCK = threading.Lock()
def remote_cmd(server, cmd, env=None):
def remote_cmd(server, cmd, env=None, cwd=None):
"""
Run command remotely using server connection.
@ -14,23 +18,38 @@ def remote_cmd(server, cmd, env=None):
default is None for localhost
:param str cmd: command to run
:param dict env: environment for remote command, default is None
:param str cwd: directory to run command in, defaults to None, which is the user's
home directory
:return: stdout when success
:rtype: str
:raises CoreCommandError: when a non-zero exit status occurs
"""
logging.info("remote cmd server(%s): %s", server, cmd)
with LOCK:
if env is None:
result = server.run(cmd, hide=False)
else:
result = server.run(cmd, hide=False, env=env, replace_env=True)
if result.exited:
raise CoreCommandError(
result.exited, result.command, result.stdout, result.stderr
)
return result.stdout.strip()
replace_env = env is not None
try:
with LOCK:
if cwd is None:
result = server.run(cmd, hide=False, env=env, replace_env=replace_env)
else:
with server.cd(cwd):
result = server.run(
cmd, hide=False, env=env, replace_env=replace_env
)
return result.stdout.strip()
except UnexpectedExit as e:
stdout, stderr = e.streams_for_display()
raise CoreCommandError(e.result.exited, cmd, stdout, stderr)
def remote_put(server, source, destination):
with LOCK:
server.put(source, destination)
def remote_put_temp(server, destination, data):
with LOCK:
temp = NamedTemporaryFile(delete=False)
temp.write(data.encode("utf-8"))
temp.close()
server.put(temp.name, destination)
os.unlink(temp.name)