diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2017-05-01 21:05:11 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2017-05-01 21:05:11 +0200 |
commit | 183591d0e09272c0eadb2fc928370ccd965bfd91 (patch) | |
tree | 7609fc700402e10f7fe27a2c7df751d6c8dc8a71 /install/ishtar-prepare-instance | |
parent | e0cc2800330d92ad577384ea72659427918f589d (diff) | |
download | Ishtar-183591d0e09272c0eadb2fc928370ccd965bfd91.tar.bz2 Ishtar-183591d0e09272c0eadb2fc928370ccd965bfd91.zip |
Update installation scripts
Diffstat (limited to 'install/ishtar-prepare-instance')
-rwxr-xr-x | install/ishtar-prepare-instance | 321 |
1 files changed, 321 insertions, 0 deletions
diff --git a/install/ishtar-prepare-instance b/install/ishtar-prepare-instance new file mode 100755 index 000000000..f35aa2b02 --- /dev/null +++ b/install/ishtar-prepare-instance @@ -0,0 +1,321 @@ +#!/bin/bash + +set -e + +do_install_instance() { + + NGINX_PORT=${NGINX_PORT-80} + NGINX_AVAILABLE_PATH='/etc/nginx/sites-available' + NGINX_ENABLE_PATH='/etc/nginx/sites-enabled' + UWSGI_AVAILABLE_PATH='/etc/uwsgi/apps-available' + UWSGI_ENABLE_PATH='/etc/uwsgi/apps-enabled' + PG_VERSION=9.4 + POSTGIS_VERSION=2.3.1 + + cat >&2 <<-'EOF' + +******************************************************************************* +++++++ Ishtar instance installation script ++++++ +******************************************************************************* + +EOF + + # check user + user="$(id -un 2>/dev/null || true)" + + sh_c='sh -c' + if [ "$user" != 'root' ]; then + if command_exists sudo; then + sh_c='sudo -E sh -c' + elif command_exists su; then + sh_c='su -c' + else + cat >&2 <<-'EOF' + Error: this installer needs the ability to run commands as root. + We are unable to find either "sudo" or "su" available to make this happen. +EOF + exit 1 + fi + fi + + 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 + + if [ ! -z '$CONFIG_PATH' ]; then + CONFIG_PATH="/etc/ishtar/" + fi + + if [ ! -f $CONFIG_PATH/config ]; then + echo ""; + echo ""$CONFIG_PATH" is not a valid config file." + echo "Have you properly install Ishtar sources?" + echo "Run ishtar-install before this script."; + echo ""; + exit 1; + fi + + source $CONFIG_PATH/config + + cd $ISHTAR_PATH + + INSTANCES_FILE=$CONFIG_PATH/instances + + if [ ! -f $INSTANCES_FILE ]; then + touch $INSTANCES_FILE + fi + + if [ -z '$INSTANCE' ]; then + if [ -d "$INSTANCE" ]; then + echo "Sorry, $INSTANCE already exists. Give another name." + exit 1 + fi + + else + INSTANCE='' + cat >&2 <<-'EOF' + +------------------------------------------------------------------------------- + You should select a code name for this instance. This code name should have + only lower alphanumeric characters, with no spaces, no accents and should not + begin with an alphabetical character. The only special character allowed is + "_". + +EOF + while [ "$INSTANCE" == '' ] + do + read -p "* Which instance code name? [my_ishtar_instance] " choice + if [ -z "$choice" ]; then + INSTANCE='my_ishtar_instance' + else + INSTANCE=$choice + fi + if [ -d "$INSTANCE" ]; then + echo "Sorry, $INSTANCE already exists. Give another name." + INSTANCE='' + fi + done + fi + + if [ ! -z '$URL' ]; then + URL='' + cat >&2 <<-'EOF' + +------------------------------------------------------------------------------- + You should select an url to join your instance. Only a full domain is + accepted. Don't forget to set up your DNS to point this url name to this + server. Only put the url not the protocol part (no http://). + For instance: ishtar.mydomain.org + +EOF + while [ "$URL" == '' ] + do + read -p "* Which url? " choice + URL=$choice + done + fi + + DEST=$ISHTAR_PATH + + cat >&2 <<-'EOF' + +------------------------------------------------------------------------------- +EOF + echo "Preparing ishtar instance: $INSTANCE under $DEST" + echo "" + # register instance + echo "$INSTANCE" >> $INSTANCES_FILE + + 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" + + + echo " * duplicate example_project into our instance" + # Duplicate example_project into our instance: + cd $ISHTAR_PATH + cp -ra example_project $INSTANCE + + rm $INSTANCE/settings.py + ln -s $DEST/example_project/settings.py $DEST/$INSTANCE/settings.py + + # 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" + + # Preparing DB: + DB_HOST=${ISHTAR_DB-127.0.0.1} + DB_PORT=${ISHTAR_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 POSTGIS_VERSION + su postgres <<'EOF' + echo " * Checking template_postgis" + if ! psql -l | grep -qs template_postgis; then + echo " - not present, creating" + createdb -E UTF8 template_postgis + psql -d postgres -c "UPDATE pg_database SET datistemplate='true' WHERE datname='template_postgis'" + psql -q -d template_postgis -f /usr/share/postgresql/$PG_VERSION/extensions/postgis--$POSTGIS_VERSION.sql + psql -d template_postgis -c "GRANT ALL ON geometry_columns TO PUBLIC;" + psql -d template_postgis -c "GRANT ALL ON geography_columns TO PUBLIC;" + psql -d template_postgis -c "GRANT ALL ON spatial_ref_sys TO PUBLIC;" + else + echo " - already present" + fi + + echo " * Checking database $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 + psql -d $DB_NAME -c "CREATE EXTENSION postgis;" + else + echo " - already present" + fi +EOF + + ### LOCAL SETTINGS + + echo " * creating config files" + # 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) + + PORT_FILE=$CONFIG_PATH/last_uswgi_port + + if [ -f $PORT_FILE ]; then + UWSGI_PORT=`cat $PORT_FILE` + UWSGI_PORT=`expr $UWSGI_PORT + 1` + else + UWSGI_PORT=8889 + fi + echo $UWSGI_PORT > $PORT_FILE + + ### local_settings.py + + sed -s "s|#APP_NAME#|$INSTANCE|g;\ + s|#INSTALL_PATH#|$INSTALL_PATH|g;\ + s|#DB_HOST#|$DB_HOST|g;\ + s|#DB_NAME#|$DB_NAME|g;\ + s|#DB_PORT#|$DB_PORT|g;\ + s|#DB_PASSWORD#|$DB_PASSWORD|g;\ + s|#APP_DIR#|$APP_DIR|g;\ + s|#SECRET_KEY#|$SECRET_KEY|g;" \ + "install/local_settings.py.sample" > \ + "$INSTANCE/local_settings.py" + + if [ -f $CONFIG_PATH"extra_settings.py" ]; then + cat $CONFIG_PATH"extra_settings.py" >> "$INSTANCE/local_settings.py" + fi + + ### UWSGI + + sed -s "s|#APP_NAME#|$INSTANCE|g;\ + s|#INSTALL_PREFIX#|$INSTALL_PREFIX|g;\ + s|#URL#|$URL|g;\ + s|#UWSGI_PORT#|$UWSGI_PORT|g;" \ + "install/uwsgi.ini.template" > \ + "$INSTANCE/uwsgi.ini" + + 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 + + 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 " * collect static data" + cd $INSTANCE + ./manage.py collectstatic --noinput > /dev/null + cd - + + # only language available + LOCALE=fr + echo " * compile translations" + for d in archaeological_* ishtar_common; do + cd $d + ../$INSTANCE/manage.py compilemessages -l $LOCALE + cd - + done + + ### DB feeding + cd $INSTANCE + echo " * db feeding" + echo " - migrations" + ./manage.py migrate + + echo " - loading fixtures" + 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 + + echo " - create superuser" + ./manage.py createsuperuser + + cat >&2 <<-'EOF' + +------------------------------------------------------------------------------- + Your instance has been configured. +EOF + echo " * instance name: "$INSTANCE + echo " * url: "$URL + + cat >&2 <<-'EOF' + + You should restart uwsgi and nginx: + systemctl restart uwsgi nginx + + And then enjoy ishtar! +EOF +} + +do_install_instance + + + + + + + + + + + + + |