Skip to content

Commit

Permalink
Initial ansible playbook.
Browse files Browse the repository at this point in the history
Uses Python 2 for now. Python 3 supported via an Ansible variable. (See
README for details)
  • Loading branch information
smajda committed Aug 2, 2015
1 parent ab8d34e commit 4eaf110
Show file tree
Hide file tree
Showing 17 changed files with 147 additions and 21 deletions.
30 changes: 30 additions & 0 deletions README.markdown
Expand Up @@ -4,6 +4,12 @@ Files for the PythonKC.com website.

## Development Quickstart Option 1 (vagrant)

First, copy `pythonkc_site/.env.example` to `pythonkc_site/.env` and add
your own [meetup api key][] and a unique [django secret key][] (`.env` will
be ignored by git)

Then you have to install some vagrant plugins and build your vagrant box:

```
vagrant plugin install vagrant-hostmanager
vagrant plugin install vagrant-hostsupdater
Expand All @@ -23,6 +29,25 @@ cd ~/vagrant/ansible
ansible-playbook vagrant.yml
```

To run the Django development server:

```
vagrant ssh
django-admin runserver 192.168.100.101:8000
```

Now go to `http://192.168.100.101:8000` in your browser. You can edit the files
on your local machine and the server should reload automatically.

For now, this is a Python 2 project. If you want to start using Python 3
and help us fix our problems, set Ansible's `python_version` variable to 3
and it will build the virtualenv using Python 3:

```
ansible-playbook vagrant.yml -e python_version=3
```


## Development Quickstart Option 2 (virtualenv)

```
Expand All @@ -38,3 +63,8 @@ Profit! $$$
## More Detailed Instructions

See: docs/local_development



[meetup api key]: https://secure.meetup.com/meetup_api/key/
[django secret key]: http://www.miniwebtool.com/django-secret-key-generator/
2 changes: 1 addition & 1 deletion ansible/group_vars/all.yml
@@ -1,2 +1,2 @@
---
example: true # TODO, put any common variables in here
virtualenv: ~/virtualenvs/pythonkc
2 changes: 2 additions & 0 deletions ansible/group_vars/production.yml
@@ -1,2 +1,4 @@
---
user: pythonkc
pythonpath: /TODO
project_root: /TODO/pythonkc_site
2 changes: 2 additions & 0 deletions ansible/group_vars/vagrant.yml
@@ -1,2 +1,4 @@
---
user: vagrant
pythonpath: /home/vagrant/vagrant
project_root: /home/vagrant/vagrant/pythonkc_site
2 changes: 2 additions & 0 deletions ansible/roles/pythonkc/defaults/main.yml
@@ -0,0 +1,2 @@
---
python_version: 2
47 changes: 47 additions & 0 deletions ansible/roles/pythonkc/tasks/django.yml
@@ -0,0 +1,47 @@
---
- name: Create virtualenvs directory
file: dest=~/virtualenvs state=directory owner={{ user }} group={{ user }}


- name: Install requirements into Python 2.7 virtualenv
pip:
requirements: "{{ project_root }}/requirements/project.txt"
virtualenv: "{{ virtualenv }}"
virtualenv_command: /usr/bin/virtualenv -p python2.7
when: python_version == 2


- name: Install requirements into Python 3 virtualenv
pip:
requirements: "{{ project_root }}/requirements/project.txt"
virtualenv: "{{ virtualenv }}"
virtualenv_command: python3 /usr/lib/python3/dist-packages/virtualenv.py -p python3
when: python_version == 3


- name: Set some env vars when activating virtualenv
lineinfile:
dest: "{{ virtualenv }}/bin/activate"
regexp: "^export {{ item.name }}="
line: "export {{ item.name }}={{ item.value }}"
state: present
insertafter: EOF
with_items:
- {name: DJANGO_SETTINGS_MODULE, value: pythonkc_site.settings}
- {name: PYTHONPATH, value: "{{ pythonpath }}"}


- name: Automatically activate the virtualenv in bashrc
lineinfile:
dest: ~/.bashrc
line: "source {{ virtualenv }}/bin/activate"
state: present
insertafter: EOF


- name: Run migrations
django_manage:
command: migrate
app_path: "{{ project_root }}"
pythonpath: "{{ pythonpath }}"
virtualenv: "{{ virtualenv }}"
9 changes: 9 additions & 0 deletions ansible/roles/pythonkc/tasks/main.yml
@@ -0,0 +1,9 @@
---
- {include: python2.yml, tags: python, when: python_version == 2}
- {include: python3.yml, tags: python, when: python_version == 3}
- include: tools.yml tags=tools
- include: django.yml tags=django

# TODO
# vim + vim configuration for python?
# postgres? (for now just using sqlite for development)
17 changes: 17 additions & 0 deletions ansible/roles/pythonkc/tasks/python2.yml
@@ -0,0 +1,17 @@
---
- name: Update apt
apt: update_cache=yes cache_valid_time=3600
become: yes
tags: apt

- name: Install some base packages
apt: pkg="{{item}}" state=latest
become: yes
tags: apt
with_items:
- build-essential
- libpq-dev
- python-dev
- python-pip
- python-software-properties
- python-virtualenv
@@ -1,19 +1,18 @@
---
- name: Update apt
apt: update_cache=yes cache_valid_time=3600
become: yes
tags: apt

- name: Install some base packages
apt: pkg="{{item}}" state=latest
become: yes
tags: apt
with_items:
- build-essential
- libpq-dev
- python3
- python3-dev
- python3-pip
- python3-software-properties
- vim-nox
- htop

# What else should go in here?
# vim + vim configuration for python?
- python3-virtualenv
16 changes: 16 additions & 0 deletions ansible/roles/pythonkc/tasks/tools.yml
@@ -0,0 +1,16 @@
---
- name: Update apt
apt: update_cache=yes cache_valid_time=3600
become: yes
tags: apt


- name: Install some dev tools packages
apt: pkg="{{item}}" state=latest
become: yes
tags: apt
with_items:
- vim-nox
- htop

# TODO basic vimrc with python plugins?
11 changes: 1 addition & 10 deletions ansible/vagrant.yml
@@ -1,14 +1,5 @@
---
- hosts: vagrant
sudo: yes

# you can have tasks right here
tasks:
- name: Say hello
shell: echo `date` > /home/vagrant/hello.txt

# and you can have 'roles'
roles:
- role: base
tags: base_role
- pythonkc

7 changes: 4 additions & 3 deletions provision.sh
Expand Up @@ -11,9 +11,10 @@ mkdir -p /var/www
ln -sf /vagrant/pythonkc_site /var/www/pythonkc_site

if [[ -z "$(which ansible)" ]]; then
echo "Installing Ansible..."
aptitude install -y python3 python3-dev python3-pip ansible
echo "Installing pip and Ansible..."
aptitude install -y python-dev python-pip
pip2 install ansible
fi

cd /home/vagrant/vagrant/ansible
ansible-playbook vagrant.yml
sudo -H -u vagrant ansible-playbook vagrant.yml
3 changes: 3 additions & 0 deletions pythonkc_site/.env.example
@@ -0,0 +1,3 @@
#!/bin/bash
export MEETUP_API_KEY='your-meetup-api-key-here'
export DJANGO_SECRET_KEY='django-secret-key-here'
1 change: 1 addition & 0 deletions pythonkc_site/manage.py
Expand Up @@ -2,6 +2,7 @@
import os
import sys


if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "pythonkc_site.settings")

Expand Down
2 changes: 2 additions & 0 deletions pythonkc_site/requirements/gondor.txt
@@ -0,0 +1,2 @@
gondor==1.0.5
wsgiref==0.1.2
3 changes: 1 addition & 2 deletions pythonkc_site/requirements/project.txt
@@ -1,10 +1,9 @@
Django==1.8.3
django-dotenv==1.3.0
django-redis-cache==0.9.2
gondor==1.0.5
mimeparse==0.1.3
psycopg2==2.6.1
python-dateutil==1.5
pythonkc-meetups==0.1.0
redis==2.10.3
requests==2.7.0
wsgiref==0.1.2
5 changes: 5 additions & 0 deletions pythonkc_site/settings.py
@@ -1,6 +1,11 @@
# Django settings for pythonkc_site project.
import os

from os.path import abspath, dirname, join

import dotenv
dotenv.read_dotenv(abspath(join(dirname(__file__), '.env')))


PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))

Expand Down

0 comments on commit 4eaf110

Please sign in to comment.