Skip to content

Commit

Permalink
Add support for MySQL
Browse files Browse the repository at this point in the history
  • Loading branch information
smallwat3r committed Nov 18, 2023
1 parent da17b36 commit 062eb34
Show file tree
Hide file tree
Showing 15 changed files with 204 additions and 108 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt -r dev-requirements.txt
pip install -r requirements.txt -r requirements.dev.txt
- name: Linting checks
run: |
# stop the build if there are any Python syntax errors
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
*.rdb

environments/
!environments/docker.dev
!environments/dev-docker-postgres.env
!environments/dev-docker-mysql.env

.coverage
tags
Expand Down
24 changes: 18 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,31 @@ help: ## Show this help menu
@echo "Usage: make [TARGET ...]"
@echo ""
@grep --no-filename -E '^[a-zA-Z_%-]+:.*?## .*$$' $(MAKEFILE_LIST) | \
awk 'BEGIN {FS = ":.*?## "}; {printf "%-10s %s\n", $$1, $$2}'
awk 'BEGIN {FS = ":.*?## "}; {printf "%-25s %s\n", $$1, $$2}'

.PHONY: dc-start
dc-start: dc-stop ## Start dev docker server
@docker compose -f docker-compose.yml up --build --scale adminer=0 -d;
@docker compose -f docker-compose-postgres.yml up --build --scale adminer=0 -d;

.PHONY: dc-start-adminer
dc-start-adminer: dc-stop ## Start dev docker server (with adminer)
@docker compose -f docker-compose.yml up --build -d;
@docker compose -f docker-compose-postgres.yml up --build -d;

.PHONY: dc-stop
dc-stop: ## Stop dev docker server
@docker compose -f docker-compose.yml stop;
@docker compose -f docker-compose-postgres.yml stop;

.PHONY: dc-start-mysql
dc-start-mysql: dc-stop ## Start dev docker server using MySQL
@docker compose -f docker-compose-mysql.yml up --build --scale adminer=0 -d;

.PHONY: dc-start-adminer-mysql
dc-start-adminer-mysql: dc-stop ## Start dev docker server using MySQL (with adminer)
@docker compose -f docker-compose-mysql.yml up --build -d;

.PHONY: dc-stop-mysql
dc-stop-mysql: ## Stop dev docker server using MySQL
@docker compose -f docker-compose-mysql.yml stop;

VENV = venv
VENV_PYTHON = $(VENV)/bin/python
Expand All @@ -36,7 +48,7 @@ venv: $(VENV_PYTHON) ## Create a Python virtual environment
.PHONY: deps
deps: ## Install Python requirements in virtual environment
$(PYTHON) -m pip install --upgrade pip
$(PYTHON) -m pip install --no-cache-dir -r requirements.txt -r dev-requirements.txt
$(PYTHON) -m pip install --no-cache-dir -r requirements.txt -r requirements.dev.txt

.PHONY: checks
checks: tests ruff mypy bandit ## Run all checks (unit tests, ruff, mypy, bandit)
Expand Down Expand Up @@ -80,5 +92,5 @@ logs: ## Follow Flask logs
docker logs shhh-app-1 -f -n 10

.PHONY: db-logs
db-logs: ## Follow Postgre logs
db-logs: ## Follow database logs
docker logs shhh-db-1 -f -n 10
35 changes: 26 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ You can find in this repo everything you need to host the app yourself.
Or you can **one-click deploy to Heroku** using the below button.
It will generate a fully configured private instance of Shhh
immediately (using your own server running Flask behind Gunicorn and Nginx,
and your own Postgres database).
and your own PostgreSQL database).

[![Deploy][heroku-shield]][heroku] (see [here](#initiate-the-database-tables) to
initiate the db tables after deploying on Heroku)
Expand Down Expand Up @@ -83,9 +83,16 @@ Once the container image has finished building and has started, you
can access:

* Shhh at <http://localhost:8081>
* Adminer at <http://localhost:8082/?pgsql=db&username=shhh&db=shhh> (if launched with `dc-start-adminer`)
* Adminer at <http://localhost:8082> (if launched with `dc-start-adminer`)

_You can find the development database credentials from the env file at [/environments/docker.dev](https://github.com/smallwat3r/shhh/blob/master/environments/docker.dev)._
_You can find the development database credentials from the env file at [/environments/dev-docker-postgres.env](https://github.com/smallwat3r/shhh/blob/master/environments/dev-docker-postgres.env)._

You have also the option to use `MySQL` instead of `PostgreSQL`, using these commands:
```sh
make dc-start-mysql # to start the app
make dc-start-adminer-mysql # to start the app with adminer (SQL editor)
make dc-stop-mysql # to stop the app
```

#### Initiate the database tables

Expand Down Expand Up @@ -158,10 +165,24 @@ Bellow is the list of environment variables used by Shhh.

#### Mandatory
* `FLASK_ENV`: the environment config to load (`testing`, `dev-local`, `dev-docker`, `heroku`, `production`).
* `POSTGRES_HOST`: Postgresql hostname
* `DB_HOST`: Database hostname
* `DB_USER`: Database username
* `DB_PASSWORD`: Database password
* `DB_NAME`: Database name
* `DB_ENGINE`: Database engine to use (ex: `postgresql+psycopg2`, `mysql+pymysql`)

Depending if you can use PostgreSQL or MySQL you might also need to set (these need to match the values
you've specified as `DB_NAME`, `DB_PASSWORD` and `DB_NAME` above):

* `POSTGRES_USER`: Postgresql username
* `POSTGRES_PASSWORD`: Postgresql password
* `POSTGRES_DB`: Database name
* `POSTGRES_DB`: Postgresql database name

or

* `MYSQLUSER`: MySQL username
* `MYSQL_PASSWORD`: MySQL password
* `MYSQL_DATABASE`: MySQL database name

#### Optional
* `SHHH_HOST`: This variable can be used to specify a custom hostname to use as the
Expand All @@ -175,10 +196,6 @@ asleep (for instance this happens often on Heroku free plans). The default retry
* `SHHH_DB_LIVENESS_SLEEP_INTERVAL`: This variable manages the interval in seconds between the database
liveness retries. The default value is 1 second.

## Acknowledgements

Special thanks: [@AustinTSchaffer](https://github.com/AustinTSchaffer), [@kleinfelter](https://github.com/kleinfelter)

## License

See [LICENSE](https://github.com/smallwat3r/shhh/blob/master/LICENSE) file.
Expand Down
29 changes: 29 additions & 0 deletions docker-compose-mysql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
version: '3.2'
services:
db:
image: mysql:8.2
env_file:
- ./environments/dev-docker-mysql.env
ports:
- 3306:3306
app:
build:
context: .
dockerfile: dockerfiles/alpine.dev.mysql.Dockerfile
image: shhh
depends_on:
- db
env_file:
- ./environments/dev-docker-mysql.env
ports:
- 8081:8081
volumes:
- .:/opt/shhh:delegated
adminer:
image: adminer
depends_on:
- db
ports:
- 8082:8080
volumes:
mysql:
29 changes: 29 additions & 0 deletions docker-compose-postgres.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
version: '3.2'
services:
db:
image: postgres:15.4-alpine3.18
env_file:
- ./environments/dev-docker-postgres.env
ports:
- 5432:5432
app:
build:
context: .
dockerfile: dockerfiles/alpine.dev.Dockerfile
image: shhh
depends_on:
- db
env_file:
- ./environments/dev-docker-postgres.env
ports:
- 8081:8081
volumes:
- .:/opt/shhh:delegated
adminer:
image: adminer
depends_on:
- db
ports:
- 8082:8080
volumes:
postgres:
40 changes: 0 additions & 40 deletions docker-compose.yml

This file was deleted.

4 changes: 2 additions & 2 deletions alpine.dev.Dockerfile → dockerfiles/alpine.dev.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ ENV TZ UTC

WORKDIR /opt/shhh

ENV GROUP=app USER=shhh UID=12345 GID=23456
ARG GROUP=app USER=shhh UID=1001 GID=1001

RUN addgroup --gid "$GID" "$GROUP" \
&& adduser --uid "$UID" --disabled-password --gecos "" \
--ingroup "$GROUP" "$USER"

USER "$USER"
USER $USER
ENV PATH="/home/$USER/.local/bin:${PATH}"

ENV CRYPTOGRAPHY_DONT_BUILD_RUST=1
Expand Down
51 changes: 51 additions & 0 deletions dockerfiles/alpine.dev.mysql.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# This dockerfile runs the application with the bare Flask
# server. As it's for development only purposes.
#
# When using Gunicorn in a more prod-like config, multiple
# workers would require to use the --preload option, else
# the scheduler would spawn multiple scheduler instances.
#
# Note it would not be comptatible with Gunicorn --reload
# flag, which is useful to reload the app on change, for
# development purposes.
#
# Example: CMD gunicorn -b :8081 -w 3 wsgi:app --preload
#
# To use Gunicorn, please use: alpine.gunicorn.Dockerfile

FROM python:3.12-alpine3.18

RUN apk update \
&& apk add --no-cache \
gcc \
g++ \
libffi-dev \
musl-dev \
mariadb-dev \
yarn \
&& python -m pip install --upgrade pip

ENV TZ UTC

WORKDIR /opt/shhh

ARG GROUP=app USER=shhh UID=1001 GID=1001

RUN addgroup --gid "$GID" "$GROUP" \
&& adduser --uid "$UID" --disabled-password --gecos "" \
--ingroup "$GROUP" "$USER"

USER $USER
ENV PATH="/home/$USER/.local/bin:${PATH}"

ENV CRYPTOGRAPHY_DONT_BUILD_RUST=1

COPY requirements.mysql.txt .
RUN pip install --no-cache-dir --no-warn-script-location \
--user -r requirements.mysql.txt

COPY --chown=$USER:$GROUP . .

RUN yarn install --modules-folder=shhh/static/vendor

CMD flask run --host=0.0.0.0 --port 8081 --reload
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ ENV TZ UTC

WORKDIR /opt/shhh

ENV GROUP=app USER=shhh UID=12345 GID=23456
ARG GROUP=app USER=shhh UID=1001 GID=1001

RUN addgroup --gid "$GID" "$GROUP" \
&& adduser --uid "$UID" --disabled-password --gecos "" \
--ingroup "$GROUP" "$USER"

USER "$USER"
USER $USER
ENV PATH="/home/$USER/.local/bin:${PATH}"

ENV CRYPTOGRAPHY_DONT_BUILD_RUST=1
Expand Down
35 changes: 0 additions & 35 deletions environments/docker.dev

This file was deleted.

2 changes: 0 additions & 2 deletions postgres/init.sql

This file was deleted.

File renamed without changes.
34 changes: 34 additions & 0 deletions requirements.mysql.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
APScheduler==3.10.4
blinker==1.7.0
certifi==2023.7.22
cffi==1.16.0
charset-normalizer==3.3.2
click==8.1.7
cryptography==41.0.5
cssmin==0.2.0
Flask==3.0.0
Flask-APScheduler==1.13.1
Flask-Assets==2.1.0
Flask-SQLAlchemy==3.1.1
gunicorn==21.2.0
htmlmin==0.1.12
idna==3.4
itsdangerous==2.1.2
Jinja2==3.1.2
jsmin==3.0.1
MarkupSafe==2.1.3
marshmallow==3.20.1
packaging==23.2
pycparser==2.21
PyMySQL==1.1.0
python-dateutil==2.8.2
pytz==2023.3.post1
requests==2.31.0
six==1.16.0
SQLAlchemy==2.0.23
typing_extensions==4.8.0
tzlocal==5.2
urllib3==2.1.0
webargs==8.3.0
webassets==2.0
Werkzeug==3.0.1
Loading

0 comments on commit 062eb34

Please sign in to comment.