Skip to content

Commit 3f8aa26

Browse files
authored
Group environment variables by the corresponding directories (cookiecutter#1295)
* Update generated project's .gitignore * Post-gen gitignore .env/ and .env * Fix linesep between gitignored entries * Persist `.env/**/*` files into cookiecutter-django's VCS * Rename .env/ to .envs/ * Reference the newly created .envs/**/.* files in local.yml * Reference the newly created .envs/**/.* files in production.yml * Delete .env.example * Refactor post-gen-project.py Closes cookiecutter#1299. * Implement production-dotenv-files-to-dotenv-file merge script * Create shared PyCharm Run Configuration for the automation script * Randomize POSTGRES_PASSWORD in ./envs/(.local|.production)/.postgres * Default POSTGRES_PASSWORD and POSTGRES_USER to random values * Fix jinja linebreaks in local.yml * Spaces in production.yml * Fix post-merge leftovers & set DJANGO_ADMIN_URL automatically * Prettify here and there * Fix FileNotFoundError * Leave a TODO in post_gen_hook.py * Introduce keep_local_envs_in_vcs option * Remove envs when not opted for * Inline pre_gen_project.py if-condition * Get rid of PROJECT_DIR_PATH in post_gen_project.py * Clean up the docs * Match copyright notices * Document envs ins and outs
1 parent 6c8538a commit 3f8aa26

24 files changed

+411
-266
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2013-2018, Daniel Greenfeld
1+
Copyright (c) 2013-2018, Daniel Roy Greenfeld
22
All rights reserved.
33

44
Redistribution and use in source and binary forms, with or without modification,

README.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ Pyup brings you automated security and dependency updates used by Google and oth
129129
Usage
130130
------
131131

132-
Let's pretend you want to create a Django project called "redditclone". Rather than using `startproject`
132+
Let's pretend you want to create a Django project called "redditclone". Rather than using ``startproject``
133133
and then editing the results to include your name, email, and various configuration issues that always get forgotten until the worst possible moment, get cookiecutter_ to do all the work.
134134

135135
First, get Cookiecutter. Trust me, it's awesome::
@@ -192,6 +192,7 @@ Answer the prompts with your own desired options_. For example::
192192
4 - Apache Software License 2.0
193193
5 - Not open source
194194
Choose from 1, 2, 3, 4, 5 [1]: 1
195+
keep_local_envs_in_vcs [y]: y
195196

196197
Enter the project and take a look around::
197198

cookiecutter.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,6 @@
3939
"use_opbeat": "n",
4040
"use_whitenoise": "y",
4141
"use_heroku": "n",
42-
"use_travisci": "n"
42+
"use_travisci": "n",
43+
"keep_local_envs_in_vcs": "y"
4344
}

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242

4343
# General information about the project.
4444
project = 'Cookiecutter Django'
45-
copyright = "2013-2016, Daniel Roy Greenfeld".format(now.year)
45+
copyright = "2013-2018, Daniel Roy Greenfeld".format(now.year)
4646

4747
# The version info for the project you're documenting, acts as replacement for
4848
# |version| and |release|, also used in various other places throughout the

docs/deployment-on-pythonanywhere.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ Database setup:
8383

8484
Go to the PythonAnywhere **Databases tab** and configure your database.
8585

86-
* For Postgres, setup your superuser password, then open a Postgres console and run a `CREATE DATABASE my-db-name`. You should probably also set up a specific role and permissions for your app, rather than using the superuser credentials. Make a note of the address and port of your postgres server.
86+
* For Postgres, setup your superuser password, then open a Postgres console and run a ``CREATE DATABASE my-db-name``. You should probably also set up a specific role and permissions for your app, rather than using the superuser credentials. Make a note of the address and port of your postgres server.
8787

8888
* For MySQL, set the password and create a database. More info here: https://help.pythonanywhere.com/pages/UsingMySQL
8989

docs/deployment-with-docker.rst

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Deployment with Docker
2-
=======================
2+
======================
33

44
.. index:: Docker, deployment
55

@@ -10,7 +10,7 @@ Prerequisites
1010
* Docker Compose (at least 1.6)
1111

1212
Understand the Compose Setup
13-
--------------------------------
13+
----------------------------
1414

1515
Before you start, check out the `production.yml` file in the root of this project. This is where each component
1616
of this application gets its configuration from. Notice how it provides configuration for these services:
@@ -73,12 +73,12 @@ You can read more about this here at `Automatic HTTPS`_ in the Caddy docs.
7373
.. _Automatic HTTPS: https://caddyserver.com/docs/automatic-https
7474

7575

76-
Optional: Postgres Data Volume Modifications
76+
(Optional) Postgres Data Volume Modifications
7777
---------------------------------------------
7878

7979
Postgres is saving its database files to the `postgres_data` volume by default. Change that if you want something else and make sure to make backups since this is not done automatically.
8080

81-
Run your app with docker-compose
81+
Run your app with Docker Compose
8282
--------------------------------
8383

8484
To get started, pull your code from source control (don't forget the `.env` file) and change to your projects root
@@ -94,15 +94,15 @@ Once this is ready, you can run it with::
9494

9595
To run a migration, open up a second terminal and run::
9696

97-
docker-compose -f production.yml run django python manage.py migrate
97+
docker-compose -f production.yml run --rm django python manage.py migrate
9898

9999
To create a superuser, run::
100100

101-
docker-compose -f production.yml run django python manage.py createsuperuser
101+
docker-compose -f production.yml run --rm django python manage.py createsuperuser
102102

103103
If you need a shell, run::
104104

105-
docker-compose -f production.yml run django python manage.py shell
105+
docker-compose -f production.yml run --rm django python manage.py shell
106106

107107
To get an output of all running containers.
108108

docs/developing-locally-docker.rst

Lines changed: 78 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,19 @@ Getting Up and Running Locally With Docker
66
The steps below will get you up and running with a local development environment.
77
All of these commands assume you are in the root of your generated project.
88

9+
910
Prerequisites
1011
-------------
1112

12-
You'll need at least Docker 1.10.
13-
14-
If you don't already have it installed, follow the instructions for your OS:
13+
* Docker; if you don't have it yet, follow the `installation instructions`_;
14+
* Docker Compose; refer to the official documentation for the `installation guide`_.
1515

16-
- On Mac OS X, you'll need `Docker for Mac`_
17-
- On Windows, you'll need `Docker for Windows`_
18-
- On Linux, you'll need `docker-engine`_
16+
.. _`installation instructions`: https://docs.docker.com/install/#supported-platforms
17+
.. _`installation guide`: https://docs.docker.com/compose/install/
1918

20-
.. _`Docker for Mac`: https://docs.docker.com/engine/installation/mac/
21-
.. _`Docker for Windows`: https://docs.docker.com/engine/installation/windows/
22-
.. _`docker-engine`: https://docs.docker.com/engine/installation/
2319

24-
Attention Windows users
25-
-----------------------
20+
Attention, Windows Users
21+
------------------------
2622

2723
Currently PostgreSQL (``psycopg2`` python package) is not installed inside Docker containers for Windows users, while it is required by the generated Django project. To fix this, add ``psycopg2`` to the list of requirements inside ``requirements/base.txt``::
2824

@@ -31,23 +27,21 @@ Currently PostgreSQL (``psycopg2`` python package) is not installed inside Docke
3127

3228
Doing this will prevent the project from being installed in an Windows-only environment (thus without usage of Docker). If you want to use this project without Docker, make sure to remove ``psycopg2`` from the requirements again.
3329

30+
3431
Build the Stack
3532
---------------
3633

37-
This can take a while, especially the first time you run this particular command
38-
on your development system::
34+
This can take a while, especially the first time you run this particular command on your development system::
3935

4036
$ docker-compose -f local.yml build
4137

42-
If you want to build the production environment you use ``production.yml`` as -f argument (``docker-compose.yml`` or ``docker-compose.yaml`` are the defaults).
38+
Generally, if you want to emulate production environment use ``production.yml`` instead. And this is true for any other actions you might need to perform: whenever a switch is required, just do it!
4339

44-
Boot the System
45-
---------------
4640

47-
This brings up both Django and PostgreSQL.
41+
Run the Stack
42+
-------------
4843

49-
The first time it is run it might take a while to get started, but subsequent
50-
runs will occur quickly.
44+
This brings up both Django and PostgreSQL. The first time it is run it might take a while to get started, but subsequent runs will occur quickly.
5145

5246
Open a terminal at the project root and run the following for local development::
5347

@@ -61,98 +55,108 @@ And then run::
6155

6256
$ docker-compose up
6357

64-
Running management commands
65-
~~~~~~~~~~~~~~~~~~~~~~~~~~~
58+
To run in a detached (background) mode, just::
6659

67-
As with any shell command that we wish to run in our container, this is done
68-
using the ``docker-compose -f local.yml run`` command.
60+
$ docker-compose up -d
6961

70-
To migrate your app and to create a superuser, run::
7162

72-
$ docker-compose -f local.yml run django python manage.py migrate
73-
$ docker-compose -f local.yml run django python manage.py createsuperuser
63+
Execute Management Commands
64+
---------------------------
7465

75-
Here we specify the ``django`` container as the location to run our management commands.
66+
As with any shell command that we wish to run in our container, this is done using the ``docker-compose -f local.yml run --rm`` command: ::
7667

77-
Add your Docker development server IP
78-
-------------------------------------
68+
$ docker-compose -f local.yml run --rm django python manage.py migrate
69+
$ docker-compose -f local.yml run --rm django python manage.py createsuperuser
7970

80-
When ``DEBUG`` is set to `True`, the host is validated against ``['localhost', '127.0.0.1', '[::1]']``. This is adequate when running a ``virtualenv``. For Docker, in the ``config.settings.local``, add your host development server IP to ``INTERNAL_IPS`` or ``ALLOWED_HOSTS`` if the variable exists.
71+
Here, ``django`` is the target service we are executing the commands against.
8172

82-
Production Mode
83-
~~~~~~~~~~~~~~~
8473

85-
Instead of using `local.yml`, you would use `production.yml`.
74+
(Optionally) Designate your Docker Development Server IP
75+
--------------------------------------------------------
8676

87-
Other Useful Tips
88-
-----------------
77+
When ``DEBUG`` is set to ``True``, the host is validated against ``['localhost', '127.0.0.1', '[::1]']``. This is adequate when running a ``virtualenv``. For Docker, in the ``config.settings.local``, add your host development server IP to ``INTERNAL_IPS`` or ``ALLOWED_HOSTS`` if the variable exists.
8978

90-
Make a machine the active unit
91-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9279

93-
This tells our computer that all future commands are specifically for the dev1 machine.
94-
Using the ``eval`` command we can switch machines as needed.
80+
Configuring the Environment
81+
---------------------------
9582

96-
::
83+
This is the excerpt from your project's ``local.yml``: ::
9784

98-
$ eval "$(docker-machine env dev1)"
85+
# ...
9986

100-
Detached Mode
101-
~~~~~~~~~~~~~
87+
postgres:
88+
build:
89+
context: .
90+
dockerfile: ./compose/production/postgres/Dockerfile
91+
volumes:
92+
- postgres_data_local:/var/lib/postgresql/data
93+
- postgres_backup_local:/backups
94+
env_file:
95+
- ./.envs/.local/.postgres
10296

103-
If you want to run the stack in detached mode (in the background), use the ``-d`` argument:
97+
# ...
10498

105-
::
99+
The most important thing for us here now is ``env_file`` section enlisting ``./.envs/.local/.postgres``. Generally, the stack's behavior is governed by a number of environment variables (`env(s)`, for short) residing in ``envs/``, for instance, this is what we generate for you: ::
106100

107-
$ docker-compose -f local.yml up -d
101+
.envs
102+
├── .local
103+
│   ├── .django
104+
│   └── .postgres
105+
└── .production
106+
├── .caddy
107+
├── .django
108+
└── .postgres
108109

109-
Debugging
110-
~~~~~~~~~~~~~
110+
By convention, for any service ``sI`` in environment ``e`` (you know ``someenv`` is an environment when there is a ``someenv.yml`` file in the project root), given ``sI`` requires configuration, a ``.envs/.e/.sI`` `service configuration` file exists.
111111

112-
ipdb
113-
"""""
112+
Consider the aforementioned ``.envs/.local/.postgres``: ::
114113

115-
If you are using the following within your code to debug:
114+
# PostgreSQL
115+
# ------------------------------------------------------------------------------
116+
POSTGRES_USER=XgOWtQtJecsAbaIyslwGvFvPawftNaqO
117+
POSTGRES_PASSWORD=jSljDz4whHuwO3aJIgVBrqEml5Ycbghorep4uVJ4xjDYQu0LfuTZdctj7y0YcCLu
116118

117-
::
119+
The two envs we are presented with here are ``POSTGRES_USER``, and ``POSTGRES_PASSWORD`` (by the way, their values have also been generated for you). You might have figured out already where these definitions will end up; it's all the same with ``django`` and ``caddy`` service container envs.
118120

119-
import ipdb; ipdb.set_trace()
120121

121-
Then you may need to run the following for it to work as desired:
122+
Tips & Tricks
123+
-------------
124+
125+
Activate a Docker Machine
126+
~~~~~~~~~~~~~~~~~~~~~~~~~
122127

123-
::
128+
This tells our computer that all future commands are specifically for the dev1 machine. Using the ``eval`` command we can switch machines as needed.::
124129

125-
$ docker-compose -f local.yml run --service-ports django
130+
$ eval "$(docker-machine env dev1)"
126131

132+
Debugging
133+
~~~~~~~~~
127134

128-
django-debug-toolbar
129-
""""""""""""""""""""
135+
ipdb
136+
"""""
137+
138+
If you are using the following within your code to debug: ::
130139

131-
In order for django-debug-toolbar to work with docker you need to add your docker-machine ip address to ``INTERNAL_IPS`` in ``local.py``
140+
import ipdb; ipdb.set_trace()
132141

142+
Then you may need to run the following for it to work as desired: ::
133143

134-
.. May be a better place to put this, as it is not Docker specific.
144+
$ docker-compose -f local.yml run --rm --service-ports django
135145

136-
You may need to add the following to your css in order for the django-debug-toolbar to be visible (this applies whether Docker is being used or not):
137146

138-
.. code-block:: css
147+
django-debug-toolbar
148+
""""""""""""""""""""
139149

140-
/* Override Bootstrap 4 styling on Django Debug Toolbar */
141-
#djDebug[hidden], #djDebug [hidden] {
142-
display: block !important;
143-
}
150+
In order for ``django-debug-toolbar`` to work designate your Docker Machine IP with ``INTERNAL_IPS`` in ``local.py``.
144151

145-
#djDebug [hidden][style='display: none;'] {
146-
display: none !important;
147-
}
148152

153+
Mailhog
154+
~~~~~~~
149155

150-
Using the Mailhog Docker Container
151-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
156+
When developing locally you can go with MailHog_ for email testing provided ``use_mailhog`` was set to ``y`` on setup. To proceed,
152157

153-
In development you can (optionally) use MailHog_ for email testing. If you selected `use_docker`, MailHog is added as a Docker container. To use MailHog:
158+
#. make sure ``mailhog`` container is up and running;
154159

155-
1. Make sure, that ``mailhog`` docker container is up and running
156-
2. Open your browser and go to ``http://127.0.0.1:8025``
160+
#. open up ``http://127.0.0.1:8025``.
157161

158162
.. _Mailhog: https://github.com/mailhog/MailHog/

docs/docker-postgres-backups.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Database Backups with Docker
33
============================
44

5-
The database has to be running to create/restore a backup. These examples show local examples. If you want to use it on a remote server, remove ``-f local.yml`` from each example.
5+
The database has to be running to create/restore a backup. These examples show local examples. If you want to use it on a remote server, use ``-f production.yml`` instead.
66

77
Running Backups
88
================
@@ -11,17 +11,17 @@ Run the app with `docker-compose -f local.yml up`.
1111

1212
To create a backup, run::
1313

14-
docker-compose -f local.yml run postgres backup
14+
docker-compose -f local.yml run --rm postgres backup
1515

1616

1717
To list backups, run::
1818

19-
docker-compose -f local.yml run postgres list-backups
19+
docker-compose -f local.yml run --rm postgres list-backups
2020

2121

2222
To restore a backup, run::
2323

24-
docker-compose -f local.yml run postgres restore filename.sql
24+
docker-compose -f local.yml run --rm postgres restore filename.sql
2525

2626
Where <containerId> is the ID of the Postgres container. To get it, run::
2727

docs/faq.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
FAQ
2-
====
2+
===
33

44
.. index:: FAQ, 12-Factor App
55

@@ -17,11 +17,11 @@ Why aren't you using just one configuration file (12-Factor App)
1717
----------------------------------------------------------------------
1818

1919
TODO
20+
.. TODO
2021
21-
Why doesn't this follow the layout from Two Scoops of Django 1.8?
22-
----------------------------------------------------------------------
23-
24-
You may notice that some elements of this project do not exactly match what we describe in chapter 3 of `Two Scoops of Django`_. The reason for that is this project, amongst other things, serves as a test bed for trying out new ideas and concepts. Sometimes they work, sometimes they don't, but the end result is that it won't necessarily match precisely what is described in the book I co-authored.
22+
Why doesn't this follow the layout from Two Scoops of Django?
23+
-------------------------------------------------------------
2524

25+
You may notice that some elements of this project do not exactly match what we describe in chapter 3 of `Two Scoops of Django 1.11`_. The reason for that is this project, amongst other things, serves as a test bed for trying out new ideas and concepts. Sometimes they work, sometimes they don't, but the end result is that it won't necessarily match precisely what is described in the book I co-authored.
2626

27-
.. _`Two Scoops of Django`: http://twoscoopspress.com/products/two-scoops-of-django-1-8
27+
.. _Two Scoops of Django 1.11: https://www.twoscoopspress.com/collections/django/products/two-scoops-of-django-1-11

0 commit comments

Comments
 (0)