2020-04-25 02:30:29 +05:30
# Work on API Instances
## Installing pre-requisites
2020-04-27 00:35:22 +05:30
1. Perform updates to the OS packages by following [this guide ](flight-manuals/working-on-virtual-machines ).
2020-04-25 02:30:29 +05:30
2020-04-27 00:35:22 +05:30
2. Install build tools for node binaries (`node-gyp` ) etc.
2020-04-25 02:30:29 +05:30
2020-04-27 00:35:22 +05:30
```console
sudo apt install build-essential
```
2020-04-25 02:30:29 +05:30
2020-04-27 00:35:22 +05:30
## First Install
2020-04-25 02:30:29 +05:30
2020-04-27 00:35:22 +05:30
Provisioning VMs with the Code
2020-04-25 02:30:29 +05:30
2020-04-07 15:34:02 +05:30
1. Install Node LTS.
2020-04-25 02:30:29 +05:30
2020-04-07 15:34:02 +05:30
2. Update `npm` and install PM2 and setup logrotate and startup on boot
2020-04-25 02:30:29 +05:30
```console
2020-04-07 15:34:02 +05:30
npm i -g npm
npm i -g pm2
pm2 install pm2-logrotate
pm2 startup
```
2020-04-25 02:30:29 +05:30
3. Clone freeCodeCamp, setup env and keys.
```console
2020-04-07 15:34:02 +05:30
git clone https://github.com/freeCodeCamp/freeCodeCamp.git
cd freeCodeCamp
```
2020-04-25 02:30:29 +05:30
4. Create the `.env` from the secure credentials storage.
2020-04-07 15:34:02 +05:30
2020-04-25 02:30:29 +05:30
5. Install dependencies
```console
2020-04-07 15:34:02 +05:30
npm ci
```
2020-04-25 02:30:29 +05:30
6. Build the server
```console
2020-04-07 15:34:02 +05:30
npm run ensure-env & & npm run build:server
```
2020-04-25 02:30:29 +05:30
7. Start Instances
```console
2020-04-07 15:34:02 +05:30
cd api-server
pm2 start production-start.js -i max --max-memory-restart 600M --name org
```
2020-04-25 02:30:29 +05:30
## Logging and Monitoring
```console
pm2 logs
```
```console
pm2 monitor
```
## Updating Instances (Maintenance)
2020-04-27 00:35:22 +05:30
Code changes need to be deployed to the API instances from time to time. It can be a rolling update or a manual update. The later is essential when changing dependencies or adding enviroment variables.
2020-04-25 02:30:29 +05:30
2020-04-27 00:35:22 +05:30
> [!DANGER]
> The automated pipelines are not handling dependencies updates at the minute. We need to do a manual update before any deployment pipeline runs.
2020-04-25 02:30:29 +05:30
2020-04-27 00:35:22 +05:30
### 1. Manual Updates - Used for updating dependencies, env variables.
2020-04-25 02:30:29 +05:30
1. Stop all instances
2020-04-27 00:35:22 +05:30
```console
pm2 stop all
```
2020-04-25 02:30:29 +05:30
2. Install dependencies
2020-04-27 00:35:22 +05:30
```console
npm ci
```
2020-04-25 02:30:29 +05:30
3. Build the server
2020-04-27 00:35:22 +05:30
```console
npm run ensure-env & & npm run build:server
```
2020-04-25 02:30:29 +05:30
4. Start Instances
2020-04-27 00:35:22 +05:30
```console
pm2 start all --update-env & & pm2 logs
```
### 2. Rolling updates - Used for logical changes to code.
```console
pm2 reload all --update-env & & pm2 logs
```
> [!NOTE]
> We are handling rolling updates to code, logic, via pipelines. You do not need to run these commands. These are here for documentation.