Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: rename to serverservice and add migrate and version subcommands #32

Merged
merged 1 commit into from
Aug 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.16
go-version: 1.17
-
name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
Expand Down
28 changes: 7 additions & 21 deletions .github/workflows/lint-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,46 +28,32 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: '1.16'

- name: Start test database
run: make docker-up

- name: Install goose for database migrations
run: go install github.com/pressly/goose/cmd/goose@v2.7.0
go-version: '1.17'

- name: Install cockroach binary
run: curl https://binaries.cockroachdb.com/cockroach-v21.1.7.linux-amd64.tgz | tar -xz && sudo cp -i cockroach-v21.1.7.linux-amd64/cockroach /usr/local/bin/

- name: Start test database
run: cockroach start-single-node --insecure --background

- name: Create test DB
run: make test-database

- name: Run golangci-lint
uses: golangci/golangci-lint-action@v2

with:
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
version: v1.41

# Optional: working directory, useful for monorepos
# working-directory: somedir

# Optional: golangci-lint command line arguments.
# args: --issues-exit-code=0
version: v1.42
args: --timeout=5m

# Optional: show only new issues if it's a pull request. The default value is `false`.
# only-new-issues: true

- name: Run go tests for generated models code
run: go test -race ./internal/models

# Don't include the models test that we tested above already. We don't want to calculate the test coverage from these into the app since we can't affect it
- name: Run go tests and generate coverage report
run: HOLLOW_DCIM_TEST_DB="host=localhost port=26257 user=root sslmode=disable dbname=hollow_dcim_test" go test -race -coverprofile=coverage.txt -covermode=atomic -tags testtools -p 1 `go list ./... | grep -v internal/models`
run: SERVERSERVICE_DB_URI="host=localhost port=26257 user=root sslmode=disable dbname=serverservice_test" go test -race -coverprofile=coverage.txt -covermode=atomic -tags testtools -p 1 `go list ./... | grep -v internal/models`

- name: Stop test database
run: make docker-down
run: cockroach quit --insecure --host=localhost:26257

- name: Upload coverage report
uses: codecov/codecov-action@v1
Expand Down
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ run:

linters-settings:
goimports:
local-prefixes: go.hollow.sh/dcim
local-prefixes: go.hollow.sh/serverservice
gofumpt:
extra-rules: true

Expand Down
6 changes: 2 additions & 4 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
project_name: hollow-dcim
project_name: serverservice
before:
hooks:
- go mod download
Expand Down Expand Up @@ -50,7 +50,5 @@ changelog:
dockers:
-
image_templates:
- "ghcr.io/metal-toolbox/{{.ProjectName}}:{{ .Tag }}"
- "ghcr.io/metal-toolbox/hollow-{{.ProjectName}}:{{ .Tag }}"
dockerfile: Dockerfile
extra_files:
- db/migrations
22 changes: 7 additions & 15 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
FROM golang:1.17 as builder

# Build the goose binary
RUN CGO_ENABLED=0 GOOS=linux go install github.com/pressly/goose/cmd/goose@v2.7.0

# Use the official Alpine image for a lean production container.
# https://hub.docker.com/_/alpine
# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
FROM alpine:3
FROM alpine:3 as alpine
RUN apk add --no-cache ca-certificates

# Copy the binary to the production image from the builder stage.
COPY hollow-dcim /hollow-dcim
FROM scratch
# Copy ca-certs from alpine
COPY --from=alpine /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/

# Copy goose and database migration files
COPY --from=builder /go/bin/goose /goose
COPY db/migrations /db-migrations
# Copy the binary that goreleaser built
COPY serverservice /serverservice

# Run the web service on container startup.
ENTRYPOINT ["/hollow-dcim"]
ENTRYPOINT ["/serverservice"]
CMD ["serve"]
8 changes: 5 additions & 3 deletions Dockerfile.dev
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ RUN go mod download
# Copy local code to the container image.
COPY . ./

RUN go mod tidy

# Build the binary.
# -mod=readonly ensures immutable go.mod and go.sum in container builds.
RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -v -o hollow-dcim
RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -v -o serverservice

# Use the official Alpine image for a lean production container.
# https://hub.docker.com/_/alpine
Expand All @@ -22,7 +24,7 @@ FROM alpine:3
RUN apk add --no-cache ca-certificates

# Copy the binary to the production image from the builder stage.
COPY --from=builder /app/hollow-dcim /hollow-dcim
COPY --from=builder /app/serverservice /serverservice

# Run the web service on container startup.
CMD ["/hollow-dcim", "serve"]
CMD ["/serverservice", "serve"]
30 changes: 15 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ all: lint test
PHONY: test coverage lint golint clean vendor local-dev-databases docker-up docker-down integration-test unit-test
GOOS=linux
DB_STRING=host=localhost port=26257 user=root sslmode=disable
DEV_DB=${DB_STRING} dbname=hollow_dcim_dev
TEST_DB=${DB_STRING} dbname=hollow_dcim_test
DEV_DB=${DB_STRING} dbname=serverservice
TEST_DB=${DB_STRING} dbname=serverservice_test

test: | unit-test integration-test

integration-test: docker-up test-database
integration-test: test-database
@echo Running integration tests...
@HOLLOW_DCIM_TEST_DB="${TEST_DB}" go test -cover -tags testtools,integration -p 1 ./...
@SERVERSERVICE_DB_URI="${TEST_DB}" go test -cover -tags testtools,integration -p 1 ./...

unit-test: | lint
@echo Running unit tests...
@go test -cover -short -tags testtools ./...

coverage: | docker-up test-database
coverage: | test-database
@echo Generating coverage report...
@HOLLOW_DCIM_TEST_DB="${TEST_DB}" go test ./... -race -coverprofile=coverage.out -covermode=atomic -tags testtools -p 1
@SERVERSERVICE_DB_URI="${TEST_DB}" go test ./... -race -coverprofile=coverage.out -covermode=atomic -tags testtools -p 1
@go tool cover -func=coverage.out
@go tool cover -html=coverage.out

Expand Down Expand Up @@ -46,13 +46,13 @@ docker-down:
docker-clean:
@docker-compose down --volumes

dev-database:
@cockroach sql --insecure -e "drop database if exists hollow_dcim_dev"
@cockroach sql --insecure -e "create database hollow_dcim_dev"
@GOOSE_DRIVER=postgres GOOSE_DBSTRING="${DEV_DB}" goose -dir=db/migrations up
dev-database: | vendor
@cockroach sql --insecure -e "drop database if exists serverservice"
@cockroach sql --insecure -e "create database serverservice"
@SERVERSERVICE_DB_URI="${DEV_DB}" go run main.go migrate up

test-database:
@cockroach sql --insecure -e "drop database if exists hollow_dcim_test"
@cockroach sql --insecure -e "create database hollow_dcim_test"
@GOOSE_DRIVER=postgres GOOSE_DBSTRING="${TEST_DB}" goose -dir=db/migrations up
@cockroach sql --insecure -e "use hollow_dcim_test; ALTER TABLE attributes DROP CONSTRAINT check_server_id_server_component_id; ALTER TABLE versioned_attributes DROP CONSTRAINT check_server_id_server_component_id;"
test-database: | vendor
@cockroach sql --insecure -e "drop database if exists serverservice_test"
@cockroach sql --insecure -e "create database serverservice_test"
@SERVERSERVICE_DB_URI="${TEST_DB}" go run main.go migrate up
@cockroach sql --insecure -e "use serverservice_test; ALTER TABLE attributes DROP CONSTRAINT check_server_id_server_component_id; ALTER TABLE versioned_attributes DROP CONSTRAINT check_server_id_server_component_id;"
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
# Hollow
# Server Service

> This repository is [Experimental](https://github.com/packethost/standards/blob/master/experimental-statement.md) meaning that it's based on untested ideas or techniques and not yet established or finalized or involves a radically new and innovative style!
> This means that support is best effort (at best!) and we strongly encourage you to NOT use this in production.

North of Neverland, `Hollow` is the magical kingdom where fairies are born and is their home.

More pragmatically, Hollow is the combination of a golang-based API built on top of cockroachdb primarily responsible for the handling of physical asset information (e.g. metadata about our physical assets)
The server service is a microservice within the Hollow eco-system. Server service is responsible for providing a store for physical server information. Support to storing the device components that make up the server is available. You are also able to create attributes and versioned-attributes for both servers and the server components.

## Running locally

Expand All @@ -15,8 +13,8 @@ To run the api server locally you can bring it up with docker-compose.
docker compose up
```

If you have never ran hollow before then the server will fail to start the first time. After the DB service is running you need to create the dev database. Run:
If you have never ran `serverservice` before then the server will fail to start the first time. After the DB service is running you need to create the dev database. Run:

```
make local-dev-databases
make dev-database
```
2 changes: 1 addition & 1 deletion cmd/doc.go
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// Package cmd provides our CLI interface for the hollow binary
package cmd // import "go.hollow.sh/dcim/cmd"
package cmd // import "go.hollow.sh/serverservice/cmd"
60 changes: 60 additions & 0 deletions cmd/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package cmd

import (

// import the crdbpgx for automatic retries of errors for crdb that support retry
_ "github.com/cockroachdb/cockroach-go/v2/crdb/crdbpgx"
"github.com/pressly/goose/v3"
"github.com/spf13/cobra"
"github.com/spf13/viper"

dbm "go.hollow.sh/serverservice/db"
)

// migrateCmd represents the migrate command
var migrateCmd = &cobra.Command{
Use: "migrate <command> [args]",
Short: "A brief description of your command",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update short description

Long: `Migrate provides a wrapper around the "goose" migration tool.

Commands:
up Migrate the DB to the most recent version available
up-by-one Migrate the DB up by 1
up-to VERSION Migrate the DB to a specific VERSION
down Roll back the version by 1
down-to VERSION Roll back to a specific VERSION
redo Re-run the latest migration
reset Roll back all migrations
status Dump the migration status for the current DB
version Print the current version of the database
create NAME [sql|go] Creates new migration file with the current timestamp
fix Apply sequential ordering to migrations
`,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
migrate(args[0], args[1:])
},
}

func init() {
rootCmd.AddCommand(migrateCmd)
}

func migrate(command string, args []string) {
db, err := goose.OpenDBWithDriver("postgres", viper.GetString("db.uri"))
if err != nil {
logger.Fatalw("failed to open DB", "error", err)
}

defer func() {
if err := db.Close(); err != nil {
logger.Fatalw("failed to close DB", "error", err)
}
}()

goose.SetBaseFS(dbm.Migrations)

if err := goose.Run(command, db, "migrations", args...); err != nil {
logger.Fatalw("migrate command failed", "command", command, "error", err)
}
}
9 changes: 6 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ var (

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "hollow-dcim",
Short: "Hollow DCIM datastore",
Use: "serverservice",
Short: "Server Service for Hollow ecosystem",
}

// Execute adds all child commands to the root command and sets flags appropriately.
Expand All @@ -35,6 +35,9 @@ func init() {
viperBindFlag("logging.debug", rootCmd.PersistentFlags().Lookup("debug"))
rootCmd.PersistentFlags().Bool("pretty", false, "enable pretty (human readable) logging output")
viperBindFlag("logging.pretty", rootCmd.PersistentFlags().Lookup("pretty"))

rootCmd.PersistentFlags().String("db-uri", "postgresql://root@localhost:26257/serverservice?sslmode=disable", "URI for database connection")
viperBindFlag("db.uri", rootCmd.PersistentFlags().Lookup("db-uri"))
}

// initConfig reads in config file and ENV variables if set.
Expand All @@ -53,7 +56,7 @@ func initConfig() {
}

viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.SetEnvPrefix("hollow_dcim")
viper.SetEnvPrefix("serverservice")
viper.AutomaticEnv() // read in environment variables that match

// If a config file is found, read it in.
Expand Down
9 changes: 3 additions & 6 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/spf13/viper"
"go.hollow.sh/toolbox/ginjwt"

"go.hollow.sh/dcim/internal/dbtools"
"go.hollow.sh/dcim/internal/dcimserver"
"go.hollow.sh/serverservice/internal/dbtools"
"go.hollow.sh/serverservice/internal/httpsrv"
)

// serveCmd represents the serve command
Expand All @@ -27,9 +27,6 @@ func init() {
serveCmd.Flags().String("listen", "0.0.0.0:8000", "address to listen on")
viperBindFlag("listen", serveCmd.Flags().Lookup("listen"))

serveCmd.Flags().String("db-uri", "postgresql://root@db:26257/hollow_dev?sslmode=disable", "URI for database connection")
viperBindFlag("db.uri", serveCmd.Flags().Lookup("db-uri"))

serveCmd.Flags().Bool("oidc", true, "use oidc auth")
viperBindFlag("oidc.enabled", serveCmd.Flags().Lookup("oidc"))
serveCmd.Flags().String("oidc-aud", "", "expected audience on OIDC JWT")
Expand All @@ -56,7 +53,7 @@ func serve() {
"address", viper.GetString("listen"),
)

hs := &dcimserver.Server{
hs := &httpsrv.Server{
Logger: logger.Desugar(),
Listen: viper.GetString("listen"),
Debug: viper.GetBool("logging.debug"),
Expand Down
22 changes: 22 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"

"go.hollow.sh/toolbox/version"
)

// versionCmd represents the version command
var versionCmd = &cobra.Command{
Use: "version",
Short: "Print the version and exit",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(version.String())
},
}

func init() {
rootCmd.AddCommand(versionCmd)
}
10 changes: 10 additions & 0 deletions db/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Package db provides an embedded filesystem containing all the database migrations
package db

import (
"embed"
)

// Migrations contain an embedded filesystem with all the sql migration files
//go:embed migrations/*.sql
var Migrations embed.FS
3 changes: 2 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ services:
context: .
dockerfile: Dockerfile.dev
environment:
- HOLLOW_JWT_AUDIENCE=https://staging.hollow.platformequinix.net
- SERVERSERVICE_OIDC_ENABLED=false
- SERVERSERVICE_DB_URI="postgresql://root@db:26257/serverservice?sslmode=disable"
ports:
- "8000:8000"
links:
Expand Down
Loading