Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docker deploy #550

Merged
merged 136 commits into from
Sep 12, 2020
Merged
Show file tree
Hide file tree
Changes from 121 commits
Commits
Show all changes
136 commits
Select commit Hold shift + click to select a range
43977cf
Create ansible scripts to deploy in docker
jmgrady Jul 16, 2020
c09f819
Add dockerfile for production
jmgrady Jul 16, 2020
9772a3f
Ignore objects derived from Ansible install/config tasks
jmgrady Jul 16, 2020
76ac176
Make Backend/launch.sh executable
jmgrady Jul 16, 2020
7963f1e
Change variable name
jmgrady Jul 16, 2020
81a7623
Set docker_config as directory for config files
jmgrady Jul 17, 2020
5af496d
Add Nginx config file
jmgrady Jul 17, 2020
89220b7
Fix nginx_config variable
jmgrady Jul 21, 2020
21c95b3
Replace /opt/TheCombine with source_dir
jmgrady Jul 21, 2020
b240ec1
Add reCAPTCHA for gradynh.com testing site.
jmgrady Jul 21, 2020
974b2ec
Add variable for source version
jmgrady Jul 22, 2020
40f2e61
Add variable for IS_STAGING value for certbot
jmgrady Jul 22, 2020
0ba0b4e
Clone entire repo - not depth of '1'
jmgrady Jul 22, 2020
0791068
Make production docker-compose an override file
jmgrady Jul 22, 2020
8ca6eef
Merge branch 'master' into docker_deploy
jmgrady Jul 22, 2020
fb55056
Remove .env.backend.auth
jmgrady Jul 22, 2020
43b22dc
Make production docker-compose file an override.
jmgrady Jul 22, 2020
8d47d42
Fix letsencrypt volume
jmgrady Jul 22, 2020
87c6c57
Add shortcuts for docker-compose commands
jmgrady Jul 23, 2020
67e8d0b
Change ASPNETCORE --> COMBINE
jmgrady Jul 23, 2020
50aba52
WIP: Common Dockerfile for development & production
jmgrady Jul 23, 2020
5990c33
Common Dockerfile for development & production
jmgrady Jul 23, 2020
276c476
Add list of envirnment vars for substitution.
jmgrady Jul 23, 2020
c249480
Migrate host file to YAML format
jmgrady Jul 24, 2020
384b022
Install docker "shortcuts" for TheCombine
jmgrady Jul 24, 2020
ebdfa81
Refactor to share common code
jmgrady Jul 24, 2020
9da0a51
Clean up text
jmgrady Jul 24, 2020
28df0f7
Add variable overrides for production
jmgrady Jul 24, 2020
c21c778
Remove dead code
jmgrady Jul 24, 2020
9251bf0
Restore "hosts" inventory for original deployment
jmgrady Jul 24, 2020
bbe9881
Refactor roles
jmgrady Jul 24, 2020
81dbca4
Set admin user and password on install
jmgrady Jul 24, 2020
e265def
Clean-up for Pull Request
jmgrady Jul 27, 2020
158aae1
Clean-up for Pull Request
jmgrady Jul 27, 2020
9b83ca7
Clean-up for Pull Request
jmgrady Jul 27, 2020
01b55c2
Clean-up for Pull Request
jmgrady Jul 27, 2020
d3e5aea
Add settings for QA servers
jmgrady Jul 27, 2020
86bc264
Merge branch 'master' into docker_deploy
jmgrady Jul 27, 2020
28c26ac
Move host variable from inventory file to host_vars files
jmgrady Jul 28, 2020
55d79cc
Ignore testing playbooks in docker_deploy
jmgrady Jul 28, 2020
d2779f3
Split playbook into docker env. and app
jmgrady Jul 28, 2020
1263050
Merge branch 'split_playbook' into docker_deploy
jmgrady Jul 28, 2020
bc80164
Fix scope of variable definitions
jmgrady Jul 28, 2020
e4954cc
Remove NUC definitions for docker deployment
jmgrady Jul 28, 2020
24d56d3
Code tweaks
jmgrady Jul 28, 2020
a9f101c
Update README files
jmgrady Jul 29, 2020
0b4c4f2
Add handler to reboot target if docker is installed
jmgrady Jul 29, 2020
df8e30e
Update documentation
jmgrady Jul 29, 2020
8468c82
Start installation with upgrade of all packages
jmgrady Jul 29, 2020
99b8dbc
Fix setting of COMBINE_CAPTCHA_REQUIRED
jmgrady Jul 30, 2020
a3bd0c8
Merge branch 'master' into docker_deploy
jmgrady Jul 31, 2020
bf41db0
Remove reference to legacy script.
jmgrady Jul 31, 2020
973f05c
Fix indentation in ./docs/docker_deploy/README.md
jmgrady Jul 31, 2020
6bc41b0
Strengthen Ansible vault files
jmgrady Aug 1, 2020
1c00988
Fix backend URL to http://backend:5000
jmgrady Aug 1, 2020
a48dcab
Simplify docker-compose override files
jmgrady Aug 1, 2020
e27243d
Specify versions with Ansible variable
jmgrady Aug 1, 2020
2059a5c
Explain why http config is not in nginx config file
jmgrady Aug 1, 2020
f9ab94a
Address review comments regarding docs/docker_deploy/README.md
jmgrady Aug 1, 2020
99b64b8
Remove superfluous entrypoint overrides
jmgrady Aug 1, 2020
3e67e4a
Clarify combine install instructions
jmgrady Aug 1, 2020
5ddc1f0
Add System Requirements
jmgrady Aug 1, 2020
c507415
Update to Docker Compose 1.26.2
jmgrady Aug 1, 2020
abb03c2
Add COMBINE_ADMIN_USERNAME environment variable
jmgrady Aug 1, 2020
4c4f53c
Merge branch 'master' into docker_deploy
jmgrady Aug 1, 2020
329e4dc
Move common environment variables to Backend/Dockerfile
jmgrady Aug 2, 2020
23acfe8
Set restart conditions in base docker-compose.yml
jmgrady Aug 2, 2020
524c42a
Test jinja variable defined & >0
jmgrady Aug 2, 2020
9431187
Restore definition of production vars for non-docker deployment
jmgrady Aug 2, 2020
e8d8d4c
Prune unused images
jmgrady Aug 2, 2020
f58e980
Fix Jinja2 check for empty variable
jmgrady Aug 2, 2020
449b9b7
Update RAM requirements for building the app
jmgrady Aug 3, 2020
5607c85
Merge branch 'master' into docker_deploy
jmgrady Aug 5, 2020
3251c46
Merge branch 'master' into docker_deploy
jmgrady Aug 5, 2020
4ba8d50
Merge branch 'master' into docker_deploy
jmgrady Aug 5, 2020
feae0a5
Remove deprecated package from install
jmgrady Aug 12, 2020
d866344
Remove runtime configs embedded in the build
jmgrady Aug 12, 2020
f8d738c
Generate config.js in scripts location for nginx
jmgrady Aug 12, 2020
d308109
Point to config directory outside of source
jmgrady Aug 12, 2020
4ade846
Backout changes for config directory
jmgrady Aug 12, 2020
f4ffe83
Create Ansible playbooks to support configuring CI/CD with Docker
jmgrady Aug 14, 2020
fd8ddee
Merge branch 'master' into docker_deploy
jmgrady Aug 14, 2020
dabcb87
Merge branch 'master' into docker_deploy
jmgrady Aug 15, 2020
96f83af
Update machine groups for target setup
jmgrady Aug 19, 2020
9aaebf2
Set image tags to include AWS repository
jmgrady Aug 19, 2020
cf04ed4
Include "combine" namespace in default image tags
jmgrady Aug 19, 2020
3629933
Merge branch 'master' into docker_deploy
jmgrady Aug 19, 2020
b9f8cbf
Merge branch 'master' into docker_deploy
jmgrady Aug 19, 2020
ab57958
Separate build from production
jmgrady Aug 26, 2020
0673df2
Merge branch 'master' into docker_deploy
jmgrady Aug 26, 2020
3fe265c
Merge branch 'master' into docker_deploy
jmgrady Aug 27, 2020
b02d65c
Merge branch 'master' into docker_deploy
jmgrady Sep 1, 2020
8a782b8
Merge branch 'master' into docker_deploy
jmgrady Sep 3, 2020
d7a2b5f
Update documentation for running under Docker
jmgrady Sep 3, 2020
0c5817d
Remove note for Docker on Windows
jmgrady Sep 4, 2020
5c24181
Allow setting of AWS ECR name by environment variable
jmgrady Sep 4, 2020
2e57329
Add Python script to create docker files
jmgrady Sep 7, 2020
990d843
Ignore generated docker files
jmgrady Sep 7, 2020
18b4ef1
Ignore nginx/scripts and nginx/conf.d
jmgrady Sep 7, 2020
0fc686b
ignore Python cache
jmgrady Sep 7, 2020
82ee98b
Tighten permissions on environment files
jmgrady Sep 7, 2020
be3c202
Update Docker instructions
jmgrady Sep 7, 2020
ef037e7
Delete unused playbooks
jmgrady Sep 7, 2020
a83b2ba
Remove obsolete roles
jmgrady Sep 10, 2020
2f9326e
Merge branch 'master' into docker_deploy
jmgrady Sep 10, 2020
8f05806
Restore roles accidentally removed
jmgrady Sep 10, 2020
3c04cbb
Remove unused and sort imports. Format code to follow PEP style guide
johnthagen Sep 11, 2020
df7efbd
Add Python 3 type hints to functions
johnthagen Sep 11, 2020
9672601
Update instructions to prescribe using isolated Python virtual enviro…
johnthagen Sep 11, 2020
91bdb38
Fix docker-compose.yml.j2 encoding to be UTF-8 instead of UTF-8-BOM
johnthagen Sep 11, 2020
1a563d7
Log which files are being written so the developer knows what has cha…
johnthagen Sep 11, 2020
e75dfe2
Update docs/docker_deploy/README.md
jmgrady Sep 11, 2020
5492e74
Remove build instructions
jmgrady Sep 11, 2020
5c905ff
Remove dead code
johnthagen Sep 11, 2020
c294649
Use pathlib to generate NGINX files
johnthagen Sep 11, 2020
bac5d5d
Update nginx config from PR#690
jmgrady Sep 11, 2020
0a626eb
Covert all paths to Path objects and use computed absolute paths to m…
johnthagen Sep 11, 2020
0dac7dc
Merge remote-tracking branch 'origin/docker_deploy' into docker_deploy
johnthagen Sep 11, 2020
929db6f
Use Path.write_text to simplify writing files
johnthagen Sep 11, 2020
afed3bd
Use OS agnostic Path for Jinja environment
johnthagen Sep 11, 2020
ca1317b
Remove unused variable
johnthagen Sep 11, 2020
adf94ee
Support disabling CAPTCHA for development Docker Compose setup
johnthagen Sep 11, 2020
213daa2
Set default values for SMTP_PORT and PASSWORD_RESET .env file values …
johnthagen Sep 11, 2020
2c97d83
Trim Jinja2 blocks so that the .env files that are created don't have…
johnthagen Sep 11, 2020
8443cc1
Simplify Docker build by avoiding copying static NGINX configuration
johnthagen Sep 11, 2020
fe757f3
Remove unused import
johnthagen Sep 11, 2020
7851fb5
Merge branch 'master' into docker_deploy
johnthagen Sep 11, 2020
1d9dd44
Fix backend volume path to match updated non-root user used in backen…
johnthagen Sep 11, 2020
665f987
Allow generating Docker Compose configuration that builds local project
johnthagen Sep 11, 2020
4144149
Allow developer to configure if they want to use a local build or pul…
johnthagen Sep 11, 2020
7096743
Update Docker README instructions
johnthagen Sep 11, 2020
71b4f58
Remove extra newline
johnthagen Sep 11, 2020
e01d993
Add a TODO in the docker_setup.py about hosted images
johnthagen Sep 11, 2020
457b373
Inline config_nginx function now that it has become trivial
johnthagen Sep 11, 2020
0b83f60
Remove installation of unneeded packages
jmgrady Sep 11, 2020
dffa799
Remove unused variable
jmgrady Sep 11, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .env.backend.auth.template

This file was deleted.

8 changes: 0 additions & 8 deletions .env.backend.template

This file was deleted.

17 changes: 13 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@

# Production
/build
production.yml

# Misc
.DS_Store
# Generated Docker files for development use
docker-compose.yml
.env.backend
.env.backend.auth
.env.frontend
.env*.local
nginx/scripts
nginx/conf.d

# Misc
.DS_Store
Session.vim
/*.js

Expand Down Expand Up @@ -56,7 +60,9 @@ mongo_database
.vagrant/

# Python
__pycache__
*.pyc
venv

# Auto-generated SSL keys and certificate requests
deploy/site_info/
Expand All @@ -67,12 +73,15 @@ deploy/roles_galaxy/
# Host file to be used for ad hoc testing/development
deploy/*.hosts
deploy/*.hosts.yml
docker_deploy/*.hosts.yml

# Playbooks for ad hoc testing/development
deploy/_playbook*.yml
docker_deploy/_playbook*.yml

# Emacs backup files (this is Jim's fault)
*~
\#*#

# JetBrains Rider
.idea
2 changes: 2 additions & 0 deletions Backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ FROM mcr.microsoft.com/dotnet/core/aspnet:3.1

ENV ASPNETCORE_URLS=http://+:5000
ENV COMBINE_IS_IN_CONTAINER=1
ENV ASPNETCORE_ENVIRONMENT=Production
ENV DOTNET_PRINT_TELEMETRY_MESSAGE=false

# Set the home directory to the app user's home.
ENV HOME=/home/app
Expand Down
Empty file modified Backend/launch.sh
100644 → 100755
Empty file.
14 changes: 6 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,23 @@ COPY . ./
RUN npm run build

# Production environment.
FROM nginx:stable-alpine
FROM staticfloat/nginx-certbot

WORKDIR /app

# Args used to configure frontend build.
ARG COMBINE_CAPTCHA_REQUIRED
ARG COMBINE_CAPTCHA_SITE
#ARG COMBINE_CAPTCHA_REQUIRED
#ARG COMBINE_CAPTCHA_SITE

ENV NGINX_HOST_DIR /usr/share/nginx/html

COPY --from=builder /app/build ${NGINX_HOST_DIR}

# Configure frontend.
COPY nginx/generate_config.sh ./
RUN ./generate_config.sh > ${NGINX_HOST_DIR}/config.js \
&& rm generate_config.sh
#COPY nginx/generate_config.sh ./
#RUN ./generate_config.sh > ${NGINX_HOST_DIR}/config.js \
# && rm generate_config.sh

# Copy default self-signed certificate.
# Overwrite this with real certificate for authentication in production.
COPY nginx/certs /ssl

COPY nginx/nginx.conf /etc/nginx/conf.d/default.conf
160 changes: 92 additions & 68 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,22 +203,6 @@ environment variable and then run:

The exit code will be set to `0` on success and non-`0` otherwise.

#### Docker

Copy `.env.backend.auth.template` to `.env.backend.auth` add fill in the username and
password environment variables.

```batch
> docker-compose build --parallel
> docker-compose up --abort-on-container-exit
```

This will create the user and exit. If successful, the exit code will be `0`,
otherwise an error will be logged and the exit code will be non-`0`.

**Important**: Remove the `COMBINE_*` environment variables from
`.env.backend.auth` so that subsequent launches will start up the backend.

### (Development Only) Grant an Existing User Admin Rights

To grant an _existing_ user database administrator rights (all permissions for
Expand Down Expand Up @@ -247,93 +231,133 @@ To generate a full report of the licenses used in production:

### Requirements

#### Docker

Install [Docker](https://docs.docker.com/get-docker/).

(Linux Only) Install [Docker Compose](https://docs.docker.com/compose/install/)
separately. This is included by default in Docker Desktop for Windows and macOS.

### Build and Run
#### Python

For more information see the
[Docker Compose docs](https://docs.docker.com/compose/).
A Python script, `docker_setup.py` is used to configure the files needed to run
*TheCombine* in Docker containers.

Copy `.env.backend.template` to `.env.backend` and fill in the environment
variables.
##### Windows Only

```batch
> docker-compose build --parallel
> docker-compose up --detach
```
* Navigate to the [Python 3.8.5 Downloads](https://www.python.org/downloads/release/python-385/) page.

Browse to https://localhost.
* Download and run the appropriate installer - it is most likely the installer labeled
*Windows x86-64 executable installer*

> By default self-signed certificates are included, so you will need to accept
> a warning in the browser. See [SSL Certificates](#ssl-certificates) for
> production deployment.
* Once Python is installed, create an isolated Python
[virtual environment](https://docs.python.org/3/library/venv.html) using the
[`py`](https://docs.python.org/3/using/windows.html#getting-started) launcher
installed globally into the `PATH`.

To view logs:
```batch
> py -m venv venv
> venv\Scripts\activate
```

##### Linux Only

```batch
> docker-compose logs --follow
To install Python 3 on Ubuntu, run the following commands:

```bash
$ sudo apt update
$ sudo apt install python3 python3-venv
```

To stop and remove any stored data:
Create an isolated Python virtual environment

```batch
> docker-compose down --volumes
```bash
$ python3 -m venv venv
$ venv/bin/activate
```

### Configuration
##### Python Packages

#### SSL Certificates

To update SSL certificates after images have been built and are running,
find the `frontend` container name. By default this will be formatted as
`<lowercase_parent_dir>_frontend_1`.
With an active virtual environment, install `Jinja2`:

```batch
> docker-compose images
Container Repository Tag Image Id Size
------------------------------------------------------------------------------
thecombine_backend_1 thecombine_backend latest 73cf7b867c22 292.2 MB
thecombine_database_1 mongo 4.2 2b2cc1f48aed 387.8 MB
thecombine_frontend_1 thecombine_frontend latest 7cca1c1f1a5f 32.55 MB
(venv) > python -m pip install Jinja2
```

Copy new certificates from local filesystem into the container:
#### Configure Docker

Run the configuration script in an activated virtual environment.

```batch
> docker cp new_cert.pem thecombine_frontend_1:/ssl/cert.pem
> docker cp new_key.pem thecombine_frontend_1:/ssl/key.pem
(venv) > python docker_setup.py
```

Restart the Docker Compose project:
### Build and Run

```batch
> docker-compose down
> docker-compose up --detatch
```
For information on *Docker Compose* see the
[Docker Compose documentation](https://docs.docker.com/compose/).

#### Modifying Build Arguments
#### Step-by-step Instructions for Running *TheCombine* In Docker

Create a file `production.yml`, and override build arguments as needed.
1. Create the required docker files by running `docker_setup.py` from *TheCombine*'s project directory:
2. The `docker_setup.py` will generate a file, `.env.backend`, that defines
the environment variables needed by the Backend container. If you have defined
them as OS variables in the [Getting Started with Development](#getting-started-with-development) section above, then these variables will already be set. If not,
then you will need to edit `.env.backend` and provide values for the variables that
are listed.
3. Build the images for the Docker containers

```yaml
version: "3.8"
services:
frontend:
build:
args:
- COMBINE_CAPTCHA_REQUIRED=false
```
```batch
> docker build -t combine/frontend:latest --pull -f Dockerfile .
> cd Backend
> docker build -t combine/backend:latest --pull -f Dockerfile .
> cd ..
```
4. Run `docker-compose` to start the containers
```batch
> docker-compose up --detach
```
5. Browse to https://localhost.

*By default self-signed certificates are included, so you will need to accept a warning in the browser.*

6. To view logs:

```batch
> docker-compose logs --follow
```

7. To stop and remove any stored data:

Use this file when building and launching the Docker Compose project.
```batch
> docker-compose down --volumes
```

### Create a New Admin User (Docker Environment)

Edit `.env.backend` as follows:
* Fill in the environment variables.
* Add the following environment variables and assign values to them:
- COMBINE_ADMIN_USERNAME
- COMBINE_ADMIN_PASSWORD
* Set the file permissions so that only you have read or write access.

Run the following command to install the admin user in the *CombineDatabase*:

```batch
> docker-compose -f docker-compose.yml -f production.yml build --parallel
> docker-compose -f docker-compose.yml -f production.yml up --detach
> docker-compose up --abort-on-container-exit
```

This will create the user and exit. If successful, the exit code will be `0`,
otherwise an error will be logged and the exit code will be non-`0`.

**Important**: Remove the `COMBINE_ADMIN_*` environment variables from
`.env.backend` so that subsequent launches will start up the backend.

### Production

The process for configuring and deploying *TheCombine* for production targets is described in ./docs/docker_deploy/README.md

## Learn More

- [React](https://reactjs.org/)
Expand Down
4 changes: 0 additions & 4 deletions deploy/roles/ansible_depends/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
# This role installs all the packages that must be on the target
# machine in order to successfully use Ansible to manage the target.

- name: Install python2-pip
package:
name: python-pip
state: present
- name: Install python3
package:
name: python3
Expand Down
2 changes: 1 addition & 1 deletion deploy/roles/mongodb/tasks/install.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
repo: "deb [arch=amd64] https://repo.mongodb.org/apt/ubuntu {{ ansible_distribution_release }}/mongodb-org/{{ mongodb_version }} multiverse"
filename: mongodb-org-{{ mongodb_version }}
update_cache: yes
when: ansible_distribution == 'Ubuntu' and ansible_distribution_release == 'bionic'
when: ansible_distribution == 'Ubuntu'

- name: Install mongodb-org
apt:
Expand Down
Loading