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

api interop layer deploy #1825

Merged
merged 11 commits into from
Oct 1, 2024
22 changes: 20 additions & 2 deletions api-interop-layer/data/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,34 @@ import mariadb from "mariadb";

import { sleep } from "../util/sleep.js";

export const openDatabase = async () => {
const connectionDetails = {
const getDatabaseConnection = () => {
if (process.env.API_INTEROP_PRODUCTION) {
// we are in a cloud.gov environment and must retrieve credentials from
// the VCAP_SERVICES environment variable
const vcap = JSON.parse(process.env.VCAP_SERVICES);
const db = vcap["aws-rds"][0];
return {
user: db.credentials.username,
password: db.credentials.password,
database: db.credentials.name,
host: db.credentials.host,
port: db.credentials.port,
};
}

// we are in a local environment: offer defaults for ease of use
return {
user: process.env.DB_USERNAME ?? "drupal",
password: process.env.DB_PASSWORD ?? "drupal",
database: process.env.DB_NAME ?? "weathergov",
host: process.env.DB_HOST ?? "database",
port: process.env.DB_PORT ?? 3306,
ssl: { rejectUnauthorized: false },
};
}

export const openDatabase = async () => {
const connectionDetails = getDatabaseConnection();
return mariadb.createConnection(connectionDetails);
};

Expand Down
2 changes: 1 addition & 1 deletion api-interop-layer/newrelic.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ exports.config = {
/**
* Array of application names.
*/
app_name: [`api-interop-${process.env.APP_NAME || 'local'}`],
app_name: [`api-interop-${process.env.API_INTEROP_NAME || 'local'}`],
/**
* Your New Relic license key.
*/
Expand Down
4 changes: 4 additions & 0 deletions api-interop-layer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,9 @@
"chai-as-promised": "^8.0.0",
"mocha": "^10.7.3",
"sinon": "^18.0.0"
},
"engines": {
"node": "18.20.4",
"npm": "10.7.0"
}
}
7 changes: 6 additions & 1 deletion api-interop-layer/util/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ export const fetchAPIJson = async (path, { wait = sleep } = {}) =>
.catch(() => wait(204).then(() => internalFetch(path)))
.catch(() => wait(337).then(() => internalFetch(path)))
.catch((e) => {
logger.error(e.cause);
if (e instanceof SyntaxError) {
// this can happen if the API or proxy returns HTML
logger.error(`error retrieving ${path}: endpoint returned invalid JSON: ${e}`);
} else {
logger.error(`error retrieving ${path}: ${e.cause}`);
}
return { ...e.cause, error: true };
});

Expand Down
2 changes: 1 addition & 1 deletion docker-compose.test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ services:
- api-interop-layer-test
environment: # we want to extend, not override
API_URL: http://api-proxy-test:9081
INTEROP_URL: http://api-interop-layer-test:9082
API_INTEROP_URL: http://api-interop-layer-test:9082
DRUPAL_DB_HOST: database-test
profiles: !override ["test"]

Expand Down
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ services:
DB_HOST: database
DB_PORT: 3306
API_URL: http://api-proxy:8081
APP_NAME: ${APP_NAME:-local}
API_INTEROP_NAME: ${API_INTEROP_NAME:-local}
NEWRELIC_LICENSE: $NEWRELIC_LICENSE
networks:
- weather.gov
Expand Down Expand Up @@ -68,7 +68,7 @@ services:
- api-interop-layer
environment:
API_URL: http://api-proxy:8081
INTEROP_URL: http://api-interop-layer:8082
API_INTEROP_URL: http://api-interop-layer:8082
DRUPAL_DB_NAME: weathergov
DRUPAL_DB_USERNAME: drupal
DRUPAL_DB_PASSWORD: drupal
Expand Down
17 changes: 17 additions & 0 deletions manifests/manifest-james.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ default_config: &defaults
env:
PHP_INI_SCAN_DIR: /home/vcap/app/php/etc/:/home/vcap/app/php/etc/php.ini.d/
NEWRELIC_LICENSE: ((newrelic-license))
API_INTEROP_URL: https://api-weathergov-james.app.cloud.gov
services:
- database
- storage
Expand All @@ -34,3 +35,19 @@ applications:
ALLOWED_IPS: ((allowed-ips))
SPACE_NAME: james
random-route: false

- name: api-weathergov-james
stack: cflinuxfs4
memory: 256M
instances: 1
buildpacks:
- nodejs_buildpack
env:
NEWRELIC_LICENSE: ((newrelic-license))
API_INTEROP_PRODUCTION: true
API_INTEROP_NAME: james
health-check-type: process
path: ../api-interop-layer
random-route: false
services:
- database
24 changes: 24 additions & 0 deletions manifests/manifest.template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ default_config: &defaults
env:
PHP_INI_SCAN_DIR: /home/vcap/app/php/etc/:/home/vcap/app/php/etc/php.ini.d/
NEWRELIC_LICENSE: ((newrelic-license))
API_INTEROP_URL: https://api-weathergov-ENVIRONMENT.app.cloud.gov
services:
- database
- storage
Expand All @@ -34,3 +35,26 @@ applications:
ALLOWED_IPS: ((allowed-ips))
SPACE_NAME: ENVIRONMENT
random-route: false

# - name: cronish
# <<: *defaults
# no-route: true
# command: ./cronish.sh
# health-check-type: process
# memory: 128M

- name: api-weathergov-ENVIRONMENT
stack: cflinuxfs4
memory: 256M
instances: 1
buildpacks:
- nodejs_buildpack
env:
NEWRELIC_LICENSE: ((newrelic-license))
API_INTEROP_PRODUCTION: true
API_INTEROP_NAME: ENVIRONMENT
health-check-type: process
path: ../api-interop-layer
random-route: false
services:
- database
7 changes: 4 additions & 3 deletions web/modules/weather_data/weather_data.module
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ function weather_data_template_preprocess_default_variables_alter(&$variables)
$lon = $route->getParameter("lon");
$weatherMetadata["point"] = ["lat" => $lat, "lon" => $lon];

$baseUrl = getEnv("INTEROP_URL");
// this is required since we no longer can interpret api.weather.gov lat/lon directly
// note: the url must have a scheme (https:// or http://) and no trailing slash
$baseUrl = getEnv("API_INTEROP_URL");

$fetch = $container->get("http_client");

try {
$data = $fetch
->get("http://api-interop-layer:8082/point/$lat/$lon")
->get("$baseUrl/point/$lat/$lon")
->getBody();
} catch (Throwable $e) {
$data = '{"error":true}';
Expand Down
5 changes: 5 additions & 0 deletions web/sites/default/settings.cloudgov.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@
"https://weathergov-eric.app.cloud.gov";
break;

case "james":
$config["samlauth.authentication"]["sp_entity_id"] =
"https://weathergov-james.app.cloud.gov";
break;

case "staging":
$config["samlauth.authentication"]["sp_entity_id"] =
"https://weathergov-staging.app.cloud.gov";
Expand Down
Loading