Skip to content
Snippets Groups Projects
Commit 76cc205f authored by Fredrik Fonn Hansen's avatar Fredrik Fonn Hansen :8ball:
Browse files

Merge branch 'fix-deploy' into 'main'

Fix deploy

See merge request !85
parents fe6efb77 53ea0c46
No related branches found
No related tags found
1 merge request!85Fix deploy
Pipeline #218261 failed
......@@ -15,6 +15,7 @@ cache:
- android-sdk/
- .gradle/wrapper
- .gradle/caches
- vm-state/
variables:
# Specify the SDK tools version and build tools version to use
......@@ -36,7 +37,10 @@ Prettier:
- npx prettier --check .
retry: 1
rules:
- if: '($CI_PIPELINE_SOURCE == "push" || $CI_COMMIT_BRANCH == "main") && $CI_PIPELINE_SOURCE != "schedule"'
- changes:
- backend/**/*.*
if: '$CI_PIPELINE_SOURCE == "push" && $CI_PIPELINE_SOURCE != "schedule"'
- if: '$CI_COMMIT_BRANCH == "main" && $CI_PIPELINE_SOURCE != "schedule"'
Typescript-compile:
image: node:16.10.0
......@@ -46,8 +50,15 @@ Typescript-compile:
- cd backend
- yarn --cache-folder ../.yarn
- yarn tsc
cache:
key: ${CI_COMMIT_REF_SLUG}-vm
paths:
- vm-state/
rules:
- if: '($CI_PIPELINE_SOURCE == "push" || $CI_COMMIT_BRANCH == "main") && $CI_PIPELINE_SOURCE != "schedule"'
- changes:
- backend/**/*.*
if: '$CI_PIPELINE_SOURCE == "push" && $CI_PIPELINE_SOURCE != "schedule"'
- if: '$CI_COMMIT_BRANCH == "main" && $CI_PIPELINE_SOURCE != "schedule"'
API-test:
image: cypress/base:14.17.0
......@@ -56,11 +67,11 @@ API-test:
variables:
DISPLAY: ":99"
script:
- if [ -d vm-state/ ]; then rsync -avm --exclude vm-state/ vm-state/ /; fi
- apt-get update && apt-get install -y xvfb
- Xvfb :99 -screen 0 1920x1080x24 &
- cd backend
- echo "FB_PRIV_KEY_JSON_ENCODED=$FB_PRIV_KEY_JSON_ENCODED" >> .env
# - echo "export FB_PRIV_KEY_JSON_ENCODED=$FB_PRIV_KEY_JSON_ENCODED"
- node keys/decode.js
- chmod 400 keys/fb-key.json
- yarn --cache-folder ../.yarn
......@@ -72,6 +83,9 @@ API-test:
- cypress/videos/*.mp4
expire_in: 1 week # Set the artifact expiration time according to your needs
rules:
- changes:
- backend/**/*.*
if: '$CI_PIPELINE_SOURCE == "push" && $CI_PIPELINE_SOURCE != "schedule"'
- if: '$CI_COMMIT_BRANCH == "main" && $CI_PIPELINE_SOURCE != "schedule"'
......@@ -80,8 +94,6 @@ Gradle-build:
stage: frontend test
needs: []
script:
# Restore Android SDK from cache
#- if [ -d android-sdk ]; then mv android-sdk/* $ANDROID_HOME/; fi
# Download and install Android SDK
- wget --quiet --output-document=android-sdk.zip https://dl.google.com/android/repository/commandlinetools-linux-${ANDROID_SDK_TOOLS}_latest.zip
- unzip -q -o android-sdk.zip -d android-sdk
......@@ -92,13 +104,18 @@ Gradle-build:
- ./gradlew build --refresh-dependencies
- gradle build
rules:
- if: '($CI_PIPELINE_SOURCE == "push" || $CI_COMMIT_BRANCH == "main") && $CI_PIPELINE_SOURCE != "schedule"'
- changes:
- frontend/**/*.*
if: '$CI_PIPELINE_SOURCE == "push" && $CI_PIPELINE_SOURCE != "schedule"'
- if: '$CI_COMMIT_BRANCH == "main" && $CI_PIPELINE_SOURCE != "schedule"'
VM Deploy:
image: node:16.10.0
stage: deploy
needs: [Typescript-compile, API-test]
before_script:
- if [ -d vm-state/ ]; then rsync -avm --exclude vm-state/ vm-state/ /; fi
- mkdir -p ~/.ssh
- echo "$NTNU_VM_SSH_KEY" | tr -d '\r' > ~/.ssh/id_rsa
- chmod 600 ~/.ssh/id_rsa
......@@ -108,7 +125,7 @@ VM Deploy:
name: production
url: http://10.212.26.72/
script:
- ssh -v -i ~/.ssh/id_rsa -o StrictHostKeyChecking=no ubuntu@10.212.26.72 -p 22 "bash /home/git/tdt4240-tank-wars/backend/vm.sh </dev/null >/dev/null 2>&1"
- ssh -v -i ~/.ssh/id_rsa -o StrictHostKeyChecking=no ubuntu@10.212.26.72 -p 22 "bash /home/git/tdt4240-tank-wars/backend/vm-startup.sh"
rules:
- if: '$CI_COMMIT_BRANCH == "main" && $CI_PIPELINE_SOURCE != "schedule"'
......@@ -132,4 +149,4 @@ VM Heartbeat:
echo "Server did not respond with 200 OK after 3 minutes"
exit 1
rules:
- if: '($CI_COMMIT_BRANCH == "main" || $CI_PIPELINE_SOURCE == "schedule") || $RUN_VM_HEARTBEAT == "true"'
\ No newline at end of file
- if: '$CI_COMMIT_BRANCH == "main" || $CI_PIPELINE_SOURCE == "schedule" || $RUN_VM_HEARTBEAT == "true"'
\ No newline at end of file
......@@ -23,7 +23,7 @@ Run the following commands in your terminal:
## CD: Deployment to virtual machine
The backend is automatically deployed to a virtual machine when a commit is pushed to the main branch. The deployment is done by a Gitlab CI server that connects to the VM with SSH and runs the `vm.sh` script.
The backend is automatically deployed to a virtual machine when a commit is pushed to the main branch. The deployment is done by a Gitlab CI server that connects to the VM with SSH and runs the `vm-startup.sh` script.
The VM is hosted on NTNU (requires VPN) and can be accessed on the following address:
IP: 10.212.26.72
......@@ -39,4 +39,4 @@ e2e tests: `yarn test`
## Restrictions
The backend will only allow 30 requests per minute from the same IP address. This is to prevent abuse of the API.
The backend will only allow 30 requests per minute from the same IP address. This is to prevent abuse of the API..
// this is config for the pm2 process manager
module.exports = {
apps: [
{
name: 'backend',
script: 'dist/src/index.js',
env: {
NODE_ENV: 'development',
},
env_production: {
NODE_ENV: 'production',
},
},
],
};
......@@ -5,9 +5,8 @@
"main": "index.js",
"scripts": {
"test": "cypress run",
"start:local": "concurrently \"tsc -w\" \"cross-env NODE_ENV=development yarn run server\"",
"start:prod": "tsc && cross-env NODE_ENV=production yarn run server",
"server": "node dist/src/index.js || yarn run server",
"start:local": "tsc && cross-env NODE_ENV=development node dist/src/index.js",
"start:prod": "tsc && pm2 startOrRestart ./config/ecosystem.config.js --env production",
"prettier": "prettier --write \"**/*.{js,ts,tsx,jsx,css,md,scss,html,json}\""
},
"author": "",
......
......@@ -43,7 +43,6 @@ export async function getUsers(): Promise<any> {
*/
export async function getUsersIds(): Promise<any> {
const usersRef = admin.firestore().collection('users');
const test = 'test';
const cacheKey = 'userIds';
const responseMapper = (firestoreResponse: QueryDocumentSnapshot[]) => {
return firestoreResponse.map((doc) => {
......@@ -116,7 +115,7 @@ export async function sendFirestoreRequest<T>(
ref: CollectionReference<T> | Query<T> | DocumentReference<T>
): Promise<Array<{ id: string; data: T }> | { id: string; data: T | null } | null> {
try {
log('firebase: sending request to firestore...');
log('firebase: sending request to firestore...', 'warning');
let data = null;
if (ref instanceof CollectionReference || ref instanceof Query) {
......
......@@ -12,14 +12,42 @@ export function expressLogger(req: Request, res: Response, next: NextFunction) {
// Store the original `.send()` method
const originalSend = res.send;
// colorize the request method
const colorCode = (method: any) => {
switch (method) {
case 'GET':
return '\x1b[32m'; // green
case 'POST':
return '\x1b[34m'; // blue
case 'PUT':
return '\x1b[33m'; // yellow
case 'DELETE':
return '\x1b[31m'; // red
default:
return '';
}
};
function sendToLogger(message: string) {
const methodColor = colorCode(req.method);
log(`${methodColor}[${req.ip}] ${req.method} ${req.url} - Response: ${message}`);
}
// Override the `.send()` method
res.send = function (body: any): Response {
const bodyString: string = body.toString();
if (bodyString.includes('<!DOCTYPE html>') || bodyString.includes('window.onload')) {
// dont print html/js files
log(`${req.method} ${req.url} - Response: HTML / JS file`);
if (bodyString.startsWith('{')) {
sendToLogger('{ Collapsed JSON }');
} else if (bodyString.includes('<!DOCTYPE html>')) {
sendToLogger('HTML file');
} else if (bodyString.includes('window.onload')) {
sendToLogger('JS file');
} else if (bodyString.startsWith('[')) {
sendToLogger('[ Collapsed array ]');
} else {
log(`${req.method} ${req.url} - Response: ${body}`);
sendToLogger(bodyString);
// log(`${methodColor}${req.method} ${req.url} - Response: ${body}`);
}
// Call the original `.send()` method with the provided body
......
#!/bin/bash
# Kill the service running on port 80, if any
sudo kill $(sudo lsof -t -i:80)
# Install PM2 globally if not already installed
if ! command -v pm2 &> /dev/null; then
sudo npm install pm2 -g
fi
# Start the Express.js app
cd /home/git/tdt4240-tank-wars/backend && sudo git pull && sudo npx yarn && sudo npx yarn start:prod >/dev/null 2>&1 &
cd /home/git/tdt4240-tank-wars/backend && sudo git pull && sudo npx yarn && sudo yarn start:prod
# Wait for the app to start and check the status
BACKEND_PID=$!
sleep 10
if ps -p $BACKEND_PID > /dev/null; then
BACKEND_STATUS=$(sudo pm2 show backend --no-color | grep "status" | awk '{print $4}')
if [ "$BACKEND_STATUS" == "online" ]; then
echo "Backend started successfully"
exit 0
else
else
echo "Backend failed to start"
exit 1
fi
fi
\ No newline at end of file
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment