Skip to content

Commit

Permalink
Merge branch 'main' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
massenz committed Feb 26, 2023
2 parents 1f90522 + 3270579 commit f9b0565
Show file tree
Hide file tree
Showing 36 changed files with 396 additions and 2,925 deletions.
31 changes: 16 additions & 15 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,34 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: build
run: |
make build
- name: Create release tag
run: |
TAG=$(./get-tag)
echo TAG=${TAG} >> $GITHUB_ENV
git config user.name "$AUTHOR"
git config user.email "<$EMAIL>"
TAG=$(./get-tag)
git tag -a $TAG -m "Release $TAG"
git push --tags
echo TAG=${TAG} >> $GITHUB_ENV
- name: Build container
run: |
IMAGE=$(make container | grep "Successfully tagged" | awk '{print $3;}')
if [[ -z $(docker images -q $IMAGE) ]]; then
echo "Image $IMAGE not found"
exit 1
fi
echo IMAGE=$IMAGE >> $GITHUB_ENV
- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Push container
run: |
docker login -u ${{ secrets.DOCKERHUB_USERNAME }} -p ${{ secrets.DOCKERHUB_PASSWORD }}
docker push $IMAGE
- name: Build and push Docker images
uses: docker/[email protected]
with:
context: .
file: docker/Dockerfile
platforms: ${{ matrix.platforms }}
push: true
tags: ghcr.io/${{ github.repository }}:${{ env.TAG }}

- uses: ncipollo/[email protected]
with:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/build.yml → .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Copyright (c) 2022 AlertAvert.com. All rights reserved.
# Author: Marco Massenzio ([email protected])
#
name: Build & Test
name: Test

on:
push:
Expand All @@ -28,4 +28,4 @@ jobs:
run: |
mkdir -p ${HOME}/.aws && cp data/credentials ${HOME}/.aws/
export AWS_REGION=us-west-2
go test ./api ./grpc ./pubsub ./server ./storage
go test ./api ./grpc ./pubsub ./storage
2 changes: 1 addition & 1 deletion .run/Run all tests.run.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<configuration default="false" name="Run all tests" type="GoTestRunConfiguration" factoryName="Go Test" singleton="false">
<module name="statemachine" />
<working_directory value="$PROJECT_DIR$" />
<parameters value="./api ./grpc ./pubsub ./storage ./server" />
<parameters value="./api ./grpc ./pubsub ./storage" />
<kind value="DIRECTORY" />
<package value="github.com/massenz/go-statemachine" />
<directory value="$PROJECT_DIR$" />
Expand Down
69 changes: 43 additions & 26 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
# Copyright (c) 2022 AlertAvert.com. All rights reserved.
# Created by M. Massenzio, 2022-03-14

bin := build/bin
out := $(bin)/sm-server
tag := $(shell ./get-tag)
image := massenz/statemachine
module := $(shell go list -m)

GOOS ?= $(shell uname -s | tr "[:upper:]" "[:lower:]")
GOARCH ?= amd64
GOMOD := $(shell go list -m)

version := v0.10.0
release := $(version)-g$(shell git rev-parse --short HEAD)
prog := sm-server
bin := out/bin/$(prog)-$(version)_$(GOOS)-$(GOARCH)
dockerbin := out/bin/$(prog)-$(version)_linux-amd64

image := massenz/statemachine
compose := docker/compose.yaml
dockerfile := docker/Dockerfile

Expand All @@ -15,7 +21,7 @@ dockerfile := docker/Dockerfile
# Edit only the packages list, when adding new functionality,
# the rest is deduced automatically.
#
pkgs := ./api ./grpc ./pubsub ./server ./storage
pkgs := ./api ./grpc ./pubsub ./storage
all_go := $(shell for d in $(pkgs); do find $$d -name "*.go"; done)
test_srcs := $(shell for d in $(pkgs); do find $$d -name "*_test.go"; done)
srcs := $(filter-out $(test_srcs),$(all_go))
Expand All @@ -40,21 +46,31 @@ help: ## Display this help.
.PHONY: clean
img=$(shell docker images -q --filter=reference=$(image))
clean: ## Cleans up the binary, container image and other data
@rm -f $(out)
@rm -f $(bin)
@[ ! -z $(img) ] && docker rmi $(img) || true
@rm -rf certs

.PHONY: build test container cov clean fmt
version: ## Displays the current version tag (release)
@echo $(release)

fmt: ## Formats the Go source code using 'go fmt'
@go fmt $(pkgs) ./cmd ./clients

##@ Development
$(out): cmd/main.go $(srcs)
go build -ldflags "-X $(module)/server.Release=$(tag)" -o $(out) cmd/main.go
@chmod +x $(out)
.PHONY: build test container cov clean fmt
$(bin): cmd/main.go $(srcs)
@mkdir -p $(shell dirname $(bin))
GOOS=$(GOOS); GOARCH=$(GOARCH); go build \
-ldflags "-X $(GOMOD)/api.Release=$(release)" \
-o $(bin) cmd/main.go

build: $(out) ## Builds the server
$(dockerbin):
GOOS=linux; GOARCH=amd64; go build \
-ldflags "-X $(GOMOD)/api.Release=$(release)" \
-o $(dockerbin) cmd/main.go

.PHONY: build
build: $(bin) ## Builds the Statemachine server binary

test: $(srcs) $(test_srcs) ## Runs all tests
ginkgo $(pkgs)
Expand All @@ -66,25 +82,25 @@ cov: $(srcs) $(test_srcs) ## Runs the Test Coverage target and opens a browser
##@ Container Management
# Convenience targets to run locally containers and
# setup the test environments.
#
.PHONY: container
container: $(out) ## Builds the container image
docker build -f $(dockerfile) -t $(image):$(tag) .

.PHONY: services
services: ## Starts the Redis and LocalStack containers
@docker compose -f $(compose) --project-name sm up -d
container: $(dockerbin) ## Builds the container image
docker build --build-arg appname=$(dockerbin) -f $(dockerfile) -t $(image):$(release) .

.PHONY: start
start: ## Starts the Redis and LocalStack containers, and Creates the SQS Queues in LocalStack
@RELEASE=$(release) BASEDIR=$(shell pwd) docker compose -f $(compose) --project-name sm up redis localstack -d
@sleep 3
@for queue in events notifications; do \
aws --no-cli-pager --endpoint-url=http://localhost:4566 \
--region us-west-2 \
sqs create-queue --queue-name $$queue; done >/dev/null
# We need to wait for the SQS Queues to be up before starting the server.
#@RELEASE=$(release) BASEDIR=$(shell pwd) docker compose -f $(compose) --project-name sm up server -d

.PHONY: stop
stop: ## Stops the Redis and LocalStack containers
@docker compose -f $(compose) --project-name sm down

.PHONY: queues
queues: ## Creates the SQS Queues in LocalStack
@for queue in events notifications acks; do \
aws --no-cli-pager --endpoint-url=http://localhost:4566 \
--region us-west-2 \
sqs create-queue --queue-name $$queue; done >/dev/null

##@ TLS Support
#
Expand All @@ -108,7 +124,8 @@ gencert: $(ca-csr) $(config) $(server-csr) ## Generates all certificates in the
-profile=server \
$(server-csr) | cfssljson -bare server
@mkdir -p certs
@mv *.pem *.csr certs/
@mv *.pem certs/
@rm *.csr
@echo "Certificates generated in $(shell pwd)/certs"

.PHONY: clean-cert
Expand Down
37 changes: 21 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,42 +297,47 @@ Logs are sent to `stdout` by default, but this can be changed using the [`slf4go

Running the server inside a container is much preferable; to build the container use:

make container
GOOS=linux make build && make container

and then:

docker run --rm -d -p 7399:7399 --name sm-server \
--env AWS_ENDPOINT=http://awslocal:4566 \
--env DEBUG=-debug --network docker_sm-net \
massenz/statemachine:$(./get-tag)
```
docker run --rm -p 7398:7398 --name sm_server --network sm_sm-net \
-v $(pwd)/docker/aws-credentials:/home/sm-bot/.aws/credentials \
-v $(pwd)/certs:/etc/statemachine/certs \
--env AWS_ENDPOINT=http://awslocal:4566 --env AWS_REGION=us-west-2 --env AWS_PROFILE=sm-bot \
--env TIMEOUT=200ms --env DEBUG=-debug \
massenz/statemachine:$(make version)
```

These are the environment variables whose values can be modified as necessary (see also the `Dockerfile`):

```
ENV AWS_REGION=us-west-2
ENV AWS_PROFILE=sm-bot
# Sensible defaults for the server
# See entrypoint.sh
ENV SERVER_PORT=7399
ENV EVENTS_Q=events
ENV ERRORS_Q=notifications
ENV REDIS=redis
ENV REDIS_PORT=6379
ENV DEBUG=""
# Optional settings for the server
ENV ACKS="-acks acks"
ENV CLUSTER="-cluster"
ENV NOTIFY_ERRORS_ONLY="-notify-errors-only"
```

Additionally, a valid `credentials` file will need to be mounted (using the `-v` flag) in the container if connecting to AWS (instead of LocalStack):

-v /full/path/to/.aws/credentials:/home/sm-bot/.aws/credentials
-v /full/path/to/.aws/credentials:/home/sm-bot/.aws/credentials

where the `[profile]` matches the value in `AWS_PROFILE`.

To run with TLS enabled, generate certs with `make gencert` and then mount the volume on the Docker image:

-v $(pwd)/certs:/etc/statemachine/certs

Alternatively, use the `-insecure` flag to disable TLS.

If you see this error:
```
2023/02/20 03:15:44 main.go:201: [FATAL] open /etc/statemachine/certs/server.pem: no such file or directory
```
run `make gencert`.

# Contributing

Expand Down
4 changes: 3 additions & 1 deletion api/fsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ const (
)

var (
Release string

MalformedConfigurationError = fmt.Errorf("this configuration cannot be parsed")
MissingNameConfigurationError = fmt.Errorf(
"configuration must always specify a name (and optionally a version)")
Expand All @@ -43,7 +45,7 @@ var (

// Logger is made accessible so that its `Level` can be changed
// or can be sent to a `NullLog` during testing.
Logger = log.NewLog("fsm")
Logger = log.NewLog("api")
)

// ConfiguredStateMachine is the internal representation of an FSM, which
Expand Down
3 changes: 0 additions & 3 deletions build.settings

This file was deleted.

Loading

0 comments on commit f9b0565

Please sign in to comment.