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

Creating production workflow #213

Merged
merged 45 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
e31e751
cflinuxfs4 fix
Sep 18, 2023
1ba8980
Begin work for develop branch
Oct 17, 2023
3eef95e
Begin work for develop branch 2
Oct 17, 2023
dff8873
Merge pull request #206 from 18F/cflinuxfs4-fix
XavierMetichecchia Oct 17, 2023
109f533
Begin work for develop branch 3
Oct 17, 2023
5efca17
Begin work for develop branch 3
Oct 17, 2023
d443e47
Change db_export cli install command 3
Oct 17, 2023
0da5ae2
Change db_export cli install command 4
Oct 17, 2023
e955c2d
Change db_export cli install command 5
Oct 17, 2023
5ea6299
Change db_export cli install command 6
Oct 17, 2023
7420b9a
Change name?
Oct 19, 2023
078d37c
Check manifest file
Oct 19, 2023
50c93fd
Reformat mainfest file
Oct 19, 2023
97f8670
Try node 18
Oct 19, 2023
822f882
Try node 18 3
Oct 19, 2023
0b776b8
Try node 18 4
Oct 19, 2023
0f2da3b
Update all packages in package.json for testing
Oct 24, 2023
a00b460
Rework environmental variables for database
Nov 27, 2023
a444d8e
Fix start command
Nov 27, 2023
e0a99d8
Fix start command 2
Nov 27, 2023
fe3bd69
Using envsub to replace env var values in manifest.yml
Nov 28, 2023
f20163d
Add PG SSL env var
Nov 29, 2023
95f8f9a
Disable testing due to error
Nov 29, 2023
aeeb6de
Add gettext package for envsubst cmd in pipeline
Nov 29, 2023
68bc3f8
add port and ssl to postgres connection knexfile
Nov 29, 2023
47ba9cf
add port and ssl to postgres connection knexfile 2
Nov 29, 2023
fccb9fb
add port and ssl to postgres connection knexfile 3
Nov 29, 2023
fdfefa3
Uncomment testing
Nov 30, 2023
6ad91c2
minor updates to README
ryanwoldatwork Nov 30, 2023
0405b45
Change API variable name and set at end of deploy
Nov 30, 2023
75d2588
Merge branch 'develop' of https://github.com/18F/analytics-reporter-a…
Nov 30, 2023
1ac1be7
Add staging code
Nov 30, 2023
d3fc5cf
Add staging code 2
Nov 30, 2023
5168fed
Add staging code 3
Nov 30, 2023
f9fcf81
Merge pull request #207 from 18F/develop
XavierMetichecchia Nov 30, 2023
1c32098
Add staging code 4
Nov 30, 2023
57773fd
Merge pull request #208 from 18F/develop
XavierMetichecchia Nov 30, 2023
52f2d00
Make Cloudgov version of config.js instead of knexfile
Dec 1, 2023
2ef0ea8
Make Cloudgov version of config.js instead of knexfile 2
Dec 1, 2023
b4b4f62
Add back cloudgov knewxfile as well
Dec 1, 2023
858d980
Merge pull request #210 from 18F/develop
XavierMetichecchia Dec 1, 2023
6d335ab
Add back cloudgov knewxfile as well 2
Dec 1, 2023
fbf7727
Merge pull request #211 from 18F/develop
XavierMetichecchia Dec 1, 2023
cca4b99
Add production workflow
Dec 4, 2023
9015518
Merge pull request #212 from 18F/develop
XavierMetichecchia Dec 4, 2023
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
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