Skip to content

Commit

Permalink
updates to packaging
Browse files Browse the repository at this point in the history
  • Loading branch information
TristenHarr committed Jul 15, 2024
1 parent fc84b77 commit b08d90e
Show file tree
Hide file tree
Showing 28 changed files with 513 additions and 292 deletions.
98 changes: 98 additions & 0 deletions .github/workflows/ndc-python-lambda-connector.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
name: "ndc-python-lambda connector"
on:
pull_request:
branches:
- main
- test-ci/**
push:
branches:
- 'main'
- test-ci/**
tags:
- v**

env:
DOCKER_REGISTRY: ghcr.io
DOCKER_IMAGE_NAME: hasura/ndc-python-lambda

jobs:
build-and-test:
name: Build and test ndc-lambda-sdk
defaults:
run:
working-directory: ./ndc-lambda-sdk
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v4
with:
python-version: '3.9'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Run tests
run: pytest

docker:
name: Build base docker image
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: docker/login-action@v3
with:
registry: ${{ env.DOCKER_REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: docker-metadata
uses: docker/metadata-action@v5
with:
images: ${{ env.DOCKER_REGISTRY }}/${{ env.DOCKER_IMAGE_NAME }}
- uses: docker/build-push-action@v5
with:
context: .
push: ${{ startsWith(github.ref, 'refs/tags/v') }}
tags: ${{ steps.docker-metadata.outputs.tags }}
labels: ${{ steps.docker-metadata.outputs.labels }}

release-connector:
name: Release connector
defaults:
run:
working-directory: ./connector-definition
runs-on: ubuntu-latest
needs:
- build-and-test
- docker
if: ${{ startsWith(github.ref, 'refs/tags/v') }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v4
with:
python-version: '3.9'
- name: Build connector definition
run: make build
- uses: actions/upload-artifact@v4
with:
name: connector-definition.tgz
path: ./connector-definition/dist/connector-definition.tgz
compression-level: 0 # Already compressed
- name: Get version from tag
id: get-version
run: |
echo "tagged_version=${GITHUB_REF#refs/tags/v}" >> $GITHUB_OUTPUT
shell: bash
- uses: mindsers/changelog-reader-action@v2
id: changelog-reader
with:
version: ${{ steps.get-version.outputs.tagged_version }}
path: ./CHANGELOG.md
- uses: softprops/action-gh-release@v1
with:
draft: false
tag_name: v${{ steps.get-version.outputs.tagged_version }}
body: ${{ steps.changelog-reader.outputs.changes }}
files: |
./connector-definition/dist/connector-definition.tgz
fail_on_unmatched_files: true
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,7 @@ There will be support for built-in scalar types (int, float, str, bool) and also

## TODO: Allow adding of async queries/mutations?

## TODO: Add Pydantic type introspections
## TODO: Add Pydantic type introspections


python3 main.py serve --configuration . --port 8087
3 changes: 3 additions & 0 deletions connector-definition/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.hasura-connector/
*.hml
__pycache__
9 changes: 9 additions & 0 deletions connector-definition/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM ghcr.io/hasura/ndc-python-lambda:v{{VERSION}}

COPY requirements.txt /functions/

WORKDIR /functions
RUN --mount=type=cache,target=/root/.cache/pip \
pip install -r requirements.txt

COPY ./ /functions
60 changes: 60 additions & 0 deletions connector-definition/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Set the default target to 'build'
.DEFAULT_GOAL := build

# Use bash as the shell for executing commands
SHELL = /usr/bin/env bash

# Extract the version from the latest git tag
RELEASE_VERSION := $(shell git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0")

# Define 'build' and 'clean' as phony targets (not associated with files)
.PHONY: build
.PHONY: clean

# The 'build' target depends on creating the connector-definition.tgz file
build: dist/connector-definition.tgz

# The 'clean' target removes the dist directory
clean:
rm -rf dist

# Create the dist and dist/.hasura-connector directories
dist:
mkdir -p dist
mkdir -p dist/.hasura-connector

# Copy connector-metadata.yaml to dist/.hasura-connector
dist/.hasura-connector/connector-metadata.yaml: connector-metadata.yaml dist
cp -f connector-metadata.yaml dist/.hasura-connector

# Copy Dockerfile to dist/.hasura-connector and replace the version
dist/.hasura-connector/Dockerfile: Dockerfile dist
cp -f Dockerfile dist/.hasura-connector/
sed -i '' 's/{{VERSION}}/$(subst v,,$(RELEASE_VERSION))/g' dist/.hasura-connector/Dockerfile
# sed -i 's/{{VERSION}}/$(subst v,,$(RELEASE_VERSION))/g' dist/.hasura-connector/Dockerfile

# Copy .dockerignore to dist/.hasura-connector
dist/.hasura-connector/.dockerignore: .dockerignore dist
cp -f .dockerignore dist/.hasura-connector/

# Find all files in the template directory
template_files := $(wildcard template/*)
# Create corresponding paths for template files in the dist directory
dist_template_files := $(patsubst template/%,dist/%,$(template_files))

# Copy all template files to the dist directory
$(dist_template_files): $(template_files)
cp -f $(template_files) dist/

# Find all files in the scripts directory
script_files := $(wildcard scripts/*)
# Create corresponding paths for script files in dist/.hasura-connector
dist_script_files := $(patsubst scripts/%,dist/.hasura-connector/%,$(script_files))

# Copy all script files to dist/.hasura-connector
$(dist_script_files): $(script_files)
cp -f $(script_files) dist/.hasura-connector/

# Create the final connector-definition.tgz file
dist/connector-definition.tgz: dist/.hasura-connector/connector-metadata.yaml dist/.hasura-connector/Dockerfile dist/.hasura-connector/.dockerignore $(dist_template_files) $(dist_script_files)
cd dist && tar -czvf connector-definition.tgz *
23 changes: 23 additions & 0 deletions connector-definition/connector-metadata.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
packagingDefinition:
type: ManagedDockerBuild
nativeToolchainDefinition:
commands:
start:
type: ShellScript
bash: ./start.sh
powershell: ./start.ps1
watch:
type: ShellScript
bash: ./watch.sh
powershell: ./start.ps1
supportedEnvironmentVariables: []
commands: {}
dockerComposeWatch:
# Rebuild the container if a new package restore is required because requirements.txt changed
- path: requirements.txt
target: /functions/requirements.txt
action: rebuild
# For any other file change, simply copy it into the existing container and restart it
- path: ./
target: /functions
action: sync+restart
3 changes: 3 additions & 0 deletions connector-definition/dist/.hasura-connector/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.hasura-connector/
*.hml
__pycache__
9 changes: 9 additions & 0 deletions connector-definition/dist/.hasura-connector/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM ghcr.io/hasura/ndc-python-lambda:v0.0.0

COPY requirements.txt /functions/

WORKDIR /functions
RUN --mount=type=cache,target=/root/.cache/pip \
pip install -r requirements.txt

COPY ./ /functions
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
packagingDefinition:
type: ManagedDockerBuild
nativeToolchainDefinition:
commands:
start:
type: ShellScript
bash: ./start.sh
powershell: ./start.ps1
watch:
type: ShellScript
bash: ./watch.sh
powershell: ./start.ps1
supportedEnvironmentVariables: []
commands: {}
dockerComposeWatch:
# Rebuild the container if a new package restore is required because requirements.txt changed
- path: requirements.txt
target: /functions/requirements.txt
action: rebuild
# For any other file change, simply copy it into the existing container and restart it
- path: ./
target: /functions
action: sync+restart
18 changes: 18 additions & 0 deletions connector-definition/dist/.hasura-connector/start.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
$ErrorActionPreference = "Stop"

$scriptDir = Get-Location

& ./check-reqs.ps1

Push-Location $env:HASURA_PLUGIN_CONNECTOR_CONTEXT_PATH
try {
# Activate virtual environment if it exists
if (Test-Path "venv\Scripts\Activate.ps1") {
.\venv\Scripts\Activate.ps1
}

# Run the Python script with the serve command
python3 connector-definition/template/functions.py serve --configuration ./
} finally {
Pop-Location
}
16 changes: 16 additions & 0 deletions connector-definition/dist/.hasura-connector/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env bash
set -eu -o pipefail

script_dir=$(pwd)

./check-reqs.sh

cd $HASURA_PLUGIN_CONNECTOR_CONTEXT_PATH

# Activate virtual environment if it exists
if [ -f "venv/bin/activate" ]; then
source venv/bin/activate
fi

# Run the Python script with the serve command
python3 connector-definition/template/functions.py serve --configuration ./
18 changes: 18 additions & 0 deletions connector-definition/dist/.hasura-connector/watch.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
$ErrorActionPreference = "Stop"

$scriptDir = Get-Location

& ./check-reqs.ps1

Push-Location $env:HASURA_PLUGIN_CONNECTOR_CONTEXT_PATH
try {
# Activate virtual environment if it exists
if (Test-Path "venv\Scripts\Activate.ps1") {
.\venv\Scripts\Activate.ps1
}

# Run watchdog to watch for file changes and restart the Python script
watchmedo auto-restart --pattern="*.py" --recursive -- python3 connector-definition/template/functions.py serve --configuration ./
} finally {
Pop-Location
}
16 changes: 16 additions & 0 deletions connector-definition/dist/.hasura-connector/watch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env bash
set -eu -o pipefail

script_dir=$(pwd)

./check-reqs.sh

cd $HASURA_PLUGIN_CONNECTOR_CONTEXT_PATH

# Activate virtual environment if it exists
if [ -f "venv/bin/activate" ]; then
source venv/bin/activate
fi

# Run watchdog to watch for file changes and restart the Python script
watchmedo auto-restart --pattern="*.py" --recursive -- python3 connector-definition/template/functions.py serve --configuration ./
Binary file added connector-definition/dist/connector-definition.tgz
Binary file not shown.
52 changes: 52 additions & 0 deletions connector-definition/dist/functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from hasura_ndc import start
from hasura_ndc.instrumentation import with_active_span
from opentelemetry.trace import get_tracer
from hasura_ndc.function_connector import FunctionConnector
from pydantic import BaseModel

connector = FunctionConnector()
tracer = get_tracer("ndc-sdk-python.server")

@connector.register_query
def do_the_thing(x: int):
return f"Hello World {x}"


@connector.register_mutation
def some_mutation_function(arg1: str,
arg2: int):
return f"Hey {arg1} {arg2}"

@connector.register_query
async def my_query(x: str) -> str:
return await with_active_span(
tracer,
"My Span",
lambda span: f"My string is {x}",
{"attr": "value"}
)

@connector.register_query
async def my_query2(x: str):
async def f(span):
# return f"My string is {x}"
return {
"hey": "x",
"var": x,
10.1: 10.1,
"dict": {
1.0: 10
},
"floatables": [1.234, 10, "yep"]
}

return await with_active_span(
tracer,
"My Span",
f,
{"attr": "value"}
)


if __name__ == "__main__":
start(connector)
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fastapi==0.110.2
googleapis-common-protos==1.63.0
grpcio==1.62.2
h11==0.14.0
hasura-ndc==0.11
hasura-ndc==0.15
idna==3.7
importlib-metadata==7.0.0
opentelemetry-api==1.24.0
Expand All @@ -34,5 +34,6 @@ starlette==0.37.2
typing_extensions==4.11.0
urllib3==2.2.1
uvicorn==0.29.0
watchdog==4.0.1
wrapt==1.16.0
zipp==3.18.1
Loading

0 comments on commit b08d90e

Please sign in to comment.