Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dulacp committed Jan 22, 2015
0 parents commit e817965
Show file tree
Hide file tree
Showing 67 changed files with 2,243 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*.log
*.pot
*.pyc
local_settings.py
repo_name
12 changes: 12 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
language: python

before_install:
- time pip install pep8

script:
- pep8 --ignore E201,E202 --max-line-length=120 --exclude='migrations' .

notifications:
email:
on_success: never
on_failure: never
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2013 Pierre Dulac (http://dulaccc.me/)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
79 changes: 79 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# cookiecutter-django-herokuapp

A [cookiecutter](https://github.com/audreyr/cookiecutter) template for Django that is *really* optimized for running on Heroku.

[[https://travis-ci.org/dulaccc/cookiecutter-django-herokuapp.svg?branch=master]](https://travis-ci.org/dulaccc/cookiecutter-django-herokuapp?branch=master)


## Concept

It tries to be minimalistic, I mean no choice are made for you concerning :
- **the css library**, specify which one you want in the bower.json file
- **external django apps**, I don't see the point to choose for you ;)

In fact the only choice made here is deploying on Heroku, well in fact deploying on a [Twelve-Factor App Plateform](http://12factor.net/).


## Requirements

This cookiecutter template uses features that exists only in cookiecutter 0.9.0 or higher.


## Features
---------

- Python 3 only, sorry guys but I had to move on...
- For [Django 1.7](https://docs.djangoproject.com/en/1.7/)
- Procfile for deploying to Heroku
- Heroku optimized server
- Basic caching setup
- Gulp tasks to build the static files and support livereload
- Static served by an Amanzon S3 instance
- Sentry preconfigured for reporting issues (see the [sentry section](#how-to-use-a-cheap-sentry-instance) on how to setup your own instance)


## Q&A

#### Why using `waitress` as the production server
> Gunicorn is actually designed to be used behing a buffering reverse proxy (e.g. nginx). In other words, without this buffering reverse proxy you expose your production server to a *slow network* attacks. If you want to dig on this particular topic, read the great article of @etianen *[Don't use Gunicorn to host your Django sites on Heroku](http://blog.etianen.com/blog/2014/01/19/gunicorn-heroku-django/)*
#### How to use a cheap sentry instance
> Deploy yours ! Check out the [sentry](https://github.com/dulaccc/sentry) project and you'll get in less than 10 minutes a private instance of the Sentry exception logger server.

## Usage

First, get cookiecutter.

```sh
$ pip install cookiecutter
```

Now run it against this repo.

```sh
$ cookiecutter https://github.com/dulaccc/cookiecutter-django-herokuapp.git
```

You'll be prompted for some questions, answer them, then it will create a Django project for you.


## Static handling

### Live reloading and Sass CSS compilation

You can take advantage of live reloading and Sass / Compass CSS compilation with the included Gulp tasks.

Make sure that [nodejs](http://nodejs.org/download/) is installed. Then in the project root run :

```sh
$ npm install
```

Now you just need :

```sh
$ gulp launch
```

Now your turn !
13 changes: 13 additions & 0 deletions cookiecutter.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"project_name": "project_name is the title of the project.",
"repo_name": "{{ cookiecutter.project_name.lower()|replace(' ', '_') }}",
"aws_s3_user_name": "heroku-{{ cookiecutter.repo_name }}",
"aws_s3_bucket_name": "{{ cookiecutter.repo_name }}",
"author_name": "Your Name",
"email": "Your email",
"description": "A short description of the project.",
"domain_name": "example.com",
"version": "0.1.0",
"now": "2015/01/13",
"year": "{{ cookiecutter.now[:4] }}"
}
36 changes: 36 additions & 0 deletions {{cookiecutter.repo_name}}/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
## Byte-compiled
__pycache__/
*.py[cod]
*.egg-info
/dist/
/build/

## Database
*.sqlite3

## Translations
*.mo

## Media files & Collected static files
static/
media/

## Testing
.coverage
.noseids
coverage.xml
violations.txt
nosetests.xml
/htmlcov/*

## Sass
.sass-cache/

## Gulp
.tmp/

## Npm modules
node_modules/

## Env
.env
47 changes: 47 additions & 0 deletions {{cookiecutter.repo_name}}/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# These targets are not files
.PHONY: contribute travis test lint coverage prepare_assets

install:
pip install -r reqs/dev.txt
make prepare_assets

upgrade:
pip install --upgrade -r reqs/dev.txt
make prepare_assets

prepare_assets:
python manage.py bower install

deploy_production:
git push --tag origin master
git push heroku master

migrate_production:
heroku run python manage.py migrate --remote heroku

collectstatic_production:
./manage.py collectstatic --noinput
aws s3 sync --acl public-read renover/static s3://renover-immo/static/

# shortcuts for deploy to the production
dp: deploy_production
dmp: deploy_production migrate_production
cp: collectstatic_production

clean:
# Remove files not in source control
find . -type f -name "*.pyc" -delete
rm -rf nosetests.xml coverage.xml htmlcov violations.txt

todo:
# Look for areas of the code that need updating when some event has taken place
grep --exclude-dir=components -rnH TODO reqs
grep --exclude-dir=components -rnH TODO renover
grep --exclude-dir=components -rnH TODO tests

css:
sass renover/assets/_sass/renover.scss:renover/assets/css/renover.css
autoprefixer renover/assets/css/renover.css

cssw:
kicker -e "make css" renover/assets/_sass/
1 change: 1 addition & 0 deletions {{cookiecutter.repo_name}}/Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: newrelic-admin run-program waitress-serve --port=$PORT wsgi:application
161 changes: 161 additions & 0 deletions {{cookiecutter.repo_name}}/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
# {{ cookiecutter.project_name }}

> Don't hesitate to put a little quote from an unknown person here.
{{ cookiecutter.description }}


## Infos utiles

- Version Python : 3.4.x
- Version Django : 1.7.x


## Local install

First create the virtualenv with the right python version

```sh
$ mkvirtualenv {{ cookiecutter.repo_name }} --python=$(which python3)
$ workon {{ cookiecutter.repo_name }}
```

Install the dependencies

```sh
$ pip install -r reqs/dev.txt
$ npm install
```

Create the local database

```sh
$ createdb {{ cookiecutter.repo_name }}
$ ./manage.py migrate
```

Now you just need to launch the watcher for the style files, and it will run the django dev server too :

```sh
$ gulp launch
```

> If you want a specific port, you can use the `--port` option : `$ gulp launch --port 8005`

## Deploy the beast

### Prepare the static server

#### create a new user

```sh
$ aws iam create-user --user-name {{ cookiecutter.aws_s3_user_name }}
```

**output**
```json
{
"User": {
"UserName": "{{ cookiecutter.aws_s3_user_name }}",
"Path": "/",
"CreateDate": "2015-01-22T14:10:08.058Z",
"UserId": "<user_id>",
"Arn": "arn:aws:iam::<user_arn_id>:user/{{ cookiecutter.aws_s3_user_name }}"
}
}
```

#### give the user some access keys

```sh
$ aws iam create-access-key --user-name {{ cookiecutter.aws_s3_user_name }}
```

**output**
```json
{
"AccessKey": {
"UserName": "{{ cookiecutter.aws_s3_user_name }}",
"Status": "Active",
"CreateDate": "2015-01-22T14:18:56.237Z",
"SecretAccessKey": "<secret_key>",
"AccessKeyId": "<access_key>"
}
}
```

Write down the `<secret_key>` and `<access_key>` values, so that we can give the values to the heroku app.

#### create the aws bucket

```sh
$ aws s3 mb s3://{{ cookiecutter.aws_s3_bucket_name }} --region eu-west-1
```

#### give the user access to the created bucket

```sh
$ aws iam put-user-policy --user-name {{ cookiecutter.aws_s3_user_name }} --policy-name AmazonS3FullAccess-{{ cookiecutter.aws_s3_user_name }} --policy-document '{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "s3:*",
"Resource": [
"arn:aws:s3:::{{ cookiecutter.aws_s3_bucket_name }}",
"arn:aws:s3:::{{ cookiecutter.aws_s3_bucket_name }}/*"
]
}
]
}'
```


### Deploy the app

Lancer les commandes dans l'ordre ci-dessous:

```sh
$ heroku create --region eu {{ cookiecutter.repo_name }}
$ heroku addons:add heroku-postgresql:hobby-dev
$ heroku config:set DJANGO_SETTINGS_MODULE="{{ cookiecutter.repo_name }}.settings.prod"
$ heroku config:set SECRET_KEY=`openssl rand -base64 32`
$ heroku config:set LOCAL_SERVER=0
$ heroku config:set DISABLE_COLLECTSTATIC=1
$ heroku config:set AWS_STORAGE_BUCKET_NAME={{ cookiecutter.repo_name }} AWS_S3_ACCESS_KEY_ID="<access_key>" AWS_S3_SECRET_ACCESS_KEY="<secret_key>"
$ git push heroku master
$ ./manage.py collectstatic --noinput
$ aws s3 sync --acl public-read {{ cookiecutter.repo_name }}/static s3://{{ cookiecutter.aws_s3_bucket_name }}/static/
$ heroku run python manage.py migrate
$ heroku run python manage.py createsuperuser
$ heroku open
```

And you're done !


## Cheatsheet

I've defined some shortcuts in the `Makefile`, feel free to explore those or add yours.

_**d**eploy to **p**roduction_
```sh
$ make dp
```

_**d**eploy & **m**igrate to **p**roduction_
```sh
$ make dmp
```

_**c**ollectstatic to **p**roduction_
```sh
$ make cp
```


## Contact

[Pierre Dulac](http://github.com/dulaccc)
[@dulaccc](https://twitter.com/dulaccc)
Loading

0 comments on commit e817965

Please sign in to comment.