django-orchestra/orchestra/management/commands/startservices.py

63 lines
2.0 KiB
Python
Raw Normal View History

2014-05-08 16:59:35 +00:00
from django.core.management.base import BaseCommand
2015-10-01 17:11:05 +00:00
from orchestra import settings
2015-04-05 10:46:24 +00:00
from orchestra.utils.sys import run, check_root
2014-05-08 16:59:35 +00:00
def run_tuple(services, action, options, optional=False):
if isinstance(services, str):
services = [services]
for service in services:
if options.get(service):
2015-05-09 17:08:45 +00:00
valid_codes = (0,1) if optional else (0,)
e = run('service %s %s' % (service, action), valid_codes=valid_codes, display=True)
2015-05-09 17:08:45 +00:00
if e.exit_code == 1:
2014-05-08 16:59:35 +00:00
return False
return True
def flatten(nested, depth=0):
2015-04-26 13:53:00 +00:00
if isinstance(nested, (list, tuple)):
2014-05-08 16:59:35 +00:00
for sublist in nested:
for element in flatten(sublist, depth+1):
yield element
else:
yield nested
class ManageServiceCommand(BaseCommand):
def __init__(self, *args, **kwargs):
super(ManageServiceCommand, self).__init__(*args, **kwargs)
2021-01-30 13:59:49 +00:00
def add_arguments(self, parser):
for service in flatten(self.services):
parser.add_argument(
'--no-%s' % service,
action='store_false',
dest=service,
default=True,
help='Do not %s %s' % (self.action, service)
2014-05-08 16:59:35 +00:00
)
@check_root
def handle(self, *args, **options):
for service in self.services:
if isinstance(service, str):
run_tuple(service, self.action, options)
else:
failure = True
for opt_service in service:
if run_tuple(opt_service, self.action, options, optional=True):
failure = False
break
if failure:
str_services = [ str(s) for s in service ]
self.stderr.write('Error %sing %s' % (self.action, ' OR '.join(str_services)))
class Command(ManageServiceCommand):
2015-10-01 17:11:05 +00:00
services = settings.ORCHESTRA_START_SERVICES
2014-05-08 16:59:35 +00:00
action = 'start'
help = 'Start all related services. Usefull for reload configuration and files.'