Skip to content

Commit 75d066f

Browse files
Merge pull request #80 from caktus/develop
Production Deploy 04/20/2021
2 parents 02b1af6 + e88402d commit 75d066f

File tree

315 files changed

+28974
-18943
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

315 files changed

+28974
-18943
lines changed

.babelrc

-12
This file was deleted.

.circleci/config.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ jobs:
5858
name: Test
5959
command: |
6060
. env/bin/activate
61-
./run_tests.sh
61+
pytest
6262
- slack/status:
6363
success_message: ":caktus: ${CIRCLE_BRANCH} tests passed. <${CIRCLE_BUILD_URL}|Build> :successful:"
6464
failure_message: ":sadcactus: ${CIRCLE_BRANCH} tests failed. <${CIRCLE_BUILD_URL}|Build> :failed:"

.dockerignore

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@ docker-compose.yml
66
jgdc.yml
77
Dockerfile
88
setup.cfg
9-
node_modules
109
*.dump
1110
*.env
1211
.git
1312
ncdata
13+
frontend/build
14+
frontend/node_modules
15+
.direnv
16+
deploy/
17+
.env
18+
.envrc

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@ npm-debug.log
4444
env-local.sh
4545
*.dump
4646
.envrc
47+
.direnv
4748
.vscode
4849
jgdc.yml
4950
aws_ec2.yml
5051
deploy/roles
5152
docker-compose.override.yml
53+
htmlcov

.npmrc

-1
This file was deleted.

.nvmrc

-1
This file was deleted.

Dockerfile

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
FROM node:10-alpine as static_files
1+
FROM node:12.16-alpine as static_files
22

33
WORKDIR /code
44
ENV PATH /code/node_modules/.bin:$PATH
5-
COPY package.json package-lock.json gulpfile.js /code/
5+
COPY frontend/package.json frontend/package-lock.json /code/
66
RUN npm install --silent
7-
COPY . /code/
7+
COPY frontend/ /code/
88
RUN npm run build
99

1010
FROM python:3.8-slim as base
@@ -56,8 +56,8 @@ RUN mkdir /code/
5656
WORKDIR /code/
5757
ADD . /code/
5858

59-
COPY --from=static_files /code/traffic_stops/static /code/traffic_stops/static
60-
COPY --from=static_files /code/node_modules/bootstrap /code/node_modules/bootstrap
59+
# Copy React SPA build into final image
60+
COPY --from=static_files /code/build /code/build
6161

6262
# uWSGI will listen on this port
6363
EXPOSE 8000

Makefile

-6
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,6 @@ setup:
2222
@echo 'Setting up the environment...'
2323
make install_requirements
2424

25-
run-dev:
26-
@echo 'Running local development'
27-
docker-compose up -d --remove-orphans
28-
npm run dev &
29-
python manage.py runserver
30-
3125
run-tests:
3226
@echo 'Checking for migrations'
3327
python manage.py makemigrations --dry-run | grep 'No changes detected' || (echo 'There are changes which require migrations.' && exit 1)

deploy/deploy-hosting-services.yml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
- name: caktus hosting services
2+
hosts: production
3+
vars:
4+
ansible_connection: local
5+
ansible_python_interpreter: "{{ ansible_playbook_python }}"
6+
gather_facts: false
7+
roles:
8+
- role: caktus.k8s-hosting-services

deploy/host_vars/production.yml

+14
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,17 @@ k8s_environment_variables:
113113
NC_FTP_HOST: "{{ env_nc_ftp_host }}"
114114
NC_FTP_USER: "{{ env_nc_ftp_user }}"
115115
NC_FTP_PASSWORD: "{{ env_nc_ftp_password }}"
116+
117+
118+
# ** TS_BACKUPS
119+
k8s_hosting_services_project_name: "{{ app_name }}"
120+
k8s_hosting_services_aws_secret_access_key: !vault |
121+
$ANSIBLE_VAULT;1.1;AES256
122+
66346166663139656536333430393532616530633234626234356435303239323530313937306430
123+
6436646430376262346230623339616538346538656432630a623531306337383064333763613532
124+
35383664646437386662326665373130323031316133353562313932346536316130613566326664
125+
3533623661316635340a663162636630343066376235383138396436343038613666383061393035
126+
30653766333761613335383633353034623737323030306266373966643831323663323661393261
127+
6338393036373961333736646565306137666636623365616636
128+
k8s_hosting_services_database_url: "{{ env_database_url }}"
129+
k8s_hosting_services_healthcheck_url: https://hc-ping.com/c80a410a-f739-4613-9aa6-bda801e7cfdb

deploy/host_vars/staging.yml

+9-9
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,15 @@ database_password: !vault |
6767
3263343333363130630a373033353533613064653033623138313334623537383037356262383662
6868
36613231353732663637316637383061376566663466373865356539626539376161
6969
70-
# k8s_container_htpasswd: !vault |
71-
# $ANSIBLE_VAULT;1.1;AES256
72-
# 31356461656536343532333632356338616462346436386566643438376237333935373531633762
73-
# 6436356465366162666366326530353936373461393932370a386665613137343137333262356235
74-
# 38353965393664313666393138643930323161343234393630616164363732633330373436306235
75-
# 3837386533393162370a663536653965383137346133333732353634323932306339626465623237
76-
# 38326131626331653931396137613861623630373162626637396637643135626531663538653332
77-
# 38326363333664626262343631666233663733313562333035663233333530383432616137323630
78-
# 333037613330396430643061653438313766
70+
k8s_container_htpasswd: !vault |
71+
$ANSIBLE_VAULT;1.1;AES256
72+
31356461656536343532333632356338616462346436386566643438376237333935373531633762
73+
6436356465366162666366326530353936373461393932370a386665613137343137333262356235
74+
38353965393664313666393138643930323161343234393630616164363732633330373436306235
75+
3837386533393162370a663536653965383137346133333732353634323932306339626465623237
76+
38326131626331653931396137613861623630373162626637396637643135626531663538653332
77+
38326363333664626262343631666233663733313562333035663233333530383432616137323630
78+
333037613330396430643061653438313766
7979
8080
k8s_environment_variables:
8181
DATABASE_URL: "{{ env_database_url }}"

deploy/requirements.yml

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
- src: https://github.com/caktus/ansible-role-django-k8s
44
name: caktus.django-k8s
5-
version: v0.0.8
5+
version: v0.0.11
66

77
- src: https://github.com/caktus/ansible-role-aws-web-stacks
88
name: caktus.aws-web-stacks
@@ -11,3 +11,7 @@
1111
- src: https://github.com/caktus/ansible-role-k8s-web-cluster
1212
name: caktus.k8s-web-cluster
1313
version: v0.0.7
14+
15+
- src: https://github.com/caktus/ansible-role-k8s-hosting-services
16+
name: caktus.k8s-hosting-services
17+
version: v0.0.1

docs/dev-setup.rst

+16-92
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ Stops project. To begin you should have the following applications installed on
66
your local development system:
77

88
- Python 3.8
9-
- NodeJS >= 10.21.0
9+
- NodeJS >= 12.6.0
1010
- `pip >= 8 or so <http://www.pip-installer.org/>`_
11-
- `virtualenv >= 1.10 <http://www.virtualenv.org/>`_
12-
- `virtualenvwrapper >= 3.0 <http://pypi.python.org/pypi/virtualenvwrapper>`_
1311
- Postgres >= 12
1412

1513

@@ -41,10 +39,6 @@ necessary requirements::
4139
$ mkvirtualenv --python=`which python3.8` traffic-stops
4240
(traffic-stops)$ pip install -U pip
4341
(traffic-stops)$ make setup
44-
(traffic-stops)$ npm install
45-
46-
If ``npm install`` fails, make sure you're using ``npm`` from a reasonable version
47-
of NodeJS, as documented at the top of this document.
4842

4943
Next, we'll set up our local environment variables. We use `django-dotenv
5044
<https://github.com/jpadilla/django-dotenv>`_ to help with this. It reads environment variables
@@ -64,19 +58,29 @@ Migrate the project databases::
6458
(traffic-stops)$ ./migrate_all_dbs.sh
6559

6660

61+
Frontend setup::
62+
63+
(traffic-stops)$ cd frontend
64+
(traffic-stops)$ npm i
65+
66+
If ``npm install`` fails, make sure you're using ``npm`` from a reasonable version
67+
of NodeJS, as documented at the top of this document.
68+
69+
6770
Development
6871
-----------
6972

70-
You should be able to run the development server via the configured ``dev`` script::
73+
Start API dev server::
7174

72-
(traffic-stops)$ npm run dev
75+
(traffic-stops)$ ./manage.py runserver
7376

74-
Or, on a custom port and address::
77+
In another terminal window, start the frontend dev server::
7578

76-
(traffic-stops)$ npm run dev -- --address=0.0.0.0 --port=8020
79+
(traffic-stops)$ cd frontend
80+
(traffic-stops)$ npm run start
7781

7882
Any changes made to Python, Javascript or Less files will be detected and rebuilt transparently as
79-
long as the development server is running.
83+
long as both dev servers are running.
8084

8185

8286
When running migrations
@@ -86,83 +90,3 @@ This is a multi-database project. Whenever you have unapplied migrations,
8690
either added locally or via an update from the source repository, the
8791
migrations need to be applied to all databases by running the
8892
``./migrate_all_dbs.sh`` command.
89-
90-
91-
Docker
92-
======
93-
94-
You can use the provided ``docker-compose`` environment to create a local development environment.
95-
See previous section for more detailed instructions about how this project is configured.
96-
97-
Setup your local development settings::
98-
99-
cp traffic_stops/settings/local.example.py traffic_stops/settings/local.py
100-
# uncomment lines below "UNCOMMENT BELOW IF USING DOCKER SETUP" in local.py
101-
echo "DJANGO_SETTINGS_MODULE=traffic_stops.settings.local" > .env
102-
103-
For basic setup, run the following commands::
104-
105-
docker-compose up -d db # start the PostgreSQL container in the background
106-
docker-compose build web # build the container (can take a while)
107-
docker-compose run --rm web createdb -E UTF-8 traffic_stops
108-
docker-compose run --rm web createdb -E UTF-8 traffic_stops_nc
109-
docker-compose run --rm web ./migrate_all_dbs.sh
110-
111-
Run ``npm install`` inside the ``web`` container (you'll need to do this for any update to
112-
``package.json``)::
113-
114-
rm -rf ./node_modules # if needed
115-
docker-compose run --rm web bash -lc "npm install"
116-
117-
You can now run the web container and tail the logs::
118-
119-
# start up the dev server, and watch the logs:
120-
docker-compose up -d web && docker-compose logs -f web
121-
122-
These are other useful docker-compose commands::
123-
124-
# explicitly execute runserver in the foreground (for breakpoints):
125-
docker-compose stop web
126-
docker-compose run --rm --service-ports web python manage.py runserver 0.0.0.0:8000
127-
128-
Visit http://localhost:8003/ in your browser.
129-
130-
131-
Restore Production Data
132-
-----------------------
133-
134-
The data import process for each state can take a long time. You can load the production data using
135-
the following steps:
136-
137-
First download a dump (in this case, NC) of the database::
138-
139-
ssh opendatapolicing.com 'sudo -u postgres pg_dump -Fc -Ox traffic_stops_nc_production' > traffic_stops_nc_production.pgdump
140-
141-
Now run ``pg_restore`` within the ``web`` container::
142-
143-
docker-compose stop web # free up connections to the DB
144-
docker-compose run --rm web dropdb traffic_stops_nc
145-
docker-compose run --rm web createdb -E UTF-8 traffic_stops_nc
146-
docker-compose run --rm web pg_restore -Ox -d traffic_stops_nc traffic_stops_nc_production.pgdump
147-
rm traffic_stops_nc_production.pgdump # so it doesn't get built into the container
148-
149-
You can also load the primary DB with user accounts and state statistics::
150-
151-
ssh opendatapolicing.com 'sudo -u postgres pg_dump -Fc -Ox traffic_stops_production' > traffic_stops_production.pgdump
152-
docker-compose stop web # free up connections to the DB
153-
docker-compose run --rm web dropdb traffic_stops
154-
docker-compose run --rm web createdb -E UTF-8 traffic_stops
155-
docker-compose run --rm web pg_restore -Ox -d traffic_stops traffic_stops_production.pgdump
156-
rm traffic_stops_production.pgdump # so it doesn't get built into the container
157-
158-
159-
Deployment
160-
----------
161-
162-
You can run a deployment from within a docker container using the following commands::
163-
164-
docker-compose run --rm web /bin/bash
165-
eval $(ssh-agent)
166-
ssh-add ~/.ssh/YOUR_KEY
167-
168-
fab -u YOUR_USER staging salt:"test.ping"

0 commit comments

Comments
 (0)