Skip to content

Commit

Permalink
Вынес все команды из makefile в папку scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
SergTyapkin committed Apr 27, 2024
1 parent 7f2209f commit 02b7801
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 86 deletions.
97 changes: 11 additions & 86 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions docker-deploy/scripts/generate-certs.sh
Original file line number Diff line number Diff line change
@@ -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)"
17 changes: 17 additions & 0 deletions docker-deploy/scripts/install-docker-if-not-exists.sh
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions docker-deploy/scripts/set-docker-not-sudo.sh
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions docker-deploy/scripts/setup-auto-renewing-certs.sh
Original file line number Diff line number Diff line change
@@ -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
28 changes: 28 additions & 0 deletions docker-deploy/scripts/setup-ci.sh
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions docker-deploy/scripts/setup-env-file.sh
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions docker-deploy/scripts/show-variables-to-github-ci.sh
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions docker-deploy/scripts/update-deploy.sh
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 02b7801

Please sign in to comment.