From e876cd446141a5d1edf7084055fa20f13652badf Mon Sep 17 00:00:00 2001 From: Marc Aymerich Date: Mon, 11 Jan 2016 10:23:19 +0000 Subject: [PATCH] Added orchestrastatus management command --- TODO.md | 3 + .../management/commands/orchestrastatus.py | 67 +++++++++++++++++++ .../management/commands/startservices.py | 2 +- 3 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 orchestra/management/commands/orchestrastatus.py diff --git a/TODO.md b/TODO.md index 3c038876..6973ecf6 100644 --- a/TODO.md +++ b/TODO.md @@ -452,3 +452,6 @@ mkhomedir_helper or create ssh homes with bash.rc and such # Reverse lOgHistory order by date (lastest first) + + +* orchestrastatus command, showing if it is runningg, in what URL and stuff diff --git a/orchestra/management/commands/orchestrastatus.py b/orchestra/management/commands/orchestrastatus.py new file mode 100644 index 00000000..8bb9284e --- /dev/null +++ b/orchestra/management/commands/orchestrastatus.py @@ -0,0 +1,67 @@ +import re + +from django.conf import settings as djsettings +from django.core.management.base import BaseCommand +from django.db import connections + +from orchestra import settings +from orchestra.utils import paths +from orchestra.utils.sys import run + +from .startservices import flatten + + +class Command(BaseCommand): + def is_running(self, context, ps): + if context['service'] == 'uwsgi': + regex = r'.*uwsgi .*/%(project_name)s.ini.*' % context + else: + regex = r'.*%(service)s.*' % context + return re.match(regex, ps) + + def handle(self, *args, **options): + context = { + 'project_name': paths.get_project_name(), + 'site_dir': paths.get_site_dir(), + } + banner = "%(project_name)s status" % context + self.stdout.write(banner) + self.stdout.write('-'*len(banner)) + if djsettings.DEBUG: + self.stdout.write(" debug enabled") + else: + self.stdout.write(" debug disabled") + ps = run('ps aux').stdout.decode().replace('\n', ' ') + for service in flatten(settings.ORCHESTRA_START_SERVICES): + context['service'] = service + if self.is_running(context, ps): + self.stdout.write(" %(service)s online" % context) + else: + self.stdout.write(" %(service)s offline" % context) + if service == 'nginx': + try: + config_path = '/etc/nginx/conf.d/%(project_name)s.conf' % context + with open(config_path, 'r') as handler: + config = handler.read().replace('\n', ' ') + except FileNotFoundError: + self.stdout.write(" * %s not found" % config_path) + else: + regex = r'location\s+([^\s]+)\s+{.*uwsgi_pass unix:///var/run/uwsgi/app/%(project_name)s/socket;.*' % context + location = re.findall(regex, config) + if location: + self.stdout.write(" * location %s" % location[0]) + else: + self.stdout.write(" * location not found") + elif service == 'postgresql': + db_conn = connections['default'] + try: + c = db_conn.cursor() + except OperationalError: + self.stdout.write(" * DB connection failed") + else: + self.stdout.write(" * DB connection succeeded") + cronbeat = 'crontab -l | grep "^.*/orchestra-beat %(site_dir)s/manage.py"' % context + if run(cronbeat, valid_codes=(0, 1)).exit_code == 0: + self.stdout.write(" cronbeat installed") + else: + self.stdout.write(" cronbeat not installed") diff --git a/orchestra/management/commands/startservices.py b/orchestra/management/commands/startservices.py index 4764ce60..6bfe9f17 100644 --- a/orchestra/management/commands/startservices.py +++ b/orchestra/management/commands/startservices.py @@ -12,7 +12,7 @@ def run_tuple(services, action, options, optional=False): for service in services: if options.get(service): valid_codes = (0,1) if optional else (0,) - e = run('service %s %s' % (service, action), valid_codes=valid_codes) + e = run('service %s %s' % (service, action), valid_codes=valid_codes, display=True) if e.exit_code == 1: return False return True