Skip to content

Commit

Permalink
chore: reusable workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
junghao committed Jun 26, 2024
1 parent df8b85f commit 9309a29
Show file tree
Hide file tree
Showing 29 changed files with 830 additions and 93 deletions.
79 changes: 29 additions & 50 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,35 @@ jobs:
- name: check output
run: |
jq . <<< '${{ steps.set.outputs.matrix }}'
build-app:
uses: GeoNet/Actions/.github/workflows/reusable-go-apps.yml@main
with:
buildSetup: |
sudo apt-get -yq update
sudo apt-get install -y xsltproc
testSetup: |
sudo apt-get -yq update
sudo apt-get install -y xsltproc
docker \
run -d \
-p 5432:5432 \
-e POSTGRES_PASSWORD=test \
-e POSTGRES_USER=fdsn_w \
-e POSTGRES_DB=fdsn \
--name postgres \
docker.io/postgis/postgis:15-3.3-alpine
echo "Waiting until Postgres is ready..."
until nc -zv -w 1 127.0.0.1 5432; do
sleep 1s
done
sleep 5s
docker logs postgres
echo "Postgres is ready"
psql postgresql://fdsn_w:[email protected]/fdsn --file=./etc/ddl/drop-create.ddl
psql postgresql://fdsn_w:[email protected]/fdsn --file=./etc/ddl/create-users.ddl
goTestExtraArgs: -p 1
build:
needs: prepare
needs: [prepare, build-app]
strategy:
matrix: ${{ fromJSON(needs.prepare.outputs.matrix) }}
uses: GeoNet/Actions/.github/workflows/reusable-docker-build.yml@main
Expand Down Expand Up @@ -66,52 +93,4 @@ jobs:
aws-region: ap-southeast-2
aws-role-arn-to-assume: arn:aws:iam::862640294325:role/github-actions-geonet-ecr-push
aws-role-duration-seconds: "3600"
go-build:
if: ${{ contains(fromJSON('["workflow_call", "push", "pull_request"]'), github.event_name) && startsWith(github.repository, 'GeoNet/') != false }}
uses: GeoNet/Actions/.github/workflows/reusable-go-build-smoke-test.yml@main
with:
paths: ${{ inputs.paths }}
gofmt:
if: ${{ contains(fromJSON('["workflow_call", "push", "pull_request"]'), github.event_name) && startsWith(github.repository, 'GeoNet/') != false }}
uses: GeoNet/Actions/.github/workflows/reusable-gofmt.yml@main
golangci-lint:
if: ${{ contains(fromJSON('["workflow_call", "push", "pull_request"]'), github.event_name) && startsWith(github.repository, 'GeoNet/') != false }}
uses: GeoNet/Actions/.github/workflows/reusable-golangci-lint.yml@main
go-vet:
if: ${{ contains(fromJSON('["workflow_call", "push", "pull_request"]'), github.event_name) && startsWith(github.repository, 'GeoNet/') != false }}
uses: GeoNet/Actions/.github/workflows/reusable-go-vet.yml@main
go-test:
runs-on: ubuntu-latest
env:
AWS_REGION: ap-southeast-2
steps:
- uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3
- uses: actions/setup-go@fac708d6674e30b6ba41289acaab6d4b75aa0753 # v4.0.1
with:
go-version-file: go.mod
cache-dependency-path: go.sum
check-latest: true
- name: setup
run: |
sudo apt-get -yq update
sudo apt-get install -y xsltproc
docker \
run -d \
-p 5432:5432 \
-e POSTGRES_PASSWORD=test \
-e POSTGRES_USER=fdsn_w \
-e POSTGRES_DB=fdsn \
--name postgres \
docker.io/postgis/postgis:15-3.3-alpine
echo "Waiting until Postgres is ready..."
until nc -zv -w 1 127.0.0.1 5432; do
sleep 1s
done
sleep 5s
docker logs postgres
echo "Postgres is ready"
psql postgresql://fdsn_w:[email protected]/fdsn --file=./etc/ddl/drop-create.ddl
psql postgresql://fdsn_w:[email protected]/fdsn --file=./etc/ddl/create-users.ddl
- name: test
run: |
./all.sh

3 changes: 2 additions & 1 deletion cmd/fdsn-holdings-consumer/data_holdings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package main

import (
"database/sql"
"github.com/GeoNet/fdsn/internal/holdings"
"testing"
"time"

"github.com/GeoNet/fdsn/internal/holdings"
)

func TestSaveHoldings(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion cmd/fdsn-holdings-consumer/log.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package main

import (
"github.com/GeoNet/kit/metrics"
"log"
"os"

"github.com/GeoNet/kit/metrics"
)

var Prefix string
Expand Down
3 changes: 2 additions & 1 deletion cmd/fdsn-quake-consumer/log.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package main

import (
"github.com/GeoNet/kit/metrics"
"log"
"os"

"github.com/GeoNet/kit/metrics"
)

var Prefix string
Expand Down
3 changes: 2 additions & 1 deletion cmd/fdsn-ws/data_holdings.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package main

import (
"database/sql"
"github.com/GeoNet/fdsn/internal/fdsn"
"time"

"github.com/GeoNet/fdsn/internal/fdsn"
)

type metric struct {
Expand Down
7 changes: 4 additions & 3 deletions cmd/fdsn-ws/data_holdings_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package main

import (
"github.com/GeoNet/fdsn/internal/fdsn"
"github.com/GeoNet/fdsn/internal/holdings"
"github.com/lib/pq"
"log"
"testing"
"time"

"github.com/GeoNet/fdsn/internal/fdsn"
"github.com/GeoNet/fdsn/internal/holdings"
"github.com/lib/pq"
)

// http://www.postgresql.org/docs/9.4/static/errcodes-appendix.html
Expand Down
2 changes: 1 addition & 1 deletion cmd/fdsn-ws/fdsn_dataselect.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type dataSelect struct {
keys []string
}

func init() {
func initDataselectTemplate() {
// Handle comma separated parameters (eg: net, sta, loc, cha, etc)
decoder.RegisterConverter([]string{}, func(input string) reflect.Value {
return reflect.ValueOf(strings.Split(input, ","))
Expand Down
2 changes: 1 addition & 1 deletion cmd/fdsn-ws/fdsn_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ var validEventTypes = strings.Split(
", ", ","),
",") // remove spaces after comma

func init() {
func initEventTemplate() {
var err error
var b bytes.Buffer

Expand Down
2 changes: 1 addition & 1 deletion cmd/fdsn-ws/fdsn_station.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ var (
s3Meta string
)

func init() {
func initStationTemplate() {
var err error
var b bytes.Buffer

Expand Down
27 changes: 14 additions & 13 deletions cmd/fdsn-ws/fdsn_station_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ import (
// NOTE: To run the test, please export :
// STATION_XML_META_KEY=fdsn-station-test.xml

func init() {
}

func TestStationFilter(t *testing.T) {
var e fdsnStationV1Search
var err error
Expand Down Expand Up @@ -207,13 +204,16 @@ Testdata timeline
Station 1:
2007-05-20T23 2011-03-06T22 2011-06-20T04
|------3 cha----|
|-----3 cha----|
|-----3 cha----------->
|------3 cha----|
|-----3 cha----|
|-----3 cha----------->
Station 2:
2010-03-11T21 2012-01-19T22
|-----3 cha----|
|-----3 cha------->
2010-03-11T21 2012-01-19T22
|-----3 cha----|
|-----3 cha------->
*/
func TestStartEnd(t *testing.T) {
var e fdsnStationV1Search
Expand Down Expand Up @@ -408,10 +408,11 @@ NZ|ARAZ|-38.627690|176.120060|420.000000|Aratiatia Landcorp Farm|2007-05-20T23:0
}

// To profiling, you'll have to use full fdsn-station xml as data source:
// 1. Put full fdsn-station.xml in etc/.
// 2. export FDSN_STATION_XML_META_KEY=fdsn-station.xml
// 3. Run `go test -bench=StationQuery -benchmem -run=^$`.
// Note: You must specify -run=^$ to skip test functions since you're not using test fdsn-station xml.
// 1. Put full fdsn-station.xml in etc/.
// 2. export FDSN_STATION_XML_META_KEY=fdsn-station.xml
// 3. Run `go test -bench=StationQuery -benchmem -run=^$`.
// Note: You must specify -run=^$ to skip test functions since you're not using test fdsn-station xml.
//
// Currently the benchmark result for my MacBookPro 2017 is:
// BenchmarkStationQuery/post-4 20000 74472 ns/op 78376 B/op 706 allocs/op
func BenchmarkStationQuery(b *testing.B) {
Expand Down
2 changes: 2 additions & 0 deletions cmd/fdsn-ws/fdsn_station_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ type NetworkType struct {
}

// May be one of NOMINAL, CALCULATED
//
//nolint:deadcode,unused // Struct based on FDSN spec so keep it
type NominalType string

Expand Down Expand Up @@ -364,6 +365,7 @@ type SampleRateType struct {
}

// A time value in seconds.
//
//nolint:deadcode,unused // Struct based on FDSN spec so keep it
type SecondType struct {
Value float64 `xml:",chardata"`
Expand Down
5 changes: 3 additions & 2 deletions cmd/fdsn-ws/log.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package main

import (
"github.com/GeoNet/kit/metrics"
"github.com/GeoNet/kit/weft"
"log"
"os"

"github.com/GeoNet/kit/metrics"
"github.com/GeoNet/kit/weft"
)

var Prefix string
Expand Down
2 changes: 1 addition & 1 deletion cmd/fdsn-ws/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

var mux *http.ServeMux

func init() {
func initRoutes() {
mux = http.NewServeMux()

mux.HandleFunc("/", weft.MakeHandler(weft.NoMatch, weft.TextError))
Expand Down
4 changes: 3 additions & 1 deletion cmd/fdsn-ws/routes_integration_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
//go:build integration
// +build integration

package main

import (
wt "github.com/GeoNet/kit/weft/wefttest"
"testing"

wt "github.com/GeoNet/kit/weft/wefttest"
)

var routesIntegration = wt.Requests{
Expand Down
3 changes: 2 additions & 1 deletion cmd/fdsn-ws/sc3ml.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package main

import (
"bytes"
"net/http"

"github.com/GeoNet/fdsn/internal/valid"
"github.com/GeoNet/kit/weft"
"net/http"
)

func s3ml(r *http.Request, h http.Header, b *bytes.Buffer) error {
Expand Down
19 changes: 10 additions & 9 deletions cmd/fdsn-ws/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,16 @@ import (
)

var (
db *sql.DB
decoder = schema.NewDecoder() // decoder for URL queries.
S3_BUCKET string // the S3 bucket storing the miniseed files used by dataselect
LOG_EXTRA bool // Whether POST body is logged.
zeroDateTime time.Time
db *sql.DB
decoder = schema.NewDecoder() // decoder for URL queries.
S3_BUCKET string // the S3 bucket storing the miniseed files used by dataselect
LOG_EXTRA bool // Whether POST body is logged.
)

var stationVersion = "1.1"
var eventVersion = "1.2"
var dataselectVersion = "1.1"

func init() {
zeroDateTime = time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC)
}
var zeroDateTime = time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC)

func main() {
var err error
Expand Down Expand Up @@ -60,6 +56,11 @@ func main() {
log.Println("ERROR: problem pinging DB - is it up and contactable? 500s will be served")
}

initDataselectTemplate()
initEventTemplate()
initStationTemplate()
initRoutes()

setupStationXMLUpdater()

log.Println("starting server")
Expand Down
7 changes: 7 additions & 0 deletions cmd/fdsn-ws/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@ import (
"net/http/httptest"
"os"
"testing"

"github.com/joho/godotenv"
)

var ts *httptest.Server

func setup(t *testing.T) {
var err error

err = godotenv.Load("env.list")
if err != nil {
t.Fatal(err)
}

S3_BUCKET = os.Getenv("S3_BUCKET")

// need a db write user for adding test data.
Expand Down
3 changes: 3 additions & 0 deletions cmd/s3-notify/s3-notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ func init() {
flag.StringVar(&keyPrefix, "key-prefix", "", "Key prefix to search in the S3 bucket.")
flag.StringVar(&sqsUrl, "sqs-url", "", "SQS queue url to send notifications to. Omit this parameter to show the list of matched keys only.")
flag.Parse()
}

func initAWS() {
var err error
s3c, err := s3.NewWithMaxRetries(100)
if err != nil {
Expand All @@ -50,6 +52,7 @@ func main() {
fmt.Println("Send to SQS:", sqsUrl)
}

initAWS()
keys, err := s3Client.ListAll(bucketName, keyPrefix)
if err != nil {
log.Fatalf("error listing S3 objects: %s", err)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ require (
github.com/aws/aws-sdk-go-v2/service/sts v1.28.4 // indirect
github.com/aws/smithy-go v1.20.1 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/joho/godotenv v1.5.1 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9Y
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/lib/pq v1.10.3 h1:v9QZf2Sn6AmjXtQeFpdoq/eaNtYP6IN+7lcrygsIAtg=
github.com/lib/pq v1.10.3/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
Loading

0 comments on commit 9309a29

Please sign in to comment.