Skip to content
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
22 changes: 14 additions & 8 deletions .github/workflows/ami-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@
name: Build AMI

# This workflow should be triggered MANUALLY when AMI infrastructure needs updating.
# Node software updates are handled automatically via Docker :latest tag.
# The AMI runs the node release that is the latest release when it is built
# (TN_NODE_TAG in /opt/tn/.env). Operators move a node to another release with
# `sudo tn-node-update <release tag>`.
#
# When to rebuild AMI:
# ✅ Infrastructure changes (scripts, services, docker-compose)
# ✅ Base OS updates (Ubuntu patches)
# ✅ AWS Marketplace submission
# ✅ Major version releases
# ✅ Major version releases, so that new nodes start on them
#
# When NOT to rebuild:
# ❌ Regular node software updates (use Docker image workflow instead)
# ❌ Bug fixes (users get them via :latest tag)
# ❌ Regular node software updates (operators run tn-node-update)

'on':
workflow_dispatch:
Expand Down Expand Up @@ -57,10 +58,11 @@ jobs:
- name: Get latest release version
id: get-version
run: |
# Get latest release tag from GitHub API
LATEST_VERSION=$(gh release list --limit 1 --json tagName --jq '.[0].tagName')
# The latest published release; the AMI runs its node image.
LATEST_VERSION=$(gh release view --json tagName --jq '.tagName')
if [ -z "$LATEST_VERSION" ]; then
LATEST_VERSION="manual"
echo "::error::No published release for the AMI to run"
exit 1
fi
echo "Latest release version: $LATEST_VERSION"
echo "version=$LATEST_VERSION" >> "$GITHUB_OUTPUT"
Expand All @@ -85,6 +87,8 @@ jobs:
cdk --version

- name: Deploy/Update AMI pipeline infrastructure
env:
NODE_TAG: ${{ steps.get-version.outputs.version }}
run: |
cd deployments/infra
echo "Destroying existing AMI pipeline to ensure fresh components..."
Expand All @@ -93,6 +97,7 @@ jobs:
cdk destroy AMI-Pipeline-${{ env.STAGE }}-Stack \
--app "go run ami-cdk.go" \
--context stage=${{ env.STAGE }} \
--context nodeTag="$NODE_TAG" \
--force || echo "Stack didn't exist or failed to destroy, continuing..."

echo "Deploying fresh AMI pipeline infrastructure for stage: ${{ env.STAGE }}..."
Expand All @@ -102,6 +107,7 @@ jobs:
cdk deploy AMI-Pipeline-${{ env.STAGE }}-Stack \
--app "go run ami-cdk.go" \
--context stage=${{ env.STAGE }} \
--context nodeTag="$NODE_TAG" \
--require-approval never

# Get pipeline ARN after deployment
Expand Down Expand Up @@ -237,7 +243,7 @@ jobs:
echo " https://aws.amazon.com/marketplace/management/products"

- name: Update latest release with AMI details
if: env.AMI_ID && steps.get-version.outputs.version != 'manual'
if: env.AMI_ID
uses: actions/github-script@v7
env:
VERSION: ${{ steps.get-version.outputs.version }}
Expand Down
12 changes: 11 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,17 @@ jobs:
with:
node-version: '20'
- name: Run infra tests
run: go test -count=1 ./config/ ./config/node/ ./lib/tn/ ./lib/constructs/validator_set/
run: go test -count=1 ./config/ ./config/node/ ./lib/tn/ ./lib/constructs/validator_set/ ./stacks/

# Tests the check that release.yaml and publish-node-image.yaml run on each
# release page.
release-upgrade-line:
runs-on: ubuntu-latest
if: ${{ !github.event.pull_request.draft }}
steps:
- uses: actions/checkout@v4
- name: Test the upgrade line check
run: scripts/release/test-check-upgrade-line.sh

acceptance-test:
runs-on: ubuntu-latest
Expand Down
9 changes: 9 additions & 0 deletions .github/workflows/publish-node-image.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v4

# A release whose page does not open with the upgrade line gets no image,
# and latest does not move (docs/release-process.md). A manual run has no
# release page, so it skips the check.
- name: Check the upgrade line
if: github.event_name == 'release'
env:
RELEASE_BODY: ${{ github.event.release.body }}
run: printf '%s' "$RELEASE_BODY" | scripts/release/check-upgrade-line.sh

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

Expand Down
9 changes: 9 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ jobs:
with:
token: ${{ secrets.GITHUB_TOKEN }}

# A release whose page does not open with the upgrade line gets no binaries
# (docs/release-process.md). Editing the page to add the line runs this
# workflow again. A manual run has no release page, so it skips the check.
- name: Check the upgrade line
if: github.event_name == 'release'
env:
RELEASE_BODY: ${{ github.event.release.body }}
run: printf '%s' "$RELEASE_BODY" | scripts/release/check-upgrade-line.sh

- name: Set Version
id: vars
run: |
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ Steps and best practices for upgrading TN nodes:

[Node Upgrade Guide](./docs/node-upgrade-guide.md)

Each release page opens with a line that says whether your node must install the release and by which block. To get release notices, choose **Watch → Custom → Releases** on this repository. Maintainers publish releases as described in the [Release Process](./docs/release-process.md).


## SDKs

Expand Down
13 changes: 8 additions & 5 deletions deployments/infra/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ This directory contains infrastructure for automated AMI building using AWS EC2

- **Automated AMI Building**: EC2 Image Builder pipeline for creating TrufNetwork node AMIs
- **Docker-in-AMI**: Pre-installed Docker with docker-compose for container orchestration
- **Always-Latest Strategy**: Containers pull latest images on startup
- **Release Tags**: The node containers run the release in `TN_NODE_TAG` in `/opt/tn/.env`, and `sudo tn-node-update <release tag>` moves them to another release
- **MCP Server Integration**: PostgreSQL MCP server for AI agent access via SSE transport
- **Current Region Distribution**: AMI distribution in deployment region (multi-region planned)
- **GitHub Actions Integration**: Automated builds triggered by releases
Expand All @@ -345,9 +345,11 @@ This directory contains infrastructure for automated AMI building using AWS EC2
cd deployments/infra

# Deploy using isolated AMI CDK app (✅ Tested and Working)
# nodeTag is the node release the AMI runs, a tag from https://github.com/trufnetwork/node/releases
cdk --app 'go run ami-cdk.go' deploy \
--context stage=dev \
--context devPrefix=test-$(whoami) \
--context nodeTag=v2.6.8 \
--require-approval never
```

Expand Down Expand Up @@ -402,8 +404,8 @@ curl http://localhost:8000/ # MCP server (returns 404 - normal)
#### Update Node Images

```bash
# On AMI instances, update to latest images
sudo update-node
# On AMI instances, install a node release by its tag
sudo tn-node-update v2.6.9
```

### AMI Architecture
Expand All @@ -418,12 +420,13 @@ The AMI includes:
- PostgreSQL MCP Server (crystaldba/postgres-mcp:latest) - Will need to be adjusted later on
- **Configuration Scripts**:
- `/usr/local/bin/tn-node-configure` - Initial setup
- `/usr/local/bin/update-node` - Update to latest images
- `/usr/local/bin/tn-node-update` - Install a node release by its tag
- **SystemD Service**: `tn-node.service` for service management
- **File Structure**:
- `/opt/tn/` - Main application directory
- `/opt/tn/docker-compose.yml` - Container orchestration
- `/opt/tn/.env` - Environment configuration
- `/opt/tn/.env` - Environment configuration, with the node release in `TN_NODE_TAG`
- `/opt/tn/ami-node-tag` - The node release the AMI was built with

### Local Testing

Expand Down
64 changes: 64 additions & 0 deletions deployments/infra/stacks/ami-scripts/tn-node-configure.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/bin/bash
set -e
TN_DIR="${TN_DIR:-/opt/tn}"
PRIVATE_KEY=""
ENABLE_MCP=false
NETWORK=""
while [[ $# -gt 0 ]]; do
case $1 in
--private-key) PRIVATE_KEY="$2"; shift 2;;
--enable-mcp) ENABLE_MCP=true; shift;;
--network) NETWORK="$2"; shift 2;;
*) echo "Unknown option"; exit 1;;
esac
done
RECONFIGURE=false
[ -f "$TN_DIR/.env" ] && RECONFIGURE=true
if [ -n "$NETWORK" ]; then
CHAIN_ID="$NETWORK"
NETWORK_TYPE="custom"
else
CHAIN_ID="tn-v2.1"
NETWORK_TYPE="mainnet"
fi
cd "$TN_DIR"
# The node release this AMI was built with. A reconfigure keeps the release
# that tn-node-update set.
NODE_TAG=$(cat ami-node-tag)
if [ "$RECONFIGURE" = true ]; then
[ -n "$PRIVATE_KEY" ] && { echo "Error: Cannot change key"; exit 1; }
[ -n "$NETWORK" ] && { echo "Error: Cannot change network"; exit 1; }
grep -q "TN_PRIVATE_KEY=" .env && EXISTING_KEY=$(grep "TN_PRIVATE_KEY=" .env | cut -d'=' -f2)
grep -q "CHAIN_ID=" .env && CHAIN_ID=$(grep "CHAIN_ID=" .env | cut -d'=' -f2)
grep -q "NETWORK_TYPE=" .env && NETWORK_TYPE=$(grep "NETWORK_TYPE=" .env | cut -d'=' -f2)
grep -q "^TN_NODE_TAG=" .env && NODE_TAG=$(grep "^TN_NODE_TAG=" .env | tail -n 1 | cut -d'=' -f2)
sudo systemctl stop tn-node || true
sudo -u tn docker compose down || true
fi
cat > .env << ENVEOF
TN_NODE_TAG=$NODE_TAG
CHAIN_ID=$CHAIN_ID
NETWORK_TYPE=$NETWORK_TYPE
ENVEOF
[ "$ENABLE_MCP" = true ] && echo "COMPOSE_PROFILES=mcp" >> .env
if [ "$RECONFIGURE" = false ] && [ -n "$PRIVATE_KEY" ]; then
echo "TN_PRIVATE_KEY=$PRIVATE_KEY" >> .env
elif [ "$RECONFIGURE" = true ] && [ -n "$EXISTING_KEY" ]; then
echo "TN_PRIVATE_KEY=$EXISTING_KEY" >> .env
fi
sudo chown tn:tn .env
sudo chmod 600 .env
sudo systemctl daemon-reload
sudo systemctl enable tn-node
sudo systemctl start tn-node
MAX_WAIT=60
ELAPSED=0
while [ $ELAPSED -lt $MAX_WAIT ]; do
sudo -u tn docker compose ps --status running 2>/dev/null | grep -q tn-node && break
sleep 2
ELAPSED=$((ELAPSED + 2))
done
echo "Done. Status: $(sudo systemctl is-active tn-node)"
if [ "$ENABLE_MCP" = true ]; then
echo "MCP: http://$(curl -s --max-time 2 http://169.254.169.254/latest/meta-data/public-ipv4 || echo localhost):8000/sse"
fi
57 changes: 57 additions & 0 deletions deployments/infra/stacks/ami-scripts/tn-node-update.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/bin/bash
# Installs a TN node release on a node built from the TN AMI.
#
# sudo tn-node-update v2.6.9 runs the release tagged v2.6.9
# sudo tn-node-update pulls the release the node runs now
#
# The tag is stored as TN_NODE_TAG in /opt/tn/.env. The script pulls that node
# image and runs docker compose up for tn-node, which recreates the node
# containers only when their image or configuration changed. Postgres and the
# MCP server keep running.
set -euo pipefail

TN_DIR="${TN_DIR:-/opt/tn}"
ENV_FILE="$TN_DIR/.env"

if [ $# -gt 1 ]; then
echo "Usage: tn-node-update [release tag, for example v2.6.9]" >&2
exit 1
fi
if [ ! -f "$ENV_FILE" ]; then
echo "Error: $ENV_FILE not found. Run tn-node-configure first." >&2
exit 1
fi

CURRENT=$(grep '^TN_NODE_TAG=' "$ENV_FILE" | tail -n 1 | cut -d= -f2- || true)
if [ $# -eq 1 ]; then
TAG=$1
else
TAG=$CURRENT
if [ -z "$TAG" ]; then
echo "Error: $ENV_FILE has no TN_NODE_TAG. Pass the release tag, for example: tn-node-update v2.6.9" >&2
exit 1
fi
fi
# Release tags are on https://github.com/trufnetwork/node/releases. latest is
# refused so that the node runs the release its operator chose.
if ! [[ $TAG =~ ^[A-Za-z0-9_][A-Za-z0-9_.-]{0,127}$ ]] || [ "$TAG" = latest ]; then
echo "Error: '$TAG' is not a release tag. Pass one from https://github.com/trufnetwork/node/releases, for example v2.6.9" >&2
exit 1
fi

cd "$TN_DIR"
# Pull before .env changes, so a tag with no image leaves the node as it was.
sudo -u tn env TN_NODE_TAG="$TAG" docker compose pull tn-postgres-roles tn-node

if [ "$TAG" != "$CURRENT" ]; then
if grep -q '^TN_NODE_TAG=' "$ENV_FILE"; then
sed -i "s/^TN_NODE_TAG=.*/TN_NODE_TAG=$TAG/" "$ENV_FILE"
else
echo "TN_NODE_TAG=$TAG" >> "$ENV_FILE"
fi
chown tn:tn "$ENV_FILE"
chmod 600 "$ENV_FILE"
fi

sudo -u tn docker compose up -d tn-node
echo "tn-node runs ghcr.io/trufnetwork/node:$TAG"
Loading
Loading