Skip to content

Commit

Permalink
build(other): custom vagrant box with packer
Browse files Browse the repository at this point in the history
refactor: upload current git file-tree into vm

So we can actually use this to test the current deployment

refactor: set up `pnpm`

Motivation
----------
`pnpm` itself has a number of advantages, two are very relevant for our repository:
* De-duplication of node modules
* Run commands across packages

This will also setup `dotenvx` to read .env files (for unique port numbers).

It will also make local development with `docker compose` obsolete for our nodejs projects.

How to test
-----------
1. `pnpm install`
2. `pnpm run -r dev`

commits:
  * fix unique ports and environments
  * fix: extraneous import
  * remove obsolete `package-lock.json`
  * run `pnpm install`
  * run `corepack use pnpm@latest`
  • Loading branch information
roschaefer committed Jul 2, 2024
1 parent e90affd commit ded95f3
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 67 deletions.
20 changes: 0 additions & 20 deletions deployment/local-testing/README.md

This file was deleted.

40 changes: 0 additions & 40 deletions deployment/local-testing/bootstrap.sh

This file was deleted.

3 changes: 3 additions & 0 deletions infrastructure/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
packer-output/
archive/**/*
!archive/.gitkeep
35 changes: 35 additions & 0 deletions infrastructure/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# 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 added infrastructure/archive/.gitkeep
Empty file.
36 changes: 36 additions & 0 deletions infrastructure/bootstrap.sh
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
69 changes: 69 additions & 0 deletions infrastructure/vagrant.pkr.hcl
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.
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
# Defines our Vagrant environment
#
# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
config.vm.box = 'generic/alpine319'
config.vm.provision :shell, path: "bootstrap.sh"
config.vm.box = "packer_dreammall"
config.vm.box_url = "file://../packer-output/package.box"
config.vm.synced_folder '.', '/vagrant', disabled: true
config.vm.network "forwarded_port", guest: 80, host: 8000
config.vm.network "forwarded_port", guest: 8080, host: 8080
Expand Down

0 comments on commit ded95f3

Please sign in to comment.