Skip to content

Commit

Permalink
FEATURE: build from dockerfiles
Browse files Browse the repository at this point in the history
list fakeserver as a debug profile

cache: migrate within the app. Retry a few more times

github: add gofmt, tests, and push on dev/main
  • Loading branch information
featheredtoast committed Nov 10, 2024
1 parent 03f2283 commit 34856cd
Show file tree
Hide file tree
Showing 15 changed files with 208 additions and 103 deletions.
59 changes: 59 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
on:
push:
pull_request:

env:
BUILDKIT_PROGRESS: plain

jobs:
lint:
runs-on: ubuntu-latest
timeout-minutes: 5
strategy:
fail-fast: true
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: ">=1.22.0"
- name: gofmt cache
working-directory: ./frmcache/src/app
run: |
if [ "$(gofmt -l . | wc -l)" -gt 0 ]; then
exit 1
fi
- name: gofmt alertmanager-config
working-directory: ./alertmanager-config/src/app
run: |
if [ "$(gofmt -l . | wc -l)" -gt 0 ]; then
exit 1
fi
test:
runs-on: ubuntu-latest
timeout-minutes: 5
strategy:
fail-fast: true
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: ">=1.22.0"
- name: Run tests
working-directory: ./frmcache/src/app
run: go test ./...

push:
if: (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/dev') && github.repository == 'featheredtoast/satisfactory-monitoring'
environment: docker deploy
runs-on: ubuntu-latest
needs: [lint, test]
env:
TAG: ${{ (github.ref == 'refs/heads/main' && 'latest') || (github.ref == 'refs/heads/dev' && 'dev') || '' }}
steps:
- uses: actions/checkout@v4
- name: build images
run: |
docker login --username featheredtoast --password ${{ secrets.DOCKER_PASSWORD }}
docker buildx build --push --load --tag featheredtoast/ficsit-remote-monitoring-cache:${{ env.TAG }} ./frmcache
docker buildx build --push --load --tag featheredtoast/ficsit-remote-monitoring-alertmanager-config:${{ env.TAG }} ./alertmanager-config
docker buildx build --push --load --tag featheredtoast/ficsit-remote-monitoring-companion:${{ env.TAG }} ./frmcompanion
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ If there are more hosts than webhooks, the remaining hosts do not have webhooks.
## Services and ports

- [grafana](http://localhost:3000): Time series graphing dashboard. Default username/password is `admin/admin`. This is the main front end for the app.
- [frmcompanion](http://localhost:9000/metrics): A webapp that converts JSON data from FRM into Prometheus metrics at `localhost:9000/metrics`. There is also a realtime map app. `localhost:8000?frmhost=localhost&frmport=8080`.
- [frmcompanion](http://localhost:9000/metrics): A webapp that converts JSON data from FRM into Prometheus metrics at `localhost:9000/metrics`.
- [prometheus](http://localhost:9090): Ingest metrics from the remote monitoring companion. Generates alert metrics for interesting anomalies.
- [alertmanager](http://localhost:9093): Forwards critical alerts to notification components.
- alertmanager-discord: Sends critical alerts to Discord.
Expand Down
13 changes: 13 additions & 0 deletions alertmanager-config/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM golang AS builder
WORKDIR /usr/src
COPY ./src/app .
ENV GOOS=linux GARCH=amd64 CGO_ENABLED=0
RUN /usr/local/go/bin/go build -o /usr/local/bin/alertmanager-config main.go

FROM scratch
ENV OUTPUT_PATH=/etc/alertmanager/alertmanager.yml \
INPUT_PATH=/var/lib/alertmanager-config/templates/alertmanager.yml.tmpl

COPY --from=builder /usr/local/bin/alertmanager-config /usr/local/bin/alertmanager-config
COPY ./src/templates/alertmanager.yml.tmpl /var/lib/alertmanager-config/templates/alertmanager.yml.tmpl
ENTRYPOINT ["/usr/local/bin/alertmanager-config"]
5 changes: 0 additions & 5 deletions alertmanager-config/init.sh

This file was deleted.

2 changes: 1 addition & 1 deletion alertmanager-config/src/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

type HostWebhookPair struct {
Host string
Host string
Webhook string
}

Expand Down
22 changes: 5 additions & 17 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
services:
fakeserver:
profiles:
- debug
image: nginx
volumes:
- ./fakeserver/metrics:/usr/share/nginx/html/metrics
Expand All @@ -8,18 +10,13 @@ services:
ports:
- "8082:8080"
frmcompanion:
image: golang
working_dir: /usr/src
entrypoint: /usr/local/bin/init.sh
volumes:
- ./frmcompanion/init.sh:/usr/local/bin/init.sh
build: ./frmcompanion
environment:
- FRM_HOST=${FRM_HOST:-host.docker.internal}
- FRM_PORT=${FRM_PORT:-8080}
- FRM_HOSTS=${FRM_HOSTS:-}
ports:
- "9000:9000"
- "8000:8000"
prometheus:
image: prom/prometheus
volumes:
Expand All @@ -28,12 +25,7 @@ services:
- "9090:9090"

frmcache:
image: golang
working_dir: /usr/src/app
entrypoint: /usr/local/bin/init.sh
volumes:
- ./frmcache/init.sh:/usr/local/bin/init.sh
- ./frmcache/src:/usr/src
build: ./frmcache
environment:
- FRM_HOST=${FRM_HOST:-host.docker.internal}
- FRM_PORT=${FRM_PORT:-8080}
Expand All @@ -51,13 +43,9 @@ services:
- POSTGRES_PASSWORD=${PG_PASSWORD:-secretpassword}

alertmanager-config:
image: golang
working_dir: /usr/src/app
entrypoint: /usr/local/bin/init.sh
build: ./alertmanager-config
volumes:
- ./alertmanager:/etc/alertmanager
- ./alertmanager-config/init.sh:/usr/local/bin/init.sh
- ./alertmanager-config/src:/usr/src
environment:
- FRM_HOST=${FRM_HOST:-host.docker.internal}
- FRM_PORT=${FRM_PORT:-8080}
Expand Down
10 changes: 10 additions & 0 deletions frmcache/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM golang AS builder
WORKDIR /usr/src/
COPY ./src/app .
ENV GOOS=linux GARCH=amd64 CGO_ENABLED=0
RUN /usr/local/go/bin/go build -o /usr/local/bin/frmcache ./...

FROM scratch
COPY --from=builder /usr/local/bin/frmcache /usr/local/bin/frmcache
COPY ./src/db/ /var/lib/frmcache/
ENTRYPOINT ["/usr/local/bin/frmcache"]
12 changes: 0 additions & 12 deletions frmcache/init.sh

This file was deleted.

13 changes: 7 additions & 6 deletions frmcache/src/app/cache_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"context"
"database/sql"
"encoding/json"
"fmt"
"log"
"net/http"
"regexp"
"time"

"fmt"
"github.com/benbjohnson/clock"
"time"
)

var Clock = clock.New()
Expand Down Expand Up @@ -158,7 +159,7 @@ func (c *CacheWorker) flushMetricHistory() error {
delete := `DELETE from cache_with_history c WHERE c.url = $1 and c.session_name = $2;`
_, err := c.db.Exec(delete, c.frmBaseUrl, c.sessionName)
if err != nil {
fmt.Println("flush metrics history db error: ", err)
log.Println("flush metrics history db error: ", err)
}
return err
}
Expand All @@ -183,7 +184,7 @@ func (c *CacheWorker) pullMetrics(metric string, route string, keepHistory bool)

func (c *CacheWorker) pullMetricsLog(metric string, route string, keepHistory bool) error {
if err := c.pullMetrics(metric, route, keepHistory); err != nil {
fmt.Println("Error when pulling metrics ", metric, ": ", err)
log.Println("Error when pulling metrics ", metric, ": ", err)
return err
}
return nil
Expand All @@ -210,11 +211,11 @@ func (c *CacheWorker) pullSessionName() {
sessionInfo := SessionInfo{}
err := retrieveSessionInfo(c.frmBaseUrl+"/getSessionInfo", &sessionInfo)
if err != nil {
fmt.Printf("error reading session name from FRM: %s\n", err)
log.Printf("error reading session name from FRM: %s\n", err)
}
newSessionName := sanitizeSessionName(sessionInfo.SessionName)
if newSessionName != "" && newSessionName != c.sessionName {
fmt.Println(c.frmBaseUrl + " has a new session name: " + newSessionName)
log.Println(c.frmBaseUrl + " has a new session name: " + newSessionName)
c.sessionName = newSessionName
c.flushMetricHistory()
}
Expand Down
26 changes: 23 additions & 3 deletions frmcache/src/app/go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,30 @@
module github.com/featheredtoast/frmcache

go 1.18
go 1.21

require github.com/lib/pq v1.10.7
toolchain go1.23.0

require (
github.com/benbjohnson/clock v1.3.0
github.com/jackc/tern/v2 v2.2.3
github.com/lib/pq v1.10.7
)

require (
dario.cat/mergo v1.0.1 // indirect
github.com/DATA-DOG/go-sqlmock v1.5.0 // indirect
github.com/benbjohnson/clock v1.3.0 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.3.0 // indirect
github.com/Masterminds/sprig/v3 v3.3.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/huandu/xstrings v1.5.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirect
github.com/jackc/pgx/v5 v5.5.5 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/shopspring/decimal v1.4.0 // indirect
github.com/spf13/cast v1.7.0 // indirect
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/text v0.18.0 // indirect
)
40 changes: 40 additions & 0 deletions frmcache/src/app/go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,46 @@
dario.cat/mergo v1.0.1 h1:Ra4+bf83h2ztPIQYNP99R6m+Y7KfnARDfID+a+vLl4s=
dario.cat/mergo v1.0.1/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60=
github.com/DATA-DOG/go-sqlmock v1.5.0/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI=
github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
github.com/Masterminds/semver/v3 v3.3.0 h1:B8LGeaivUe71a5qox1ICM/JLl0NqZSW5CHyL+hmvYS0=
github.com/Masterminds/semver/v3 v3.3.0/go.mod h1:4V+yj/TJE1HU9XfppCwVMZq3I84lprf4nC11bSS5beM=
github.com/Masterminds/sprig/v3 v3.3.0 h1:mQh0Yrg1XPo6vjYXgtf5OtijNAKJRNcTdOOGZe3tPhs=
github.com/Masterminds/sprig/v3 v3.3.0/go.mod h1:Zy1iXRYNqNLUolqCpL4uhk6SHUMAOSCzdgBfDb35Lz0=
github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A=
github.com/benbjohnson/clock v1.3.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/huandu/xstrings v1.5.0 h1:2ag3IFq9ZDANvthTwTiqSSZLjDc+BedvHPAp5tJy2TI=
github.com/huandu/xstrings v1.5.0/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 h1:L0QtFUgDarD7Fpv9jeVMgy/+Ec0mtnmYuImjTz6dtDA=
github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.5.5 h1:amBjrZVmksIdNjxGW/IiIMzxMKZFelXbUoPNb+8sjQw=
github.com/jackc/pgx/v5 v5.5.5/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A=
github.com/jackc/tern/v2 v2.2.3 h1:UWD24+m3zP7eRSlX9vYg2tb6Bf0V161IdOuo4YWWyd4=
github.com/jackc/tern/v2 v2.2.3/go.mod h1:EStqJVUowhII9OpCTcZISE1BfpGlwE4oq0oQtHAGuuI=
github.com/lib/pq v1.10.7 h1:p7ZhMD+KsSRozJr34udlUrhboJwWAgCg34+/ZZNvZZw=
github.com/lib/pq v1.10.7/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw=
github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s=
github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ=
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k=
github.com/shopspring/decimal v1.4.0/go.mod h1:gawqmDU56v4yIKSwfBSFip1HdCCXN8/+DMd9qYNcwME=
github.com/spf13/cast v1.7.0 h1:ntdiHjuueXFgm5nzDRdOS4yfT43P5Fnud6DH50rz/7w=
github.com/spf13/cast v1.7.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A=
golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70=
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
41 changes: 38 additions & 3 deletions frmcache/src/app/main.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package main

import (
"context"
"database/sql"
"fmt"
_ "github.com/lib/pq"
"log"
"os"
"os/signal"
"runtime"
"strconv"
"strings"
"syscall"
"time"

"github.com/jackc/pgx/v5"
"github.com/jackc/tern/v2/migrate"
_ "github.com/lib/pq"
)

func lookupEnvWithDefault(variable string, defaultVal string) string {
Expand All @@ -33,14 +39,43 @@ func main() {
pgPassword := lookupEnvWithDefault("PG_PASSWORD", "secretpassword")
pgUser := lookupEnvWithDefault("PG_USER", "postgres")
pgDb := lookupEnvWithDefault("PG_DB", "postgres")
migrationLocation := lookupEnvWithDefault("MIGRATION_DIR", "/var/lib/frmcache")

psqlconn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", pgHost, pgPort, pgUser, pgPassword, pgDb)
db, err := sql.Open("postgres", psqlconn)
CheckError(err)
defer db.Close()
err = db.Ping()
retries := 5
for i := 0; i < retries; i++ {
err = db.Ping()
if err == nil {
break
}
log.Printf("connecting to database...")
time.Sleep(2 * time.Second)
}
CheckError(err)

var m *migrate.Migrator
migrateConn, err := pgx.Connect(context.Background(), psqlconn)
if err != nil {
log.Printf("Unable to establish connection: %v", err)
return
}
m, err = migrate.NewMigrator(context.Background(), migrateConn, "schema_version")
if err != nil {
log.Printf("Unable to create migrator: %v", err)
return
}
m.LoadMigrations(os.DirFS(migrationLocation))
m.OnStart = func(_ int32, name, direction, _ string) {
log.Printf("Migrating %s: %s", direction, name)
}
if err = m.Migrate(context.Background()); err != nil {
log.Printf("Unexpected failure migrating: %v", err)
return
}

cacheWorkers := []*CacheWorker{}
if frmHostnames == "" {
cacheWorkers = append(cacheWorkers, NewCacheWorker("http://"+frmHostname+":"+frmPort, db))
Expand All @@ -56,7 +91,7 @@ func main() {
go cacheWorker.Start()
}

fmt.Printf(`
log.Printf(`
FRM Cache started
Press ctrl-c to exit`)

Expand Down
Loading

0 comments on commit 34856cd

Please sign in to comment.