diff --git a/Makefile b/Makefile index 89cda9b..6a9c6be 100644 --- a/Makefile +++ b/Makefile @@ -13,110 +13,35 @@ down: docker compose down generate-certs: - cd docker-deploy && \ - docker compose down && \ - docker compose up -d nginx-certbot && \ - docker compose run --rm certbot certonly --webroot --webroot-path /var/www/certbot/ -d $(DOMAIN_URL) + bash ./docker-deploy/scripts/generate-certs.sh renew-certs: cd docker-deploy && \ docker compose run --rm certbot renew -set-auto-renewing-certs: - sudo apt-get update - sudo apt-get install cron - echo "----------------------------------------------------------------------------------------------" && \ - echo "Now you must add this string-command in your crontab to auto executing it each 1 month. Command:" && \ - echo "" && \ - echo "0 0 1 * * cd $$(pwd) && make run renew-certs >> ./certbot-renew.log" && \ - echo "" && \ - echo "Copy that command and [press Enter]. Then add this string in end of opened file." && \ - read ENTER - crontab -e +setup-auto-renewing-certs: + bash ./docker-deploy/scripts/setup-auto-renewing-certs.sh update: - git fetch --all - git reset --hard origin/$$(sed -n "s/^DEPLOY_BRANCH=//p" "docker-deploy/.env") - git log --oneline -1 - cd docker-deploy && \ - docker compose up -d nginx --build - echo "Frontend updated successfully" + bash ./docker-deploy/scripts/update-deploy.sh setup-ci: - #id -u github &>/dev/null || sudo adduser github # Add user if not exists - #sudo chown -R github:legend . - #sudo -u github ssh-keygen - #sudo cat /home/github/.ssh/id_rsa.pub >> /home/github/.ssh/authorized_keys - #sudo chmod 777 -R /home/github/.ssh - #sudo chmod 666 /home/github/.ssh/authorized_keys - #sudo -u github ssh-keygen - #echo "" && \ -# echo 'Add this politics for github user: [press Enter]' && \ -# echo 'AllowUsers github' && \ -# echo 'Match User github' && \ -# echo 'PasswordAuthentication no' && \ -# echo 'AllowTCPForwarding no' && \ -# echo 'X11Forwarding no' && \ -# echo '[press Enter...]' && \ -# read ENTER - #nano /etc/ssh/sshd_config - #echo "" && \ -# echo 'Add this public rsa key to Deploy keys in your github directory: ' && \ -# sudo cat /home/github/.ssh/id_rsa.pub - ssh-keygen -f /tmp/tmp_key - sudo mkdir -p ~/.ssh - sudo cat /tmp/tmp_key.pub >> ~/.ssh/authorized_keys - echo '' && \ - echo 'Add this private rsa key secret deploy environment variables to SSH_DEPLOY_KEY on your github repo: ' && \ - echo '[To see key press Enter...]' && \ - read ENTER - sudo less /tmp/tmp_key + bash ./docker-deploy/scripts/setup-ci.sh -install-docker-if-not-exists: # fully copied from https://docs.docker.com/engine/install/ubuntu/ - if ! command -v docker; then \ - sudo apt-get update; \ - sudo apt-get install -y --no-install-recommended ca-certificates curl; \ - sudo install -m 0755 -d /etc/apt/keyrings; \ - sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc; \ - sudo chmod a+r /etc/apt/keyrings/docker.asc; \ - # Add the repository to Apt sources: \ - echo \ - "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \ - $(. /etc/os-release && echo "$${VERSION_CODENAME}") stable" | \ - sudo tee /etc/apt/sources.list.d/docker.list > /dev/null; \ - sudo apt-get update; \ - sudo apt-get install -y --no-install-recommended docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin; \ - make set-docker-not-sudo; \ - else \ - echo "Docker already installed!"; \ - fi +install-docker-if-not-exists: + bash ./docker-deploy/scripts/install-docker-if-not-exists.sh set-docker-not-sudo: - # add user to docker group - getent group docker || sudo groupadd docker # Add group if not exists - sudo usermod -aG docker ${USER} - newgrp docker - sudo systemctl restart docker + bash ./docker-deploy/scripts/set-docker-not-sudo.sh setup-env-file: - cp --no-clobber ./docker-deploy/.env.example ./docker-deploy/.env - echo '' && \ - echo 'Edit .env file. Write right DOMAIN_URL without https:// and url paths!' && \ - echo '[press Enter...]' && \ - read ENTER - nano ./docker-deploy/.env + bash ./docker-deploy/scripts/setup-env-file.sh all: make install-docker-if-not-exists make setup-env-file make generate-certs - make set-auto-renewing-certs + make setup-auto-renewing-certs make down make setup-ci sudo chmod ugo+rwx -R ~/docker-deploy/certbot/ make update - echo 'Now read README.md and setup another deploy environments variables.' && \ - echo 'It seems like values must be:' && \ - echo 'SERVER_USERNAME=$$(whoami) - [! MUST BE SECRET VARIABLE !]' && \ - echo 'DEPLOY_HOST=$$(cat ./docker-deploy/.env | grep DOMAIN_URL | sed "s/.*=//") - [NOT SECURE]' && \ - echo 'PROJECT_PATH=$$(pwd) - [NOT SECURE]' && \ - echo '[press Enter to finish setup]' - read ENTER + bash ./docker-deploy/scripts/show-variables-to-github-ci.sh diff --git a/docker-deploy/scripts/generate-certs.sh b/docker-deploy/scripts/generate-certs.sh new file mode 100644 index 0000000..ed4364b --- /dev/null +++ b/docker-deploy/scripts/generate-certs.sh @@ -0,0 +1,4 @@ +cd docker-deploy || exit +docker compose down +docker compose up -d nginx-certbot +docker compose run --rm certbot certonly --webroot --webroot-path /var/www/certbot/ -d "$(DOMAIN_URL)" diff --git a/docker-deploy/scripts/install-docker-if-not-exists.sh b/docker-deploy/scripts/install-docker-if-not-exists.sh new file mode 100644 index 0000000..9abb167 --- /dev/null +++ b/docker-deploy/scripts/install-docker-if-not-exists.sh @@ -0,0 +1,17 @@ +# fully copied from https://docs.docker.com/engine/install/ubuntu/ +if ! command -v docker; then + sudo apt-get update; + sudo apt-get install -y --no-install-recommended ca-certificates curl; + sudo install -m 0755 -d /etc/apt/keyrings; + sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc; + sudo chmod a+r /etc/apt/keyrings/docker.asc; + # Add the repository to Apt sources: + echo \ + "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo "$${VERSION_CODENAME}") stable" | \ + sudo tee /etc/apt/sources.list.d/docker.list > /dev/null; + sudo apt-get update; + sudo apt-get install -y --no-install-recommended docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin; + make set-docker-not-sudo; +else + echo "Docker already installed!"; +fi diff --git a/docker-deploy/scripts/set-docker-not-sudo.sh b/docker-deploy/scripts/set-docker-not-sudo.sh new file mode 100644 index 0000000..392c3ad --- /dev/null +++ b/docker-deploy/scripts/set-docker-not-sudo.sh @@ -0,0 +1,5 @@ +# add user to docker group +getent group docker || sudo groupadd docker # Add group if not exists +sudo usermod -aG docker "${USER}" +newgrp docker +sudo systemctl restart docker diff --git a/docker-deploy/scripts/setup-auto-renewing-certs.sh b/docker-deploy/scripts/setup-auto-renewing-certs.sh new file mode 100644 index 0000000..4ddbd59 --- /dev/null +++ b/docker-deploy/scripts/setup-auto-renewing-certs.sh @@ -0,0 +1,10 @@ +sudo apt-get update +sudo apt-get install cron +echo "----------------------------------------------------------------------------------------------" +echo "Now you must add this string-command in your crontab to auto executing it each 1 month. Command:" +echo "" +echo "0 0 1 * * cd $(pwd) && make run renew-certs >> ./certbot-renew.log" +echo "" +echo "Copy that command and [press Enter]. Then add this string in end of opened file." +read ENTER +crontab -e diff --git a/docker-deploy/scripts/setup-ci.sh b/docker-deploy/scripts/setup-ci.sh new file mode 100644 index 0000000..1274f3d --- /dev/null +++ b/docker-deploy/scripts/setup-ci.sh @@ -0,0 +1,28 @@ +#id -u github &>/dev/null || sudo adduser github # Add user if not exists +#sudo chown -R github:legend . +#sudo -u github ssh-keygen +#sudo cat /home/github/.ssh/id_rsa.pub >> /home/github/.ssh/authorized_keys +#sudo chmod 777 -R /home/github/.ssh +#sudo chmod 666 /home/github/.ssh/authorized_keys +#sudo -u github ssh-keygen +#echo "" && \ +#echo 'Add this politics for github user: [press Enter]' && \ +#echo 'AllowUsers github' && \ +#echo 'Match User github' && \ +#echo 'PasswordAuthentication no' && \ +#echo 'AllowTCPForwarding no' && \ +#echo 'X11Forwarding no' && \ +#echo '[press Enter...]' && \ +#read ENTER +#nano /etc/ssh/sshd_config +#echo "" && \ +#echo 'Add this public rsa key to Deploy keys in your github directory: ' && \ +#sudo cat /home/github/.ssh/id_rsa.pub +ssh-keygen -f /tmp/tmp_key +sudo mkdir -p ~/.ssh +sudo cat /tmp/tmp_key.pub | sudo tee -a ~/.ssh/authorized_keys > /dev/null +echo '' +echo 'Add this private rsa key secret deploy environment variables to SSH_DEPLOY_KEY on your github repo: ' +echo '[To see key press Enter...]' +read ENTER +sudo less /tmp/tmp_key diff --git a/docker-deploy/scripts/setup-env-file.sh b/docker-deploy/scripts/setup-env-file.sh new file mode 100644 index 0000000..ecb2f1a --- /dev/null +++ b/docker-deploy/scripts/setup-env-file.sh @@ -0,0 +1,6 @@ +cp --no-clobber ./docker-deploy/.env.example ./docker-deploy/.env +echo "" +echo "Edit .env file. Write right DOMAIN_URL without https:// and url paths!" +echo "[press Enter...]" +read ENTER +nano ./docker-deploy/.env diff --git a/docker-deploy/scripts/show-variables-to-github-ci.sh b/docker-deploy/scripts/show-variables-to-github-ci.sh new file mode 100644 index 0000000..c3622fd --- /dev/null +++ b/docker-deploy/scripts/show-variables-to-github-ci.sh @@ -0,0 +1,9 @@ +echo "Now read README.md and setup another deploy environments variables." +echo "It seems like values must be:" +echo "" +echo "SERVER_USERNAME=$(whoami) [! MUST BE SECRET VARIABLE !]" +echo "DEPLOY_HOST=$(< ./docker-deploy/.env grep DOMAIN_URL | sed 's/.*=//')" +echo "PROJECT_PATH=$(pwd)" +echo "" +echo "[press Enter to finish setup]" +read ENTER diff --git a/docker-deploy/scripts/update-deploy.sh b/docker-deploy/scripts/update-deploy.sh new file mode 100644 index 0000000..6ed753d --- /dev/null +++ b/docker-deploy/scripts/update-deploy.sh @@ -0,0 +1,7 @@ +git fetch --all +git reset --hard "origin/$(sed -n "s/^DEPLOY_BRANCH=//p" "docker-deploy/.env")" +echo "Deploying last commit:" +git log --oneline -1 +cd docker-deploy || exit +docker compose up -d nginx --build +echo "Frontend updated successfully"