Skip to content

Commit

Permalink
Sort out all deployment & images
Browse files Browse the repository at this point in the history
  • Loading branch information
mhadam committed Nov 4, 2023
1 parent cdadc09 commit 652f7ec
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 51 deletions.
26 changes: 3 additions & 23 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,26 +56,6 @@ jobs:
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}

copy-repo:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run a one-line script
run: echo Hello from Octo Organization

- name: rsync deployment
# You may pin to the exact commit or the version.
# uses: Burnett01/rsync-deployments@45d84ad5f6c174f3e0ffc50e9060a9666d09c16e
uses: Burnett01/[email protected]
with:
switches: -avzr --delete
# path: src/
remote_path: ${{ secrets.DEPLOY_PATH }}
remote_host: ${{ secrets.DEPLOY_HOST }}
remote_port: ${{ secrets.DEPLOY_PORT }}
remote_user: ${{ secrets.DEPLOY_USER }}
remote_key: ${{ secrets.DEPLOY_KEY }}

deploy:
runs-on: ubuntu-latest
steps:
Expand All @@ -84,9 +64,9 @@ jobs:
env:
NAME: "Root"
with:
hosts: '[email protected] [email protected]:2222'
privateKey: ${{ secrets.PRIVATE_KEY }}
hosts: '[email protected]'
privateKey: ${{ secrets.CI_PRIVATE_KEY }}
debug: false
command: |
ls -lah
/usr/bin/systemctl start deploy-princetonpy.service
echo "I am $NAME"
11 changes: 7 additions & 4 deletions app/Dockerfile-cron.prod
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ RUN apt-get install -y netcat
# create directory for the app user
RUN mkdir -p /home/app

ENV UID="${UID}:-1001"
ENV GID="${GID}:-1001"

# create the app user
RUN groupadd -r app && useradd --no-log-init -r -g app app
RUN groupadd -r -g $GID app && useradd app --no-log-init -r -u $UID -g $GID

# create the appropriate directories
ENV HOME=/home/app
Expand All @@ -63,12 +66,12 @@ COPY . $APP_HOME

# chown all the files to the app user
RUN mkdir -p ./static
RUN chown -R app:app $APP_HOME
RUN chown -R $UID:$GID $APP_HOME
RUN mkdir -p /prod_backup
RUN chown -R app:app /prod_backup
RUN chown -R $UID:$GID /prod_backup

# change to the app user
USER app
USER $UID:$GID

# run entrypoint.prod.sh
ENTRYPOINT ["/home/app/web/entrypoint-cron.prod.sh"]
8 changes: 5 additions & 3 deletions app/Dockerfile.prod
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ RUN apt-get install -y netcat
# create directory for the app user
RUN mkdir -p /home/app

ENV UID="${UID}:-1001"
ENV GID="${GID}:-1001"
# create the app user
RUN groupadd -r -g 1001 app && useradd app --no-log-init -r -u 1001 -g 1001
RUN groupadd -r -g $GID app && useradd app --no-log-init -r -u $UID -g $GID

# create the appropriate directories
ENV HOME=/home/app
Expand All @@ -67,10 +69,10 @@ COPY . $APP_HOME

# chown all the files to the app user
RUN mkdir -p ./static
RUN chown -R 1001:1001 $APP_HOME
RUN chown -R $UID:$GID $APP_HOME

# change to the app user
USER 1001:1001
USER $UID:$GID

# run entrypoint.prod.sh
ENTRYPOINT ["/home/app/web/entrypoint.prod.sh"]
6 changes: 6 additions & 0 deletions app/entrypoint-cron.prod.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
#!/bin/sh

if [ -n "$UID" ] && [ -n "$GID" ]; then
usermod -u "$UID" app
groupmod -g "$GID" app
chown -R "$UID":"$GID" "$HOME"
fi

exec "$@"
6 changes: 6 additions & 0 deletions app/entrypoint.prod.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,10 @@ python manage.py collectstatic -c --noinput
python manage.py makemigrations
python manage.py migrate

if [ -n "$UID" ] && [ -n "$GID" ]; then
usermod -u "$UID" app
groupmod -g "$GID" app
chown -R "$UID":"$GID" "$HOME"
fi

exec "$@"
3 changes: 3 additions & 0 deletions devops/install_scripts.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

install -v -m 550 -o princeton -g princeton ./scripts/pull-image.sh /usr/local/sbin/
6 changes: 2 additions & 4 deletions devops/install_systemd_units.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
cp ./systemd/*.{service,path} /etc/systemd/system/

systemctl enable deploy-caddy.{path,service}
systemctl enable deploy-project.{path,service}
systemctl enable project.path
systemctl enable deploy-princetonpy.{path,service}
systemctl enable princetonpy-cron.service
systemctl enable princetonpy-web.service
systemctl start deploy-caddy.{path,service}
systemctl start deploy-project.{path,service}
systemctl start project.path
systemctl start deploy-princetonpy.{path,service}
systemctl start princetonpy-cron.service
systemctl start princetonpy-web.service
4 changes: 4 additions & 0 deletions devops/scripts/pull-image.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash

</home/princeton/ghtoken docker login ghcr.io --username mhadam --password-stdin
/usr/bin/docker pull ghcr.io/pugip/princetonpy.org:release
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ Requires=docker.service
[Service]
TimeoutStartSec=120
Type=oneshot
ExecStartPre=/usr/bin/docker compose \
-f /home/princetonpy/repos/princetonpy/docker-compose.prod.yml \
build web cron
ExecStartPre=/usr/local/sbin/pull-image.sh
ExecStart=/usr/bin/systemctl restart princetonpy-cron.service
ExecStart=/usr/bin/systemctl restart princetonpy-web.service

Expand Down
9 changes: 0 additions & 9 deletions devops/systemd/project.path

This file was deleted.

3 changes: 1 addition & 2 deletions devops/uninstall_systemd_units.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#!/usr/bin/env bash

systemctl disable deploy-caddy.{path,service}
systemctl disable deploy-project.{path,service}
systemctl disable project.path
systemctl disable deploy-princetonpy.{path,service}
systemctl disable princetonpy-cron.service
systemctl disable princetonpy-web.service
# rm ./systemd/deploy-caddy.{path,service}
10 changes: 7 additions & 3 deletions docker-compose.prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ services:
env_file:
- .env.prod.db
web:
build:
context: ./app
dockerfile: Dockerfile.prod
image: ghcr.io/pugip/princetonpy.org:release
command: hypercorn princetonpy.asgi:application --bind 0.0.0.0:8000
volumes:
- princetonpy_prod_backup:/prod_backup
expose:
- 8000
environment:
- UID=1001
- GID=1001
env_file:
- .env.prod.web
- .env.prod.aws
Expand All @@ -29,6 +30,9 @@ services:
volumes:
- $PWD/app:/home/app/web
- princetonpy_prod_backup:/prod_backup
environment:
- UID=1001
- GID=1001
env_file:
- .env.prod.web
- .env.prod.aws
Expand Down

0 comments on commit 652f7ec

Please sign in to comment.