Skip to content

Commit

Permalink
Merge pull request #213 from 18F/stage
Browse files Browse the repository at this point in the history
Creating production workflow
  • Loading branch information
XavierMetichecchia authored Dec 4, 2023
2 parents d89937d + 9015518 commit f9c5cd2
Show file tree
Hide file tree
Showing 8 changed files with 334 additions and 48 deletions.
226 changes: 218 additions & 8 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
version: 2
version: 2.1
jobs:
build:
develop_deploy:
docker:
- image: cimg/node:16.19.1-browsers
environment:
Expand All @@ -11,23 +11,233 @@ jobs:
POSTGRES_DB: analytics-api-test
steps:
- checkout

- restore_cache:
keys:
- v1-dependencies-{{ checksum "package.json" }}
- v1-dependencies-

- run:
name: install dependencies
command: npm install

- save_cache:
paths:
- ./node_modules
key: v1-dependencies-{{ checksum "package.json" }}

- run:
name: run tests
command: npm test
name: run tests
command: npm test

- run:
name: deploy site
name: Replace Data URL in manifest.yml file when deploying to develop
command: |
if [ "${CIRCLE_BRANCH}" == "master" ]; then
bin/deploy-ci.sh
fi
sudo apt-get update
sudo apt-get install gettext
sed -i 's@name: analytics-reporter-api@name: analytics-reporter-api-develop@g' manifest.yml
sed -i 's@- analytics-reporter-database@- analytics-reporter-database-develop@g' manifest.yml
mv manifest.yml manifest.yml.src
envsubst < manifest.yml.src > manifest.yml
- run:
name: Run sed on entrypoint.sh when deploying to develop
command: |
sed -i 's@NEW_RELIC_APP_NAME="analytics-reporter-api"@NEW_RELIC_APP_NAME="analytics-reporter-api-develop"@g' entrypoint.sh
- run:
name: Delete Config.js and drop sufffix on Config.js.cloudgov
command: |
rm ./src/config.js
mv ./src/config.js.cloudgov ./src/config.js
rm knexfile.js
mv knexfile.js.cloudgov knexfile.js
- run:
name: Install CF CLI
command: |
sudo curl -v -L -o cf8-cli-installer_8.7.4_x86-64.deb 'https://packages.cloudfoundry.org/stable?release=debian64&version=8.7.4'
sudo dpkg -i cf8-cli-installer_8.7.4_x86-64.deb
- run:
name: deploy
command: |
set -e
# Log into cloud.gov
cf api api.fr.cloud.gov
cf login -u $CF_USERNAME_DEV -p $CF_PASSWORD_DEV -o gsa-opp-analytics -s analytics-dev
cat manifest.yml
cf push -f "./manifest.yml"
cf set-env analytics-reporter-api-develop API_DATA_GOV_SECRET "$API_SECRET"
cf restage analytics-reporter-api-develop
cf logout
staging_deploy:
docker:
- image: cimg/node:16.19.1-browsers
environment:
POSTGRES_USER: postgres
NODE_ENV: test
- image: circleci/postgres:9.5-alpine
environment:
POSTGRES_DB: analytics-api-test
steps:
- checkout

- restore_cache:
keys:
- v1-dependencies-{{ checksum "package.json" }}
- v1-dependencies-

- run:
name: install dependencies
command: npm install

- save_cache:
paths:
- ./node_modules
key: v1-dependencies-{{ checksum "package.json" }}

- run:
name: run tests
command: npm test

- run:
name: Replace Data URL in manifest.yml file when deploying to staging
command: |
sudo apt-get update
sudo apt-get install gettext
sed -i 's@name: analytics-reporter-api@name: analytics-reporter-api-staging@g' manifest.yml
sed -i 's@- analytics-reporter-database@- analytics-reporter-database-staging@g' manifest.yml
mv manifest.yml manifest.yml.src
envsubst < manifest.yml.src > manifest.yml
- run:
name: Run sed on entrypoint.sh when deploying to staging
command: |
sed -i 's@NEW_RELIC_APP_NAME="analytics-reporter-api"@NEW_RELIC_APP_NAME="analytics-reporter-api-staging"@g' entrypoint.sh
- run:
name: Delete Knexfile.js and drop sufffix on Knexfile.js.cloudgov
command: |
rm knexfile.js
mv knexfile.js.cloudgov knexfile.js
rm ./src/config.js
mv ./src/config.js.cloudgov ./src/config.js
- run:
name: Install CF CLI
command: |
sudo curl -v -L -o cf8-cli-installer_8.7.4_x86-64.deb 'https://packages.cloudfoundry.org/stable?release=debian64&version=8.7.4'
sudo dpkg -i cf8-cli-installer_8.7.4_x86-64.deb
- run:
name: deploy
command: |
set -e
# Log into cloud.gov
cf api api.fr.cloud.gov
cf login -u $CF_STAGING_SPACE_DEPLOYER_USERNAME -p $CF_STAGING_SPACE_DEPLOYER_PASSWORD -o gsa-opp-analytics -s analytics-staging
cat manifest.yml
cf push -f "./manifest.yml"
cf set-env analytics-reporter-api-staging API_DATA_GOV_SECRET "$API_SECRET"
cf restage analytics-reporter-api-staging
cf logout
main_deploy:
docker:
- image: cimg/node:16.19.1-browsers
environment:
POSTGRES_USER: postgres
NODE_ENV: test
- image: circleci/postgres:9.5-alpine
environment:
POSTGRES_DB: analytics-api-test
steps:
- checkout

- restore_cache:
keys:
- v1-dependencies-{{ checksum "package.json" }}
- v1-dependencies-

- run:
name: install dependencies
command: npm install

- save_cache:
paths:
- ./node_modules
key: v1-dependencies-{{ checksum "package.json" }}

- run:
name: run tests
command: npm test

- run:
name: Replace Data URL in manifest.yml file when deploying to production
command: |
sudo apt-get update
sudo apt-get install gettext
sed -i 's@name: analytics-reporter-api@name: analytics-reporter-api-production@g' manifest.yml
sed -i 's@- analytics-reporter-database@- analytics-reporter-database-production@g' manifest.yml
mv manifest.yml manifest.yml.src
envsubst < manifest.yml.src > manifest.yml
- run:
name: Run sed on entrypoint.sh when deploying to main
command: |
sed -i 's@NEW_RELIC_APP_NAME="analytics-reporter-api"@NEW_RELIC_APP_NAME="analytics-reporter-api-production"@g' entrypoint.sh
- run:
name: Delete Knexfile.js and drop sufffix on Knexfile.js.cloudgov
command: |
rm knexfile.js
mv knexfile.js.cloudgov knexfile.js
rm ./src/config.js
mv ./src/config.js.cloudgov ./src/config.js
- run:
name: Install CF CLI
command: |
sudo curl -v -L -o cf8-cli-installer_8.7.4_x86-64.deb 'https://packages.cloudfoundry.org/stable?release=debian64&version=8.7.4'
sudo dpkg -i cf8-cli-installer_8.7.4_x86-64.deb
- run:
name: deploy
command: |
set -e
# Log into cloud.gov
cf api api.fr.cloud.gov
cf login -u $CF_PRODUCTION_SPACE_DEPLOYER_USERNAME -p $CF_PRODUCTION_SPACE_DEPLOYER_PASSWORD -o gsa-opp-analytics -s analytics-prod
cat manifest.yml
cf push -f "./manifest.yml"
cf set-env analytics-reporter-api-production API_DATA_GOV_SECRET "$API_SECRET"
cf restage analytics-reporter-api-production
cf logout
workflows:
develop_workflow:
jobs:
- develop_deploy:
filters:
branches:
only:
- develop

staging:
jobs:
- staging_deploy:
filters:
branches:
only:
- stage

main_workflow:
jobs:
- main_deploy:
filters:
branches:
only:
- master

36 changes: 23 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,26 @@
# Analytics API

A system for publishing data retrieved from the Google Analytics API by the
[Analytics Reporter](https://github.com/18F/analytics-reporter). The analytics
API serves data written to a Progress database by the Analytics Reporter in
response to HTTP requests.
[Analytics Reporter](https://github.com/18F/analytics-reporter).
This Analytics API serves data written to a PostgreSQL database by the Analytics Reporter,
in response to HTTP requests.

# Setup

The Analytics API maintains the schema for the database that the Analytics
Reporter writes to. Because of this, the Analytics API must be setup and
This Analytics API maintains the schema for the database that the
[Analytics Reporter](https://github.com/18F/analytics-reporter)
writes to.
Thus, the Analytics API must be setup and
configured before the Analytics Reporter starts writing data.

First, the database needs to be created:
First, create the database:

```shell
createdb analytics-reporter
```

Once the database is created, the app can be cloned and the dependencies can be
installed via NPM. The install script has a postinstall hook that will migrate
Once the database is created, clone the app and install the dependencies via NPM.
The install script has a postinstall hook that will migrate
the database.

```shell
Expand All @@ -31,17 +33,17 @@ cd analytics-reporter-api
npm install
```

Once all of the dependencies are installed, the app can be started.
Once the dependencies are installed, the app can be started.

```shell
npm start
```

The API is not available at `http://localhost:4444/`
The API should now be available at `http://localhost:4444/`

Note that the API will not render any data until Analytics Reporter is
configured to write to the same database and run with the `--write-to-database`
option.
Note that the API will not render any data until
[Analytics Reporter](https://github.com/18F/analytics-reporter)
is configured to write to the same database and run with the `--write-to-database` option.

# Using the API

Expand Down Expand Up @@ -106,14 +108,21 @@ If you need to migrate the database, you can create a new migration via `knex`,
See [knex documentation](https://knexjs.org/#Installation-migrations) for more details.

# Running database migrations

## Locally

`npm run migrate`

## In production

In production, you can run database migrations via `cf run-task`. As with anything in production, be careful when doing this! First, try checking the current status of migrations using the `migrate:status` command

```
cf run-task analytics-reporter-api --command "knex migrate:status" --name check_migration_status
```

This will kick off a task - you can see the output by running:

```
cf logs analytics-reporter-api --recent
# the output will look something like...
Expand All @@ -127,6 +136,7 @@ cf logs analytics-reporter-api --recent
```

To actually run the migration, you would run:

```
cf run-task analytics-reporter-api --command "knex migrate:latest" --name run_db_migrations
```
Expand Down
5 changes: 5 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
export NEW_RELIC_APP_NAME="analytics-reporter-api"
export NODE_ENV="test"
export PATH="$PATH:/home/vcap/deps/0/bin"
npm start
2 changes: 1 addition & 1 deletion knexfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = {
host: process.env.POSTGRES_HOST,
user: process.env.POSTGRES_USER,
password: process.env.POSTGRES_PASSWORD,
database: process.env.POSTGRES_DATABASE
database: process.env.POSTGRES_DATABASE,
},
pool: {
min: 2,
Expand Down
39 changes: 39 additions & 0 deletions knexfile.js.cloudgov
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

const VCAP_SERVICES_JSON = JSON.parse(process.env.VCAP_SERVICES);

module.exports = {
development: {
client: 'postgresql',
connection: {
database: 'analytics-reporter'
}
},
production: {
client: 'postgresql',
connection: {
host : VCAP_SERVICES_JSON["aws-rds"][0]["credentials"]["host"],
user : VCAP_SERVICES_JSON["aws-rds"][0]["credentials"]["username"],
password : VCAP_SERVICES_JSON["aws-rds"][0]["credentials"]["password"],
database : VCAP_SERVICES_JSON["aws-rds"][0]["credentials"]["db_name"],
port: 5432,
ssl : true
},
pool: {
min: 2,
max: 10
},
migrations: {
tableName: 'knex_migrations'
}
},
test: {
client: 'postgresql',
connection: {
user: process.env.CIRCLECI ? 'postgres' : undefined,
database: 'analytics-api-test'
},
migrations: {
tableName: 'knex_migrations'
}
}
};
Loading

0 comments on commit f9c5cd2

Please sign in to comment.