summaryrefslogtreecommitdiffstats
path: root/create-dirty-tenant.py
blob: a6c13942012056d647cf0fff96adb6da6ce887c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/bin/sh -e

cd /home/thomas/univnautes-idp
. /home/thomas/venv/bin/activate
export UNIVNAUTES_IDP_SETTINGS_INI=/home/thomas/univnautes-idp/settings.ini.example 

TENANT=$(echo "$1" | tr -dc a-z0-9_-)
if [ x"$1" != x"$TENANT" ]
then
    echo $1 not a valid tenant name
    exit 1
fi

python << EOF
import os, sys
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "univnautes_idp.settings")
from entrouvert.djommon.multitenant.models import Tenant
if Tenant.objects.filter(schema_name="${TENANT}"):
    print >> sys.stderr, "tenant ${TENANT} already exists"
    sys.exit(1)
EOF

echo ""
echo "creating tenant..."
echo ""
python manage.py create-tenant $TENANT.univnautes-idp.dev.entrouvert.org $TENANT

echo ""
echo "creating 'admin' user in this tenant"
echo ""
python << EOF
import os, sys
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "univnautes_idp.settings")
from django.db import connection
from django.contrib.auth.models import User
connection.set_schema("${TENANT}")
u=User(username='admin')
u.set_password("${TENANT}")
u.is_staff=True
u.is_superuser=True
u.save()
print 'users:', User.objects.all()
EOF

echo ""
echo "tenant ${TENANT} created"