2017-11-16 00:43:27 +01:00
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
from django.conf import settings
|
2016-04-05 00:27:37 +02:00
|
|
|
from django.core.management import call_command
|
2020-06-11 00:54:34 +02:00
|
|
|
from django.core.management.base import BaseCommand, CommandError, CommandParser
|
2016-04-05 00:27:37 +02:00
|
|
|
|
2018-04-06 02:21:16 +02:00
|
|
|
from zerver.forms import check_subdomain_available
|
2020-01-14 21:59:46 +01:00
|
|
|
from zerver.lib.import_realm import do_import_realm, do_import_system_bots
|
|
|
|
|
2016-09-12 00:49:00 +02:00
|
|
|
|
2016-04-05 00:27:37 +02:00
|
|
|
class Command(BaseCommand):
|
2018-05-28 15:29:21 +02:00
|
|
|
help = """Import extracted Zulip database dump directories into a fresh Zulip instance.
|
2016-04-05 00:27:37 +02:00
|
|
|
|
|
|
|
This command should be used only on a newly created, empty Zulip instance to
|
2017-10-06 17:45:22 +02:00
|
|
|
import a database dump from one or more JSON files."""
|
2016-04-05 00:27:37 +02:00
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def add_arguments(self, parser: CommandParser) -> None:
|
2016-11-03 10:22:19 +01:00
|
|
|
parser.add_argument('--destroy-rebuild-database',
|
|
|
|
dest='destroy_rebuild_database',
|
|
|
|
default=False,
|
|
|
|
action="store_true",
|
|
|
|
help='Destroys and rebuilds the databases prior to import.')
|
|
|
|
|
|
|
|
parser.add_argument('--import-into-nonempty',
|
|
|
|
dest='import_into_nonempty',
|
|
|
|
default=False,
|
|
|
|
action="store_true",
|
|
|
|
help='Import into an existing nonempty database.')
|
2017-10-06 17:45:22 +02:00
|
|
|
|
2018-04-06 02:21:16 +02:00
|
|
|
parser.add_argument('subdomain', metavar='<subdomain>',
|
|
|
|
type=str, help="Subdomain")
|
|
|
|
|
2018-05-28 15:29:21 +02:00
|
|
|
parser.add_argument('export_paths', nargs='+',
|
|
|
|
metavar='<export path>',
|
|
|
|
help="list of export directories to import")
|
2019-01-25 20:40:49 +01:00
|
|
|
parser.add_argument('--processes',
|
|
|
|
dest='processes',
|
|
|
|
action="store",
|
2020-06-21 13:18:08 +02:00
|
|
|
default=settings.DEFAULT_DATA_EXPORT_IMPORT_PARALLELISM,
|
2019-01-25 20:40:49 +01:00
|
|
|
help='Number of processes to use for uploading Avatars to S3 in parallel')
|
2017-10-06 17:41:01 +02:00
|
|
|
parser.formatter_class = argparse.RawTextHelpFormatter
|
2016-04-05 00:27:37 +02:00
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def do_destroy_and_rebuild_database(self, db_name: str) -> None:
|
2016-04-05 00:27:37 +02:00
|
|
|
call_command('flush', verbosity=0, interactive=False)
|
|
|
|
subprocess.check_call([os.path.join(settings.DEPLOY_ROOT, "scripts/setup/flush-memcached")])
|
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def handle(self, *args: Any, **options: Any) -> None:
|
2019-01-25 20:40:49 +01:00
|
|
|
num_processes = int(options['processes'])
|
|
|
|
if num_processes < 1:
|
|
|
|
raise CommandError('You must have at least one process.')
|
|
|
|
|
2018-04-06 02:21:16 +02:00
|
|
|
subdomain = options['subdomain']
|
|
|
|
|
2016-04-05 00:27:37 +02:00
|
|
|
if options["destroy_rebuild_database"]:
|
|
|
|
print("Rebuilding the database!")
|
|
|
|
db_name = settings.DATABASES['default']['NAME']
|
|
|
|
self.do_destroy_and_rebuild_database(db_name)
|
2018-05-28 15:29:21 +02:00
|
|
|
elif options["import_into_nonempty"]:
|
|
|
|
print("NOTE: The argument 'import_into_nonempty' is now the default behavior.")
|
2016-04-05 00:27:37 +02:00
|
|
|
|
2018-04-23 20:02:45 +02:00
|
|
|
check_subdomain_available(subdomain, from_management_command=True)
|
2018-04-20 19:38:33 +02:00
|
|
|
|
2018-08-13 22:30:43 +02:00
|
|
|
paths = []
|
2018-05-28 15:29:21 +02:00
|
|
|
for path in options['export_paths']:
|
2018-08-13 22:30:43 +02:00
|
|
|
path = os.path.realpath(os.path.expanduser(path))
|
2016-04-05 00:27:37 +02:00
|
|
|
if not os.path.exists(path):
|
2020-06-10 06:41:04 +02:00
|
|
|
raise CommandError(f"Directory not found: '{path}'")
|
2018-05-28 15:29:21 +02:00
|
|
|
if not os.path.isdir(path):
|
2019-05-03 23:20:39 +02:00
|
|
|
raise CommandError("Export file should be folder; if it's a "
|
|
|
|
"tarball, please unpack it first.")
|
2018-08-13 22:30:43 +02:00
|
|
|
paths.append(path)
|
2016-04-05 00:27:37 +02:00
|
|
|
|
2018-08-13 22:30:43 +02:00
|
|
|
for path in paths:
|
2020-06-10 06:41:04 +02:00
|
|
|
print(f"Processing dump: {path} ...")
|
2019-01-25 20:40:49 +01:00
|
|
|
realm = do_import_realm(path, subdomain, num_processes)
|
2017-12-15 13:34:48 +01:00
|
|
|
print("Checking the system bots.")
|
|
|
|
do_import_system_bots(realm)
|