django-orchestra/scripts/containers/deploy.sh

157 lines
5.7 KiB
Bash
Raw Normal View History

2014-05-08 16:59:35 +00:00
#!/bin/bash
2015-04-29 21:35:56 +00:00
set -ue
2015-09-30 20:33:25 +00:00
2015-10-02 12:09:05 +00:00
# bash <( curl https://raw.githubusercontent.com/glic3rinu/django-orchestra/master/scripts/containers/deploy.sh ) [--noinput username]
2015-10-01 18:02:23 +00:00
function main () {
2015-10-02 12:17:03 +00:00
noinput=''
user=$USER
2015-10-02 12:22:43 +00:00
if [[ $# -eq 2 ]]; then
2015-10-02 12:09:05 +00:00
if [[ $1 != '--noinput' ]]; then
2015-10-02 12:22:43 +00:00
echo -e "\nErr. What argument is $1?\n" >&2
2015-10-02 12:09:05 +00:00
exit 2
elif ! id $2; then
2015-10-02 12:22:43 +00:00
echo -e "\nErr. User $2 does not exist\n" >&2
2015-10-02 12:09:05 +00:00
exit 3
fi
2015-10-02 12:22:43 +00:00
[ $(whoami) != 'root' ] && {
echo -e "\nErr. --noinput should run as root\n" >&2
exit 1
}
2015-10-02 12:17:03 +00:00
noinput='--noinput'
2015-10-02 13:31:19 +00:00
run () { echo " ${bold}\$ ${@}${normal}"; ${@}; }
surun () { echo " ${bold}\$ su $user -c \"${@}\"${normal}"; su $user -c "${@}"; }
2015-10-02 12:17:03 +00:00
user=$2
2015-10-02 12:22:43 +00:00
elif [[ $# -eq 1 ]]; then
2015-10-02 12:09:05 +00:00
if [[ $1 != '--noinput' ]]; then
2015-10-02 12:22:43 +00:00
echo -e "\nErr. What argument is $1?\n" >&2
2015-10-02 12:09:05 +00:00
else
2015-10-02 12:22:43 +00:00
echo -e "\nErr. --noinput should provide a username\n" >&2
2015-10-02 12:09:48 +00:00
fi
2015-10-02 12:09:05 +00:00
exit 1
else
[ $(whoami) == 'root' ] && {
echo -e "\nErr. This script should run as a regular user\n" >&2
exit 1
}
2015-10-02 13:31:19 +00:00
run () { echo " ${bold}\$ ${@}${normal}"; ${@}; }
surun () { echo " ${bold}\$ ${@}${normal}"; ${@}; }
2015-10-02 12:09:05 +00:00
# Test sudo privileges
sudo true
fi
2015-10-02 11:36:24 +00:00
2015-10-02 12:09:05 +00:00
bold=$(tput -T ${TERM:-xterm} bold)
normal=$(tput -T ${TERM:-xterm} sgr0)
2015-10-02 13:31:19 +00:00
2015-10-02 12:09:05 +00:00
project_name="panel"
2015-10-02 12:17:03 +00:00
if [[ $noinput == '' ]]; then
2015-10-02 12:09:05 +00:00
while true; do
read -p "Enter a project name [panel]: " project_name
if [[ "$project_name" == "" ]]; then
project_name="panel"
break
elif [[ ! $(echo "$project_name" | grep '^[_a-zA-Z]\w*$') ]]; then
if [[ ! $(echo "$project_name" | grep '^[_a-zA-Z]') ]]; then
message='make sure the name begins with a letter or underscore'
else
message='use only numbers, letters and underscores'
fi
echo "'$project_name' is not a valid %s name. Please $message."
2015-09-30 20:33:25 +00:00
else
2015-10-02 12:09:05 +00:00
break
fi
done
fi
2015-09-30 20:33:25 +00:00
2015-10-02 12:09:05 +00:00
task=cronbeat
2015-10-02 12:17:03 +00:00
if [[ $noinput == '' ]]; then
2015-10-02 12:09:05 +00:00
while true; do
read -p "Do you want to use celery or cronbeat (orchestra.contrib.tasks) for task execution [cronbeat]? " task
case $task in
'celery' ) task=celery; break;;
'cronbeat' ) task=cronbeat; break;;
'' ) task=cronbeat; break;;
* ) echo "Please answer celery or cronbeat.";;
esac
done
fi
2015-10-02 13:31:19 +00:00
2015-10-02 12:31:58 +00:00
run sudo pip3 install django-orchestra==dev \
2015-09-30 20:33:25 +00:00
--allow-external django-orchestra \
--allow-unverified django-orchestra
2015-10-02 12:31:58 +00:00
run sudo orchestra-admin install_requirements
2015-10-02 12:48:48 +00:00
cd $(eval echo ~$user)
2015-09-30 20:33:25 +00:00
2015-10-02 12:47:27 +00:00
surun "orchestra-admin startproject $project_name"
2015-10-02 12:48:48 +00:00
cd $project_name
2015-09-30 20:33:25 +00:00
2015-10-02 12:31:58 +00:00
run sudo service postgresql start
2015-10-02 13:01:01 +00:00
if [[ $noinput == '--noinput' ]]; then
db_password=$(ps aux | sha256sum | base64 | head -c 10)
run sudo python3 -W ignore manage.py setuppostgres --noinput --db_password $db_password
else
run sudo python3 -W ignore manage.py setuppostgres
fi
2015-10-02 12:47:27 +00:00
surun "python3 -W ignore manage.py migrate $noinput"
if [[ $noinput == '--noinput' ]]; then
# Create orchestra superuser
2015-10-02 13:02:12 +00:00
cat <<- EOF | surun "python3 -W ignore manage.py shell"
2015-10-02 12:47:27 +00:00
from orchestra.contrib.accounts.models import Account
if not Account.objects.filter(username="$user").exists():
print('Creating orchestra superuser')
Account.objects.create_superuser("$user", "$user@localhost", "orchestra")
2015-10-02 12:47:27 +00:00
EOF
fi
2015-10-02 11:20:52 +00:00
if [[ "$task" == "celery" ]]; then
2015-10-02 12:31:58 +00:00
run sudo apt-get install rabbitmq-server
2015-10-02 12:47:27 +00:00
run sudo python3 -W ignore manage.py setupcelery --username $user
2015-10-02 11:20:52 +00:00
else
2015-10-02 12:47:27 +00:00
surun "python3 -W ignore manage.py setupcronbeat"
surun "python3 -W ignore manage.py syncperiodictasks"
2015-09-30 20:33:25 +00:00
fi
2015-10-02 12:31:58 +00:00
run sudo python3 -W ignore manage.py setuplog --noinput
2015-10-02 12:47:27 +00:00
surun "python3 -W ignore manage.py collectstatic --noinput"
2015-10-02 12:31:58 +00:00
run sudo apt-get install nginx-full uwsgi uwsgi-plugin-python3
run sudo python3 -W ignore manage.py setupnginx --user $user $noinput
run sudo python3 -W ignore manage.py restartservices
run sudo python3 -W ignore manage.py startservices
2015-10-02 12:47:27 +00:00
surun "python3 -W ignore manage.py check --deploy"
2015-10-02 11:36:24 +00:00
2015-10-02 13:31:19 +00:00
# Test if serving requests
2015-10-02 11:36:24 +00:00
ip_addr=$(ip addr show eth0 | grep 'inet ' | sed -r "s/.*inet ([^\s]*).*/\1/" | cut -d'/' -f1)
if [[ $ip_addr == '' ]]; then
ip_addr=127.0.0.1
fi
2015-10-02 11:48:30 +00:00
echo
2015-10-02 13:31:19 +00:00
echo ${bold}
echo "> Checking if Orchestra is serving on https://${ip_addr}/admin/ ..."
if [[ $noinput == '--noinput' ]]; then
echo -e " - username: $user"
2015-10-02 13:34:09 +00:00
echo -e " - password: orchestra${normal}"
2015-10-02 13:31:19 +00:00
fi
2015-10-02 13:34:09 +00:00
if [[ $(curl -L -k -c /tmp/cookies.txt -b /tmp/cookies.txt https://$ip_addr/admin/ | grep 'Panel Hosting Management') ]]; then
2015-10-02 13:31:19 +00:00
token=$(grep csrftoken /tmp/cookies.txt | awk {'print $7'})
2015-10-02 13:34:09 +00:00
if [[ $(curl -L -k -c /tmp/cookies.txt -b /tmp/cookies.txt \
2015-10-02 13:31:19 +00:00
-d "username=$user&password=orchestra&csrfmiddlewaretoken=$token" \
2015-10-02 13:32:55 +00:00
-e https://$ip_addr/admin/ \
https://$ip_addr/admin/login/?next=/admin/ | grep '<title>Panel Hosting Management</title>') ]]; then
2015-10-02 13:34:09 +00:00
echo "${bold} ** Orchestra appears to be working!${normal}"
2015-10-02 13:31:19 +00:00
else
2015-10-02 13:34:09 +00:00
echo "${bold} ** Err. Couldn't login :(${normal}" >&2
2015-10-02 13:04:57 +00:00
fi
2015-10-02 11:36:24 +00:00
else
2015-10-02 13:34:09 +00:00
echo "${bold} ** Err. Orchestra is not responding responding on https://${ip_addr}/admin/${normal}" >&2
2015-10-02 11:36:24 +00:00
fi
2015-10-02 13:34:09 +00:00
echo
}
# Wrap it all on a function to avoid partial executions when running through wget/curl
2015-10-02 12:22:43 +00:00
main $@