Skip to content

Commit

Permalink
feat: Update cat-gateway packaging, update README.md with guidance ho…
Browse files Browse the repository at this point in the history
…w to locally spin up cat-gateway with event-db (#437)

* update cat-gateway packaging

* fix

* fix

* fix

* fix

* fix spelling

* update entry.sh comments

* wip

* fix

---------

Co-authored-by: Oleksandr Prokhorenko <[email protected]>
  • Loading branch information
Mr-Leshiy and minikin authored Apr 19, 2024
1 parent f585c2f commit 3676b95
Show file tree
Hide file tree
Showing 12 changed files with 139 additions and 132 deletions.
35 changes: 18 additions & 17 deletions catalyst-gateway/Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ build:
FROM +builder

TRY
RUN /scripts/std_build.py --cov_report="coverage-report.info" \
--bins="cat-gateway/cat-gateway"
RUN /scripts/std_build.py \
--cov_report="coverage-report.info" \
--bins="cat-gateway/cat-gateway"
FINALLY
SAVE ARTIFACT --if-exists target/nextest/ci/junit.xml AS LOCAL cat-gateway.junit-report.xml
SAVE ARTIFACT --if-exists coverage-report.info AS LOCAL cat-gateway.coverage-report.info
Expand All @@ -47,29 +48,29 @@ all-hosts-build:
BUILD --platform=linux/amd64 --platform=linux/arm64 +build

package-cat-gateway:
ARG tag="latest"

FROM alpine:3.19
WORKDIR /cat-gateway

RUN apk add --no-cache gcc bash

COPY +build/cat-gateway .
COPY ./scripts/entry.sh .

ENTRYPOINT ./entry.sh
SAVE IMAGE cat-gateway:$tag

package-cat-gateway-with-preprod-snapshot:
ARG tag="latest"
ARG address
ARG db_url
ARG log_level="error"

RUN apk add --no-cache gcc
FROM +package-cat-gateway

COPY +build/cat-gateway .
# copy preprod mithril snapshot to /tmp/preprod dir
COPY github.com/input-output-hk/catalyst-ci/earthly/mithril_snapshot:v2.11.1+package-preprod-snapshot/snapshot /tmp/preprod

ENTRYPOINT ./cat-gateway run --address $address --database-url $db_url --log-level $log_level
SAVE IMAGE cat-gateway:$tag

# Publish packages if all integration tests have passed. (Failure to pass tests will prevent packages being published.)
# publish:
# FROM scratch

## -----------------------------------------------------------------------------

# This step simulates the full CI run for local purposes only.
local-ci-run:
BUILD +check
BUILD +build
# BUILD +package
# BUILD +publish
42 changes: 42 additions & 0 deletions catalyst-gateway/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
* [`./bin`](#bin)
* [`./crates`](#crates)
* [`./event-db`](#event-db)
* [Build and Run](#build-and-run)
* [Docker images](#docker-images)
* [Rust binary](#rust-binary)

The Catalyst Data Gateway is the backend of the Catalyst Voices hosted stack.

Expand All @@ -26,3 +29,42 @@ They are also able to be used stand-alone in other projects and can be published

Defines the Postgres Catalyst Event Database that the Catalyst gateway uses for running Catalyst Events.
This is DB definition and data only, the actual db interface code is located at `./bin/src/event-db`.

## Build and Run

There are several options how you can build this service,
as a Rust binary from the Rust code explicitly,
or you can build a docker image and run everything with the `docker-compose`.

### Docker images

To build and run docker images follow these steps:

1. Run `earthly +package-cat-gateway` or `earthly +package-cat-gateway-with-preprod-snapshot`
to build a cat-gateway docker image without `preprod-snapshot` or with it.
2. Run `earthly ./event-db+build` to build an event-db docker image.
3. Run `docker-compose up cat-gateway` to spin up cat-gateway with event-db from already built images.

Note that every time when you are building an image it obsoletes an old image but does not remove it,
so dont forget to cleanup dangling images of the event-db and cat-gateway in your docker environment.

### Rust binary

To build and run a Rust binary follow these steps:

1. Run `cargo build -p cat-gateway --release`
to compile a release version of the cat-gateway
2. Run `earthly ./event-db+build` to build an event-db docker image
3. If you need to have a `preprod-snapshot` unarchive snapshot data to the `/tmp/preprod/` dir.
You can download `preprod-snapshot` from this
[resource](https://mithril.network/explorer/?aggregator=https%3A%2F%2Faggregator.release-preprod.api.mithril.network%2Faggregator).
4. Run

```sh
./target/release/cat-gateway run \
--address "127.0.0.1:3030" \
--database-url=postgres://catalyst-event-dev:CHANGE_ME@localhost/CatalystEventDev \
--log-level=debug \
--log-format=compact \
--metrics-address "127.0.0.1:3032"
```
50 changes: 17 additions & 33 deletions catalyst-gateway/bin/src/service/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,48 +93,32 @@ pub(crate) fn mk_api(

// Get localhost name
if let Ok(hostname) = gethostname().into_string() {
let hostname_addresses = add_protocol_prefix(
&format!("{hostname}:{port}"),
settings.http_auto_servers,
settings.https_auto_servers,
);
for hostname_address in hostname_addresses {
service = service.server(
ServerObject::new(hostname_address).description("Server at localhost name"),
);
}
let hostname_address = format!("http://{hostname}:{port}");
service = service
.server(ServerObject::new(hostname_address).description("Server at localhost name"));
}

// Get local IP address v4 and v6
if let Ok(network_interfaces) = list_afinet_netifas() {
for (name, ip) in &network_interfaces {
if *name == "en0" {
let (ip_with_port, desc) = match ip {
IpAddr::V4(_) => (format!("{ip}:{port}"), "Server at local IPv4 address"),
IpAddr::V6(_) => (format!("[{ip}]:{port}"), "Server at local IPv6 address"),
let (address, desc) = match ip {
IpAddr::V4(_) => {
(
format!("http://{ip}:{port}"),
"Server at local IPv4 address",
)
},
IpAddr::V6(_) => {
(
format!("http://[{ip}]:{port}"),
"Server at local IPv6 address",
)
},
};
let ip_addresses = add_protocol_prefix(
&ip_with_port,
settings.http_auto_servers,
settings.https_auto_servers,
);
for address in ip_addresses {
service = service.server(ServerObject::new(address).description(desc));
}
service = service.server(ServerObject::new(address).description(desc));
}
}
}
service
}

/// Function to add protocol prefix based on flags.
fn add_protocol_prefix(address: &String, is_http: bool, is_https: bool) -> Vec<String> {
let mut addresses = Vec::new();
if is_http {
addresses.push(format!("http://{address}"));
}
if is_https {
addresses.push(format!("https://{address}"));
}
addresses
}
8 changes: 0 additions & 8 deletions catalyst-gateway/bin/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,6 @@ pub(crate) struct DocsSettings {
#[clap(long, default_value = ADDRESS_DEFAULT, env = "ADDRESS")]
pub(crate) address: SocketAddr,

/// Flag for adding "http" to servers
#[clap(long, default_value = "false", env = "HTTP_AUTO_SERVERS")]
pub(crate) http_auto_servers: bool,

/// Flag for adding "https" to servers
#[clap(long, default_value = "true", env = "HTTPS_AUTO_SERVERS")]
pub(crate) https_auto_servers: bool,

/// Server name
#[clap(long, env = "SERVER_NAME")]
pub(crate) server_name: Option<String>,
Expand Down
42 changes: 42 additions & 0 deletions catalyst-gateway/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
version: "3"

services:
event-db:
image: event-db:latest
environment:
# Required environment variables for migrations
- DB_HOST=localhost
- DB_PORT=5432
- DB_NAME=CatalystEventDev
- DB_DESCRIPTION="Catalyst Event DB"
- DB_SUPERUSER=postgres
- DB_SUPERUSER_PASSWORD=postgres
- DB_USER=catalyst-event-dev
- DB_USER_PASSWORD=CHANGE_ME

- INIT_AND_DROP_DB=true
- WITH_MIGRATIONS=true
- WITH_SEED_DATA=true
ports:
- 5432:5432
healthcheck:
test: ["CMD-SHELL", "pg_isready -U $${DB_SUPERUSER} -d $${DB_SUPERUSER_PASSWORD}"]
interval: 10s
timeout: 5s
retries: 10

cat-gateway:
image: cat-gateway:latest
environment:
- DB_URL=postgres://catalyst-event-dev:CHANGE_ME@event-db/CatalystEventDev
- CAT_ADDRESS=0.0.0.0:3030
- LOG_LEVEL=info
- DATA_REFRESH_TICK=5
- CHECK_CONFIG_TICK=5
- MACHINE_ID="UID"
hostname: 127.0.0.1
ports:
- 3030:3030
depends_on:
event-db:
condition: service_healthy
18 changes: 2 additions & 16 deletions catalyst-gateway/event-db/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,8 @@ This crate defines the necessary migrations, seed data and docker image builder

Firstly you will need to prepare a docker images with all migrations and data.

Prepare a event-db docker image with the historic and test data
Prepare and build an event-db docker image

```sh
earthly ./catalyst-gateway/event-db+build
```

Run a event db docker container

```sh
docker-compose -f catalyst-gateway/event-db/docker-compose.yml up event-db
```

This will run postgres on port `5432`.

To test that docker image builds fine and migrations correctly applies run

```sh
earthly -P ./catalyst-gateway/event-db+test
earthly +build
```
23 changes: 0 additions & 23 deletions catalyst-gateway/event-db/docker-compose.yml

This file was deleted.

25 changes: 8 additions & 17 deletions catalyst-gateway/bin/entry.sh → catalyst-gateway/scripts/entry.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,15 @@
# ---------------------------------------------------------------
#
# This script serves as the entrypoint for the cat-gateway service container.
# It sets up the environment, performing optional database initialization
# if configured, and then runs the cat-gateway service.
#
# It expects the following environment variables to be set except where noted:
#
# DATABASE_URL - URL for the EventDB.
# DEBUG - If set, the script will print debug information (optional)
# DB_URL - URL for the EventDB.
# CAT_ADDRESS - Address to bind the cat-gateway service to.
# LOG_LEVEL - Log level for the cat-gateway service.
# DEBUG_SLEEP - If set, the script will sleep for the specified number of seconds (optional)
# ---------------------------------------------------------------

# Enable strict mode
set +x
set -o errexit
set -o pipefail
set -o nounset
set -o functrace
set -o errtrace
set -o monitor
set -o posix
shopt -s dotglob

check_env_vars() {
local env_vars=("$@")
Expand All @@ -49,7 +38,9 @@ echo ">>> Starting entrypoint script..."

# Check if all required environment variables are set
REQUIRED_ENV=(
"DATABASE_URL"
"DB_URL"
"CAT_ADDRESS"
"LOG_LEVEL"
)
check_env_vars "${REQUIRED_ENV[@]}"

Expand All @@ -58,7 +49,7 @@ debug_sleep

# Define the command to be executed
ARGS=$*
CMD="/app/cat-gateway run $ARGS"
CMD="./cat-gateway run --address $CAT_ADDRESS --database-url $DB_URL --log-level $LOG_LEVEL $ARGS"
echo ">>> Executing command: $CMD"

# Wait for DEBUG_SLEEP seconds if the DEBUG_SLEEP environment variable is set
Expand All @@ -77,4 +68,4 @@ set -e
if [ $EXIT_CODE -ne 0 ]; then
echo "Error: cat-gateway returned with exit code $EXIT_CODE"
exit 1
fi
fi
4 changes: 1 addition & 3 deletions catalyst-gateway/tests/Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,14 @@ fuzzer-api:
FROM earthly/dind:alpine-3.19
RUN apk update && apk add iptables-legacy # workaround for https://github.com/earthly/earthly/issues/3784
RUN apk add yq zstd
ARG DB_URL="postgres://catalyst-event-dev:CHANGE_ME@localhost/CatalystEventDev"
ARG CAT_ADDRESS="127.0.0.1:3030"
ARG OPENAPI_SPEC="http://127.0.0.1:3030/docs/cat-gateway.json"

COPY schemathesis-docker-compose.yml .
WITH DOCKER \
--compose schemathesis-docker-compose.yml \
--load schemathesis:latest=(+package-schemathesis --openapi_spec=$OPENAPI_SPEC) \
--load event-db:latest=(../event-db+build) \
--load cat-gateway:latest=(../+package-cat-gateway --address=$CAT_ADDRESS --db_url=$DB_URL) \
--load cat-gateway:latest=(../+package-cat-gateway) \
--service event-db \
--service cat-gateway \
--allow-privileged
Expand Down
16 changes: 2 additions & 14 deletions catalyst-gateway/tests/api_tests/Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,6 @@ builder:
COPY ./snapshot_tool-56364174.json .
DO github.com/input-output-hk/catalyst-ci/earthly/python:v2.11.1+BUILDER

cat-gateway-test:
ARG DB_URL="postgres://catalyst-event-dev:CHANGE_ME@event-db/CatalystEventDev"
ARG CAT_ADDRESS="0.0.0.0:3030"
ARG LOG_LEVEL="error"

FROM ../../+package-cat-gateway --address=$CAT_ADDRESS --db_url=$DB_URL --log_level=$LOG_LEVEL

# copy preprod mithril snapshot to /tmp/preprod dir
COPY github.com/input-output-hk/catalyst-ci/earthly/mithril_snapshot:v2.11.1+package-preprod-snapshot/snapshot /tmp/preprod

SAVE IMAGE cat-gateway-test:latest

test:
FROM +builder

Expand All @@ -28,8 +16,8 @@ test:
WITH DOCKER \
--compose docker-compose.yml \
--load event-db:latest=(../../event-db+build) \
--load cat-gateway-test:latest=(+cat-gateway-test) \
--load cat-gateway:latest=(../../+package-cat-gateway-with-preprod-snapshot) \
--service cat-gateway \
--allow-privileged
RUN poetry run pytest -s
END
END
Loading

0 comments on commit 3676b95

Please sign in to comment.