Skip to content

Commit b1a2836

Browse files
committed
need to sort out logging
1 parent a603a53 commit b1a2836

File tree

2 files changed

+68
-80
lines changed

2 files changed

+68
-80
lines changed

chapter_10_production_readiness.asciidoc

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -459,14 +459,42 @@ We have a container that we're ready to ship to production!
459459
Find out how in the next exciting installment...
460460

461461

462-
////
462+
463463
=== TODO: log files
464464

465-
provoke a 500 error somehow and make sure we see tracebacks for it?
465+
466+
provoke a 500 error somehow and make sure we see tracebacks for it.
467+
468+
469+
eg delete db.sqlite3
470+
471+
Will show no logs, because by default when DEBUG=True logs are emailed to admins
472+
473+
https://docs.djangoproject.com/en/5.0/ref/logging/#default-logging-configuration
474+
475+
simple logging config, send everything to stdout:
476+
477+
478+
[role="sourcecode"]
479+
.src/superlists/settings.py (ch10l009)
480+
====
481+
[source,python]
482+
----
483+
LOGGING = {
484+
"version": 1,
485+
"disable_existing_loggers": False,
486+
"handlers": {
487+
"console": {"class": "logging.StreamHandler"},
488+
},
489+
"loggers": {
490+
"root": {"handlers": ["console"], "level": "INFO"},
491+
},
492+
}
493+
----
494+
====
495+
466496

467497
docker logs might be enough.
468-
alternatively, mount logfiles from host.
469-
////
470498

471499

472500
[role="pagebreak-before less_space"]

chapter_11_ansible.asciidoc

Lines changed: 36 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,17 @@ false, "AttachStdout": true, "Cmd": ["gunicorn", "--bind", ":8888",
523523
----
524524

525525
TODO: sort out macos M1/arch issues, `docker build --platform linux/amd64` etc.
526+
////
527+
- name: Build container image locally
528+
community.docker.docker_image:
529+
name: superlists
530+
source: build
531+
state: present
532+
build:
533+
path: ../Dockerfile
534+
platform: linux/amd64
535+
delegate_to: 127.0.0.1
536+
////
526537

527538
Looks ok! Let's see if that worked?
528539

@@ -574,6 +585,7 @@ config either.
574585
DJANGO_DEBUG_FALSE=1
575586
DJANGO_SECRET_KEY="{{ secret_key }}"
576587
DJANGO_ALLOWED_HOST="{{ host }}"
588+
577589
----
578590
====
579591

@@ -583,7 +595,8 @@ DJANGO_ALLOWED_HOST="{{ host }}"
583595
====
584596
[source,yaml]
585597
----
586-
598+
- name: Import container image on server
599+
[...]
587600
588601
- name: Ensure .env file exists
589602
ansible.builtin.template:
@@ -594,6 +607,13 @@ DJANGO_ALLOWED_HOST="{{ host }}"
594607
secret_key: "{{ lookup('password', '/dev/null length=32 chars=ascii_letters,digits') }}"
595608
force: false # do not recreate file if it already exists.
596609
610+
- name: Run container
611+
community.docker.docker_container:
612+
name: superlists
613+
image: superlists
614+
state: started
615+
recreate: true
616+
env_file: ~/superlists.env
597617
----
598618
====
599619

@@ -616,95 +636,33 @@ show ssh, curl localhosts maybe.
616636
====
617637
[source,yaml]
618638
----
619-
- name: Allow nonroot user to bind to port 80
620-
ansible.posix.sysctl:
621-
name: net.ipv4.ip_unprivileged_port_start
622-
value: 80
623-
reload: true
624-
become: true
625-
626639
- name: Run container
627-
containers.podman.podman_container:
640+
community.docker.docker_container:
628641
name: superlists
629642
image: superlists
630643
state: started
631644
recreate: true
632-
env_file: /superlists.env
645+
env_file: ~/superlists.env
633646
ports: 80:8888
634647
----
635648
====
636649

637650

638-
==== Using Systemd to Make Sure Our Container Starts on Boot
651+
////
652+
==== Making Sure Our Container Starts on Boot
639653
640-
((("Systemd")))
641654
((("Container", "automatic booting/reloading of")))
642655
Our final step is to make sure
643656
that the server starts up our container automatically on boot,
644657
and reloads it automatically if it crashes.
645658
646-
Ansible and the podman plugins have some modules for this:
647-
648-
649-
[role="sourcecode"]
650-
.infra/ansible-provision.yaml (ch11l006)
651-
====
652-
[source,yaml]
653-
----
654-
- name: Create container
655-
containers.podman.podman_container:
656-
name: superlists
657-
image: superlists
658-
state: stopped
659-
recreate: true
660-
env_file: ~/superlists.env
661-
ports: 80:8888
662-
663-
- name: Generate Systemd config file
664-
containers.podman.podman_generate_systemd:
665-
name: superlists
666-
dest: ~/.config/systemd/user/
667-
668-
- name: Container must be started and enabled on systemd
669-
ansible.builtin.systemd:
670-
name: container-superlists
671-
daemon_reload: true
672-
state: started
673-
enabled: true
674-
----
675-
====
659+
(used to need systemd, now you can just set restart_policy.
660+
////
676661

677662

678-
----
679-
vagrant@ubuntu-jammy:~$ cat ~/.config/systemd/user/container-superlists.service
680-
# container-superlists.service
681-
# autogenerated by Podman 3.4.4
682-
# Wed Oct 25 10:55:38 UTC 2023
683-
684-
[Unit]
685-
Description=Podman container-superlists.service
686-
Documentation=man:podman-generate-systemd(1)
687-
Wants=network-online.target
688-
After=network-online.target
689-
RequiresMountsFor=/run/user/1000/containers
690-
691-
[Service]
692-
Environment=PODMAN_SYSTEMD_UNIT=%n
693-
Restart=on-failure
694-
TimeoutStopSec=70
695-
ExecStart=/usr/bin/podman start superlists
696-
ExecStop=/usr/bin/podman stop -t 10 superlists
697-
ExecStopPost=/usr/bin/podman stop -t 10 superlists
698-
PIDFile=/run/user/1000/containers/overlay-containers/c058e368b446388cf3b3faecdf1d8186d14d8b0a01fbf64bfca5714ae56d42fe/userdata/conmon.pid
699-
Type=forking
700-
701-
[Install]
702-
WantedBy=default.target
703-
----
663+
=== Mounting the database on the server
704664

705-
Systemd is joyously simple to configure (especially if you've ever had the
706-
dubious pleasure of writing an `init.d` script), and is fairly
707-
self-explanatory.
665+
TODOOooo
708666

709667

710668
[role="small-code"]
@@ -723,12 +681,14 @@ A few more places to look and things to try, now that we've introduced
723681
Podman and Systemd into the mix, should things not go according to plan:
724682
725683
- You can check the Container logs using
726-
`podman logs superlists`.
727-
((("debugging", "Podman")))
684+
`docker logs superlists`.
685+
686+
- You can get detailed info on the Container using
687+
`docker inspect superlists`.
728688
729-
- You can check the Systemd logs using
730-
`journalctl --user -u container-superlists`.
731-
((("debugging", "Systemd")))
689+
- And you can inspect the image with
690+
`docker image inspect superlists`.
691+
((("debugging", "Docker")))
732692
733693
734694
*******************************************************************************

0 commit comments

Comments
 (0)