diff --git a/Dockerfile b/Dockerfile index c2fb66e1..842133fd 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,15 +8,17 @@ FROM node:14 AS builder COPY package.json /app/ -RUN cd /app && npm install --production +RUN cd /app && npm install COPY . /app/ RUN cd /app && npm run build -FROM node:14-alpine3.13 AS builder +FROM node:14-alpine3.13 COPY --from=builder /app/dist/ /app/ -COPY --from=builder /app/node_modules /app/node_modules +COPY --from=builder /app/package.json /app/ + +RUN cd /app && npm install --production CMD node /app/index.js diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..3b4983a7 --- /dev/null +++ b/Makefile @@ -0,0 +1,3 @@ +.PHONY: docker-push +docker-push: + bash scripts/build-and-push-docker.sh \ No newline at end of file diff --git a/README.md b/README.md index 9d0e3084..52c11a89 100644 --- a/README.md +++ b/README.md @@ -17,17 +17,17 @@ You need nodejs installed on your enviroment, we are using the latest Active LTS Example: ``` -STAGE=local NETWORK=testnet MAX_ADDRESS_GAP=20 -SERVICE_NAME=hathor-wallet-service +WALLET_SERVICE_NAME=hathor-wallet-service +WALLET_SERVICE_STAGE=local DEFAULT_SERVER=http://fullnode_url/v1a/ ``` -`STAGE` - Wallet-Service's deployment stage, e.g. `local`, `production`, `staging` `NETWORK` - The current hathor network we want to connect to `MAX_ADDRESS_GAP` - The full-node configured GAP between addresses -`SERVICE_NAME` - The Wallet-Service's service name as it was registered on AWS +`WALLET_SERVICE_NAME` - The Wallet-Service's service name as it was registered on AWS +`WALLET_SERVICE_STAGE` - Wallet-Service's deployment stage, e.g. `local`, `production`, `staging` `DEFAULT_SERVER` - The full-node API url If the wallet-service is not running locally, you also need to specify the AWS-SDK env variables: @@ -55,7 +55,7 @@ The recommended way to deploy this service is to use docker. #### Running: ``` -docker run -d -e STAGE="production" \ +docker run -d -e WALLET_SERVICE_STAGE="production" \ -e NODE_ENV="production" \ -e AWS_REGION="us-east-1" \ -e AWS_DEFAULT_REGION="us-east-1" \ @@ -64,7 +64,7 @@ docker run -d -e STAGE="production" \ -e NETWORK="testnet" \ -e MAX_ADDRESS_GAP=20 \ -e NETWORK="testnet" \ - -e SERVICE_NAME="hathor-wallet-service" \ + -e WALLET_SERVICE_NAME="hathor-wallet-service" \ -e DEFAULT_SERVER="http://fullnode:8082/v1a/" \ -ti localhost/hathor/sync-daemon ``` diff --git a/scripts/build-and-push-docker.sh b/scripts/build-and-push-docker.sh new file mode 100644 index 00000000..fee8629e --- /dev/null +++ b/scripts/build-and-push-docker.sh @@ -0,0 +1,21 @@ +set -e +set -o pipefail + +if [ -z "$AWS_ACCOUNT_ID" ]; then + echo "Please export a AWS_ACCOUNT_ID env var before running this"; + exit 1; +fi + +if [ -z "$DOCKER_IMAGE_TAG" ]; then + commit=`git rev-parse HEAD`; + timestamp=`date +%s`; + export DOCKER_IMAGE_TAG="dev-$commit-$timestamp"; +fi; + +echo $DOCKER_IMAGE_TAG; + +aws ecr get-login-password --region eu-central-1 | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.eu-central-1.amazonaws.com; + +docker build -t $AWS_ACCOUNT_ID.dkr.ecr.eu-central-1.amazonaws.com/hathor-wallet-service-sync-daemon:$DOCKER_IMAGE_TAG .; + +docker push $AWS_ACCOUNT_ID.dkr.ecr.eu-central-1.amazonaws.com/hathor-wallet-service-sync-daemon:$DOCKER_IMAGE_TAG; diff --git a/src/api/lambda.ts b/src/api/lambda.ts index 7b8182cd..d08d2d57 100644 --- a/src/api/lambda.ts +++ b/src/api/lambda.ts @@ -14,7 +14,7 @@ import { } from '../types'; AWS.config.update({ - region: 'us-east-1', + region: process.env.AWS_REGION, }); /** @@ -26,13 +26,13 @@ AWS.config.update({ export const lambdaCall = (fnName: string, payload: any): Promise => new Promise((resolve, reject) => { const lambda = new AWS.Lambda({ apiVersion: '2015-03-31', - endpoint: process.env.STAGE === 'local' + endpoint: process.env.WALLET_SERVICE_STAGE === 'local' ? process.env.WALLET_SERVICE_LOCAL_URL || 'http://localhost:3002' : `https://lambda.${process.env.AWS_REGION}.amazonaws.com`, }); const params = { - FunctionName: `${process.env.SERVICE_NAME}-${process.env.STAGE}-${fnName}`, + FunctionName: `${process.env.WALLET_SERVICE_NAME}-${process.env.WALLET_SERVICE_STAGE}-${fnName}`, Payload: JSON.stringify({ body: payload, }),