Skip to content

Commit

Permalink
Allow to migrate from the install command
Browse files Browse the repository at this point in the history
  • Loading branch information
sevein committed Aug 16, 2024
1 parent 6b77650 commit 99b3d2d
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 44 deletions.
7 changes: 1 addition & 6 deletions hack/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,13 @@ manage: ## Run Django /manage.py on Dashboard, suppling <command> [options] as

bootstrap: ## Bootstrap the database.
$(call create_db,CCP)
docker compose run \
--rm \
--no-deps \
--entrypoint /src/worker/manage.py \
archivematica-worker \
migrate --noinput
docker compose run \
--rm \
--no-deps \
--entrypoint /src/worker/manage.py \
archivematica-worker \
install \
--migrate \
--username="test" \
--password="test" \
--email="test@test.com" \
Expand Down
101 changes: 63 additions & 38 deletions worker/worker/main/management/commands/install.py
Original file line number Diff line number Diff line change
@@ -1,51 +1,23 @@
import uuid
from argparse import BooleanOptionalAction

from django.contrib.auth import get_user_model
from django.core.management import call_command
from django.core.management.base import BaseCommand
from django.utils import termcolors
from tastypie.models import ApiKey

from worker.main.models import Agent
from worker.main.models import DashboardSetting


def create_super_user(username, email, password, key):
UserModel = get_user_model()
# Create the new super user if it doesn't already exist
try:
user = UserModel._default_manager.get(**{UserModel.USERNAME_FIELD: username})
except UserModel.DoesNotExist:
# User doesn't exist, create it
user = UserModel._default_manager.db_manager("default").create_superuser(
username, email, password
)
# Create or update the user's api key
ApiKey.objects.update_or_create(user=user, defaults={"key": key})


def setup_pipeline(org_name, org_identifier, site_url):
dashboard_uuid = get_setting("dashboard_uuid")
# Setup pipeline only if dashboard_uuid doesn't already exists
if dashboard_uuid:
return

# Assign UUID to Dashboard
dashboard_uuid = str(uuid.uuid4())
set_setting("dashboard_uuid", dashboard_uuid)

if org_name != "" or org_identifier != "":
agent = Agent.objects.default_organization_agent()
agent.name = org_name
agent.identifiertype = "repository code"
agent.identifiervalue = org_identifier
agent.save()

if site_url:
set_setting("site_url", site_url)


class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument(
"--migrate",
default=False,
action=BooleanOptionalAction,
help="Apply migrations.",
)
parser.add_argument("--username", required=True)
parser.add_argument("--email", required=True)
parser.add_argument("--password", required=True)
Expand All @@ -61,19 +33,37 @@ def add_arguments(self, parser):
parser.add_argument("--site-url", required=False)

def handle(self, *args, **options):
# Not needed in Django 1.9+.
self.style.SUCCESS = termcolors.make_style(opts=("bold",), fg="green")
if options["migrate"] is True:
self.print_banner("Apply migrations")
call_command("migrate", interactive=False)
self.print_done()

self.print_banner("Set up pipeline")
setup_pipeline(options["org_name"], options["org_id"], options["site_url"])
self.print_done()

self.print_banner("Create superuser")
create_super_user(
options["username"],
options["email"],
options["password"],
options["api_key"],
)
self.print_done()

self.print_banner("Set up API allowlist")
set_api_allowlist(options["whitelist"] or options["allowlist"])
self.print_done()

def print_done(self):
self.stdout.write(self.style.SUCCESS("Done!\n"))

def print_banner(self, text):
length = len(text)
border = "+" + "-" * (length + 2) + "+"
middle = f"+ {text} +"
self.stdout.write(f"\n{border}\n{middle}\n{border}\n")


def get_setting(setting, default=""):
try:
Expand Down Expand Up @@ -106,3 +96,38 @@ def set_api_allowlist(allowlist):
if not allowlist:
allowlist = ""
return set_setting("api_whitelist", allowlist)


def create_super_user(username, email, password, key):
UserModel = get_user_model()
# Create the new super user if it doesn't already exist
try:
user = UserModel._default_manager.get(**{UserModel.USERNAME_FIELD: username})
except UserModel.DoesNotExist:
# User doesn't exist, create it
user = UserModel._default_manager.db_manager("default").create_superuser(
username, email, password
)
# Create or update the user's api key
ApiKey.objects.update_or_create(user=user, defaults={"key": key})


def setup_pipeline(org_name, org_identifier, site_url):
dashboard_uuid = get_setting("dashboard_uuid")
# Setup pipeline only if dashboard_uuid doesn't already exists
if dashboard_uuid:
return

# Assign UUID to Dashboard
dashboard_uuid = str(uuid.uuid4())
set_setting("dashboard_uuid", dashboard_uuid)

if org_name != "" or org_identifier != "":
agent = Agent.objects.default_organization_agent()
agent.name = org_name
agent.identifiertype = "repository code"
agent.identifiervalue = org_identifier
agent.save()

if site_url:
set_setting("site_url", site_url)

0 comments on commit 99b3d2d

Please sign in to comment.