hobo/hobo/contrib/ozwillo/management/commands/ozwillo_worker.py

69 lines
2.5 KiB
Python

# Ozwillo plugin to deploy Publik
# Copyright (C) 2017 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import logging
from hobo.contrib.ozwillo.models import OzwilloInstance
from django.db.transaction import atomic
from django.core.management.base import BaseCommand
class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument('args', nargs='*')
@atomic
def handle(self, *args, **options):
qs = OzwilloInstance.objects.select_for_update()
# deployment
if args:
# allow deploying manually failed instances
to_deploy = qs.fiter(
domain_slug__in=args,
state__in=[OzwilloInstance.STATE_TO_DEPLOY, OzwilloInstance.STATE_DEPLOY_ERROR])
else:
# deploy everything to be deployed
to_deploy = qs.filter(
state=OzwilloInstance.STATE_TO_DEPLOY)
for instance in to_deploy:
try:
with atomic():
instance.deploy()
except Exception:
logger.exception(u'ozwillo: deploying %s from CRON failed', instance)
instance.deploy_error()
# destruction
if args:
# allow deploying manually failed instances
to_destroy = qs.filter(
domain_slug__in=args,
state__in=[OzwilloInstance.STATE_TO_DESTROY, OzwilloInstance.STATE_DESTROY_ERROR])
else:
# destroy everything to be destroyed
to_destroy = qs.filter(state=OzwilloInstance.STATE_TO_DESTROY)
for instance in to_destroy:
try:
with atomic():
instance.destroy()
except Exception:
logger.exception(u'ozwillo: destroying %s from CRON failed', instance)
instance.destroy_error()
logger = logging.getLogger(__name__)