From ee091151c749a714f7f8e609ca5145d1d4629342 Mon Sep 17 00:00:00 2001 From: LeeZeitz Date: Mon, 9 Jul 2018 20:07:50 -0700 Subject: [PATCH 1/6] route init logs to file, show custom log message --- src/cumulus/cli.py | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/cumulus/cli.py b/src/cumulus/cli.py index 2aaf04b..e7cc539 100644 --- a/src/cumulus/cli.py +++ b/src/cumulus/cli.py @@ -31,6 +31,9 @@ import subprocess import yaml import sys +from datetime import datetime +from io import StringIO + START_SHELL = "./start_shell.sh" DOCKER_HUB = "strcum/" @@ -47,11 +50,11 @@ "rails": "rails server -b 0.0.0.0:{0}".format(PORTS["rails"])} DOCKER_COMPOSE_VERSION = '3.6' - def main(): """Main CLI entrypoint.""" import commands options = docopt(__doc__, version=VERSION) + for (k, v) in options.items(): if hasattr(commands, k) and v: module = getattr(commands, k) @@ -60,14 +63,18 @@ def main(): for command in commands if command[0] != 'Base'][0] command = command(options) command.run() + clean_logs() return - exit(__doc__) def start_container(service): if service: - subprocess.call([DOCKER_COMPOSE, "up", "-d", service]) + print ('Starting {0}...'.format(service)) + subprocess.call([DOCKER_COMPOSE, "up", "-d", service])#, stdout=log_file, stderr=subprocess.STDOUT) + print ('{0} Started!'.format(service)) + #output, _ = subprocess.Popen([DOCKER_COMPOSE, "up", "-d", service], stdout=log_file, stderr=subprocess.STDOUT).communicate() + #log_file.write(StringIO(output)) else: subprocess.call([DOCKER_COMPOSE, "up", "-d"]) @@ -84,8 +91,13 @@ def restart_container(service): def init_container(service): - subprocess.call([DOCKER_COMPOSE, "run", service, "INIT"]) - subprocess.call([DOCKER_COMPOSE, "rm", "-f", service]) + log_time_string = '\n\n{0}\n'.format(datetime.now().strftime('%m-%d-%Y %H:%M:%S')) + with open(LOGFILE, 'a') as log_file: + log_file.write(log_time_string) + print ('Initializing {0}...'.format(service)) + subprocess.call([DOCKER_COMPOSE, "run", service, "INIT"], stdout=log_file, stderr=subprocess.STDOUT) + subprocess.call([DOCKER_COMPOSE, "rm", "-f", service], stdout=log_file, stderr=subprocess.STDOUT) + print ("{0} initialized! \nRun 'cumulus start ' to start the container".format(service)) def display_logs(service, follow): @@ -177,6 +189,16 @@ def current_shell(): shell = "sh" return shell +# Clean garbage (docker ansi color logs) out of log file +def clean_logs(): + with open(LOGFILE, 'r+') as log_file: + lines = log_file.readlines() + log_file.seek(0) + for line in lines: + if '\x1b' not in line: + log_file.write(line) + log_file.truncate() + if __name__ == "__main__": main() From e7f4a6532f2505ab070558fbd43cffe8a369cda2 Mon Sep 17 00:00:00 2001 From: LeeZeitz Date: Wed, 11 Jul 2018 09:34:03 -0700 Subject: [PATCH 2/6] reorder log timestamp --- src/cumulus/cli.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/cumulus/cli.py b/src/cumulus/cli.py index e7cc539..928f9ba 100644 --- a/src/cumulus/cli.py +++ b/src/cumulus/cli.py @@ -91,9 +91,11 @@ def restart_container(service): def init_container(service): - log_time_string = '\n\n{0}\n'.format(datetime.now().strftime('%m-%d-%Y %H:%M:%S')) + with open(LOGFILE, 'a') as log_file: + log_time_string = '\n\n{0}\n'.format(datetime.now().strftime('%m-%d-%Y %H:%M:%S')) log_file.write(log_time_string) + with open(LOGFILE, 'a') as log_file: print ('Initializing {0}...'.format(service)) subprocess.call([DOCKER_COMPOSE, "run", service, "INIT"], stdout=log_file, stderr=subprocess.STDOUT) subprocess.call([DOCKER_COMPOSE, "rm", "-f", service], stdout=log_file, stderr=subprocess.STDOUT) From dd541457eb33af75a9630ed466aa4684766b26ea Mon Sep 17 00:00:00 2001 From: LeeZeitz Date: Fri, 13 Jul 2018 16:22:26 -0700 Subject: [PATCH 3/6] reorder routed init log messages --- src/cumulus/cli.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/cumulus/cli.py b/src/cumulus/cli.py index e7cc539..eab634f 100644 --- a/src/cumulus/cli.py +++ b/src/cumulus/cli.py @@ -94,6 +94,7 @@ def init_container(service): log_time_string = '\n\n{0}\n'.format(datetime.now().strftime('%m-%d-%Y %H:%M:%S')) with open(LOGFILE, 'a') as log_file: log_file.write(log_time_string) + with open(LOGFILE, 'a') as log_file: print ('Initializing {0}...'.format(service)) subprocess.call([DOCKER_COMPOSE, "run", service, "INIT"], stdout=log_file, stderr=subprocess.STDOUT) subprocess.call([DOCKER_COMPOSE, "rm", "-f", service], stdout=log_file, stderr=subprocess.STDOUT) From 79575a034b554c8cebdccfc89351b111755b240c Mon Sep 17 00:00:00 2001 From: LeeZeitz Date: Fri, 13 Jul 2018 16:24:32 -0700 Subject: [PATCH 4/6] remove commented code --- src/cumulus/cli.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/cumulus/cli.py b/src/cumulus/cli.py index 928f9ba..88a1cb7 100644 --- a/src/cumulus/cli.py +++ b/src/cumulus/cli.py @@ -54,7 +54,6 @@ def main(): """Main CLI entrypoint.""" import commands options = docopt(__doc__, version=VERSION) - for (k, v) in options.items(): if hasattr(commands, k) and v: module = getattr(commands, k) @@ -71,10 +70,8 @@ def main(): def start_container(service): if service: print ('Starting {0}...'.format(service)) - subprocess.call([DOCKER_COMPOSE, "up", "-d", service])#, stdout=log_file, stderr=subprocess.STDOUT) + subprocess.call([DOCKER_COMPOSE, "up", "-d", service]) print ('{0} Started!'.format(service)) - #output, _ = subprocess.Popen([DOCKER_COMPOSE, "up", "-d", service], stdout=log_file, stderr=subprocess.STDOUT).communicate() - #log_file.write(StringIO(output)) else: subprocess.call([DOCKER_COMPOSE, "up", "-d"]) From 870b8b0d21adf574afd3ae41f3993f448e7d0f4e Mon Sep 17 00:00:00 2001 From: LeeZeitz Date: Sun, 15 Jul 2018 17:08:38 -0700 Subject: [PATCH 5/6] check initialization success and log accordingly --- src/cumulus/cli.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/cumulus/cli.py b/src/cumulus/cli.py index 88a1cb7..20875ac 100644 --- a/src/cumulus/cli.py +++ b/src/cumulus/cli.py @@ -94,9 +94,12 @@ def init_container(service): log_file.write(log_time_string) with open(LOGFILE, 'a') as log_file: print ('Initializing {0}...'.format(service)) - subprocess.call([DOCKER_COMPOSE, "run", service, "INIT"], stdout=log_file, stderr=subprocess.STDOUT) - subprocess.call([DOCKER_COMPOSE, "rm", "-f", service], stdout=log_file, stderr=subprocess.STDOUT) - print ("{0} initialized! \nRun 'cumulus start ' to start the container".format(service)) + result1 = subprocess.run([DOCKER_COMPOSE, "run", service, "INIT"], stdout=log_file, stderr=subprocess.STDOUT) + result2 = subprocess.run([DOCKER_COMPOSE, "rm", "-f", service], stdout=log_file, stderr=subprocess.STDOUT) + if not result1.returncode and not result2.returncode: + print ("{0} initialized! \nRun 'cumulus start ' to start the container".format(service)) + else: + print ("Error initializing {0}. Check docker-compose-log.out for more details.".format(service)) def display_logs(service, follow): From a8be7b3375e875315b3e8600aeaf5305b112d696 Mon Sep 17 00:00:00 2001 From: LeeZeitz Date: Sun, 15 Jul 2018 17:39:20 -0700 Subject: [PATCH 6/6] remove whitespace --- src/cumulus/cli.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/cumulus/cli.py b/src/cumulus/cli.py index 16178f6..a13eec6 100644 --- a/src/cumulus/cli.py +++ b/src/cumulus/cli.py @@ -31,7 +31,6 @@ from docopt import docopt import os import subprocess - import prerequisite from collections import defaultdict