Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
AmirMohammadFakhimi committed Dec 20, 2023
2 parents e6ea0b1 + 7fb35f1 commit 3396491
Show file tree
Hide file tree
Showing 11 changed files with 459 additions and 0 deletions.
54 changes: 54 additions & 0 deletions .github/scripts/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/bin/bash

set -eo pipefail

SERVICE_NAME=
VERSION=
DOCKER_USER=
DOCKER_PASS=

function main() {
inflate_options "$@"

# Set all versions to the given version, so that any of them if built is tagged by that version.
export WEB_IMAGE_VERSION=$VERSION
export FRONT_IMAGE_VERSION=$VERSION
export STATIC_DATA_PROVIDER_IMAGE_VERSION=$VERSION

echo $DOCKER_PASS | docker login -u $DOCKER_USER --password-stdin
docker-compose build $SERVICE_NAME
docker-compose push $SERVICE_NAME
}

function inflate_options() {
while [ -n "${1+x}" ]; do
case "$1" in
--service-name)
shift
SERVICE_NAME="$1"
shift
;;
--version)
shift
VERSION="$1"
shift
;;
--docker-user)
shift
DOCKER_USER="$1"
shift
;;
--docker-pass)
shift
DOCKER_PASS="$1"
shift
;;
*)
echo "Unknown option: $1"
exit 2
;;
esac
done
}

main "$@"
77 changes: 77 additions & 0 deletions .github/scripts/release-image.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/bin/bash

set -euo pipefail

SERVICE_NAME=
SRC_TAG=
RELEASE_TAG=
DOCKER_USER=
DOCKER_PASS=

FULL_SRC_TAG=
FULL_RELEASE_TAG=
FULL_LATEST_TAG=

function main() {
inflate_options "$@"
check_options

echo $DOCKER_PASS | docker login -u $DOCKER_USER --password-stdin

echo "Releasing image '$FULL_SRC_TAG' as '$FULL_RELEASE_TAG'"

docker pull $FULL_SRC_TAG
docker tag $FULL_SRC_TAG $FULL_RELEASE_TAG
docker tag $FULL_SRC_TAG $FULL_LATEST_TAG
docker push $FULL_RELEASE_TAG
docker push $FULL_LATEST_TAG
}

function inflate_options() {
while [ -n "${1+x}" ]; do
case "$1" in
--service-name)
shift
SERVICE_NAME="$1"
shift
;;
--src-tag)
shift
SRC_TAG="$1"
shift
;;
--release-tag)
shift
RELEASE_TAG="$1"
shift
;;
--docker-user)
shift
DOCKER_USER="$1"
shift
;;
--docker-pass)
shift
DOCKER_PASS="$1"
shift
;;
*)
echo "Unknown option: $1"
exit 2
;;
esac
done

FULL_SRC_TAG="$DOCKER_USER/$SERVICE_NAME:$SRC_TAG"
FULL_RELEASE_TAG="$DOCKER_USER/$SERVICE_NAME:$RELEASE_TAG"
FULL_LATEST_TAG="$DOCKER_USER/$SERVICE_NAME:latest"
}

function check_options() {
if [ -z ${SERVICE_NAME+x} ] || [ -z ${SRC_TAG} ] || [ -z ${RELEASE_TAG} ] || [ -z ${DOCKER_USER} ] || [ -z ${DOCKER_PASS} ]; then
echo "ERROR: All of inputs should be specified: --service-name --src-tag --release-tag --docker-user --docker-pass"
exit 3
fi
}

main "$@"
98 changes: 98 additions & 0 deletions .github/scripts/smoke-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#!/bin/bash

set -o pipefail

HOST=
TAG=
DOCKER_USER=
DOCKER_PASS=
CHECK_LOGS=false
CHECK_CERT=true

function main() {
inflate_options "$@"
check_options
if [ -n "${TAG+x}" ]; then
run_local_server_by_tag
HOST=https://localhost
CHECK_LOGS=true
CHECK_CERT=false
echo "Waiting for server to be up ..."
sleep 20
fi
smoke_test_host
}

function inflate_options(){
while [ -n "$1" ]; do
case "$1" in
--host)
shift
HOST="$1"
shift
;;
--tag)
shift
TAG="$1"
shift
;;
--docker-user)
shift
DOCKER_USER="$1"
shift
;;
--docker-pass)
shift
DOCKER_PASS="$1"
shift
;;
*)
echo "Unknown option: $1"
exit 2
esac
done
}

function check_options() {
if [ -n "${TAG+x}" ] && [ -n "${HOST+x}" ]; then
echo "Exactly one of '--tag' or '--host' should be specified."
exit 3
fi

if [ -n "${TAG+x}" ] && ( [ -z ${DOCKER_USER+x} ] || [ -z ${DOCKER_PASS+x} ] ); then
echo "Specifying '--docker-user' and '--docker-pass' is mandatory when using '--tag'."
exit 3
fi
}

function run_local_server_by_tag() {
export WEB_IMAGE_VERSION=$TAG
echo $DOCKER_PASSWORD | docker login -u $DOCKER_USERNAME --password-stdin
docker-compose pull
docker-compose up -d --no-build
}

function smoke_test_host() {
check_cert_flag=
if ! $CHECK_CERT; then
check_cert_flag='--no-check-certificate'
fi
wget_output=$(wget --server-response $check_cert_flag "$HOST" 2>&1)
status_code=$(echo "$wget_output" | awk '/^ HTTP/{print $2}')

if [[ "$status_code" == "200" ]]; then
echo "Test Succeeded!"
exit 0
else
echo "Test Failed!"
echo "Request to the application failed"
echo "Status code: $status_code"
echo "wget output:"
echo "$wget_output"
if $CHECK_LOGS; then
echo "Services logs:"
docker-compose logs
fi
exit 1
fi
}
29 changes: 29 additions & 0 deletions .github/workflows/backend.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

name: deploy backend


on:
workflow_dispatch:
push:
branches:
- old
paths:
- 'backend/**'


jobs:
deploy-back:
runs-on: ubuntu-20.04
steps:
- name: Deploy
uses: appleboy/[email protected]
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USER }}
port: ${{ secrets.PORT }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
script: |
cd ${{ secrets.TARGET_PATH_BACKEND }}
git checkout old
git pull origin old
docker-compose up --build -d
48 changes: 48 additions & 0 deletions .github/workflows/build_infra_as_image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
name: "Build Infra as an Image"
env:
DOCKER_USERNAME: sharifwss
on:
workflow_dispatch:
jobs:
setup-infra:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
- name: Force GitHub Actions' docker daemon to use vfs.
run: |
sudo systemctl stop docker
echo '{"cgroup-parent":"/actions_job","storage-driver":"vfs"}' | sudo tee /etc/docker/daemon.json
sudo systemctl start docker
- run: echo "TARGET_HOST=192.168.2.2" >> $GITHUB_ENV
- run: docker network create --subnet 192.168.2.0/24 wss-net
- run: docker run -d
--network wss-net
--ip $TARGET_HOST
--privileged
--name infra_container
rastasheep/ubuntu-sshd:18.04
- name: Run setup_infra.yml
run: docker run
--network wss-net
-v `pwd`:/app-src/:ro
--workdir /app-src/deploy/
--env TARGET_HOST=$TARGET_HOST
spy86/ansible:latest
bash -c '
DNS_SERVER=$(cat /etc/resolv.conf | grep -m 1 nameserver | cut -d" " -f2) &&
ansible-playbook
-i inventory.yml
--extra-vars="
shecan_dns_1=$DNS_SERVER
shecan_dns_2=$DNS_SERVER
ansible_host=$TARGET_HOST
ansible_user=root
ansible_ssh_pass=root
docker_hub_password=${{ secrets.DOCKER_PASSWORD }}"
setup_infra.yml
'
- run: docker stop infra_container
- run: docker commit infra_container $DOCKER_USERNAME/infra-image:latest
- run: echo ${{ secrets.DOCKER_PASSWORD }} | docker login -u $DOCKER_USERNAME --password-stdin
- run: docker push $DOCKER_USERNAME/infra-image:latest
17 changes: 17 additions & 0 deletions .github/workflows/create_super_user.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
name: "Create Super User"
env:
TARGET_HOST: sharif-wss.ir
HOST_SSH_USER: wss
WSS_ADMIN_USER: wss-admin
on:
workflow_dispatch:
jobs:
create-super-user:
runs-on: ubuntu-20.04
container:
image: spy86/ansible:latest
steps:
- run: sshpass -p ${{ secrets.HOST_SSH_PASSWORD }} ssh -o StrictHostKeyChecking=no $HOST_SSH_USER@$TARGET_HOST "
cd wss/ && docker-compose exec -T web bash -c \"echo from django.contrib.auth import get_user_model\; User = get_user_model\(\)\; User.objects.create_superuser\(\'$WSS_ADMIN_USER\', \'[email protected]\', \'${{ secrets.WSS_ADMIN_PASS }}\'\) | python manage.py shell\"
"
22 changes: 22 additions & 0 deletions .github/workflows/db_backup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# ---
# name: "Backup Database"
# env:
# TARGET_HOST: sharif-wss.ir
# HOST_SSH_USER: wss
# on:
# schedule:
# - cron: "30 23 * * *" # every night 23:30 UTC = 03:00 ASIA/TEHRAN
# push:
# branches:
# - master
# workflow_dispatch:
# jobs:
# backup:
# runs-on: ubuntu-20.04
# container:
# image: spy86/ansible:latest
# steps:
# - name: Backup database to WSS Dropbox
# run: sshpass -p ${{ secrets.HOST_SSH_PASSWORD }} ssh -o StrictHostKeyChecking=no $HOST_SSH_USER@$TARGET_HOST "
# cd wss/ && docker-compose exec -T web bash -c 'python ./manage.py dbbackup --clean'
# "
17 changes: 17 additions & 0 deletions .github/workflows/db_restore.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
name: "Restore Database Last Backup"
env:
TARGET_HOST: sharif-wss.ir
HOST_SSH_USER: wss
on:
workflow_dispatch:
jobs:
restore:
runs-on: ubuntu-20.04
container:
image: spy86/ansible:latest
steps:
- name: Restore database from WSS Dropbox (latest file)
run: sshpass -p ${{ secrets.HOST_SSH_PASSWORD }} ssh -o StrictHostKeyChecking=no $HOST_SSH_USER@$TARGET_HOST "
cd wss/ && docker-compose exec -T web bash -c 'python ./manage.py dbrestore --noinput'
"
Loading

0 comments on commit 3396491

Please sign in to comment.