diff --git a/README.md b/README.md index cd53f32..3861ac0 100755 --- a/README.md +++ b/README.md @@ -11,8 +11,8 @@ This app provides basic crypto and blockchain-related functions including: This service is mostly stateless - it holds no user data or keys - but it does hold developer (app) registrations in order to issue api keys ## Starting service -- Create .env file in project's root directory - copy example.env to .env and replace values -- Generate a public/private Key pair for server's use (see example.env BASE_PUBLIC_KEY, BASE_PRIVATE_KEY) +- Create .env file in project's root directory - copy .env.example to .env and replace values +- Generate a public/private Key pair for server's use (see .env.example BASE_PUBLIC_KEY, BASE_PRIVATE_KEY) - Important: Run Node 12.0 or higher on your local machine - ### `npm run dev` diff --git a/circle.yml b/circle.yml index eea71b2..3f680d2 100644 --- a/circle.yml +++ b/circle.yml @@ -6,85 +6,107 @@ # than the prescribed lifecycle of 1.0. In general, we recommend using this generated # configuration as a reference rather than using it in production, though in most # cases it should duplicate the execution of your original 1.0 config. -version: 2 +version: 2.1 + +workflows: + build_deploy: + jobs: + - build + - deploy: + requires: + - build + context: + - gcloud-<< pipeline.git.branch >> + filters: + branches: + only: + - master + - staging + - dev +orbs: + node: circleci/node@5.0.0 + jq: circleci/jq@2.2.0 + jobs: build: - working_directory: ~/API-market/crypto-service + working_directory: ~/API-market/crypto-service/src parallelism: 1 - shell: /bin/bash --login + shell: /bin/bash --login -e environment: MONGO_URL: mongodb://localhost:27017/data - CIRCLE_ARTIFACTS: /tmp/circleci-artifacts - CIRCLE_TEST_REPORTS: /tmp/circleci-test-results - PYTHONPATH: ${PYTHONPATH}:${HOME}/google_appengine GCLOUD_PROJECT: cryptoservice SERVICE_VERSION: 1 # In CircleCI 2.0 you can now specify your own image, or use one of our pre-configured images. # To see the list of pre-built images that CircleCI provides for most common languages see # https://circleci.com/docs/2.0/circleci-images/ docker: - - image: circleci/build-image:ubuntu-14.04-XXL-upstart-1189-5614f37 + - image: cimg/base:stable-18.04 - image: circleci/mongo:4.2.8 steps: # Machine Setup - - checkout - # 'See docs on artifact collection here https://circleci.com/docs/2.0/artifacts/' - - run: mkdir -p $CIRCLE_ARTIFACTS $CIRCLE_TEST_REPORTS - - run: - working_directory: ~/API-market/crypto-service - command: nvm install 12.12.0 && nvm alias default 12.12.0 - # Dependencies - # This would typically go in either a build or a build-and-test job when using workflows - # Restore the dependency cache - - restore_cache: - keys: - # This branch if available - - v1-dep-{{ .Branch }}- - # Default branch if not - - v1-dep-master- - # Any branch if there are none on the default branch - this should be unnecessary if you have your default branch configured correctly - - v1-dep- + - checkout: + path: ".." + - node/install: + node-version: '12.12.0' + # install npm dependencies and run build script + - node/install-packages: + override-ci-command: npm install + # Linter + - run: npm run lint + # Unit Tests + - run: npm test + - persist_to_workspace: + root: ~/API-market + paths: + - crypto-service + deploy: + working_directory: ~/API-market/crypto-service/src + parallelism: 1 + shell: /bin/bash --login -e + docker: + - image: google/cloud-sdk:363.0.0 + steps: + - attach_workspace: + at: ~/API-market + - node/install: + node-version: '12.12.0' + - jq/install # install gcloud and login to it - run: - working_directory: ~/API-market/crypto-service/src + working_directory: ~/ command: | - if [ "$CIRCLE_BRANCH" == "master" ] || [ "$CIRCLE_BRANCH" == "staging" ] - then - curl -o $HOME/google_appengine_1.9.30.zip https://storage.googleapis.com/appengine-sdks/featured/google_appengine_1.9.30.zip - unzip -q -d $HOME $HOME/google_appengine_1.9.30.zip - echo $GCLOUD_SERVICE_KEY | base64 --decode > ${HOME}/gcloud-service-key.json - echo Y | gcloud components update app - gcloud auth activate-service-account --key-file ${HOME}/gcloud-service-key.json - gcloud config set project $GCLOUD_PROJECT - fi - - # copy .env file from circleci injected environmental variables + echo $GCLOUD_SERVICE_ACCT_KEY | base64 --decode > ${HOME}/gcloud-service-key.json + gcloud auth activate-service-account --key-file ${HOME}/gcloud-service-key.json + gcloud config set project $GOOGLE_PROJECT_ID + gcloud config set app/cloud_build_timeout 2100 - run: - working_directory: ~/API-market/crypto-service/src command: | - if [ "$CIRCLE_BRANCH" == "master" ] - then - cp -rf ../scripts/config/prod.app.yaml ./app.yaml - echo $ENV_PROD | base64 --decode >> ./.env - echo $ENV_TEST_PROD | base64 --decode >> ./backend/test/config/.env.test - fi - if [ "$CIRCLE_BRANCH" == "staging" ] - then - cp -rf ../scripts/config/staging.app.yaml ./app.yaml - echo $ENV_STAGING | base64 --decode >> ./.env - echo $ENV_TEST_STAGING | base64 --decode >> ./backend/test/config/.env.test + # use new variable to map master -> prod + DEPLOY_ENV=$CIRCLE_BRANCH + if [ "$CIRCLE_BRANCH" == "master" ]; then + DEPLOY_ENV=prod fi - if [ "$CIRCLE_BRANCH" != "master" ] && [ "$CIRCLE_BRANCH" != "staging" ] - then - cp -rf ../scripts/config/staging.app.yaml ./app.yaml - echo $ENV_STAGING | base64 --decode >> ./.env - echo $ENV_TEST_STAGING | base64 --decode >> ./backend/test/config/.env.test + + cp -rf ../scripts/config/${DEPLOY_ENV}.app.yaml ./app.yaml + # process env_version.json + SECRET_VERSION=`cat env_version.json | jq -r ".${DEPLOY_ENV}.version"` + ENV_VERSION=`cat env_version.json | jq -r '.envVersion'` + # pull this from GCP + gcloud secrets versions access ${SECRET_VERSION} \ + --secret="$SECRET_NAME_CRYPTO_SERVICE" \ + --format='get(payload.data)' | tr '_-' '/+' | base64 -d > .env + echo "Pulled down secret into .env" + # disable fail on error to output meaningful message + set +e + # grep ENV_VERSION is in env. This will fail if the incorrect version is not in the .env file + CHECK_ENV=`grep $ENV_VERSION .env` + if [ "$?" == "1" ]; then + echo ".env SCHEMA does not match: ${ENV_VERSION} not found in version: ${SECRET_VERSION}"; + exit 1; fi - - # create deploy_version file + # create deploy_version file - run: - working_directory: ~/API-market/crypto-service/src command: | ENV_HASH=$(sha1sum ./.env | awk '{print $1}') echo ENV_HASH = "\"$ENV_HASH\"" >> ./deploy-version @@ -93,60 +115,13 @@ jobs: CURRENTDATE=`date +"%A, %b %d, %Y %I:%M %p"` echo DEPLOY_DATE = "\"$CURRENTDATE\"" >> ./deploy-version - - # install npm dependencies and run build script - - run: - working_directory: ~/API-market/crypto-service/src - command: | - npm install - npm run build - # copy the folders we will deploy to a dist folder - - # install npm dependencies and run build script - - # Linter - - run: - working_directory: ~/API-market/crypto-service/src - command: npm run lint - - # Unit Tests - - run: - working_directory: ~/API-market/crypto-service/src - command: npm test + # build + - run: npm run build # deploy to Google Cloud - run: - working_directory: ~/API-market/crypto-service/src + no_output_timeout: 15m # timeout the build if the deploy hangs for longer than 15 minutes command: | - if [ "$CIRCLE_BRANCH" == "master" ] || [ "$CIRCLE_BRANCH" == "staging" ] - then - rm -rf ./node_modules - # deploy to AppEngine - #- ../scripts/deploy.sh --version=$SERVICE_VERSION - ../scripts/deploy.sh - fi - - # Save dependency cache - - save_cache: - key: v1-dep-{{ .Branch }}-{{ epoch }} - paths: - # This is a broad list of cache paths to include many possible development environments - # You can probably delete some of these entries - - vendor/bundle - - ~/virtualenvs - - ~/.m2 - - ~/.ivy2 - - ~/.bundle - - ~/.go_workspace - - ~/.gradle - - ~/.cache/bower - - # Teardown - # Save test results - - store_test_results: - path: /tmp/circleci-test-results - # Save artifacts - - store_artifacts: - path: /tmp/circleci-artifacts - - store_artifacts: - path: /tmp/circleci-test-results + rm -rf ./node_modules + # deploy to AppEngine + ../scripts/deploy.sh \ No newline at end of file diff --git a/dispatch.yaml b/dispatch.yaml index 4c85c5d..b3b71cf 100644 --- a/dispatch.yaml +++ b/dispatch.yaml @@ -17,6 +17,10 @@ dispatch: - url: "staging.api.crypto-service.io/*" service: crypto-service-staging + # dev - service +- url: "dev.api.crypto-service.io/*" + service: crypto-service-dev + # web - url: "wwww.crypto-service.io/*" service: default diff --git a/scripts/config/dev.app.yaml b/scripts/config/dev.app.yaml new file mode 100755 index 0000000..4f3050b --- /dev/null +++ b/scripts/config/dev.app.yaml @@ -0,0 +1,22 @@ +# Copyright 2017, Google, Inc. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# [START app_yaml] +runtime: nodejs +env: flex +service: crypto-service-dev +resources: + cpu: 1 + memory_gb: 1.5 + +# [END app_yaml] diff --git a/src/example.env b/src/.env.example similarity index 93% rename from src/example.env rename to src/.env.example index 3a05bea..90772e6 100644 --- a/src/example.env +++ b/src/.env.example @@ -1,7 +1,7 @@ ### CRYPTO-SERVICE STAGING .ENV ### Last Change YYYY-MM-DD_HH:MM (in PST) -ENV_VERSION = "2021-01-01_09:00" +ENV_VERSION = "2020-01-24_09:00" PORT = 8080 ENVIRONMENT = "production" diff --git a/src/env_version.json b/src/env_version.json new file mode 100644 index 0000000..3767320 --- /dev/null +++ b/src/env_version.json @@ -0,0 +1,12 @@ +{ + "envVersion": "2020-01-24_09:00", + "dev": { + "version": "1" + }, + "staging": { + "version": "2" + }, + "prod": { + "version": "1" + } +} \ No newline at end of file diff --git a/src/package.json b/src/package.json index ad7cc3f..31b89d5 100755 --- a/src/package.json +++ b/src/package.json @@ -3,7 +3,8 @@ "version": "2.1.0", "private": false, "engines": { - "node": "12.x.x" + "npm": ">= 8.1.0", + "node": "16.13.2" }, "main": "", "scripts": { @@ -17,20 +18,20 @@ "test": "true" }, "dependencies": { - "@open-rights-exchange/chainjs": "^2.3.2", + "@open-rights-exchange/chainjs": "^2.3.3", "analytics-node": "^3.5.0", - "axios": "^0.21.1", + "axios": "^0.25.0", "concurrently": "^4.1.2", "cors": "^2.8.4", - "dotenv": "^8.2.0", + "dotenv": "^8.6.0", "flatted": "^2.0.2", "helmet": "^4.4.1", "js-base64": "^3.6.0", "js-sha256": "^0.9.0", "lodash": "^4.17.21", "mongoose": "5.10.11", - "newrelic": "^7.1.3", - "rollbar": "^2.21.0", + "newrelic": "^7.5.2", + "rollbar": "^2.25.0", "ts-jest": "^26.5.3", "ts-node": "^8.10.1" },