Skip to content

Commit

Permalink
Add support for connector upgrades
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-chambers committed Jan 21, 2025
1 parent 2711113 commit e0990e8
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 1 deletion.
10 changes: 10 additions & 0 deletions .github/workflows/ndc-nodejs-lambda-connector.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,19 @@ jobs:
with:
images: ${{ env.DOCKER_REGISTRY }}/${{ env.DOCKER_IMAGE_NAME }}

- name: Get npm package version
id: get-npm-package-version
run: |
PACKAGE_VERSION=`npm version | sed -rn "2 s/.*: '([^']*)'.*/\1/g; 2 p"`
echo "package_version=${PACKAGE_VERSION}" >> $GITHUB_OUTPUT
shell: bash
working-directory: ./ndc-lambda-sdk

- uses: docker/build-push-action@v6
with:
context: .
build-args: |
CONNECTOR_VERSION=${{ steps.get-npm-package-version.outputs.package_version }}
push: ${{ startsWith(github.ref, 'refs/tags/v') }}
platforms: linux/amd64,linux/arm64
tags: ${{ steps.docker-metadata.outputs.tags }}
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ This changelog documents the changes between release versions.
## [Unreleased]
Changes to be included in the next upcoming release

- The connector now supports being upgraded with the forthcoming `ddn connector upgrade` command

## [1.10.0] - 2024-11-21
- The connector now exits during startup if there are compiler errors in the functions code. The compiler errors are printed to stderr. Previously the connector would print the errors and start "successfully", but with an empty schema. The new behaviour ensures that when the connector is used with `ddn connector introspect`, `ddn` is aware that a problem has occurred (because the connector fails to start) and will prompt the user to print the logs to see the compiler errors.

Expand Down
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
FROM node:20-alpine
ARG CONNECTOR_VERSION

RUN apk add jq curl

COPY /docker /scripts
RUN : "${CONNECTOR_VERSION:?Connector version must be set}"
RUN echo ${CONNECTOR_VERSION} > /scripts/CONNECTOR_VERSION

COPY /functions /functions
RUN /scripts/package-restore.sh
Expand Down
1 change: 1 addition & 0 deletions connector-definition/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ dist dist/.hasura-connector:

dist/.hasura-connector/connector-metadata.yaml: connector-metadata.yaml dist
cp -f connector-metadata.yaml dist/.hasura-connector
sed -i -E 's/\{\{VERSION\}\}/$(RELEASE_VERSION)/g' dist/.hasura-connector/connector-metadata.yaml

dist/.hasura-connector/Dockerfile: Dockerfile dist/.hasura-connector $(RELEASE_VERSION_DEP)
cp -f Dockerfile dist/.hasura-connector/
Expand Down
6 changes: 5 additions & 1 deletion connector-definition/connector-metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ nativeToolchainDefinition:
bash: ./watch.sh
powershell: ./watch.ps1
supportedEnvironmentVariables: []
commands: {}
commands:
upgradeConfiguration:
type: Dockerized
dockerImage: ghcr.io/hasura/ndc-nodejs-lambda:v{{VERSION}}
dockerCommand: ["/scripts/upgrade-connector.sh"]
dockerComposeWatch:
# Rebuild the container if a new package restore is required because package[-lock].json changed
- path: package.json
Expand Down
59 changes: 59 additions & 0 deletions docker/upgrade-connector.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/env sh
set -eu -o pipefail

connector_path="${HASURA_PLUGIN_CONNECTOR_CONTEXT_PATH:-/functions}"
target_connector_version="$(cat /scripts/CONNECTOR_VERSION)"

rm -rf /tmp/connector-upgrade
mkdir /tmp/connector-upgrade

# We copy the package.json and package-lock.json to a temporary directory
# so that we can upgrade the @hasura/ndc-lambda-sdk package without touching the
# existing node_modules directory. This is because the existing node_modules directory
# may have been installed on a different platform since it is being volume mounted
# into a Linux container
echo -n "Copying package.json, package-lock.json to a temporary location for upgrade... "
cd "$connector_path"
cp "package.json" "package-lock.json" /tmp/connector-upgrade/
echo "done"


cd /tmp/connector-upgrade

set +e
existing_connector_version=$(jq '.dependencies["@hasura/ndc-lambda-sdk"]' -r package.json)
exit_status=$?
if [ $exit_status -ne 0 ]; then
echo "Unable to read the @hasura/ndc-lambda-sdk version from your package.json"
echo "Please manually upgrade the @hasura/ndc-lambda-sdk package in your package.json to version $target_connector_version"
exit 1
fi

if [ $existing_connector_version = "null" ]; then
# This is very strange, their package.json must have the SDK installed but doesn't
# We'll roll with it and just install the package
echo "Missing the @hasura/ndc-lambda-sdk package in your package.json. Installing version $target_connector_version"
else
echo "Upgrading @hasura/ndc-lambda-sdk package from version $existing_connector_version to version $target_connector_version"
fi

npm install "@hasura/ndc-lambda-sdk@$target_connector_version" --save-exact --no-update-notifier
exit_status=$?
set -e

if [ $exit_status -ne 0 ]; then
echo "Failed to upgrade @hasura/ndc-lambda-sdk package to version $target_connector_version"
echo "Please manually upgrade the @hasura/ndc-lambda-sdk package in your package.json to version $target_connector_version"
exit 1
fi

# We overwrite the existing file contents instead of copying because this causes the existing
# file permissions/ownership to be retained, which is important since the container is likely
# running as a different user to what's running on the docker host machine
echo -n "Copying upgraded package.json, package-lock.json back to connector files... "
cat package.json > "$connector_path/package.json"
cat package-lock.json > "$connector_path/package-lock.json"
echo "done"

echo "Successfully upgraded @hasura/ndc-lambda-sdk package to version $target_connector_version"
echo "You may need to run 'npm install' to install the new dependencies locally"

0 comments on commit e0990e8

Please sign in to comment.