Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update runtimes and add pgstac customization options #100

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
62 changes: 52 additions & 10 deletions lib/database/bootstrapper_runtime/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""

import json
import logging

import boto3
import httpx
Expand All @@ -13,6 +14,8 @@
from pypgstac.db import PgstacDB
from pypgstac.migrate import Migrate

logger = logging.getLogger("eoapi-bootstrap")


def send(
event,
Expand Down Expand Up @@ -66,8 +69,10 @@ def send(
timeout=30,
)
print("Status code: " + response.status_code)
logger.debug(f"OK - Status code: {response.status_code}")
except Exception as e:
print("send(..) failed executing httpx.put(..): " + str(e))
logger.debug(f"NOK - failed executing PUT requests: {e}")
emileten marked this conversation as resolved.
Show resolved Hide resolved


def get_secret(secret_name):
Expand Down Expand Up @@ -140,6 +145,33 @@ def register_extensions(cursor) -> None:
cursor.execute(sql.SQL("CREATE EXTENSION IF NOT EXISTS postgis;"))


###############################################################################
# PgSTAC Customization
###############################################################################
def customization(cursor, params) -> None:
"""
CUSTOMIZED YOUR PGSTAC DATABASE

ref: https://github.com/stac-utils/pgstac/blob/main/docs/src/pgstac.md

"""
if params.get("context", False):
# Add CONTEXT=ON
pgstac_settings = """
INSERT INTO pgstac_settings (name, value)
VALUES ('context', 'on')
ON CONFLICT ON CONSTRAINT pgstac_settings_pkey DO UPDATE SET value = excluded.value;"""
cursor.execute(sql.SQL(pgstac_settings))

if params.get("mosaic_index", False):
# Create index of searches with `mosaic`` type
cursor.execute(
sql.SQL(
"CREATE INDEX IF NOT EXISTS searches_mosaic ON searches ((true)) WHERE metadata->>'type'='mosaic';"
)
)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is taken from eoapi repo



def handler(event, context):
"""Lambda Handler."""
print(f"Handling {event}")
Expand Down Expand Up @@ -203,12 +235,12 @@ def handler(event, context):
)
)

pgdb = PgstacDB(dsn=stac_db_admin_dsn, debug=True)
print(f"Current {pgdb.version=}")
with PgstacDB(dsn=stac_db_admin_dsn, debug=True) as pgdb:
print(f"Current {pgdb.version=}")

# As admin, run migrations
print("Running migrations...")
Migrate(pgdb).run_migration(params["pgstac_version"])
# As admin, run migrations
print("Running migrations...")
Migrate(pgdb).run_migration(params["pgstac_version"])
emileten marked this conversation as resolved.
Show resolved Hide resolved

# Assign appropriate permissions to user (requires pgSTAC migrations to have run)
with psycopg.connect(admin_db_conninfo, autocommit=True) as conn:
Expand All @@ -220,17 +252,27 @@ def handler(event, context):
username=user_params["username"],
)

print("Adding mosaic index...")
print("Customize PgSTAC database...")
with psycopg.connect(
stac_db_admin_dsn,
autocommit=True,
options="-c search_path=pgstac,public -c application_name=pgstac",
) as conn:
conn.execute(
sql.SQL(
"CREATE INDEX IF NOT EXISTS searches_mosaic ON searches ((true)) WHERE metadata->>'type'='mosaic';"
)
with conn.cursor() as cur:
customization(cursor=cur, params=params)

# Make sure the user can access the database
stac_db_user_dsn = (
"postgresql://{user}:{password}@{host}:{port}/{dbname}".format(
dbname=user_params.get("dbname", "postgres"),
user=user_params["username"],
password=user_params["password"],
host=connection_params["host"],
port=connection_params["port"],
)
)
with PgstacDB(dsn=stac_db_user_dsn, debug=True) as pgdb:
print(f"User can access pgstac: {pgdb.version}")
emileten marked this conversation as resolved.
Show resolved Hide resolved

except Exception as e:
print(f"Unable to bootstrap database with exception={e}")
Expand Down
16 changes: 8 additions & 8 deletions lib/database/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ import { Construct } from "constructs";
import { CustomLambdaFunctionProps } from "../utils";

const instanceSizes: Record<string, number> = require("./instance-memory.json");
const DEFAULT_PGSTAC_VERSION = "0.7.10";
const DEFAULT_PGSTAC_VERSION = "0.8.4";
vincentsarago marked this conversation as resolved.
Show resolved Hide resolved

let defaultPgSTACCustomOptions :{ [key: string]: any } = {
"context": true,
"mosaic_index": true
}

function hasVpc(
instance: rds.DatabaseInstance | rds.IDatabaseInstance
Expand Down Expand Up @@ -106,12 +111,7 @@ export class PgStacDatabase extends Construct {
// connect to database
this.db.connections.allowFrom(handler, ec2.Port.tcp(5432));

let customResourceProperties : { [key: string]: any} = {};

// if customResourceProperties are provided, fill in the values.
if (props.customResourceProperties) {
Object.assign(customResourceProperties, props.customResourceProperties);
}
let customResourceProperties : { [key: string]: any} = props.customResourceProperties ? { ...defaultPgSTACCustomOptions, ...props.customResourceProperties } : defaultPgSTACCustomOptions;

// update properties
customResourceProperties["conn_secret_arn"] = this.db.secret!.secretArn;
Expand Down Expand Up @@ -195,7 +195,7 @@ export interface PgStacDatabaseProps extends rds.DatabaseInstanceProps {
/**
* Lambda function Custom Resource properties. A custom resource property is going to be created
* to trigger the boostrapping lambda function. This parameter allows the user to specify additional properties
* on top of the defaults ones.
* on top of the defaults ones.
*
*/
readonly customResourceProperties?: {
Expand Down
5 changes: 3 additions & 2 deletions lib/tipg-api/runtime/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ FROM --platform=linux/amd64 public.ecr.aws/lambda/python:${PYTHON_VERSION}
WORKDIR /tmp
RUN python -m pip install pip -U

RUN python -m pip install tipg==0.3.1 "mangum>=0.14,<0.15" -t /asset --no-binary pydantic
COPY runtime/requirements.txt requirements.txt
RUN python -m pip install -r requirements.txt "mangum>=0.14,<0.15" -t /asset --no-binary pydantic

# Reduce package size and remove useless files
RUN cd /asset && find . -type f -name '*.pyc' | while read f; do n=$(echo $f | sed 's/__pycache__\///' | sed 's/.cpython-[0-9]*//'); cp $f $n; done;
Expand All @@ -14,4 +15,4 @@ RUN find /asset -type d -a -name 'tests' -print0 | xargs -0 rm -rf

COPY runtime/src/*.py /asset/

CMD ["echo", "hello world"]
CMD ["echo", "hello world"]
1 change: 1 addition & 0 deletions lib/tipg-api/runtime/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tipg==0.6.3
4 changes: 2 additions & 2 deletions lib/titiler-pgstac-api/runtime/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
titiler.pgstac==0.5.1
psycopg[binary, pool]
titiler.pgstac==1.2.2
psycopg[binary, pool]
Loading