Skip to content

Commit

Permalink
Merge pull request #43 from hilbert/feature/hilbert_client
Browse files Browse the repository at this point in the history
Updates and Fixes due to (initial) testing installation and its presentation
  • Loading branch information
malex984 authored Dec 9, 2016
2 parents e7e8593 + a7d2407 commit 6454c5d
Show file tree
Hide file tree
Showing 11 changed files with 883 additions and 182 deletions.
62 changes: 42 additions & 20 deletions config/hilbert_cli_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
from abc import *

###############################################################
logging.basicConfig(format='%(levelname)s [%(filename)s:%(lineno)d]: %(message)s', level=logging.DEBUG)
# logging.basicConfig(format='%(levelname)s [%(filename)s:%(lineno)d]: %(message)s', level=logging.DEBUG)
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
# log.setLevel(logging.DEBUG)

_pp = PP.PrettyPrinter(indent=4)

Expand Down Expand Up @@ -95,22 +95,22 @@ def pprint(cfg):


###############################################################
def _execute(_cmd, timeout=None, shell=False, stdout=None, stderr=None): # True??? Try several times? Overall timeout?
# timeout=None,
def _execute(_cmd, shell=False, stdout=None, stderr=None): # True??? Try several times? Overall timeout?
global PEDANTIC
__cmd = ' '.join(_cmd)
# stdout = tmp, stderr = open("/dev/null", 'w')
# stdout=open("/dev/null", 'w'), stderr=open("/dev/null", 'w'))
log.debug("Executing shell command: '{}'...".format(__cmd))

retcode = None
with subprocess.Popen(_cmd, shell=shell, stdout=stdout, stderr=stderr) as p:
try:
retcode = p.wait(timeout=timeout) # ?
except:
p.kill()
p.wait()
log.exception("Could not execute '{0}'! Exception: {1}".format(__cmd, sys.exc_info()))
raise
try:
# with subprocess.Popen(_cmd, shell=shell, stdout=stdout, stderr=stderr) as p:
# timeout=timeout,
retcode = subprocess.call(_cmd, shell=shell, stdout=stdout, stderr=stderr)
except:
log.exception("Could not execute '{0}'! Exception: {1}".format(__cmd, sys.exc_info()))
raise

assert retcode is not None
log.debug("Exit code: '{}'".format(retcode))
Expand Down Expand Up @@ -1154,13 +1154,14 @@ def ssh(self, cmd, **kwargs):
def check_ssh_alias(cls, _h, **kwargs):
"""Check for ssh alias"""
global PEDANTIC
timeout = kwargs.pop('timeout', 2)

log.debug("Checking ssh alias: '{0}'...".format(text_type(_h)))
try:
# client = paramiko.SSHClient()
# client.load_system_host_keys()

_cmd = ["ssh", "-q", "-F", os.path.join(os.environ['HOME'], ".ssh", "config"), "-o", "ConnectTimeout=1", _h, "exit 0"]
_cmd = ["ssh", "-q", "-F", os.path.join(os.environ['HOME'], ".ssh", "config"), "-o", "ConnectTimeout={}".format(timeout), _h, "exit 0"]
retcode = _execute(_cmd, **kwargs) # , stdout=open("/dev/null", 'w'), stderr=open("/dev/null", 'w')

if retcode:
Expand Down Expand Up @@ -1516,6 +1517,8 @@ def __init__(self, *args, **kwargs):
def check_service(self, _f, _n):
global PEDANTIC

return True # TODO: FIXME: takes TOOOOO long at HITS!?!?

# TODO: Check the corresponding file for such a service -> Service in DockerService!
fd, path = tempfile.mkstemp()
try:
Expand Down Expand Up @@ -1766,7 +1769,21 @@ def shutdown(self):
try:
_ret = _a.ssh([self._HILBERT_STATION, "stop"], shell=False)
except:
s = "Could not shutdown station {}".format(_a)
s = "Could not stop Hilbert on the station {}".format(_a)
if not PEDANTIC:
log.warning(s)
return False
else:
log.exception(s)
raise

if not _ret:
return _ret

try:
_ret = _a.ssh([self._HILBERT_STATION, "shutdown"], shell=False)
except:
s = "Could not schedule a shutdown on the station {}".format(_a)
if not PEDANTIC:
log.warning(s)
return False
Expand All @@ -1776,6 +1793,7 @@ def shutdown(self):

return _ret


def deploy(self):
global PEDANTIC

Expand Down Expand Up @@ -1807,7 +1825,7 @@ def deploy(self):
assert _serviceIDs is not None
assert isinstance(_serviceIDs, ServiceList) # list of ServiceID

_serviceIDs = _serviceIDs.get_data()
_serviceIDs = _serviceIDs.get_data() # Note: IDs from config file - NOT Service::ref!
assert isinstance(_serviceIDs, list) # list of strings (with ServiceIDs)?

_a = self.get_address()
Expand All @@ -1824,10 +1842,14 @@ def deploy(self):
# NOTE: ATM only compose && Application/ServiceIDs == refs to the same docker-compose.yml!
# TODO: NOTE: may differ depending on Station::type!
tmp.write("hilbert_station_profile_services=\"{}\"\n".format(' '.join(_serviceIDs)))

for k in _settings:
tmp.write("{0}=\"{1}\"\n".format(k, str(_settings.get(k, ''))))

tmp.write("background_services=\"${hilbert_station_profile_services}\"\n")
tmp.write("default_app=\"${hilbert_station_default_application}\"\n")

tmp.write("default_app=\"${hilbert_station_default_application}\"\n") # ID!

# TODO: collect all compatible applications!
tmp.write("possible_apps=\"${default_app}\"\n")

Expand All @@ -1853,11 +1875,11 @@ def deploy(self):
# log.debug("New Station Configuration: {}".format(s))
# os.remove(path)

_cmd = [self._HILBERT_STATION, "prepare", os.path.join("/tmp", os.path.basename(path))]
_cmd = [self._HILBERT_STATION, "init", os.path.join("/tmp", os.path.basename(path))]
try:
_a.ssh(_cmd, shell=False)
except:
s = "Could not prepare the station using the new configuration file with {}".format(' '.join(_cmd))
s = "Could not initialize the station using the new configuration file with {}".format(' '.join(_cmd))
if not PEDANTIC:
log.warning(s)
return False
Expand All @@ -1877,7 +1899,7 @@ def deploy(self):
# def finish_service(self, action_args):
# raise NotImplementedError("Cannot finish a service/application on this station!")

def app_switch(self, app_id):
def app_change(self, app_id):
global PEDANTIC

_a = self.get_address()
Expand All @@ -1886,7 +1908,7 @@ def app_switch(self, app_id):
assert isinstance(_a, HostAddress)

try:
_ret = _a.ssh([self._HILBERT_STATION, "app_switch", app_id], shell=False)
_ret = _a.ssh([self._HILBERT_STATION, "app_change", app_id], shell=False)
except:
s = "Could not change top application on the station '{0}' to '{1}'".format(_a, app_id)
if not PEDANTIC:
Expand Down Expand Up @@ -1938,7 +1960,7 @@ def run_action(self, action, action_args):
elif action == 'stop':
self.shutdown() # action_args
elif action == 'app_change':
self.app_switch(action_args) # ApplicationID
self.app_change(action_args) # ApplicationID

# elif action == 'start':
# self.start_service(action_args)
Expand Down
96 changes: 49 additions & 47 deletions config/subcmdparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import logging # NOQA
from operator import attrgetter

log = logging.getLogger(__name__) #

#################################
# decorator
#################################
Expand Down Expand Up @@ -43,9 +45,6 @@ def __init__(self, *args, **kwargs):
kwargs['indent_increment'] = 1
kwargs['max_help_position'] = 17
super(SortingHelpFormatter, self).__init__(*args, **kwargs)
# def add_arguments(self, actions):
# actions = sorted(actions, key=attrgetter('help'))
# super(SortingHelpFormatter, self).add_arguments(actions)

#########################
class SubCommandHandler(argparse.ArgumentParser):
Expand All @@ -70,11 +69,6 @@ def __init__(self, *args, **kwargs):
self._use_subcommand_help = kwargs.pop('use_subcommand_help', False)
self._enable_autocompletion = kwargs.pop('enable_autocompletion', False)

self._log = kwargs.pop('log', None)
if self._log is None:
self._log = logging.getLogger(__name__)

self._logging_handler_done = False

self._ignore_remainder = False
self._use_subcommands = True
Expand Down Expand Up @@ -130,46 +124,13 @@ def set_subcommands(self, subcommand_lookup):

return

def logging_handler(parser, args):
if not parser._logging_handler_done:

_args = vars(args)

log = parser._log
level = log.level

# NOTE: logging levels are as follows:
# logging.CRITICAL = 50
# logging.ERROR = 40
# logging.WARNING = 30
# logging.INFO = 20
# logging.DEBUG = 10
# logging.NOTSET = 0

delta = _args.get('verbose', None)
if delta is not None:
level = max(logging.DEBUG, level - int(delta) * logging.DEBUG)

delta = _args.get('quiet', None)
if delta is not None:
level = min(logging.CRITICAL, level + int(delta) * logging.DEBUG)

if log.level != level:
log.debug("Changing logging level: {0} -> {1}"
.format(logging.getLevelName(log.level), logging.getLevelName(level)))
log.setLevel(level)
log.debug("New logging level: {0}".format(logging.getLevelName(log.level)))

parser._logging_handler_done = True

def parse_args(self, argv=None):
"""
Works the same as `argparse.ArgumentParser.parse_args`.
"""

group = self.add_mutually_exclusive_group()
group.add_argument("-v", "--verbose", action="count", help='increase verbosity')
group.add_argument("-q", "--quiet", action="count", help='decrease verbosity')
group.add_argument("-v", "--verbose", action=CountedVerboseAction, help='increase verbosity')
group.add_argument("-q", "--quiet", action=CountedQuietAction, help='decrease verbosity')

# add_argument, set_logging_level, set_subcommands,
# handler.set_logging_argument('-l', '--log_level', default_level=logging.INFO)
Expand Down Expand Up @@ -258,7 +219,7 @@ def run(self, argv=None, context_fxn=None):
# # call the logging config fxn
# self._logging_config_fxn(level, args)

self.logging_handler(args)
# self.logging_handler(args)
# pedantic_handler(self, vars(args))

# generate the context
Expand All @@ -268,15 +229,56 @@ def run(self, argv=None, context_fxn=None):

# create the sub command argument parser
scmd_parser = argparse.ArgumentParser(prog='%s %s' % (self.prog, args.cmd), add_help=True)
scmd_parser._log = self._log

# handle the subcommands
self._subcommand_lookup[args.cmd](scmd_parser, context, args.cargs)

return args # run()

# logging.CRITICAL = 50
# logging.ERROR = 40
# logging.WARNING = 30
# logging.INFO = 20
# logging.DEBUG = 10
# logging.NOTSET = 0
class CountedVerboseAction(argparse._CountAction):
def __init__(self, *args, **kwargs):
super(CountedVerboseAction, self).__init__(*args, **kwargs)

def __call__(self, parser, namespace, values, option_string=None):
new_count = argparse._ensure_value(namespace, self.dest, 0) + 1
setattr(namespace, self.dest, new_count)

_log = logging.getLogger() # root logger!
level = max(logging.DEBUG, _log.level - logging.DEBUG)

if _log.level != level:
log.debug("Changing logging level: %s -> %s",
logging.getLevelName(_log.level),
logging.getLevelName(level))
_log.setLevel(level)
log.debug("New logging level: %s", logging.getLevelName(_log.level))


class CountedQuietAction(argparse._CountAction):
def __init__(self, *args, **kwargs):
super(CountedQuietAction, self).__init__(*args, **kwargs)

def __call__(self, parser, namespace, values, option_string=None):
new_count = argparse._ensure_value(namespace, self.dest, 0) + 1
setattr(namespace, self.dest, new_count)

_log = logging.getLogger() # root logger!
level = min(logging.CRITICAL, _log.level + logging.DEBUG)

if _log.level != level:
log.debug("Changing logging level: %s -> %s",
logging.getLevelName(_log.level),
logging.getLevelName(level))
_log.setLevel(level)
log.debug("New logging level: %s", logging.getLevelName(_log.level))

class MyHelpAction(argparse.Action):
class MyHelpAction(argparse.Action): ### _HelpAction??
def __init__(self,
option_strings,
dest=argparse.SUPPRESS,
Expand All @@ -294,7 +296,7 @@ def __call__(self, parser, namespace, values, option_string=None):
raise BaseException("Help was printed!")


class HelpAllAction(argparse.Action):
class HelpAllAction(argparse.Action): ### _HelpAction??
def __init__(self, option_strings, *args, **kwargs):
super(HelpAllAction, self).__init__(option_strings=option_strings, *args, **kwargs)

Expand Down
15 changes: 8 additions & 7 deletions station/compose.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ done

export XAUTH=${XAUTH:-/tmp/.docker.xauth}

# if [ -n "${DISPLAY}" ]; then
if [ -n "${DISPLAY}" ]; then
# : echo "DISPLAY: '${DISPLAY}', XAUTHORITY: '${XAUTHORITY}' -> '${XAUTH}'"
# : [ ! -f "${XAUTH}" ] && touch "${XAUTH}"
# : xauth nlist "${DISPLAY}" | sed -e 's/^..../ffff/' | sort | uniq | xauth -f "${XAUTH}" nmerge -
# : [ ! -s "${XAUTH}" ] && echo "WARNING: something is wrong with '${XAUTH}': `ls -al ${XAUTH}`"
# fi
# : xhost +
unset XAUTHORITY
# [ ! -f "${XAUTH}" ] && touch "${XAUTH}"
(xauth nlist "${DISPLAY}" | sed -e 's/^..../ffff/' | sort | uniq | xauth -f "${XAUTH}" nmerge - ) 1> /dev/null 2>&1
# [ ! -s "${XAUTH}" ] && echo "WARNING: something is wrong with '${XAUTH}': `ls -al ${XAUTH}`"
(xhost +) 1> /dev/null 2>&1
fi

#unset XAUTHORITY


export HB_PORT="${HB_PORT:-8888}"
Expand Down
8 changes: 8 additions & 0 deletions station/default.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ SELFDIR=`dirname "$0"`
SELFDIR=`cd "$SELFDIR" && pwd`
cd "${SELFDIR}/"

### set -e
## unset DISPLAY

#! NOTE: cleanup all previously started containers:
# docker ps -aq | xargs --no-run-if-empty docker rm -fv
# docker images -q -a | xargs --no-run-if-empty docker rmi
Expand All @@ -12,6 +15,11 @@ if [ -r "./station.cfg" ]; then
. "./station.cfg"
fi

#if [ -r "./startup.cfg" ]; then
# . "./startup.cfg"
#fi


station_default_app="${station_default_app:-$default_app}"

## TODO: FIXME: check stopped containers!
Expand Down
Loading

0 comments on commit 6454c5d

Please sign in to comment.