...

Text file src/edge-infra.dev/hack/postgres/justfile

Documentation: edge-infra.dev/hack/postgres

     1set dotenv-load
     2
     3kubectl := "bazel run --config=quiet //hack/tools:kubectl --"
     4
     5database_username := "${DATABASE_USERNAME:-edge-user}"
     6database_host := "${DATABASE_HOST:-localhost}"
     7database_port := "${DATABASE_PORT:-5432}"
     8database_name := "${DATABASE_NAME:-edge-db}"
     9database_password := "${DATABASE_PASSWORD:-Pa55word}"
    10
    11# Creates a postgres docker container to be used for L2 integration tests
    12up:
    13    docker run \
    14        --name postgres \
    15        --restart unless-stopped \
    16        -p "{{ database_port }}:5432" \
    17        -e "POSTGRES_PASSWORD={{ database_password }}" \
    18        -e "POSTGRES_USER={{ database_username }}" \
    19        -e "POSTGRES_DB={{ database_name }}" \
    20        -d \
    21        postgres
    22
    23# Updates your test/config.json with appropriate connection details for the postgres container
    24update-config:
    25    #!/usr/bin/env bash
    26    set -euo pipefail
    27
    28    # jq does not support update file in place, mimick this behaviour using a
    29    # temp file and move
    30    tempfile=$(mktemp)
    31
    32    # - Generate the expected configuration from the supplied env var ($want jq var)
    33    # - Check if this will overwrite any existing config in config.json
    34    #   - (. + $want) != ($want + .) (addition is not commutative in jq, so the resulting dicts will be different if addition is done in a different order when overwriting data)
    35    # - Return error and leave file unchanged if existing data will be overwrittend
    36    # - Otherwise populate temp file and move into place
    37    #
    38    # Want to update file in place if it is present, otherwise create new file
    39    ( cat ../../test/config.json 2>/dev/null || echo '{}' ) | \
    40    jq \
    41        --argjson level 2 \
    42        --arg username "{{ database_username }}" \
    43        --argjson port "{{ database_port }}" \
    44        --arg name "{{ database_name }}" \
    45        --arg host "{{ database_host }}" \
    46        --arg password "{{ database_password }}" \
    47        '{
    48            "integration-level": $level,
    49            "postgres-database": $name,
    50            "postgres-user": $username,
    51            "postgres-pass": $password,
    52            "postgres-port": $port,
    53            "postgres-host": $host,
    54            "postgres-k8s-host": "postgres.dockerhost.svc.cluster.local."
    55        } as $want | 
    56        if (. + $want) != ($want + .) 
    57        then "\nERROR: This will overwrite existing config.json values. Please manually update your config.json with the following contents\n\n" + ($want | tostring) + "\n" | halt_error
    58        else (. + $want) end' - > $tempfile && mv $tempfile ../../test/config.json || rm $tempfile
    59
    60
    61# The following uses https://github.com/kubernetes-sigs/kind/issues/1200#issuecomment-1304855791
    62
    63# k8s gives access to the host's docker from within the k8s cluster.
    64k8s:
    65    @just {{ if os() == "linux" { "_k8s-linux" } else if os() == "macos" { "_k8s-macos" } else { '_k8s-unsupported' } }}
    66
    67_k8s-unsupported:
    68    echo currently this platform ({{ os() }}) is unsupported && exit 1
    69
    70_k8s-linux:
    71    #!/usr/bin/env bash
    72    just ../kind/ensure-kind
    73    dockerip="$(docker network inspect bridge | jq -r '.[0].IPAM.Config | .[0].Gateway')"
    74    sed -e 's/DOCKERIP/'"$dockerip"'/g' -e 's/DATABASE_PORT/'"{{ database_port }}"'/g' service-linux.yaml | kubectl apply -f -
    75
    76_k8s-macos:
    77    #!/usr/bin/env bash
    78    just ../kind/ensure-kind
    79    sed 's/DATABASE_PORT/'"{{ database_port }}"'/g' service-macos.yaml | kubectl apply -f -

View as plain text