From 242d0825c812ac56a84dfa30243dc7f12030b014 Mon Sep 17 00:00:00 2001 From: Marc Aymerich Date: Wed, 29 Apr 2015 13:17:21 +0000 Subject: [PATCH] Fixes on create and deploy container script --- README.md | 6 +- orchestra/bin/orchestra-admin | 215 +----------------- .../systemusers/migrations/0001_initial.py | 6 +- scripts/container/create.sh | 11 +- scripts/container/deploy.sh | 43 ++-- 5 files changed, 49 insertions(+), 232 deletions(-) diff --git a/README.md b/README.md index a79eb745..c8435201 100644 --- a/README.md +++ b/README.md @@ -40,10 +40,14 @@ If you are planing to do some development or perhaps just checking out this proj https://raw.github.com/glic3rinu/django-orchestra/master/scripts/container/create.sh sudo bash /tmp/create.sh sudo lxc-start -n orchestra + # root/root ``` -2. Deploy Django-orchestra development environment inside the container +2. Deploy Django-orchestra development environment **inside the container** ```bash + # Make sure your container is connected to the Internet + # Probably you will have to configure the NAT first: + # sudo iptables -t nat -A POSTROUTING -s `container_ip` -j MASQUERADE wget -O /tmp/deploy.sh \ https://raw.github.com/glic3rinu/django-orchestra/master/scripts/container/deploy.sh cd /tmp/ # Moving away from /root before running deploy.sh diff --git a/orchestra/bin/orchestra-admin b/orchestra/bin/orchestra-admin index 0e9129cb..70b9d60d 100755 --- a/orchestra/bin/orchestra-admin +++ b/orchestra/bin/orchestra-admin @@ -1,11 +1,14 @@ #!/bin/bash set -u +set -e bold=$(tput bold) normal=$(tput sgr0) +PYTHON_BIN='python3' + function help () { if [[ $# -gt 1 ]]; then CMD="print_${2}_help" @@ -71,11 +74,11 @@ export -f check_root get_orchestra_dir () { - if ! $(echo "import orchestra"|python 2> /dev/null); then - echo -e "\nErr. orchestra not installed.\n" >&2 + 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) + PATH=$(echo "import orchestra, os; print(os.path.dirname(os.path.realpath(orchestra.__file__)))" | $PYTHON_BIN) echo $PATH } export -f get_orchestra_dir @@ -116,20 +119,19 @@ function install_requirements () { unset OPTIND unset opt - check_root - ORCHESTRA_PATH=$(get_orchestra_dir) + check_root || true + ORCHESTRA_PATH=$(get_orchestra_dir) || true APT="python3 \ python3-pip \ python3-psycopg2 \ + python3-lxml \ postgresql \ rabbitmq-server \ python3-dev \ bind9utils \ python3-cracklib \ libz-dev \ - libxml2-dev \ - libxslt1-dev \ wkhtmltopdf \ xvfb \ ca-certificates \ @@ -154,8 +156,7 @@ function install_requirements () { django-filter==0.9.2 \ passlib==1.6.2 \ jsonfield==0.9.22 \ - lxml==3.3.5 \ - python-dateutil==2.2 \ + python-dateutil==2.4.2 \ django-iban==0.3.0 \ requests \ phonenumbers \ @@ -166,20 +167,18 @@ function install_requirements () { if $testing; then APT="${APT} \ iceweasel \ - dnsutils \ - python3-mysqldb" + dnsutils" PIP="${PIP} \ selenium \ xvfbwrapper \ freezegun \ coverage \ flake8 \ - orchestra-orm==dev \ django-debug-toolbar==1.3.0 \ https://github.com/django-nose/django-nose/archive/master.zip \ sqlparse \ pyinotify \ - --allow-external orchestra-orm --allow-unverified orchestra-orm" + PyMySQL" fi # Make sure locales are in place before installing postgres @@ -215,12 +214,6 @@ function install_requirements () { | xargs sed -i "s/${IMPORT}, SortedDict/${IMPORT}\n ${COLLECTIONS}/" ls /usr/local/lib/python*/dist-packages/passlib/ext/django/utils.py \ | xargs sed -i "s/SortedDict/OrderedDict/g" - - # Patch dateutil - sed -i "s/elif not isinstance(dt2, datetime.datetime):/else:/" \ - /usr/local/lib/python2.7/dist-packages/dateutil/relativedelta.py - sed -i "s/if not type(dt1) == type(dt2): #isinstance(dt1, type(dt2)):/if not isinstance(dt2, type(dt1)):/" \ - /usr/local/lib/python2.7/dist-packages/dateutil/relativedelta.py } export -f install_requirements @@ -286,189 +279,5 @@ function startproject () { export -f startproject -function print_install_certificate_help () { - cat <<- EOF - - ${bold}NAME${normal} - ${bold}orchetsra-admin install_certificate${normal} - Installs a valid all-purpose self signed certificate that is valid for the next ten years - - ${bold}OPTIONS${normal} - ${bold}-h, --help${normal} - Displays this help text - - EOF -} - - -function install_certificate () { - opts=$(getopt -o h -l help -- "$@") || exit 1 - set -- $opts - - while [ $# -gt 0 ]; do - case $1 in - -h|--help) print_deploy_help; exit 0 ;; - (--) shift; break;; - (-*) echo "$0: Err. - unrecognized option $1" 1>&2; exit 1;; - (*) break;; - esac - shift - done - unset OPTIND - unset opt - - check_root - run openssl req -new -x509 -days 3650 -nodes -newkey rsa:4096 -out /etc/ssl/certs/mailserver.pem -keyout /etc/ssl/private/mailserver.pem - run chmod go= /etc/ssl/private/mailserver.pem -} -export -f install_certificate - - -function print_uninstall_certificate_help () { - cat <<- EOF - - ${bold}NAME${normal} - ${bold}orchetsra-admin uninstall_certificate${normal} - Remove self signed certificate - - ${bold}OPTIONS${normal} - ${bold}-h, --help${normal} - Displays this help text - - EOF -} - -function uninstall_certificate () { - opts=$(getopt -o h -l help -- "$@") || exit 1 - set -- $opts - - while [ $# -gt 0 ]; do - case $1 in - -h|--help) print_deploy_help; exit 0 ;; - (--) shift; break;; - (-*) echo "$0: Err. - unrecognized option $1" 1>&2; exit 1;; - (*) break;; - esac - shift - done - unset OPTIND - unset opt - - check_root - run rm -f /etc/ssl/private/mailserver.pem -} -export -f uninstall_certificate - - -function print_install_postfix_help () { - cat <<- EOF - - ${bold}NAME${normal} - ${bold}orchetsra-admin install_postfix${normal} - Installs postfix server and its dependencies (dovecot, amavis, spamassassin and clamav) using apt-get. Also it generates a valid all-purpose certificate self signed that is valid for the next ten years. - - ${bold}OPTIONS${normal} - ${bold}-h, --help${normal} - Displays this help text - - EOF -} - - -function install_postfix () { - opts=$(getopt -o h -l help -- "$@") || exit 1 - set -- $opts - - while [ $# -gt 0 ]; do - case $1 in - -h|--help) print_deploy_help; exit 0 ;; - (--) shift; break;; - (-*) echo "$0: Err. - unrecognized option $1" 1>&2; exit 1;; - (*) break;; - esac - shift - done - unset OPTIND - unset opt - - check_root - ORCHESTRA_PATH=$(get_orchestra_dir) - - APT="postfix postfix-pgsql \ - swaks \ - dovecot-core dovecot-pop3d dovecot-imapd dovecot-antispam \ - dovecot-pgsql dovecot-sieve dovecot-managesieved dovecot-solr \ - amavisd-new spamassassin \ - clamav-freshclam clamav-base clamav clamav-daemon clamav-testfiles \ - " - run apt-get update - export DEBIAN_FRONTEND=noninteractive - run apt-get install -y $APT - unset $DEBIAN_FRONTEND; - run /usr/bin/freshclam - run apt-get --purge remove 'exim4*' -y - if [ ! -f /etc/ssl/private/mailserver.pem ]; then - install_certificate - fi; -} -export -f install_postfix - - -function print_uninstall_postfix_help () { - cat <<- EOF - - ${bold}NAME${normal} - ${bold}orchetsra-admin uninstall_postfix${normal} - Uninstalls postfix server and its dependencies (dovecot, amavis, spamassassin and clamav) using dpkg and remove self signed certificate - - ${bold}OPTIONS${normal} - ${bold}-h, --help${normal} - Displays this help text - - EOF -} - - -function uninstall_postfix () { - opts=$(getopt -o h -l help -- "$@") || exit 1 - set -- $opts - - while [ $# -gt 0 ]; do - case $1 in - -h|--help) print_deploy_help; exit 0 ;; - (--) shift; break;; - (-*) echo "$0: Err. - unrecognized option $1" 1>&2; exit 1;; - (*) break;; - esac - shift - done - unset OPTIND - unset opt - - check_root - ORCHESTRA_PATH=$(get_orchestra_dir) - - APT="postfix postfix-pgsql \ - swaks \ - dovecot-core dovecot-pop3d dovecot-imapd dovecot-antispam \ - dovecot-pgsql dovecot-sieve dovecot-managesieved dovecot-solr \ - amavisd-new spamassassin \ - clamav-freshclam clamav-base clamav clamav-daemon libclamav6 clamav-testfiles \ - " - run dpkg -P --force-depends $APT - run apt-get update - run apt-get -f install -y - - if [ -d /var/run/amavis ]; then - run rm -rf /var/run/amavis - fi; - - if [ -d /var/lib/clamav ]; then - run rm -rf /var/lib/clamav - fi; - - if [ -f /etc/ssl/private/mailserver.pem ]; then - uninstall_certificate - fi; -} -export -f uninstall_postfix - - [ $# -lt 1 ] && print_help $1 "${@}" diff --git a/orchestra/contrib/systemusers/migrations/0001_initial.py b/orchestra/contrib/systemusers/migrations/0001_initial.py index 2470371d..9c06eca9 100644 --- a/orchestra/contrib/systemusers/migrations/0001_initial.py +++ b/orchestra/contrib/systemusers/migrations/0001_initial.py @@ -8,9 +8,9 @@ from django.conf import settings class Migration(migrations.Migration): -# dependencies = [ -# migrations.swappable_dependency(settings.AUTH_USER_MODEL), -# ] + dependencies = [ + ('orders', '0001_initial'), + ] operations = [ migrations.CreateModel( diff --git a/scripts/container/create.sh b/scripts/container/create.sh index 5ddd1603..2fb1c7f6 100755 --- a/scripts/container/create.sh +++ b/scripts/container/create.sh @@ -22,19 +22,26 @@ lxc-create -h &> /dev/null || { exit 1 } +lxc-ls | grep -E "(^|\s)$NAME($|\s)" && { + echo -e "\nErr. Container with name $NAME already exists." + echo -e " You can destroy it by: sudo lxc-destroy -n $NAME\n" >&2 + exit 1 +} + lxc-create -n $NAME -t debian +trap "umount $CONTAINER/{dev,sys}; exit 1;" INT TERM EXIT mount --bind /dev $CONTAINER/dev mount -t sysfs none $CONTAINER/sys -trap "umount $CONTAINER/{dev,sys}; exit 1;"INT TERM EXIT + sed -i "s/\tlocalhost$/\tlocalhost $NAME/" $CONTAINER/etc/hosts sed -i "s/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/" $CONTAINER/etc/locale.gen chroot $CONTAINER locale-gen - +chroot $CONTAINER apt-get update chroot $CONTAINER apt-get install -y --force-yes \ nano git screen sudo iputils-ping python3 python3-pip wget curl dnsutils rsyslog diff --git a/scripts/container/deploy.sh b/scripts/container/deploy.sh index a698cb40..575190ca 100755 --- a/scripts/container/deploy.sh +++ b/scripts/container/deploy.sh @@ -4,8 +4,9 @@ # This script is safe to run several times, for example in order to upgrade your deployment - set -u +set -e + bold=$(tput bold) normal=$(tput sgr0) @@ -20,7 +21,7 @@ PASSWORD='orchestra' HOME="/home/$USER" PROJECT_NAME='panel' BASE_DIR="$HOME/$PROJECT_NAME" - +PYTHON_BIN="python3" surun () { echo " ${bold}\$ su $USER -c \"${@}\"${normal}" @@ -34,20 +35,20 @@ run () { # Create a system user for running Orchestra -useradd orchestra -s "/bin/bash" +useradd $USER -s "/bin/bash" || true echo "$USER:$PASSWORD" | chpasswd -mkdir $HOME +mkdir -p $HOME chown $USER.$USER $HOME -run adduser $USER sudo +groups $USER | grep -E "(^|\s)$USER($|\s)" > /dev/null || run adduser $USER sudo -CURRENT_VERSION=$(python3 -c "from orchestra import get_version; print get_version();" 2> /dev/null || false) - +CURRENT_VERSION=$($PYTHON_BIN -c "from orchestra import get_version; print(get_version());" 2> /dev/null || false) || true if [[ ! $CURRENT_VERSION ]]; then # First Orchestra installation run "apt-get -y install git python3-pip" - surun "git clone https://github.com/glic3rinu/django-orchestra.git ~/django-orchestra" - echo $HOME/django-orchestra/ | sudo tee /usr/local/lib/python3*/dist-packages/orchestra.pth + surun "git clone https://github.com/glic3rinu/django-orchestra.git ~/django-orchestra" || 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" run "cp $HOME/django-orchestra/orchestra/bin/orchestra-admin /usr/local/bin/" fi @@ -64,40 +65,36 @@ MANAGE="$BASE_DIR/manage.py" 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! - POSTGRES_VERSION=$(psql --version | head -n1 | awk {'print $3'} | sed -r "s/(^[0-9\.]*).*/\1/") + . /usr/share/postgresql-common/init.d-functions + POSTGRES_VERSION=$(psql --version | head -n1 | sed -r "s/^.*\s([0-9]+\.[0-9]+).*/\1/") sed -i "s/^#fsync =\s*.*/fsync = off/" \ /etc/postgresql/${POSTGRES_VERSION}/main/postgresql.conf sed -i "s/^#full_page_writes =\s*.*/full_page_writes = off/" \ /etc/postgresql/${POSTGRES_VERSION}/main/postgresql.conf run "service postgresql restart" - run "python3 $MANAGE setuppostgres --db_name orchestra --db_user orchestra --db_password orchestra" + run "$PYTHON_BIN $MANAGE setuppostgres --db_name orchestra --db_user orchestra --db_password orchestra" # Create database permissions are needed for running tests sudo su postgres -c 'psql -c "ALTER USER orchestra CREATEDB;"' fi -if [[ $CURRENT_VERSION ]]; then - # Per version upgrade specific operations - run "python3 $MANAGE postupgradeorchestra --no-restart --from $CURRENT_VERSION" -else - run "python3 $MANAGE syncdb --noinput" - run "python3 $MANAGE migrate --noinput" -fi +run "$PYTHON_BIN $MANAGE syncdb --noinput" +run "$PYTHON_BIN $MANAGE migrate --noinput" sudo python $MANAGE setupcelery --username $USER --processes 2 # Install and configure Nginx web server -surun "mkdir $BASE_DIR/static" -surun "python3 $MANAGE collectstatic --noinput" +surun "mkdir -p $BASE_DIR/static" +surun "$PYTHON_BIN $MANAGE collectstatic --noinput" run "apt-get install -y nginx uwsgi uwsgi-plugin-python3" -run "python3 $MANAGE setupnginx" +run "$PYTHON_BIN $MANAGE setupnginx" run "service nginx start" # Apply changes -run "python3 $MANAGE restartservices" +run "$PYTHON_BIN $MANAGE restartservices" # Create a orchestra user -cat <<- EOF | python3 $MANAGE shell +cat <<- EOF | $PYTHON_BIN $MANAGE shell from orchestra.apps.accounts.models import Account if not Account.objects.filter(username="$USER").exists(): print 'Creating orchestra superuser'