2013-10-17 16:33:04 +02:00
|
|
|
from __future__ import absolute_import
|
2015-11-01 17:11:06 +01:00
|
|
|
from __future__ import print_function
|
2016-06-04 16:52:18 +02:00
|
|
|
|
2013-10-17 16:33:04 +02:00
|
|
|
from optparse import make_option
|
|
|
|
import sys
|
2016-06-04 16:52:18 +02:00
|
|
|
from typing import Any
|
2013-10-17 16:33:04 +02:00
|
|
|
|
2016-11-03 10:22:19 +01:00
|
|
|
from django.core.management.base import BaseCommand, CommandParser
|
2013-10-17 16:33:04 +02:00
|
|
|
|
2016-11-16 21:16:02 +01:00
|
|
|
from zerver.models import get_realm_by_string_id
|
2013-10-17 16:33:04 +02:00
|
|
|
from zerver.lib.create_user import random_api_key
|
|
|
|
from zerver.management.commands.create_realm import Command as CreateRealm
|
|
|
|
|
|
|
|
from zilencer.models import Deployment
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
help = """Create a deployment and accompanying realm."""
|
|
|
|
|
2016-11-03 10:22:19 +01:00
|
|
|
def add_arguments(self, parser):
|
|
|
|
# type: (CommandParser) -> None
|
|
|
|
parser.add_argument('--no-realm',
|
|
|
|
dest='no_realm',
|
|
|
|
action='store_true',
|
|
|
|
default=False,
|
|
|
|
help='Do not create a new realm; associate with '
|
|
|
|
'an existing one. In this case, only the '
|
2016-11-16 21:16:02 +01:00
|
|
|
'realm and URLs need to be specified.')
|
2016-11-03 10:22:19 +01:00
|
|
|
|
|
|
|
parser.add_argument('-a', '--api-url', dest='api', type=str)
|
|
|
|
parser.add_argument('-w', '--web-url', dest='web', type=str)
|
2013-10-17 16:33:04 +02:00
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
2016-06-04 16:52:18 +02:00
|
|
|
# type: (*Any, **Any) -> None
|
2016-11-16 21:16:02 +01:00
|
|
|
if None in (options["api"], options["web"], options["string_id"]):
|
|
|
|
print("\033[1;31mYou must provide a subdomain or string_id, an API URL, and a web URL.\033[0m\n", file=sys.stderr)
|
2016-11-22 01:44:16 +01:00
|
|
|
self.print_help("./manage.py", "create_realm")
|
2013-10-17 16:33:04 +02:00
|
|
|
exit(1)
|
|
|
|
|
|
|
|
if not options["no_realm"]:
|
|
|
|
CreateRealm().handle(*args, **options)
|
2015-11-01 17:11:06 +01:00
|
|
|
print() # Newline
|
2013-10-17 16:33:04 +02:00
|
|
|
|
2016-11-16 21:16:02 +01:00
|
|
|
realm = get_realm_by_string_id(options["string_id"])
|
2013-10-17 16:33:04 +02:00
|
|
|
if realm is None:
|
2015-11-01 17:11:06 +01:00
|
|
|
print("\033[1;31mRealm does not exist!\033[0m\n", file=sys.stderr)
|
2013-10-17 16:33:04 +02:00
|
|
|
exit(2)
|
|
|
|
|
|
|
|
dep = Deployment()
|
|
|
|
dep.api_key = random_api_key()
|
|
|
|
dep.save()
|
2013-10-25 23:24:26 +02:00
|
|
|
old_dep = realm.deployment
|
|
|
|
if old_dep is not None:
|
|
|
|
old_dep.realms.remove(realm)
|
|
|
|
old_dep.save()
|
2013-10-17 16:33:04 +02:00
|
|
|
dep.realms = [realm]
|
2013-10-25 23:35:24 +02:00
|
|
|
dep.base_api_url = options["api"]
|
2013-11-14 16:15:25 +01:00
|
|
|
dep.base_site_url = options["web"]
|
2013-10-17 16:33:04 +02:00
|
|
|
dep.save()
|
2015-11-01 17:11:06 +01:00
|
|
|
print("Deployment %s created." % (dep.id,))
|
|
|
|
print("DEPLOYMENT_ROLE_NAME = %s" % (dep.name,))
|
|
|
|
print("DEPLOYMENT_ROLE_KEY = %s" % (dep.api_key,))
|