Skip to content

Commit

Permalink
Add user-data script for installing service on a cloud provider VM
Browse files Browse the repository at this point in the history
The script will:
1. Install necessary dependencies
2. Create the DB file
3. Run the migrations
4. Initialize the DB with the seed data
5. Move the systemd unit file to the correct location
6. Enable the systemd service

The script will NOT:
- Create the `.env` file
- Start the server (the systemd service will start the server on reboot tho)
  • Loading branch information
szabolcs-horvath committed Jan 3, 2025
1 parent 456d28c commit d65cb63
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 4 deletions.
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ SQLITE_DB_FILE ?= sqlite/nutrition-tracker.db
IT_SQLITE_DB_FILE ?= it/nutrition-tracker-test.db
SQLITE_MIGRATIONS_DIR ?= sqlite/migrations
GOCOVERDIR ?= coverage
CGO_ENABLED=1 # Required for sqlite3 driver

SQLC_VERSION ?= v1.27.0
GOLANG_MIGRATE_VERSION ?= v4.18.1
Expand All @@ -12,9 +11,16 @@ HTMX_VERSION ?= 2.0.3
BOOTSTRAP_VERSION ?= 5.3.3
BOOTSTRAP_ICONS_VERSION ?= 1.11.3

install: install-go-deps init-db build
cp -u prod/nutrition-tracker.service /etc/systemd/system/nutrition-tracker.service
systemctl enable nutrition-tracker.service

start:
systemctl start nutrition-tracker.service

install-go-deps:
go install -v github.com/sqlc-dev/sqlc/cmd/sqlc@$(SQLC_VERSION)
go install -v -tags 'sqlite3' github.com/golang-migrate/migrate/v4/cmd/migrate@$(GOLANG_MIGRATE_VERSION)
CGO_ENABLED=1 go install -v -tags 'sqlite3' github.com/golang-migrate/migrate/v4/cmd/migrate@$(GOLANG_MIGRATE_VERSION)
go install -v golang.org/x/tools/cmd/stringer@$(STRINGER_VERSION)

init-db: migrate-up
Expand Down
19 changes: 19 additions & 0 deletions prod/nutrition-tracker.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[Unit]
Description=Nutrition Tracker server
ConditionFileNotEmpty=/root/nutrition-tracker/.env
ConditionPathExists=/root/nutrition-tracker/sqlite/nutrition-tracker.db
After=multi-user.target
Wants=multi-user.target
StartLimitIntervalSec=600
StartLimitBurst=5

[Service]
Type=simple
ExecStartPre=/bin/bash -c 'echo "$(date +"%%F-%%T") starting" >> /var/log/nutrition-tracker-starts.log'
ExecStart=/bin/bash -c '/root/nutrition-tracker/out/nutrition-tracker /root/nutrition-tracker/.env 2>&1 | tee /var/log/nutrition-tracker.log && exit ${PIPESTATUS[0]}'
ExecStartPost=/bin/bash -c 'echo "$(date +"%%F-%%T") started" >> /var/log/nutrition-tracker-starts.log'
Restart=on-failure
TimeoutSec=30

[Install]
WantedBy=multi-user.target
49 changes: 49 additions & 0 deletions prod/user-data.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/bash

# logging
exec >> /var/log/user-data.log
exec 2>&1

set -ex -o pipefail

export HOME=/root

install_go() {
curl -LO https://go.dev/dl/go1.23.1.linux-amd64.tar.gz
rm -rf /usr/local/go
tar -C /usr/local -xzf go1.23.1.linux-amd64.tar.gz
echo "export PATH=$PATH:/usr/local/go/bin:/root/go/bin" >> /etc/profile
echo "export GOPATH=/root/go" >> /etc/profile
source /etc/profile
go version
}

main() {
apt-get update

if ! which make || ! which gcc; then
apt-get install -y build-essential
which make
which gcc
fi

if ! which go; then
install_go
fi

if ! which sqlite3; then
apt-get install -y sqlite3
which sqlite3
fi

if [[ ! -d ~/nutrition-tracker ]]; then
cd ~
git clone https://github.com/szabolcs-horvath/nutrition-tracker.git
cd nutrition-tracker
make install
else
echo "Nutrition Tracker already installed."
fi
}

main "$@"
11 changes: 9 additions & 2 deletions repository/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,22 @@ import (
"github.com/szabolcs-horvath/nutrition-tracker/util"
"html/template"
"io"
"os"
"path/filepath"
"time"
)

var templates *template.Template

func init() {
exe, err := os.Executable()
if err != nil {
panic(err.Error())
}
pwd := filepath.Dir(exe)
templates = template.New("templates").Funcs(util.TemplateFuncs()).Funcs(TemplateFuncs())
template.Must(templates.ParseGlob("web/templates/*.gohtml"))
template.Must(templates.ParseGlob("web/templates/**/*.gohtml"))
template.Must(templates.ParseGlob(pwd + "/../web/templates/*.gohtml"))
template.Must(templates.ParseGlob(pwd + "/../web/templates/**/*.gohtml"))
}

func Render(w io.Writer, name string, data any) error {
Expand Down

0 comments on commit d65cb63

Please sign in to comment.