From 4eaf1104bebedbae8e38bb348efea490dad2867d Mon Sep 17 00:00:00 2001 From: Jon Smajda Date: Sun, 2 Aug 2015 16:11:54 -0500 Subject: [PATCH] Initial ansible playbook. Uses Python 2 for now. Python 3 supported via an Ansible variable. (See README for details) --- README.markdown | 30 ++++++++++++ ansible/group_vars/all.yml | 2 +- ansible/group_vars/production.yml | 2 + ansible/group_vars/vagrant.yml | 2 + ansible/roles/pythonkc/defaults/main.yml | 2 + ansible/roles/pythonkc/tasks/django.yml | 47 +++++++++++++++++++ ansible/roles/pythonkc/tasks/main.yml | 9 ++++ ansible/roles/pythonkc/tasks/python2.yml | 17 +++++++ .../main.yml => pythonkc/tasks/python3.yml} | 9 ++-- ansible/roles/pythonkc/tasks/tools.yml | 16 +++++++ ansible/vagrant.yml | 11 +---- provision.sh | 7 +-- pythonkc_site/.env.example | 3 ++ pythonkc_site/manage.py | 1 + pythonkc_site/requirements/gondor.txt | 2 + pythonkc_site/requirements/project.txt | 3 +- pythonkc_site/settings.py | 5 ++ 17 files changed, 147 insertions(+), 21 deletions(-) create mode 100644 ansible/roles/pythonkc/defaults/main.yml create mode 100644 ansible/roles/pythonkc/tasks/django.yml create mode 100644 ansible/roles/pythonkc/tasks/main.yml create mode 100644 ansible/roles/pythonkc/tasks/python2.yml rename ansible/roles/{base/tasks/main.yml => pythonkc/tasks/python3.yml} (74%) create mode 100644 ansible/roles/pythonkc/tasks/tools.yml create mode 100644 pythonkc_site/.env.example create mode 100644 pythonkc_site/requirements/gondor.txt diff --git a/README.markdown b/README.markdown index 150361d..12ebdaf 100644 --- a/README.markdown +++ b/README.markdown @@ -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 @@ -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) ``` @@ -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/ diff --git a/ansible/group_vars/all.yml b/ansible/group_vars/all.yml index 66ba6ee..a7c25ae 100644 --- a/ansible/group_vars/all.yml +++ b/ansible/group_vars/all.yml @@ -1,2 +1,2 @@ --- -example: true # TODO, put any common variables in here +virtualenv: ~/virtualenvs/pythonkc diff --git a/ansible/group_vars/production.yml b/ansible/group_vars/production.yml index e5a7d4e..dd9c76e 100644 --- a/ansible/group_vars/production.yml +++ b/ansible/group_vars/production.yml @@ -1,2 +1,4 @@ --- user: pythonkc +pythonpath: /TODO +project_root: /TODO/pythonkc_site diff --git a/ansible/group_vars/vagrant.yml b/ansible/group_vars/vagrant.yml index f0da763..61df658 100644 --- a/ansible/group_vars/vagrant.yml +++ b/ansible/group_vars/vagrant.yml @@ -1,2 +1,4 @@ --- user: vagrant +pythonpath: /home/vagrant/vagrant +project_root: /home/vagrant/vagrant/pythonkc_site diff --git a/ansible/roles/pythonkc/defaults/main.yml b/ansible/roles/pythonkc/defaults/main.yml new file mode 100644 index 0000000..cef7389 --- /dev/null +++ b/ansible/roles/pythonkc/defaults/main.yml @@ -0,0 +1,2 @@ +--- +python_version: 2 diff --git a/ansible/roles/pythonkc/tasks/django.yml b/ansible/roles/pythonkc/tasks/django.yml new file mode 100644 index 0000000..168c1ff --- /dev/null +++ b/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 }}" diff --git a/ansible/roles/pythonkc/tasks/main.yml b/ansible/roles/pythonkc/tasks/main.yml new file mode 100644 index 0000000..580670f --- /dev/null +++ b/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) diff --git a/ansible/roles/pythonkc/tasks/python2.yml b/ansible/roles/pythonkc/tasks/python2.yml new file mode 100644 index 0000000..3acaf12 --- /dev/null +++ b/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 diff --git a/ansible/roles/base/tasks/main.yml b/ansible/roles/pythonkc/tasks/python3.yml similarity index 74% rename from ansible/roles/base/tasks/main.yml rename to ansible/roles/pythonkc/tasks/python3.yml index 8f17219..536f083 100644 --- a/ansible/roles/base/tasks/main.yml +++ b/ansible/roles/pythonkc/tasks/python3.yml @@ -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 diff --git a/ansible/roles/pythonkc/tasks/tools.yml b/ansible/roles/pythonkc/tasks/tools.yml new file mode 100644 index 0000000..e975012 --- /dev/null +++ b/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? diff --git a/ansible/vagrant.yml b/ansible/vagrant.yml index fa741db..b03563c 100644 --- a/ansible/vagrant.yml +++ b/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 diff --git a/provision.sh b/provision.sh index c1e7ffe..62d57d4 100644 --- a/provision.sh +++ b/provision.sh @@ -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 diff --git a/pythonkc_site/.env.example b/pythonkc_site/.env.example new file mode 100644 index 0000000..e56fb5e --- /dev/null +++ b/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' diff --git a/pythonkc_site/manage.py b/pythonkc_site/manage.py index 17ec4b6..085cc5a 100755 --- a/pythonkc_site/manage.py +++ b/pythonkc_site/manage.py @@ -2,6 +2,7 @@ import os import sys + if __name__ == "__main__": os.environ.setdefault("DJANGO_SETTINGS_MODULE", "pythonkc_site.settings") diff --git a/pythonkc_site/requirements/gondor.txt b/pythonkc_site/requirements/gondor.txt new file mode 100644 index 0000000..5124d33 --- /dev/null +++ b/pythonkc_site/requirements/gondor.txt @@ -0,0 +1,2 @@ +gondor==1.0.5 +wsgiref==0.1.2 diff --git a/pythonkc_site/requirements/project.txt b/pythonkc_site/requirements/project.txt index 8d28382..17bb9e6 100644 --- a/pythonkc_site/requirements/project.txt +++ b/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 diff --git a/pythonkc_site/settings.py b/pythonkc_site/settings.py index 249cf15..2b12fef 100644 --- a/pythonkc_site/settings.py +++ b/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__))