Add Python scripts
authorLorin Hochstein <lhochstein@netflix.com>
Tue, 24 Jan 2017 06:00:44 +0000 (22:00 -0800)
committerLorin Hochstein <lhochstein@netflix.com>
Tue, 24 Jan 2017 06:00:44 +0000 (22:00 -0800)
ch13/ansible/roles/mezzanine/files/setadmin.py [new file with mode: 0644]
ch13/ansible/roles/mezzanine/files/setsite.py [new file with mode: 0644]

diff --git a/ch13/ansible/roles/mezzanine/files/setadmin.py b/ch13/ansible/roles/mezzanine/files/setadmin.py
new file mode 100644 (file)
index 0000000..4ddba5b
--- /dev/null
@@ -0,0 +1,25 @@
+#!/usr/bin/env python
+# A script to set the admin credentials
+# Assumes two environment variables
+#
+# PROJECT_DIR: the project directory (e.g., ~/projname)
+# PROJECT_APP: name of the project app
+# ADMIN_PASSWORD: admin user's password
+
+import os
+import sys
+
+# Add the project directory to system path
+proj_dir = os.path.expanduser(os.environ['PROJECT_DIR'])
+sys.path.append(proj_dir)
+
+proj_app = os.environ['PROJECT_APP']
+os.environ['DJANGO_SETTINGS_MODULE'] = proj_app + '.settings'
+import django
+django.setup()
+from django.contrib.auth import get_user_model
+User = get_user_model()
+u, _ = User.objects.get_or_create(username='admin')
+u.is_staff = u.is_superuser = True
+u.set_password(os.environ['ADMIN_PASSWORD'])
+u.save()
diff --git a/ch13/ansible/roles/mezzanine/files/setsite.py b/ch13/ansible/roles/mezzanine/files/setsite.py
new file mode 100644 (file)
index 0000000..83e3d8b
--- /dev/null
@@ -0,0 +1,23 @@
+#!/usr/bin/env python
+# A script to set the site domain
+# Assumes two environment variables
+#
+# WEBSITE_DOMAIN: the domain of the site (e.g., www.example.com)
+# PROJECT_DIR: root directory of the project
+# PROJECT_APP: name of the project app
+import os
+import sys
+
+# Add the project directory to system path
+proj_dir = os.path.expanduser(os.environ['PROJECT_DIR'])
+sys.path.append(proj_dir)
+
+proj_app = os.environ['PROJECT_APP']
+os.environ['DJANGO_SETTINGS_MODULE'] = proj_app + '.settings'
+import django
+django.setup()
+from django.conf import settings
+from django.contrib.sites.models import Site
+domain = os.environ['WEBSITE_DOMAIN']
+Site.objects.filter(id=settings.SITE_ID).update(domain=domain)
+Site.objects.get_or_create(domain=domain)