install: Don't create internal realm in the installation process.

This commit is contained in:
Mateusz Mandera 2020-02-12 16:39:12 +01:00 committed by Tim Abbott
parent bde495db87
commit 27b15a9722
5 changed files with 13 additions and 42 deletions

View File

@ -32,17 +32,6 @@ cd "$THIS_DIR/../.."
./manage.py migrate --noinput
./manage.py createcachetable third_party_api_results
if ! ./manage.py initialize_production_database; then
set +x
echo
echo -e '\033[32mPopulating default database failed.'
echo "After you fix the problem, you will need to do the following before rerunning this:"
echo " * supervisorctl stop all # to stop all services that might be accessing the database"
echo " * scripts/setup/postgres-init-db # run as root to drop and re-create the database"
echo -e '\033[0m'
exit 1
fi
# Check if the supervisor socket exists. If not, it could be:
#
# A) A normal installation went bad (supervisor hasn't started)

View File

@ -53,6 +53,7 @@ from zerver.lib.realm_logo import get_realm_logo_data
from zerver.lib.retention import move_messages_to_archive
from zerver.lib.send_email import send_email, FromAddress, send_email_to_admins, \
clear_scheduled_emails, clear_scheduled_invitation_emails
from zerver.lib.server_initialization import create_internal_realm, server_initialized
from zerver.lib.storage import static_path
from zerver.lib.stream_subscription import (
get_active_subscriptions_for_stream_id,
@ -3753,6 +3754,9 @@ def do_create_realm(string_id: str, name: str,
emails_restricted_to_domains: Optional[bool]=None) -> Realm:
if Realm.objects.filter(string_id=string_id).exists():
raise AssertionError("Realm %s already exists!" % (string_id,))
if not server_initialized():
logging.info("Server not yet initialized. Creating the internal realm first.")
create_internal_realm()
kwargs = {} # type: Dict[str, Any]
if emails_restricted_to_domains is not None:

View File

@ -1,6 +1,5 @@
from django.conf import settings
from zerver.lib.actions import do_change_is_admin
from zerver.lib.bulk_create import bulk_create_users
from zerver.models import Realm, UserProfile, email_to_username, get_client, \
get_system_bot
@ -8,9 +7,11 @@ from zerver.models import Realm, UserProfile, email_to_username, get_client, \
from typing import Iterable, Optional, Tuple
def server_initialized() -> bool:
return Realm.objects.count() > 0
return Realm.objects.exists()
def create_internal_realm() -> None:
from zerver.lib.actions import do_change_is_admin
realm = Realm.objects.create(string_id=settings.SYSTEM_BOT_REALM)
# Create the "website" and "API" clients:

View File

@ -1,29 +0,0 @@
from argparse import ArgumentParser
from typing import Any
from django.conf import settings
from django.core.management.base import BaseCommand
from zerver.lib.server_initialization import create_internal_realm, server_initialized
settings.TORNADO_SERVER = None
class Command(BaseCommand):
help = "Populate system realm and bots for a Zulip production server"
def add_arguments(self, parser: ArgumentParser) -> None:
parser.add_argument('--extra-users',
dest='extra_users',
type=int,
default=0,
help='The number of extra users to create')
def handle(self, *args: Any, **options: Any) -> None:
if server_initialized():
print("Database already initialized; doing nothing.")
return
create_internal_realm()
self.stdout.write("Successfully populated database with initial data.\n")
self.stdout.write("Please run ./manage.py generate_realm_creation_link "
"to generate link for creating organization")

View File

@ -35,6 +35,12 @@ class RealmTest(ZulipTestCase):
new_realm_name: str) -> None:
self.assertEqual(user_profile.realm.name, new_realm_name)
def test_realm_creation_ensures_internal_realms(self) -> None:
with mock.patch("zerver.lib.actions.server_initialized", return_value=False):
with mock.patch("zerver.lib.actions.create_internal_realm") as mock_create_internal:
do_create_realm("testrealm", "Test Realm")
mock_create_internal.assert_called_once()
def test_do_set_realm_name_caching(self) -> None:
"""The main complicated thing about setting realm names is fighting the
cache, and we start by populating the cache for Hamlet, and we end