Last active
February 17, 2016 21:14
-
-
Save serjs/afa483569840089886c2 to your computer and use it in GitHub Desktop.
ds
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -u | |
set -e | |
bold=$(tput -T ${TERM:-xterm} bold) | |
normal=$(tput -T ${TERM:-xterm} sgr0) | |
PYTHON_BIN='python3' | |
function help () { | |
if [[ $# -gt 1 ]]; then | |
CMD="print_${2}_help" | |
$CMD | |
else | |
print_help | |
fi | |
} | |
function print_help () { | |
cat <<- EOF | |
${bold}NAME${normal} | |
${bold}orchestra-admin${normal} - Orchetsra administration script | |
${bold}OPTIONS${normal} | |
${bold}install_requirements${normal} | |
Installs Orchestra requirements using apt-get and pip | |
${bold}startproject${normal} | |
Creates a new Django-orchestra instance | |
${bold}help${normal} | |
Displays this help text or related help page as argument | |
for example: | |
${bold}orchestra-admin help startproject${normal} | |
EOF | |
} | |
show () { | |
echo " ${bold}\$ ${@}${normal}" | |
} | |
export -f show | |
run () { | |
show "${@}" | |
"${@}" | |
} | |
export -f run | |
check_root () { | |
[ $(whoami) != 'root' ] && { echo -e "\nErr. This should be run as root\n" >&2; exit 1; } | |
} | |
export -f check_root | |
get_orchestra_dir () { | |
if ! $(echo "import orchestra" | $PYTHON_BIN 2> /dev/null); then | |
echo -e "\norchestra not installed.\n" >&2 | |
exit 1 | |
fi | |
PATH=$(echo "import orchestra, os; print(os.path.dirname(os.path.realpath(orchestra.__file__)))" | $PYTHON_BIN) | |
echo $PATH | |
} | |
export -f get_orchestra_dir | |
function print_install_requirements_help () { | |
cat <<- EOF | |
${bold}NAME${normal} | |
${bold}orchetsra-admin install_requirements${normal} - Installs all Orchestra requirements using apt-get and pip | |
${bold}OPTIONS${normal} | |
${bold}-t, --testing${normal} | |
Install Orchestra normal requirements plus those needed for running functional tests | |
${bold}-h, --help${normal} | |
Displays this help text | |
EOF | |
} | |
function install_requirements () { | |
opts=$(getopt -o h,t -l help,testing -- "$@") || exit 1 | |
set -- $opts | |
testing=false | |
while [ $# -gt 0 ]; do | |
case $1 in | |
-h|--help) print_deploy_help; exit 0 ;; | |
-t|--testing) testing=true; shift ;; | |
(--) shift; break;; | |
(-*) echo "$0: Err. - unrecognized option $1" 1>&2; exit 1;; | |
(*) break;; | |
esac | |
shift | |
done | |
unset OPTIND | |
unset opt | |
check_root || true | |
ORCHESTRA_PATH=$(get_orchestra_dir) || true | |
# Make sure locales are in place before installing postgres | |
if [[ $({ perl --help > /dev/null; } 2>&1|grep 'locale failed') ]]; then | |
run sed -i "s/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/" /etc/locale.gen | |
run locale-gen | |
update-locale LANG=en_US.UTF-8 | |
fi | |
# lxml: libxml2-dev, libxslt1-dev, zlib1g-dev | |
APT="bind9utils \ | |
ca-certificates \ | |
gettext \ | |
libcrack2-dev \ | |
libxml2-dev \ | |
libxslt1-dev \ | |
python3 \ | |
python3-pip \ | |
python3-dev \ | |
ssh-client \ | |
wget \ | |
xvfb \ | |
zlib1g-dev" | |
if $testing; then | |
APT="${APT} \ | |
git \ | |
iceweasel \ | |
dnsutils" | |
fi | |
run apt-get update | |
run apt-get install -y $APT | |
# Install ca certificates before executing pip install | |
if [[ ! -e /usr/local/share/ca-certificates/cacert.org ]]; then | |
mkdir -p /usr/local/share/ca-certificates/cacert.org | |
wget -P /usr/local/share/ca-certificates/cacert.org \ | |
http://www.cacert.org/certs/root.crt \ | |
http://www.cacert.org/certs/class3.crt | |
update-ca-certificates | |
fi | |
# cracklib and lxml are excluded on the requirements.txt because they need unconvinient system dependencies | |
PIP="$(wget http://git.io/orchestra-requirements.txt -O - | tr '\n' ' ') \ | |
cracklib \ | |
lxml==3.3.5" | |
if $testing; then | |
PIP="${PIP} \ | |
selenium \ | |
xvfbwrapper \ | |
freezegun \ | |
coverage \ | |
flake8 \ | |
django-debug-toolbar==1.3.0 \ | |
django-nose==1.4 \ | |
sqlparse \ | |
pyinotify \ | |
PyMySQL" | |
fi | |
run pip3 install $PIP | |
} | |
export -f install_requirements | |
print_startproject_help () { | |
cat <<- EOF | |
${bold}NAME${normal} | |
${bold}orchestra-admin startproject${normal} - Create a new Django-Orchestra instance | |
${bold}SYNOPSIS${normal} | |
Options: [ -h ] | |
${bold}OPTIONS${normal} | |
${bold}-h, --help${normal} | |
This help message | |
${bold}EXAMPLES${normal} | |
orchestra-admin startproject controlpanel | |
EOF | |
} | |
function startproject () { | |
local PROJECT_NAME="$2"; shift | |
opts=$(getopt -o h -l help -- "$@") || exit 1 | |
set -- $opts | |
set -- $opts | |
while [ $# -gt 0 ]; do | |
case $1 in | |
-h|--help) print_startproject_help; exit 0 ;; | |
(--) shift; break;; | |
(-*) echo "$0: Err. - unrecognized option $1" 1>&2; exit 1;; | |
(*) break;; | |
esac | |
shift | |
done | |
unset OPTIND | |
unset opt | |
[ $(whoami) == 'root' ] && { echo -e "\nYou don't want to run this as root\n" >&2; exit 1; } | |
ORCHESTRA_PATH=$(get_orchestra_dir) || { echo "Error getting orchestra dir"; exit 1; } | |
if [[ ! -e $PROJECT_NAME/manage.py ]]; then | |
run django-admin.py startproject $PROJECT_NAME --template="${ORCHESTRA_PATH}/conf/project_template" | |
# This is a workaround for this issue https://github.com/pypa/pip/issues/317 | |
run chmod +x $PROJECT_NAME/manage.py | |
# End of workaround ### | |
else | |
echo "Not cloning: $PROJECT_NAME already exists." | |
fi | |
# Install bash autocompletition for django commands | |
if [[ ! $(grep 'source $HOME/.django_bash_completion.sh' ~/.bashrc &> /dev/null) ]]; then | |
# run wget https://raw.github.com/django/django/master/extras/django_bash_completion \ | |
# --no-check-certificate -O ~/.django_bash_completion.sh | |
cp ${ORCHESTRA_PATH}/bin/django_bash_completion.sh ~/.django_bash_completion.sh | |
echo 'source $HOME/.django_bash_completion.sh' >> ~/.bashrc | |
fi | |
} | |
export -f startproject | |
[ $# -lt 1 ] && { print_help; exit 1; } | |
$1 "${@}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment