Skip to content

Commit 4eaf110

Browse files
committed
Initial ansible playbook.
Uses Python 2 for now. Python 3 supported via an Ansible variable. (See README for details)
1 parent ab8d34e commit 4eaf110

File tree

17 files changed

+147
-21
lines changed

17 files changed

+147
-21
lines changed

README.markdown

+30
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ Files for the PythonKC.com website.
44

55
## Development Quickstart Option 1 (vagrant)
66

7+
First, copy `pythonkc_site/.env.example` to `pythonkc_site/.env` and add
8+
your own [meetup api key][] and a unique [django secret key][] (`.env` will
9+
be ignored by git)
10+
11+
Then you have to install some vagrant plugins and build your vagrant box:
12+
713
```
814
vagrant plugin install vagrant-hostmanager
915
vagrant plugin install vagrant-hostsupdater
@@ -23,6 +29,25 @@ cd ~/vagrant/ansible
2329
ansible-playbook vagrant.yml
2430
```
2531

32+
To run the Django development server:
33+
34+
```
35+
vagrant ssh
36+
django-admin runserver 192.168.100.101:8000
37+
```
38+
39+
Now go to `http://192.168.100.101:8000` in your browser. You can edit the files
40+
on your local machine and the server should reload automatically.
41+
42+
For now, this is a Python 2 project. If you want to start using Python 3
43+
and help us fix our problems, set Ansible's `python_version` variable to 3
44+
and it will build the virtualenv using Python 3:
45+
46+
```
47+
ansible-playbook vagrant.yml -e python_version=3
48+
```
49+
50+
2651
## Development Quickstart Option 2 (virtualenv)
2752

2853
```
@@ -38,3 +63,8 @@ Profit! $$$
3863
## More Detailed Instructions
3964

4065
See: docs/local_development
66+
67+
68+
69+
[meetup api key]: https://secure.meetup.com/meetup_api/key/
70+
[django secret key]: http://www.miniwebtool.com/django-secret-key-generator/

ansible/group_vars/all.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
example: true # TODO, put any common variables in here
2+
virtualenv: ~/virtualenvs/pythonkc

ansible/group_vars/production.yml

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
---
22
user: pythonkc
3+
pythonpath: /TODO
4+
project_root: /TODO/pythonkc_site

ansible/group_vars/vagrant.yml

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
---
22
user: vagrant
3+
pythonpath: /home/vagrant/vagrant
4+
project_root: /home/vagrant/vagrant/pythonkc_site
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
python_version: 2
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
- name: Create virtualenvs directory
3+
file: dest=~/virtualenvs state=directory owner={{ user }} group={{ user }}
4+
5+
6+
- name: Install requirements into Python 2.7 virtualenv
7+
pip:
8+
requirements: "{{ project_root }}/requirements/project.txt"
9+
virtualenv: "{{ virtualenv }}"
10+
virtualenv_command: /usr/bin/virtualenv -p python2.7
11+
when: python_version == 2
12+
13+
14+
- name: Install requirements into Python 3 virtualenv
15+
pip:
16+
requirements: "{{ project_root }}/requirements/project.txt"
17+
virtualenv: "{{ virtualenv }}"
18+
virtualenv_command: python3 /usr/lib/python3/dist-packages/virtualenv.py -p python3
19+
when: python_version == 3
20+
21+
22+
- name: Set some env vars when activating virtualenv
23+
lineinfile:
24+
dest: "{{ virtualenv }}/bin/activate"
25+
regexp: "^export {{ item.name }}="
26+
line: "export {{ item.name }}={{ item.value }}"
27+
state: present
28+
insertafter: EOF
29+
with_items:
30+
- {name: DJANGO_SETTINGS_MODULE, value: pythonkc_site.settings}
31+
- {name: PYTHONPATH, value: "{{ pythonpath }}"}
32+
33+
34+
- name: Automatically activate the virtualenv in bashrc
35+
lineinfile:
36+
dest: ~/.bashrc
37+
line: "source {{ virtualenv }}/bin/activate"
38+
state: present
39+
insertafter: EOF
40+
41+
42+
- name: Run migrations
43+
django_manage:
44+
command: migrate
45+
app_path: "{{ project_root }}"
46+
pythonpath: "{{ pythonpath }}"
47+
virtualenv: "{{ virtualenv }}"

ansible/roles/pythonkc/tasks/main.yml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
- {include: python2.yml, tags: python, when: python_version == 2}
3+
- {include: python3.yml, tags: python, when: python_version == 3}
4+
- include: tools.yml tags=tools
5+
- include: django.yml tags=django
6+
7+
# TODO
8+
# vim + vim configuration for python?
9+
# postgres? (for now just using sqlite for development)
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
- name: Update apt
3+
apt: update_cache=yes cache_valid_time=3600
4+
become: yes
5+
tags: apt
6+
7+
- name: Install some base packages
8+
apt: pkg="{{item}}" state=latest
9+
become: yes
10+
tags: apt
11+
with_items:
12+
- build-essential
13+
- libpq-dev
14+
- python-dev
15+
- python-pip
16+
- python-software-properties
17+
- python-virtualenv
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
---
22
- name: Update apt
33
apt: update_cache=yes cache_valid_time=3600
4+
become: yes
45
tags: apt
56

67
- name: Install some base packages
78
apt: pkg="{{item}}" state=latest
9+
become: yes
810
tags: apt
911
with_items:
1012
- build-essential
13+
- libpq-dev
1114
- python3
1215
- python3-dev
1316
- python3-pip
1417
- python3-software-properties
15-
- vim-nox
16-
- htop
17-
18-
# What else should go in here?
19-
# vim + vim configuration for python?
18+
- python3-virtualenv
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
- name: Update apt
3+
apt: update_cache=yes cache_valid_time=3600
4+
become: yes
5+
tags: apt
6+
7+
8+
- name: Install some dev tools packages
9+
apt: pkg="{{item}}" state=latest
10+
become: yes
11+
tags: apt
12+
with_items:
13+
- vim-nox
14+
- htop
15+
16+
# TODO basic vimrc with python plugins?

ansible/vagrant.yml

+1-10
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
11
---
22
- hosts: vagrant
3-
sudo: yes
4-
5-
# you can have tasks right here
6-
tasks:
7-
- name: Say hello
8-
shell: echo `date` > /home/vagrant/hello.txt
9-
10-
# and you can have 'roles'
113
roles:
12-
- role: base
13-
tags: base_role
4+
- pythonkc
145

provision.sh

+4-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ mkdir -p /var/www
1111
ln -sf /vagrant/pythonkc_site /var/www/pythonkc_site
1212

1313
if [[ -z "$(which ansible)" ]]; then
14-
echo "Installing Ansible..."
15-
aptitude install -y python3 python3-dev python3-pip ansible
14+
echo "Installing pip and Ansible..."
15+
aptitude install -y python-dev python-pip
16+
pip2 install ansible
1617
fi
1718

1819
cd /home/vagrant/vagrant/ansible
19-
ansible-playbook vagrant.yml
20+
sudo -H -u vagrant ansible-playbook vagrant.yml

pythonkc_site/.env.example

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
export MEETUP_API_KEY='your-meetup-api-key-here'
3+
export DJANGO_SECRET_KEY='django-secret-key-here'

pythonkc_site/manage.py

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
import sys
44

5+
56
if __name__ == "__main__":
67
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "pythonkc_site.settings")
78

pythonkc_site/requirements/gondor.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
gondor==1.0.5
2+
wsgiref==0.1.2
+1-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
Django==1.8.3
2+
django-dotenv==1.3.0
23
django-redis-cache==0.9.2
3-
gondor==1.0.5
44
mimeparse==0.1.3
55
psycopg2==2.6.1
66
python-dateutil==1.5
77
pythonkc-meetups==0.1.0
88
redis==2.10.3
99
requests==2.7.0
10-
wsgiref==0.1.2

pythonkc_site/settings.py

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# Django settings for pythonkc_site project.
22
import os
33

4+
from os.path import abspath, dirname, join
5+
6+
import dotenv
7+
dotenv.read_dotenv(abspath(join(dirname(__file__), '.env')))
8+
49

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

0 commit comments

Comments
 (0)