summaryrefslogtreecommitdiff
path: root/ishtar-prepare-instance
blob: f91dbd9db536e6a738e8a088049d3844278946ec (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/bin/sh
#
# Stripped-down version of the install.sh script, only dealing with
# customizing files shipped through a Debian package which also pulls
# a bunch of dependencies.
set -e
set -u

# Instance then project names:
INSTANCE=${INSTANCE-iggdrasil}
PROJECT=${PROJECT-$INSTANCE}
URL=${URL-localhost}
DEST=/var/lib/python-django-ishtar
INSTANCES_FILE=$DEST/INSTANCES

echo "*** Preparing ishtar (instance: $INSTANCE, project: $PROJECT) under $DEST ***"

export LANG=fr_FR.UTF-8
if [ "$(locale 2>&1 >/dev/null|wc -l)" != 0 ]; then
    echo "Unable to set LANG=$LANG properly"
    echo "Try: 'dpkg-reconfigure locales' or install the 'locales-all' package"
    exit 1
fi

# XXX: Should probably ship those from the package, and make them
#      either ishtar or python-django-ishtar, or django/ishtar
#      alternatively:
mkdir -p            /var/log/django
chown root:www-data /var/log/django
touch               "/var/log/django/ishtar-$INSTANCE.log"
chown root:www-data "/var/log/django/ishtar-$INSTANCE.log"
chmod g+w           "/var/log/django/ishtar-$INSTANCE.log"


# Duplicate example_project into our instance:
cd $DEST
if [ -d "$INSTANCE" ]; then
    echo "Sorry, $INSTANCE already exists, not going further"
    exit 1
fi
cp -ra example_project $INSTANCE

rm $INSTANCE/settings.py
ln -s $DEST/example_project/settings.py $DEST/$INSTANCE/settings.py

# Preparing DB:
PG_VERSION=9.1
DB_HOST=${DB_HOST-127.0.0.1}
DB_PORT=${DB_PORT-5432}
DB_PASSWORD=${DB_PASSWORD-''}
DB_NAME="ishtar-$INSTANCE"

# Generate a password on the fly if none was specified:
if [ -z "$DB_PASSWORD" ]; then
    DB_PASSWORD=$(apg -a 0 -M ncl -n 1 -x 10 -m 10)
fi

export PG_VERSION DB_HOST DB_PORT DB_PASSWORD DB_NAME PROJECT
su postgres <<'EOF'
  echo "Checking template_postgis"
  if ! psql -l | grep -qs template_postgis; then
    echo " - not present, creating"
    createdb template_postgis
    psql -q -d template_postgis -f /usr/share/postgresql/$PG_VERSION/contrib/postgis-1.5/postgis.sql
    psql -q -d template_postgis -f /usr/share/postgresql/$PG_VERSION/contrib/postgis-1.5/spatial_ref_sys.sql
  else
    echo " - already present"
  fi

  echo "Checking $DB_NAME"
  if ! psql -l | grep -qs "$DB_NAME"; then
    echo " - not present, creating"
    createuser --echo --adduser --createdb --encrypted $DB_NAME
    psql --command "ALTER USER \""$DB_NAME"\" with password '"$DB_PASSWORD"';"
    createdb -T template_postgis --echo --owner $DB_NAME --encoding UNICODE $DB_NAME "$PROJECT"
  else
    echo " - already present"
  fi
EOF


# Permissions:
mkdir -p -m 755     "$INSTANCE/media"
mkdir -p -m 755     "$INSTANCE/media/imported"
mkdir -p -m 755     "$INSTANCE/media/upload"
chown -R www-data:www-data "$INSTANCE/media"

### LOCAL SETTINGS

# Set some variables to avoid changing sed calls too much compared to
# the initial install/install.sh script:
INSTALL_PATH=$DEST
INSTALL_PREFIX=$DEST
APP_DIR="$DEST/$INSTANCE"
DATE=`date +%F`
SECRET_KEY=$(apg -a 0 -M ncl -n 1 -x 10 -m 40)

# Declare these in advance, at least the port is needed:
UWSGI_PORT=${UWSGI_PORT-8891}
UWSGI_AVAILABLE_PATH='/etc/uwsgi/apps-available'
UWSGI_ENABLE_PATH='/etc/uwsgi/apps-enabled'

sed -s "s|#APP_NAME#|$INSTANCE|g;\
        s|#INSTALL_PATH#|$INSTALL_PATH|g;\
        s|#DATE#|$DATE|g;\
        s|#PROJECT_NAME#|$PROJECT|g;\
        s|#DB_HOST#|$DB_HOST|g;\
        s|#DB_NAME#|$DB_NAME|g;\
        s|#DB_PORT#|$DB_PORT|g;\
        s|#APP_DIR#|$APP_DIR|g;\
        s|#SECRET_KEY#|$SECRET_KEY|g;\
        s|#DB_PASSWORD#|$DB_PASSWORD|g;\
        s|#UWSGI_PORT#|$UWSGI_PORT|g;" \
    "install/local_settings.py.sample" > \
    "$INSTANCE/local_settings.py"

# Collect static data:
cd $INSTANCE
./manage.py collectstatic --noinput
cd -

# Only language available:
LOCALE=fr
for d in archaeological_* ishtar_common; do
    cd $d
    ../$INSTANCE/manage.py compilemessages -l $LOCALE
    cd -
done

### DB feeding
cd $INSTANCE
./manage.py syncdb --noinput
./manage.py migrate
./manage.py flush --noinput

FIXTURES="$DEST/fixtures/initial_data-auth-fr.json $DEST/ishtar_common/fixtures/initial_data-fr.json $DEST/ishtar_common/fixtures/initial_importtypes-fr.json $DEST/archaeological_operations/fixtures/initial_data-fr.json $DEST/archaeological_context_records/fixtures/initial_data-fr.json $DEST/archaeological_files/fixtures/initial_data-fr.json $DEST/archaeological_finds/fixtures/initial_data-fr.json $DEST/archaeological_warehouse/fixtures/initial_data-fr.json"
for data in $FIXTURES; do
    echo $data;
    ./manage.py loaddata $data;
done

./manage.py createsuperuser
./manage.py migrate --fake
cd -

echo "*** DB all done ***"


### UWSGI

sed -s "s|#APP_NAME#|$INSTANCE|g;\
        s|#DB_NAME#|$DB_NAME|g;\
        s|#INSTALL_PREFIX#|$INSTALL_PREFIX|g;\
        s|#URL#|$URL|g;\
        s|#UWSGI_PORT#|$UWSGI_PORT|g;" \
    "install/uwsgi.ini.template" > \
    "$INSTANCE/uwsgi.ini"

# XXX: Generation of django.wsgi was entirely dropped.

# XXX: Using -f might be a bit too aggressive
ln -sf "$DEST/$INSTANCE/uwsgi.ini" \
       "$UWSGI_AVAILABLE_PATH/ishtar-$INSTANCE.ini"
ln -sf "$UWSGI_AVAILABLE_PATH/ishtar-$INSTANCE.ini" \
       "$UWSGI_ENABLE_PATH/ishtar-$INSTANCE.ini"


### NGINX

NGINX_PORT=${NGINX_PORT-80}
NGINX_AVAILABLE_PATH='/etc/nginx/sites-available'
NGINX_ENABLE_PATH='/etc/nginx/sites-enabled'

sed -s "s|#APP_NAME#|$INSTANCE|g;\
        s|#UWSGI_PORT#|$UWSGI_PORT|g;\
        s|#DB_NAME#|$DB_NAME|g;\
        s|#DATE#|$DATE|g;\
        s|#NGINX_PORT#|$NGINX_PORT|g;\
        s|#INSTALL_PREFIX#|$INSTALL_PREFIX|g;\
        s|#URL#|$URL|g;" \
    "install/nginx.conf.template" > \
    "$INSTANCE/nginx.conf"

ln -sf "$DEST/$INSTANCE/nginx.conf" \
       "$NGINX_AVAILABLE_PATH/ishtar-$INSTANCE.conf"
ln -sf "$NGINX_AVAILABLE_PATH/ishtar-$INSTANCE.conf" \
       "$NGINX_ENABLE_PATH/ishtar-$INSTANCE.conf"

echo "*** uwsgi and nginx configured, restart them and you're done ***"
printf "\n    /etc/init.d/uwsgi restart\n    /etc/init.d/nginx restart\n\n Enjoy ishtar!\n"

# Register instance:
echo "$INSTANCE" >> $INSTANCES_FILE