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

add error handling to startup logic #239

Merged
merged 3 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
backend/node_modules
45 changes: 22 additions & 23 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,41 @@ ARG node_image=node:${node_version}

FROM $node_image AS builder

ENV NEXT_TELEMETRY_DISABLED=1 \
NODE_ENV=development
WORKDIR /app
COPY . /app/

ENV NEXT_TELEMETRY_DISABLED=1
ENV NODE_ENV=development

WORKDIR /app
RUN yarn --network-timeout 300000
RUN yarn --network-timeout 300000; \
NODE_ENV=production yarn build
WORKDIR /app/backend
RUN yarn --network-timeout 300000

ENV NODE_ENV=production

RUN yarn build
WORKDIR /app
RUN yarn build
RUN yarn --network-timeout 300000; \
NODE_ENV=production yarn build

FROM node:${node_version}-alpine AS production

WORKDIR /app

ENV NODE_ENV=production
RUN npm install --global pm2
RUN apk add -U nginx openssl
FROM alpine AS intermediate

COPY ./.env.example /app/
COPY ./docker-assets /app/docker-assets/
RUN rm /etc/nginx/http.d/default.conf; \
ln -s /app/docker-assets/siren-http.conf /etc/nginx/http.d/siren-http.conf

COPY --from=builder /app/backend/package.json /app/backend/package.json
COPY --from=builder /app/backend/node_modules /app/backend/node_modules
COPY --from=builder /app/backend/dist /app/backend/dist

COPY --from=builder /app/siren.js /app/siren.js
COPY --from=builder /app/package.json /app/package.json
COPY --from=builder /app/siren.js /app/package.json /app/
COPY --from=builder /app/node_modules /app/node_modules
COPY --from=builder /app/public /app/public
COPY --from=builder /app/.next /app/.next


FROM node:${node_version}-alpine AS production

ENV NODE_ENV=production
RUN npm install --global pm2; \
apk add -U nginx openssl curl

RUN rm /etc/nginx/http.d/default.conf; \
ln -s /app/docker-assets/siren-http.conf /etc/nginx/http.d/siren-http.conf

COPY --from=intermediate /app /app/

ENTRYPOINT /app/docker-assets/docker-entrypoint.sh
41 changes: 37 additions & 4 deletions docker-assets/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,24 +1,57 @@
#!/bin/ash

# if no .env found, dump the default to stdout and exit
if [ ! -f /app/.env ]
then
printf "No \`/.env\` file found at the expected location (\`/app/.env\`). \n\
Please adapt this default file to your needs and mount it within the container: \n\
----------------\n"
cat /app/.env.example
printf "----------------\n"
exit 1
fi

# load .env
set -a; \
. /app/.env; \
set +a

# if bn/vc api unreachable, print message and exit
tests="${BEACON_URL:-http://your-BN-ip:5052} ${VALIDATOR_URL:-http://your-VC-ip:5062}"
for test in $tests; do
nc -z "${test#*//}"
if [ $? -eq 1 ]; then
printf "${test} unreachable, check settings and connection\n"
fail=true
fi
done
# check api token
api_response_code=$(curl -sIX GET "${VALIDATOR_URL:-http://127.0.0.1}/lighthouse/version" -H "Authorization: Bearer ${API_TOKEN:-default_siren_token}" | head -n 1 | awk '{print $2}')
if [ "$api_response_code" != '200' ]; then
printf "validator api issue, server response: %s \n" "${api_response_code:-no_response}"
fail=true
fi

if [ $fail ]; then exit 1; fi

if [ $SSL_ENABLED = true ] ; then
## generate cert if not present
if [ ! -f /certs/cert.pem ] ; then
mkdir -p /certs
openssl req -x509 -newkey rsa:4096 -keyout /certs/key.pem -out /certs/cert.pem -days 365 -passout pass:'sigmaprime' -subj "/C=AU/CN=siren/emailAddress=noreply@sigmaprime.io"
echo 'sigmaprime' > /certs/key.pass
fi
## nginx ssl stuff
ln -s /app/docker-assets/siren-https.conf /etc/nginx/http.d/siren-https.conf
ln -s /app/docker-assets/siren-https.conf /etc/nginx/http.d/siren-https.conf
fi

nginx &

# test config, start nginx
nginx -t && nginx &

# start backend
cd /app/backend
PM2_HOME='~/.pm2-backend' pm2-runtime yarn --interpreter sh -- start:prod &

# start frontend
cd /app
PM2_HOME='~/.pm2-frontend' pm2-runtime yarn --interpreter sh -- start
PM2_HOME='~/.pm2-frontend' pm2-runtime yarn --interpreter sh -- start