django-orchestra-test/scripts/container/deploy.sh

135 lines
4.1 KiB
Bash
Raw Normal View History

2014-05-08 16:59:35 +00:00
#!/bin/bash
# Automated development deployment of django-orchestra
# This script is safe to run several times, for example in order to upgrade your deployment
2015-04-29 21:35:56 +00:00
set -ue
2014-05-08 16:59:35 +00:00
bold=$(tput bold)
normal=$(tput sgr0)
[ $(whoami) != 'root' ] && {
echo -e "\nErr. This script should run as root\n" >&2
exit 1
}
USER='orchestra'
2014-10-04 17:46:03 +00:00
PASSWORD='orchestra'
2014-05-08 16:59:35 +00:00
HOME="/home/$USER"
PROJECT_NAME='panel'
BASE_DIR="$HOME/$PROJECT_NAME"
2015-05-05 20:11:03 +00:00
MANAGE="$BASE_DIR/manage.py"
PYTHON_BIN="python3"
2015-05-05 19:42:55 +00:00
CELERY=false
2014-05-08 16:59:35 +00:00
2014-10-04 17:40:13 +00:00
surun () {
2014-05-08 16:59:35 +00:00
echo " ${bold}\$ su $USER -c \"${@}\"${normal}"
su $USER -c "${@}"
}
2014-10-04 17:40:13 +00:00
run () {
echo " ${bold}\$ ${@}${normal}"
${@}
}
2014-05-08 16:59:35 +00:00
# Create a system user for running Orchestra
useradd $USER -s "/bin/bash" || true
2014-05-08 16:59:35 +00:00
echo "$USER:$PASSWORD" | chpasswd
mkdir -p $HOME
2014-05-08 16:59:35 +00:00
chown $USER.$USER $HOME
groups $USER | grep -E "(^|\s)$USER($|\s)" > /dev/null || run adduser $USER sudo
2014-05-08 16:59:35 +00:00
CURRENT_VERSION=$($PYTHON_BIN -c "from orchestra import get_version; print(get_version());" 2> /dev/null || false) || true
2014-05-08 16:59:35 +00:00
if [[ ! $CURRENT_VERSION ]]; then
# First Orchestra installation
2015-04-04 17:44:07 +00:00
run "apt-get -y install git python3-pip"
2015-04-29 21:35:56 +00:00
surun "git clone https://github.com/glic3rinu/django-orchestra.git ~/django-orchestra" || {
# Finishing partial installation
surun "export GIT_DIR=~/django-orchestra/.git; git pull"
}
PYTHON_PATH=$($PYTHON_BIN -c "import sys; print([path for path in sys.path if path.startswith('/usr/local/lib/python')][0]);")
echo $HOME/django-orchestra/ | sudo tee "$PYTHON_PATH/orchestra.pth"
2014-10-04 17:40:13 +00:00
run "cp $HOME/django-orchestra/orchestra/bin/orchestra-admin /usr/local/bin/"
2014-05-08 16:59:35 +00:00
fi
2014-10-04 21:29:05 +00:00
sudo orchestra-admin install_requirements --testing
2014-10-04 13:31:11 +00:00
2014-05-08 16:59:35 +00:00
if [[ ! -e $BASE_DIR ]]; then
cd $HOME
2014-10-04 17:40:13 +00:00
surun "orchestra-admin startproject $PROJECT_NAME"
2014-05-08 16:59:35 +00:00
cd -
2015-04-29 21:35:56 +00:00
else
2015-05-05 20:11:03 +00:00
echo "$BASE_DIT already existis, doing postupgrade."
$PYTHON_BIN $MANAGE migrate postupgradeorchestra --from $CURRENT_VERSION
2014-05-08 16:59:35 +00:00
fi
2015-05-05 20:11:58 +00:00
run apt-get -y install postgresql
2014-05-08 16:59:35 +00:00
if [[ ! $(sudo su postgres -c "psql -lqt" | awk {'print $1'} | grep '^orchestra$') ]]; then
# orchestra database does not esists
# Speeding up tests, don't do this in production!
. /usr/share/postgresql-common/init.d-functions
POSTGRES_VERSION=$(psql --version | head -n1 | sed -r "s/^.*\s([0-9]+\.[0-9]+).*/\1/")
2014-10-04 17:40:13 +00:00
sed -i "s/^#fsync =\s*.*/fsync = off/" \
2014-05-08 16:59:35 +00:00
/etc/postgresql/${POSTGRES_VERSION}/main/postgresql.conf
2014-10-04 17:40:13 +00:00
sed -i "s/^#full_page_writes =\s*.*/full_page_writes = off/" \
2014-05-08 16:59:35 +00:00
/etc/postgresql/${POSTGRES_VERSION}/main/postgresql.conf
2014-10-04 17:40:13 +00:00
run "service postgresql restart"
run "$PYTHON_BIN $MANAGE setuppostgres --db_name orchestra --db_user orchestra --db_password orchestra"
2014-05-08 16:59:35 +00:00
# Create database permissions are needed for running tests
sudo su postgres -c 'psql -c "ALTER USER orchestra CREATEDB;"'
fi
2015-04-29 21:35:56 +00:00
# admin_tools needs accounts and does not have migrations
2015-04-29 14:50:44 +00:00
run "$PYTHON_BIN $MANAGE migrate --noinput accounts"
run "$PYTHON_BIN $MANAGE migrate --noinput"
2014-05-08 16:59:35 +00:00
2015-05-05 19:42:55 +00:00
if [[ $CELERY == true ]]; then
run apt-get install -y rabbitmq
sudo $PYTHON_BIN $MANAGE setupcelery --username $USER --processes 2
else
run "$PYTHON_BIN $MANAGE setupcronbeat"
fi
2014-05-08 16:59:35 +00:00
2015-04-29 21:35:56 +00:00
# Install and configure Nginx+uwsgi web services
surun "mkdir -p $BASE_DIR/static"
surun "$PYTHON_BIN $MANAGE collectstatic --noinput"
2015-04-04 17:44:07 +00:00
run "apt-get install -y nginx uwsgi uwsgi-plugin-python3"
2015-05-05 19:42:55 +00:00
run "$PYTHON_BIN $MANAGE setupnginx --user $USER --noinput"
2014-10-04 17:40:13 +00:00
run "service nginx start"
2014-05-08 16:59:35 +00:00
2015-04-29 21:35:56 +00:00
# Apply changes on related services
2015-05-05 19:42:55 +00:00
run "$PYTHON_BIN $MANAGE reloadservices"
2014-05-08 16:59:35 +00:00
2015-04-29 21:35:56 +00:00
# Create orchestra superuser
cat <<- EOF | $PYTHON_BIN $MANAGE shell
2015-04-29 14:50:44 +00:00
from orchestra.contrib.accounts.models import Account
2014-10-04 17:46:03 +00:00
if not Account.objects.filter(username="$USER").exists():
2015-04-29 14:50:44 +00:00
print('Creating orchestra superuser')
Account.objects.create_superuser("$USER", "$USER@localhost", "$PASSWORD")
2014-05-08 16:59:35 +00:00
EOF
# Change to development settings
PRODUCTION="from orchestra.conf.production_settings import \*"
DEVEL="from orchestra.conf.devel_settings import \*"
sed -i "s/^$PRODUCTION/# $PRODUCTION/" $BASE_DIR/$PROJECT_NAME/settings.py
sed -i "s/^#\s*$DEVEL/$DEVEL/" $BASE_DIR/$PROJECT_NAME/settings.py
cat << EOF
${bold}
* Admin interface login *
- username: $USER
- password: $PASSWORD
${normal}
EOF