Skip to content

Commit 981958a

Browse files
committed
Deploy to Dreamhost (and with a proper cert!)
I thought I'd be using Kubernetes on AWS but I had trouble figuring it all out since I'm a noob at both of those things. So let's do the simplest thing that will work in the interests of shipping early and often. How: * An (Openstack) instance on Dreamhost DreamCompute created with a Fedora 32 image and the lowest, cheapest specs. * Some ansible to provision that host with what we need, and bring up our service. * Docker-compose in production without any front end. (It seems like most people are using nginx. Maybe I'll need that later, but so far it seems I can get away without it.) * Note that docker compose starts up separate container for postgres just like we do in development. So the database is just sitting there in the VM's volume for now. (Might want to do something different with that in future.) * Note the TiddlyWiki files themselves are stored in S3. (That's not new here, that part has been working for a while now.) * Letsencrypt SSL cert. The DNS is handled in AWS's Route 43, for which there is a certbot plugin that supports the cname creation method used to verify things for letsencrypt. * Change the name of the certs to prevent confusion. We can use the same name for devel and for prod, but if it's call localssl in prod that's gonna be confusion. Also move where the local certs are kept so there's a chance we can still run the prod container locally. There's some inconsistency now between the way certs are done in dev and in prod. Will worry about that later. There's still more to do, e.g. we probably don't come back up after a restart, hence this isn't quite production ready. I'm not planning to "go live" in a big way yet, but I'm going to leave the server up for a bit and declare version 0.0.1 soon.
1 parent 05c74c9 commit 981958a

File tree

10 files changed

+170
-12
lines changed

10 files changed

+170
-12
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ rails/public/packs/
88
rails/public/packs-test/
99
rails/config/master.key
1010
.postgresql-data
11-
etc/certs
11+
certs
1212
etc/build-info.txt
13+
etc/certbot-dns-user-credentials.yml

Dockerfile.base

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ RUN echo deb https://dl.yarnpkg.com/debian/ stable main > /etc/apt/sources.list.
3434
RUN apt-get update -qq && apt-get install -y --no-install-recommends postgresql-client nodejs yarn vim-tiny
3535

3636
# Install self-signed SSL cert
37-
COPY --chown=$APP_USER:$APP_USER etc/certs $CERT_PATH
37+
COPY --chown=$APP_USER:$APP_USER ./certs $CERT_PATH
3838

3939
# Install script to help start rails
4040
COPY --chown=$APP_USER:$APP_USER etc/start-rails.sh /bin

Dockerfile.prod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ USER $APP_USER
1313
ENV RAILS_ENV=production
1414
ENV APP_DATABASE_PASSWORD=notsecure123
1515
ENV RAILS_SERVE_STATIC_FILES=true
16-
ENV RAILS_LOG_TO_STDOUT=true
1716

1817
# Copy our app into the container
1918
# Ignores things listed in .dockerignore

Makefile

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ build-prod:
1111
./etc/create-build-info.sh
1212
docker-compose -f docker-compose-prod.yml build prod
1313

14+
# FIXME: The auth doesn't work from make for some reason
15+
push-prod:
16+
@echo docker push sbaird/tiddlyhost
17+
1418
rails-init:
1519
docker-compose run --rm base bash -c "bundle install && \
1620
bundle exec rails webpacker:install && \
@@ -85,16 +89,32 @@ cleanup:
8589

8690
# Generate an SSL cert
8791
# (If the cert exists, assume the key exists too.)
88-
cert: etc/certs/localssl.cert
92+
cert: certs/ssl.cert
8993

90-
etc/certs/localssl.cert:
94+
certs/ssl.cert:
9195
@cd ./etc && ./create-local-ssl-cert.sh
9296

9397
clear-cert:
94-
@rm ./etc/certs/localssl.cert
95-
@rm ./etc/certs/localssl.key
98+
@rm ./certs/ssl.cert
99+
@rm ./certs/ssl.key
96100

97101
redo-cert: clear-cert cert
98102

99103
github-url:
100104
@echo https://github.com/simonbaird/tiddlyhost
105+
106+
PLAY = ansible-playbook -i ansible/inventory.yml
107+
deploy:
108+
$(PLAY) ansible/deploy.yml
109+
110+
deploy-deps:
111+
$(PLAY) -v ansible/deploy.yml --tags=deps
112+
113+
deploy-certs:
114+
$(PLAY) -v ansible/deploy.yml --tags=certs
115+
116+
deploy-app:
117+
$(PLAY) -v ansible/deploy.yml --tags=app
118+
119+
deploy-ssh:
120+
@ssh fedora@tiddlyhost.com

ansible/deploy.yml

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
2+
- hosts: all
3+
become: yes
4+
become_user: root
5+
6+
vars:
7+
- home_dir: /home/{{ ansible_user }}
8+
root_home_dir: /root
9+
10+
certbot_dns_credentials: ../etc/certbot-dns-user-credentials.yml
11+
docker_compose_file: ../docker-compose-prod.yml
12+
13+
cert_host: tiddlyhost.com
14+
letsencrypt_dir: /etc/letsencrypt/live/{{ cert_host }}
15+
certs_dir: "{{ home_dir }}/certs"
16+
17+
tasks:
18+
19+
- name: Add repo for docker-ce
20+
command:
21+
cmd: dnf config-manager --add-repo https://download.docker.com/linux/fedora/docker-ce.repo
22+
creates: /etc/yum.repos.d/docker-ce.repo
23+
warn: false
24+
tags: deps
25+
26+
- name: Install docker and certbot
27+
dnf:
28+
state: present
29+
name:
30+
- docker-ce
31+
- docker-ce-cli
32+
- containerd.io
33+
- certbot
34+
tags: deps
35+
36+
- name: Start docker
37+
systemd:
38+
name: docker
39+
state: started
40+
enabled: yes
41+
tags: deps
42+
43+
- name: Pip install docker-compose and certbot-dns-route53
44+
pip:
45+
name:
46+
- docker-compose
47+
- certbot-dns-route53
48+
tags: deps
49+
50+
- name: Create dir for certbot aws credentials
51+
file:
52+
path: "{{ root_home_dir }}/.aws"
53+
state: directory
54+
tags: certs
55+
56+
- name: Install AWS credentials for certbot
57+
copy:
58+
src: "{{ certbot_dns_credentials }}"
59+
dest: "{{ root_home_dir }}/.aws/credentials"
60+
tags: certs
61+
62+
- name: Create a cert if it doesn't exist
63+
command:
64+
cmd: >-
65+
certbot certonly
66+
-d {{ cert_host }} -d *.{{ cert_host }}
67+
--dns-route53
68+
-n --agree-tos -m simon.baird@gmail.com
69+
70+
creates: "{{ letsencrypt_dir }}/fullchain.pem"
71+
tags: certs
72+
73+
- name: Renew certs maybe
74+
command:
75+
# TODO: The post-hook is untested, not sure if it will work
76+
cmd: certbot renew --post-hook "docker-compose restart prod"
77+
register: certbot_renew
78+
tags: certs
79+
80+
- name: Show renew output
81+
debug:
82+
var: certbot_renew.stdout_lines
83+
tags: certs
84+
85+
- name: Create dir for certs
86+
file:
87+
path: "{{ certs_dir }}"
88+
state: directory
89+
tags: certs
90+
91+
- name: Copy cert files to where the container looks for them
92+
# TODO: It would be better to only do this if they changed
93+
command:
94+
cmd: cp {{ letsencrypt_dir }}/{{ item.from }} {{ certs_dir }}/{{ item.to }}
95+
with_items:
96+
- from: fullchain.pem
97+
to: ssl.cert
98+
- from: privkey.pem
99+
to: ssl.key
100+
tags: certs
101+
102+
- name: Update owner of the cert files
103+
file:
104+
path: "{{ certs_dir }}"
105+
recurse: yes
106+
owner: "{{ ansible_user }}"
107+
group: "{{ ansible_user }}"
108+
tags: certs
109+
110+
- name: Copy docker-compose file
111+
become: no
112+
copy:
113+
src: "{{ docker_compose_file }}"
114+
dest: "{{ home_dir }}/docker-compose.yml"
115+
tags: app
116+
117+
- name: Pull latest images
118+
# So docker-compose doesn't try to build
119+
command:
120+
cmd: docker-compose pull -q
121+
tags: app
122+
123+
- name: Start our service
124+
command:
125+
cmd: docker-compose up --detach
126+
environment:
127+
# (Maybe we do need to copy up the key file)
128+
RAILS_MASTER_KEY: "{{ lookup('file', '../rails/config/master.key') }}"
129+
tags: app

ansible/inventory.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
all:
2+
hosts:
3+
tiddlyhost.com:
4+
ansible_user: fedora
File renamed without changes.

docker-compose-prod.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
services:
22

33
prod:
4-
image: tiddlyhost:latest
4+
image: sbaird/tiddlyhost:latest
55

66
build:
77
context: .
88
dockerfile: Dockerfile.prod
99

1010
ports:
1111
- "443:3000"
12+
- "80:3000"
1213

1314
depends_on:
1415
- db
1516

17+
volumes:
18+
- ./certs/ssl.cert:/opt/certs/ssl.cert:Z
19+
- ./certs/ssl.key:/opt/certs/ssl.key:Z
20+
1621
environment:
1722
RAILS_MASTER_KEY:
1823

etc/create-local-ssl-cert.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ openssl x509 -req -sha256 -days 365 -in server.csr -signkey server.key -out serv
2525
# Clean up
2626
rm server.pass.key
2727
rm server.csr
28-
mv server.key certs/localssl.key
29-
mv server.crt certs/localssl.cert
28+
mv server.key ../certs/ssl.key
29+
mv server.crt ../certs/ssl.cert
3030

31-
echo Created certs/localssl.cert and certs/localssl.key
31+
echo Created ../certs/ssl.cert and ../certs/ssl.key

etc/start-rails.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
rm -f tmp/pids/server.pid
55

66
# Start rails
7-
exec bin/rails s -p 3000 -b 'ssl://0.0.0.0?key=/opt/certs/localssl.key&cert=/opt/certs/localssl.cert'
7+
exec bin/rails s -p 3000 -b 'ssl://0.0.0.0?key=/opt/certs/ssl.key&cert=/opt/certs/ssl.cert'

0 commit comments

Comments
 (0)