Skip to content

Commit 8117e16

Browse files
committed
Experiment with rearranging the parts structure
1 parent 6b86be1 commit 8117e16

7 files changed

+305
-288
lines changed

Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ part%.forbook.asciidoc: part%.asciidoc
2727
book.html: part1.forbook.asciidoc
2828
book.html: part2.forbook.asciidoc
2929
book.html: part3.forbook.asciidoc
30+
book.html: part4.forbook.asciidoc
3031
book.html: $(SOURCES)
3132

3233

atlas.json

+8-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"pre-requisite-installations.asciidoc",
1111
"video_plug.asciidoc",
1212
"acknowledgments.asciidoc",
13+
1314
"part1.asciidoc",
1415
"chapter_01.asciidoc",
1516
"chapter_02_unittest.asciidoc",
@@ -18,19 +19,23 @@
1819
"chapter_05_post_and_database.asciidoc",
1920
"chapter_06_explicit_waits_1.asciidoc",
2021
"chapter_07_working_incrementally.asciidoc",
21-
"part2.asciidoc",
2222
"chapter_08_prettification.asciidoc",
23+
24+
"part2.asciidoc",
2325
"chapter_09_docker.asciidoc",
2426
"chapter_10_production_readiness.asciidoc",
2527
"chapter_11_server_prep.asciidoc",
2628
"chapter_12_ansible.asciidoc",
29+
30+
"part3.asciidoc",
2731
"chapter_13_organising_test_files.asciidoc",
2832
"chapter_14_database_layer_validation.asciidoc",
2933
"chapter_15_simple_form.asciidoc",
3034
"chapter_16_advanced_forms.asciidoc",
35+
36+
"part4.asciidoc",
3137
"chapter_17_javascript.asciidoc",
3238
"chapter_18_second_deploy.asciidoc",
33-
"part3.asciidoc",
3439
"chapter_19_spiking_custom_auth.asciidoc",
3540
"chapter_20_mocking_1.asciidoc",
3641
"chapter_21_mocking_2.asciidoc",
@@ -39,6 +44,7 @@
3944
"chapter_24_outside_in.asciidoc",
4045
"chapter_25_CI.asciidoc",
4146
"chapter_26_page_pattern.asciidoc",
47+
4248
"chapter_27_hot_lava.asciidoc",
4349
"epilogue.asciidoc",
4450
"appendix_I_PythonAnywhere.asciidoc",

book.asciidoc

+5-3
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,23 @@ include::chapter_04_philosophy_and_refactoring.asciidoc[]
2727
include::chapter_05_post_and_database.asciidoc[]
2828
include::chapter_06_explicit_waits_1.asciidoc[]
2929
include::chapter_07_working_incrementally.asciidoc[]
30+
include::chapter_08_prettification.asciidoc[]
3031

3132
include::part2.forbook.asciidoc[]
32-
include::chapter_08_prettification.asciidoc[]
3333
include::chapter_09_docker.asciidoc[]
3434
include::chapter_10_production_readiness.asciidoc[]
3535
include::chapter_11_server_prep.asciidoc[]
3636
include::chapter_12_ansible.asciidoc[]
37+
38+
include::part3.forbook.asciidoc[]
3739
include::chapter_13_organising_test_files.asciidoc[]
3840
include::chapter_14_database_layer_validation.asciidoc[]
3941
include::chapter_15_simple_form.asciidoc[]
4042
include::chapter_16_advanced_forms.asciidoc[]
43+
44+
include::part4.forbook.asciidoc[]
4145
include::chapter_17_javascript.asciidoc[]
4246
include::chapter_18_second_deploy.asciidoc[]
43-
44-
include::part3.forbook.asciidoc[]
4547
include::chapter_19_spiking_custom_auth.asciidoc[]
4648
include::chapter_20_mocking_1.asciidoc[]
4749
include::chapter_21_mocking_2.asciidoc[]

chapter_09_docker.asciidoc

+24-217
Original file line numberDiff line numberDiff line change
@@ -10,144 +10,31 @@ This will be the 9th chapter of the final book. The GitHub repo is available at
1010
If you have comments about how we might improve the content and/or examples in this book, or if you notice missing material within this chapter, please reach out to the author at [email protected].
1111
****
1212

13-
[quote, 'http://bit.ly/2uhCXnH[Devops Borat]']
13+
[quote, Malvina Reynolds]']
1414
______________________________________________________________
15-
Is all fun and game until you are need of put it in production.
15+
Little boxes, all the same
1616
______________________________________________________________
1717

18-
It's time to deploy the first version of our site and make it public.
19-
They say that if you wait until you feel _ready_ to ship,
20-
then you've waited too long.
21-
22-
Is our site usable? Is it better than nothing? Can we make lists on it?
23-
Yes, yes, yes.
24-
25-
No, you can't log in yet.
26-
No, you can't mark tasks as completed.
27-
But do we really need any of that stuff?
28-
Not really--and you can never be sure
29-
what your users are _actually_ going to do with your site
30-
once they get their hands on it.
31-
We think our users want to use the site for to-do lists,
32-
but maybe they actually want to use it
33-
to make "top 10 best fly-fishing spots" lists,
34-
for which you don't _need_ any kind of "mark completed" function.
35-
We won't know until we put it out there.
36-
37-
Over the next couple of chapters we're going to go through
38-
and actually deploy our site to a real, live web server.
39-
40-
You might be tempted to skip this bit--there's lots of daunting stuff in it,
41-
and maybe you think this isn't what you signed up for.
42-
But I _strongly_ urge you to give it a go.
43-
This is one of the sections of the book I'm most pleased with,
44-
and it's one that people often write to me
45-
saying they were really glad they stuck through it.
46-
47-
If you've never done a server deployment before,
48-
it will demystify a whole world for you,
49-
and there's nothing like the feeling of seeing your site live
50-
on the actual internet.
51-
Give it a buzzword name like "DevOps"
52-
if that's what it takes to convince you it's worth it.
53-
54-
.New content alert! Pls send feedback.
55-
****
56-
As part of my work on the third edition of the book,
57-
I've made big changes to the deployment chapters,
58-
so this stuff is all very fresh.
18+
In this chapter, we'll start by adapting our FTs so that they can run against a container,
19+
And then we'll set about containerising our app,
20+
and get those tests passing our code running inside Docker.
5921

60-
What that means is that I'd, really, really love feedback from readers.
61-
Please have a go at following along and let me know what you think!
62-
I'm [email protected], or you can open up
63-
https://github.com/hjwp/Book-TDD-Web-Dev-Python/issues[GitHub Issues]
64-
or Pull Requests.
22+
* We'll build a minimal Dockerfile with everything we need to run our site.
6523

66-
Let me know how you got on, if you get stuck on anything,
67-
if any explanations don't make sense,
68-
or if any of the instructions don't work for you.
24+
* We'll learn how to build and run a container on our machine.
6925

70-
I hope you enjoy the new version!
71-
****
26+
* We'll make a few changes to our source code layout. like using an _src_ folder.
7227

73-
=== The Danger Areas of Deployment
74-
// XUAN: I read the title as meaning _TDD and_ the danger areas of deployment, but the following section seems to be talking about the danger areas of deployment?
75-
76-
Deploying a site to a live web server can be a tricky topic.
77-
Oft-heard is the forlorn cry __"but it works on my machine!"__
78-
79-
((("deployment", "danger areas of")))
80-
Some of the danger areas of deployment include:
81-
82-
Networking::
83-
Once we're off our own machine, networking issues come in:
84-
making sure that DNS is routing our domain to the correct IP address for our server,
85-
making sure our server is configured to listen to traffic coming in from the world,
86-
making sure it's using the right ports,
87-
and making sure any firewalls in the way are configured to let traffic through.
88-
89-
Dependencies::
90-
We need to make sure that the packages our software relies on
91-
(Python, Django, and so on) are installed on the server,
92-
and have the correct versions.
93-
94-
The database::
95-
There can be permissions and path issues,
96-
and we need to be careful about preserving data between deploys.
97-
98-
Static files (CSS, JavaScript, images, etc.)::
99-
Web servers usually need special configuration for serving these.
100-
((("static files", "challenges of")))
101-
102-
Security and Configuration::
103-
Once we're on the public internet,
104-
we need to worry more about security.
105-
Various settings that are really useful for local development
106-
(like the Django debug page)
107-
become dangerous in production
108-
(because they expose our source code in tracebacks).
109-
110-
Reproducibility and Divergence between local dev and prod::
111-
All of the above add up to differences between your local development environemnt
112-
and the way code runs in production.
113-
We want to be able to reproduce the way things work on our machine,
114-
as closely as possible, in production (and vice-versa)
115-
to give us as much confidence as possible that
116-
"it works on my machine" means "it's going to work in production".
117-
118-
119-
One way to approach the problem is to get a server
120-
and start manually configuring and installing everything,
121-
hacking about until it works,
122-
and maybe think about automating things laterfootnote:[
123-
This was, more or less, the approach I took in earlier editions of the book.
124-
With a fair bit of testing thrown in of course.].
125-
126-
But if there's one thing we've learned
127-
in the world of agile/lean software development,
128-
it's that taking smaller steps usually pays off.
129-
130-
How can we take smaller, safer steps towards a production deployment?
131-
Can we _simulate_ the process of moving to a server
132-
so that we can iron out all the bugs
133-
before we actually take the plunge?
134-
Can we then make small changes one at a time,
135-
solving problems one by one,
136-
rather than having to bite off everything in one mouthful?
137-
Can we use our existing test suite to make sure things
138-
work on the server, as well as locally?
139-
140-
Absolutely we can. And from the title of the chapter,
141-
I'm sure you're already guessing that Docker is going
142-
to be part of the answer.
28+
* We'll start flushing out a few issues around networking and the database.
14329

14430

14531
=== Docker, Containers and Virtualization
14632

14733
Docker is a commercial product that wraps several free
14834
and open source technologies from the world of Linux,
14935
sometimes referred to as "containerization".
150-
(Feel free to skip this section if you already know all about Docker.)
36+
37+
NOTE: Feel free to skip this section if you already know all about Docker.
15138

15239
You may have already heard of the idea of "virtualization",
15340
which allows a single physical computer to pretend to be several machines.
@@ -176,10 +63,13 @@ So you can run multiple operating systems using separate VMs
17663
on the same physical box, as in <<virtualization-diagram>>.
17764

17865
[[virtualization-diagram]]
179-
.Virtualenvs vs Virtual Machines
180-
image::images/virtualenv-vs-vm.png["A diagram showing a virtualenv running in an operating system, vs multiple virtual machines running different operating systems on a single real machine"]
66+
.Physical vs Virtual Machines
67+
image::images/virtualenv-vs-vm.png["A diagram showing a physical machine, with an operating system and a Python virtualenv running inside it, vs multiple virtual machines running different operating systems on a single real machine"]
18168

18269

70+
// TODO; remove virtualenvs from this diagram, they just confuse things.
71+
// add another diagram later to contrast venvs with dockers.
72+
18373
Containerization works at the operating system level:
18474
it gives you multiple virtual operating systems that
18575
all run on a single real OS.footnote:[
@@ -207,11 +97,6 @@ If you're running Linux containers on a Mac or a PC,
20797
it's because you're actually running them on a Linux VM under the hood.].
20898
See <<containers-diagram>> for an illustration.
20999

210-
[[containers-diagram]]
211-
.Containers Share a Kernel in the Host Operating System
212-
image::images/containers-diagram.png["Diagram showing one or more containers running on a single host operating system, showing that each container uses the kernel from the host OS, but is able to have its own filesystem, based on an image, but also possibly mounting directories from the host filesystem"]
213-
214-
215100
The upshot of this is that containers are much "cheaper".
216101
You can start one up in milliseconds,
217102
and you can run hundreds on the same machine.
@@ -224,6 +109,9 @@ NOTE: If you're new to all this, I know it's a lot to wrap your head around!
224109
Hopefully, following along with these chapters and seeing them working in practice
225110
will help you to better understand the theory.
226111

112+
[[containers-diagram]]
113+
.Containers Share a Kernel in the Host Operating System
114+
image::images/containers-diagram.png["Diagram showing one or more containers running on a single host operating system, showing that each container uses the kernel from the host OS, but is able to have its own filesystem, based on an image, but also possibly mounting directories from the host filesystem"]
227115

228116

229117
==== Why not just use a virtualenv?
@@ -246,6 +134,8 @@ and they don't need to understand the intricacies of any particular
246134
language's packaging systems.
247135

248136

137+
138+
249139
==== Docker and your CV
250140

251141
That's all well and good for the _theoretical_ justification,
@@ -265,97 +155,14 @@ For many working developers, a container image is the final artifact of their wo
265155
it's what they "deliver",
266156
and often the rest of the deployment process is something they rarely have to think about.
267157

158+
In any case, without further ado, let's get into it!
268159

269160

270161

271-
=== An Overview of Our Deployment Procedure
272-
273-
Over the next three chapters, I'm going to go through _a_ deployment procedure.
274-
It isn't meant to be the _perfect_ deployment procedure,
275-
so please don't take it as being best practice,
276-
or a recommendation--it's meant to be an illustration,
277-
to show the kinds of issues involved in putting code into production,
278-
and where testing fits in.
279-
280-
281-
**This chapter: Containerizing our software**
282-
283-
* Adapt our FTs so they can run against a container.
284-
285-
* Build a minimal Dockerfile with everything we need to run our site.
286-
287-
* Learn how to build and run a container on our machine.
288-
289-
* Get a first cut of our code up and running inside Docker,
290-
with passing tests.
291-
292-
293-
**<<chapter_10_production_readiness,Next chapter>>: Moving to a production-ready configuration**
294-
295-
* Gradually, incrementally change the container configuration
296-
to make it production-ready.
297-
298-
* Regularly re-run the FTs to check we didn't break anything.
299-
300-
* Address issues to do with the database, static files, secrets, and so on.
301-
302-
303-
**<<chapter_11_server_prep,Third chapter>>: Preparing our staging server and deployment tools**
304-
305-
* We'll set up a "staging":footnote:[
306-
Some people prefer the term pre-prod or test environment.
307-
It's all the same idea.]
308-
server, using the same infrastructure a we plan to use for production.
309-
* Set up a real domain name and point it at this server
310-
* Install Ansible and flush out any networking issues
311-
312-
313-
**<<chapter_12_ansible,Final chapter>>: Preparing our staging server and deployment tools**
314-
315-
* Gradually build up an Ansible playbook to deploy our containers on a real server.
316-
317-
* Again, use our FTs to check for any problems.
318-
319-
* Learn how to SSH in to the server to debug things,
320-
where to find logs and other useful information.
321-
322-
* Confidently deploy to production once we have a working deployment script for staging.
323-
324-
325-
326-
=== TDD and Docker vs the Danger Areas of Deployment
327-
328-
Hopefully you can start to see how the combination of TDD, Docker, Staging,
329-
and automation are going to help minimise the risk of the various "Danger Areas".
330-
331-
* Containers will act as mini-servers
332-
letting us flush out issues with dependencies, static files, and so on.
333-
A key advantage is that they'll give us a way of getting faster feedback cycles,
334-
because we can spin them up locally very quickly, and make changes quickly.
335-
336-
* Our containers will package up both our Python and system dependencies,
337-
including a production-ready web server and static files system.
338-
as well as many production settings and configuration differences.
339-
This minimises the difference between what we can test locally,
340-
and what we will have on our servers.
341-
342-
* Our FTs mean that we'll have a fully automated way of checking
343-
that everything works.
344-
345-
* Later, when we deploy our containers to a staging server,
346-
we can run the FTs against that too.
347-
It'll be slightly slower and might involve some fiddly compromises,
348-
but it'll give us one more layer of reassurance.
349-
350-
* Finally, by fully automating container creation and deployment,
351-
and by testing the end results of both these things,
352-
we maximise reproducibility, thus minimising the risk of deployment to production.
353-
354-
355162
=== As Always, Start with a Test
356163

357164
((("environment variables")))
358-
Let's adapt our functional tests slightly
165+
Let's adapt our functional tests
359166
so that they can run against a standalone server,
360167
instead of the one that `LiveServerTestCase` creates for us.
361168
We'll do it by checking for an environment variable
@@ -496,7 +303,7 @@ Currently, all our code is source code really, so we move everything into _src_
496303
(we'll be seeing some new files appearing outside _src_ shortly).footnote:[
497304
A common thing to find outside of the _src_ folder is a folder called _tests_.
498305
We won't be doing that while we're relying on the standard Django test framework,
499-
but it's a good thing to do if you're using pytest, for example.]
306+
but it can be a good thing to do if you're using pytest, for example.]
500307

501308

502309

0 commit comments

Comments
 (0)