From cfda16b64bd711fc71fb672981e63233ed74f4f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Sch=C3=A4fer?= Date: Tue, 25 Jun 2024 17:21:49 +0200 Subject: [PATCH] build(other): test deployment locally Context ------- There was a similar PR #1263 attempting to do the same with docker. I was running into isolation issues with docker (setting hostname not allowed in the container and some system folders are read-only) and learned that for this use case, an actual hypervisor would be better and vagrant is a conventional tool to set up these virtual machines. Motivation ---------- On the weekend we had a downtime on production because `nginx` configurations got out of sync with the code in newer releases. To prevent this and further downtimes I suggest to check in our deployment configuration and make it *reproducible*. This PR is an attempt to create a virtual machine that behaves as similar as possible to production and can be used as a sandbox for changes to our deployment configuration. How to test ----------- 1. Install [Vagrant](https://www.vagrantup.com/) on your machine 2. `cd deployment/local-testing` 3. `vagrant up` .. and wait 4. Following services are running: * http://localhost:8080/ * http://localhost:8080/api * http://localhost:8080/docs 5. Be amazed --- deployment/local-testing/.gitignore | 1 + deployment/local-testing/Vagrantfile | 14 ++++++++++ deployment/local-testing/bootstrap.sh | 37 +++++++++++++++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 deployment/local-testing/.gitignore create mode 100644 deployment/local-testing/Vagrantfile create mode 100644 deployment/local-testing/bootstrap.sh diff --git a/deployment/local-testing/.gitignore b/deployment/local-testing/.gitignore new file mode 100644 index 0000000000..8000dd9db4 --- /dev/null +++ b/deployment/local-testing/.gitignore @@ -0,0 +1 @@ +.vagrant diff --git a/deployment/local-testing/Vagrantfile b/deployment/local-testing/Vagrantfile new file mode 100644 index 0000000000..83ac6aadd4 --- /dev/null +++ b/deployment/local-testing/Vagrantfile @@ -0,0 +1,14 @@ +# Defines our Vagrant environment +# +# -*- mode: ruby -*- +# vi: set ft=ruby : + +image = "debian/buster64" + +Vagrant.configure("2") do |config| + config.vm.box = 'generic/alpine319' + config.vm.provision :shell, path: "bootstrap.sh" + config.vm.network "forwarded_port", guest: 8080, host: 3000 + config.vm.network "forwarded_port", guest: 80, host: 3001 + config.vm.network "forwarded_port", guest: 8082, host: 3082 +end diff --git a/deployment/local-testing/bootstrap.sh b/deployment/local-testing/bootstrap.sh new file mode 100644 index 0000000000..14a9ffb169 --- /dev/null +++ b/deployment/local-testing/bootstrap.sh @@ -0,0 +1,37 @@ +#!/bin/sh + +apk update +apk upgrade +apk add nginx openrc nodejs npm git mysql mysql-client + + +npm install pm2 -g +pm2 startup + +rc-update add pm2 boot + +service mariadb setup +rc-update add mariadb boot +sed -e '/skip-networking/ s/^#*/#/' -i /etc/my.cnf.d/mariadb-server.cnf +service mariadb start + + +cd /var/www/localhost/htdocs/ +git clone https://github.com/dreammall-earth/dreammall.earth.git +cd dreammall.earth + +mkdir -p /etc/nginx/http.d +cp -f ./deployment/nginx/default.conf /etc/nginx/http.d/default.conf +cp ./deployment/nginx/frontend.conf /etc/nginx/http.d/frontend.conf +cp ./deployment/nginx/admin.conf /etc/nginx/http.d/admin.conf + +service nginx restart + +mysql -e "CREATE USER 'dreammall'@'localhost' IDENTIFIED BY 'SECRET'; GRANT ALL PRIVILEGES ON * . * TO 'dreammall'@'localhost'; FLUSH PRIVILEGES;" + +cp backend/.env.dist backend/.env +cp presenter/.env.dist presenter/.env +cp frontend/.env.dist frontend/.env +cp admin/.env.dist admin/.env + +deployment/deploy.sh