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

137 lines
4.4 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:23:18 +00:00
run_ () {
echo " ${bold}\$ ${@}${normal}"
${@}
}
surun_ () {
echo " ${bold}\$ su $user -c \"${@}\"${normal}"
su $user -c "${@}"
}
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 12:18:49 +00:00
alias run=surun_
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 12:18:49 +00:00
alias run=run_
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)
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-01 18:57:31 +00:00
# TODO detect if already installed and don't ask stupid question
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-09-30 20:33:25 +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-09-30 20:33:25 +00:00
run cd $(eval echo ~$USER)
run orchestra-admin startproject $project_name
run cd $project_name
2015-10-02 12:31:58 +00:00
run sudo service postgresql start
run sudo python3 -W ignore manage.py setuppostgres $noinput
2015-10-01 18:57:31 +00:00
run python3 -W ignore manage.py migrate
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
run sudo python3 -W ignore manage.py setupcelery --username $USER
2015-10-02 11:20:52 +00:00
else
2015-10-01 18:57:31 +00:00
run python3 -W ignore manage.py setupcronbeat
run 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-01 18:57:31 +00:00
run 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 11:48:30 +00:00
run python3 -W ignore manage.py check --deploy
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 12:09:05 +00:00
echo
echo "${bold}> Checking if Orchestra is serving at https://${ip_addr}/admin/${normal} ..."
2015-10-02 11:48:30 +00:00
if [[ $(curl https://$ip_addr/admin/ -I -k -s | grep 'HTTP/1.1 302 FOUND') ]]; then
2015-10-02 12:09:05 +00:00
echo -e "${bold} ** Orchestra appears to be working!${normal}\n"
2015-10-02 11:36:24 +00:00
else
2015-10-02 12:09:05 +00:00
echo -e "${bold} ** Err. Orchestra is not responding responding at https://${ip_addr}/admin/${normal}\n" >&2
2015-10-02 11:36:24 +00:00
fi
}
# Wrap it all on a function to avoid partial executions when running through wget/curl
2015-10-02 12:22:43 +00:00
main $@