Skip to content

Commit

Permalink
* Fixup the provisioning scripts and add a section for a clean ubuntu…
Browse files Browse the repository at this point in the history
… 16.04 machine.
  • Loading branch information
bgroff committed Aug 9, 2018
1 parent 8001bd5 commit e52fb95
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 24 deletions.
2 changes: 1 addition & 1 deletion deploy/Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ Vagrant.configure("2") do |config|
vb.memory = "1024"
end

config.vm.provision "shell", path: "provision/provision.sh", args: [PROJECT, PROJECT_URL]
config.vm.provision "shell", path: "provision/vagrant.sh", args: [PROJECT, PROJECT_URL]
end
12 changes: 8 additions & 4 deletions deploy/provision/dev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ if ! sudo -u postgres -- psql -tAc "SELECT 1 from pg_database WHERE datname='kal
fi

# Load Data
source /home/vagrant/.zshrc
source $HOME/.bashrc
source $HOME/.virtualenvs/kala/bin/activate
cd /srv/$PROJECT-app/django_$PROJECT
python3 manage.py migrate
echo "from auth.models import User; user = User.objects.create(**{'email': 'teststaff@example.com', 'is_active': True, 'is_staff': True, 'is_superuser': True, 'last_name': 'Staff', 'username': 'teststaff'}); user.set_password('test'); user.save();" | python3 manage.py shell
Expand All @@ -30,7 +31,10 @@ echo "from organizations.models import Organization; from projects.models import
echo "from organizations.models import Organization; from projects.models import Project; from auth.models import User, Permissions, Permission; Permissions.objects.create(user=User.objects.get(email='testuser@example.com'), permission=Permission.objects.get(codename='delete_project'), object_uuid=Project.objects.get(name='Test Project').uuid)" | python3 manage.py shell

# add the admin static pages.
ln -s /home/vagrant/.virtualenvs/$PROJECT/lib/python3.5/site-packages/django/contrib/admin/static/admin /srv/$PROJECT-app/django_$PROJECT/django_$PROJECT/static
ln -s /home/vagrant/.virtualenvs/$PROJECT/lib/python3.5/site-packages/debug_toolbar/static/debug_toolbar /srv/$PROJECT-app/django_$PROJECT/django_$PROJECT/static

if ! [ -f /srv/$PROJECT-app/django_$PROJECT/django_$PROJECT/static ]; then
ln -s /home/vagrant/.virtualenvs/$PROJECT/lib/python3.5/site-packages/django/contrib/admin/static/admin /srv/$PROJECT-app/django_$PROJECT/django_$PROJECT/static
fi
if ! [ -f /srv/$PROJECT-app/django_$PROJECT/django_$PROJECT/static ]; then
ln -s /home/vagrant/.virtualenvs/$PROJECT/lib/python3.5/site-packages/debug_toolbar/static/debug_toolbar /srv/$PROJECT-app/django_$PROJECT/django_$PROJECT/static
fi
service uwsgi restart
18 changes: 18 additions & 0 deletions deploy/provision/migrate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Migrate the database
source $HOME/.bashrc
source $HOME/.virtualenvs/kala/bin/activate
cd /srv/$PROJECT-app/django_$PROJECT
python3 manage.py migrate

# Create a new secret key
python -c "from django.core.management.utils import get_random_secret_key; print('env = SECRET_KEY={0}'.format(get_random_secret_key()));" >> /etc/uwsgi/apps-available/$PROJECT_URL.ini

cat >> $HOME/.bashrc <<- EOM
export DEPLOYMENT_ENVIRONMENT=$DEPLYOMENT
export DATABASE_USER=$DATABASE_USER
export DATABASE_PASSWORD=$DATABASE_PASSWORD
export DATABASE_NAME=$DATABASE_NAME
export DATABASE_HOST=$DATABASE_HOST
export HOST_NAME=$HOST_NAME
EOM

2 changes: 1 addition & 1 deletion deploy/provision/nginx.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# Clear out the nginx config if needed
if [ -f /etc/nginx/sites-available/default ]; then
rm /etc/nginx/sites-enabled/defaultx
rm /etc/nginx/sites-enabled/default
rm /etc/nginx/sites-available/default
fi

Expand Down
2 changes: 1 addition & 1 deletion deploy/provision/packages.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ LC_ALL=en_US.UTF-8
LANG=en_US.UTF-8
EOM

sudo apt install postgresql-client libcurl4-openssl-dev uwsgi uwsgi-plugin-python3 nginx
apt install -y postgresql-client libcurl4-openssl-dev uwsgi uwsgi-plugin-python3 nginx python3-pip python3
5 changes: 3 additions & 2 deletions deploy/provision/provision.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#!/bin/bash

export
bash $WORKING_DIR/deploy/provision/packages.sh
bash $WORKING_DIR/deploy/provision/virtualenv.sh
if [ "$DEPLOYMENT" == "dev" ]; then
bash $WORKING_DIR/deploy/provision/dev.sh
fi
bash $WORKING_DIR/deploy/provision/nginx.sh
bash $WORKING_DIR/deploy/provision/uwsgi.sh
bash $WORKING_DIR/deploy/provision/virtualenv.sh
bash $WORKING_DIR/deploy/provision/migrate.sh

# Restart the services
systemctl restart uwsgi
Expand Down
15 changes: 15 additions & 0 deletions deploy/provision/ubuntu.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env bash

export PROJECT=kala
export PROJECT_URL=`curl -s http://169.254.169.254/latest/meta-data/public-hostname`
export HOME=/home/ubuntu
export DEPLOYMENT=dev
export WORKING_DIR=/srv/kala-app
export DATABASE_USER=kala
export DATABASE_PASSWORD=`openssl rand -base64 32`
export DATABASE_NAME=kala
export DATABASE_HOST=localhost
export HOST_NAME=$PROJECT_URL

bash $WORKING_DIR/deploy/provision/provision.sh

5 changes: 3 additions & 2 deletions deploy/provision/uwsgi.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ module = django_$PROJECT.wsgi:application
touch-reload = /srv/$PROJECT-app/reload
virtualenv = /home/vagrant/.virtualenvs/$PROJECT
env = DEPLOYMENT=$DEPLYOMENT
env = DEPLOYMENT_ENVIRONMENT=$DEPLOYMENT
env = DATABASE_USER=$DATABASE_USER
env = DATABASE_PASSWORD=$DATABASE_PASSWORD
env = DATABASE_NAME=$DATABASE_NAME
env = HOST_NAME=$DATABASE_HOST
env = DATABASE_HOST=$DATABASE_HOST
env = HOST_NAME=$HOST_NAME
EOM


Expand Down
8 changes: 5 additions & 3 deletions deploy/provision/vagrant.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

export PROJECT=$1
export PROJECT_URL=$2
export HOME=/home/vagrant
export DEPLOYMENT=dev
export WORKING_DIR=/vagrant
export WORKING_DIR=/srv/kala-app
export DATABASE_USER=kala
export DATABASE_PASSWORD=kala
export DATABASE_NAME=kala
export HOST_NAME=kala
export DATABASE_HOST=localhost
export HOST_NAME=localhost

bash $WORKING_DIR/deploy/provision/virtualenv.sh
bash $WORKING_DIR/deploy/provision/provision.sh
11 changes: 1 addition & 10 deletions deploy/provision/virtualenv.sh
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
#!/usr/bin/env bash

# Make it so pip does not complain if pip is an older version.
pip install -U pip
sudo pip3 install -U pip virtualenv

# Create the virtualenv
pip install virtualenv
virtualenv -p /usr/bin/python3 /home/vagrant/.virtualenvs/$PROJECT
source /home/vagrant/.virtualenvs/$PROJECT/bin/activate
pip install -r /srv/$PROJECT-app/requirements-dev.txt
chown -R vagrant:vagrant /home/vagrant/.virtualenvs/$PROJECT

# Migrate the database
source /home/vagrant/.zshrc
cd /srv/$PROJECT-app/django_$PROJECT
python3 manage.py migrate

# Create a new secret key
python -c "from django.core.management.utils import get_random_secret_key; print('env = SECRET_KEY={0}'.format(get_random_secret_key()));" >> /etc/uwsgi/apps-available/$PROJECT_URL.ini

0 comments on commit e52fb95

Please sign in to comment.