-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build(other): custom vagrant box with packer
Motivation ---------- This PR solves two problems: 1. A custom vagrant box is built for better reproducibility (no more changes during runtime) 1. The code in the vagrant box needs the *local code* so we can actually test out local changes, not the ones pushed to Github. How to test ----------- 1. Follow infrastructure/README.md
- Loading branch information
1 parent
43c4db3
commit 455706c
Showing
9 changed files
with
141 additions
and
67 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
packer-output/ | ||
archive/**/* | ||
!archive/.gitkeep |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Infrastructure | ||
|
||
This folder contains our infrastructure as code. | ||
|
||
The [benefits](https://www.redhat.com/en/topics/automation/what-is-infrastructure-as-code-iac#benefits-of-iac) of this paradigm are: | ||
- Cost reduction | ||
- Increase in speed of deployments | ||
- Reduce errors | ||
- Improve infrastructure consistency | ||
- Eliminate configuration drift | ||
|
||
## Local setup | ||
|
||
The following will set up virtual machines locally so you can test out if | ||
a change breaks the deployment. | ||
|
||
1. Install [packer](https://www.packer.io/) on your machine | ||
1. `cd infrastructure/` | ||
1. `packer init ./vagrant.pkr.hcl` | ||
|
||
### (Re)build Vagrant box | ||
|
||
1. Install [Vagrant](https://www.vagrantup.com/) and [VirtualBox](https://www.virtualbox.org/) on your machine | ||
1. `packer build --force --on-error=ask ./vagrant.pkr.hcl` | ||
1. `cd vagrant && vagrant up` | ||
1. Following services are running: | ||
- <http://localhost:8000/> | ||
- <http://localhost:8000/api> | ||
- <http://localhost:8000/docs> | ||
- <http://localhost:8080> | ||
- <http://localhost:8082> |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -euxo pipefail | ||
|
||
apk update | ||
apk upgrade | ||
apk add nginx openrc nodejs npm git mysql mysql-client | ||
|
||
npm install -g pnpm pm2 | ||
pm2 startup | ||
|
||
rc-update add pm2 boot | ||
rc-update add nginx boot | ||
service nginx start | ||
|
||
service mariadb setup | ||
rc-update add mariadb boot | ||
sed -e '/skip-networking/ s/^#*/#/' -i /etc/my.cnf.d/mariadb-server.cnf | ||
service mariadb start | ||
|
||
|
||
PROJECT_ROOT=/var/www/localhost/htdocs/dreammall.earth | ||
|
||
mkdir -p /etc/nginx/http.d | ||
cp -f $PROJECT_ROOT/deployment/nginx/default.conf /etc/nginx/http.d/default.conf | ||
cp $PROJECT_ROOT/deployment/nginx/frontend.conf /etc/nginx/http.d/frontend.conf | ||
cp $PROJECT_ROOT/deployment/nginx/admin.conf /etc/nginx/http.d/admin.conf | ||
|
||
mysql -e "CREATE USER 'dreammall'@'localhost' IDENTIFIED BY 'SECRET'; GRANT ALL PRIVILEGES ON * . * TO 'dreammall'@'localhost'; FLUSH PRIVILEGES;" | ||
|
||
cp $PROJECT_ROOT/backend/.env.dist $PROJECT_ROOT/backend/.env | ||
cp $PROJECT_ROOT/presenter/.env.dist $PROJECT_ROOT/presenter/.env | ||
cp $PROJECT_ROOT/frontend/.env.dist $PROJECT_ROOT/frontend/.env | ||
cp $PROJECT_ROOT/admin/.env.dist $PROJECT_ROOT/admin/.env | ||
|
||
$PROJECT_ROOT/deployment/deploy.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
packer { | ||
required_plugins { | ||
vagrant = { | ||
version = "~> 1" | ||
source = "github.com/hashicorp/vagrant" | ||
} | ||
} | ||
} | ||
|
||
source "vagrant" "dreammall" { | ||
communicator = "ssh" | ||
source_path = "generic/alpine319" | ||
provider = "virtualbox" | ||
add_force = true | ||
|
||
output_dir = "packer-output" | ||
} | ||
|
||
build { | ||
sources = ["sources.vagrant.dreammall"] | ||
|
||
|
||
provisioner "shell-local" { | ||
inline = [ | ||
"rm -rf archive", | ||
"git clone ../ archive", | ||
"cd archive", | ||
"git remote set-url origin https://github.com/dreammall-earth/dreammall.earth.git", # only necessary because of the `git pull -ff` in `deploy.sh` | ||
"git checkout master", | ||
"git pull -ff", | ||
"touch .gitkeep", | ||
] | ||
} | ||
|
||
provisioner "file" { | ||
source = "archive" | ||
destination = "/tmp" | ||
} | ||
|
||
provisioner "shell" { | ||
execute_command = "echo 'packer' | sudo -S sh -c '{{ .Vars }} {{ .Path }}'" | ||
inline = [ | ||
"mkdir -p /var/www/localhost/htdocs/", | ||
"mv /tmp/archive /var/www/localhost/htdocs/dreammall.earth", | ||
] | ||
} | ||
|
||
provisioner "shell" { | ||
execute_command = "echo 'packer' | sudo -S sh -c '{{ .Vars }} {{ .Path }}'" | ||
script = "./bootstrap.sh" | ||
} | ||
|
||
provisioner "shell-local" { | ||
inline = [ | ||
"rm -rf archive/", | ||
"mkdir -p archive/", | ||
"touch archive/.gitkeep", | ||
] | ||
} | ||
|
||
error-cleanup-provisioner "shell-local" { | ||
inline = [ | ||
"rm -rf archive/", | ||
"mkdir -p archive/", | ||
"touch archive/.gitkeep", | ||
] | ||
} | ||
} | ||
|
File renamed without changes.
9 changes: 2 additions & 7 deletions
9
deployment/local-testing/Vagrantfile → infrastructure/vagrant/Vagrantfile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters