# !!! READ THIS FIRST !!!

We've switched to a different system where all the server configuration files, environment files, and server addresses, are assigned on an "as needed" basis. They are no longer stored on Dropbox, but instead directly on the server that users them.

This means the following:
* If you want to directly `ssh` to a server instance, you now must run a jenkins task that directly queries AWS for the latest `dns` entry for the instance.
* All these tasks are only available on the jenkins web server (https://jenkins.bigscreenvr.com) under the "Info" tab.
* You now can only ssh into the various servers via the jenkins build server itself.

So, the rough formula for ssh-ing into an application server is as follows:
1. On a web browser, login into jenkins.bigscreenvr.com
2. Go to the "Info" tab.
3. Find the network and server fleet of the server you want to SSH into (e.g. `dev` `cherries` = `aws_network_info dev_cherries`) and click on the Jenkins task for it.
4. Build the info task if you want the latest server addresses - it should run in about 5 seconds.
5. Go to the console output for the most recent build (under "Build History")
6. Browse through the json data in the console output for the server you want. e.g.:
```
    "dns": "ec2-35-87-221-240.us-west-2.compute.amazonaws.com",
    "tag": [
      {
        "Key": "Name",
        "Value": "[dev-cherries] dev-cherries-cloud-http"
      }
    ]
  ...snip...
```
7. Copy the "dns" entry.
8. `ssh` into the jenkins server from you local dev machine (see below for `$BIGSCREEN_DROPBOX_PATH` info).
```
ssh -i "$BIGSCREEN_DROPBOX_PATH/Server_Keys/_Jenkins/JenkinsBuildServerA.pem" ubuntu@ec2-35-165-77-91.us-west-2.compute.amazonaws.com
ssh -i "/C/Users/DavidPetrie/Bigscreen/server_keys/_Jenkins/JenkinsBuildServerA.pem" ubuntu@ec2-35-165-77-91.us-west-2.compute.amazonaws.com
```
9. `sudo ssh` to ssh into the server you want to access, using the .pem key in the `networks/$network/$fleet/` folder, using the `ubuntu` user, e.g.:
```
$ sudo ssh -i networks/dev/cherries/aws/dev_cherries.pem ubuntu@ec2-35-87-221-240.us-west-2.compute.amazonaws.com
```

TODO:
1. Simplify the jenkins info task output.
2. Split the jenkins production server into a separate server that only VIPs can access.
3. Change jenkins so it uses jenkins 


# !!! READ THIS SECOND !!!

Key files are shared over dropbox, in shared folders outside the normal Bigscreen shared folder. No this is not ideal.

Define `$BIGSCREEN_DROPBOX_PATH` to point to this dropbox path in your environment variables, e.g.:

`BIGSCREEN_DROPBOX_PATH=/C/Users/David/Dropbox`

In windows, you can set this in Environment Variables -> System Variables.

## STRONG RECOMMEND: make a symbolic link to the Dropbox folder

Dropbox imposes some restrictions on the folder it users (e.g. it names it `C:\Users\Username\Dropbox (Bigscreen, Inc.)`, and you can't change it without Dropbox complaining) and don't want to deal with all the escaping problems, I suggest making a symbolic link to the folder. Use powershell in administrator mode, and then:

```
New-Item -Path C:\Users\YOURUSERNAME\Dropbox -ItemType SymbolicLink -Value 'C:\Users\YOURUSERNAME\Dropbox (Bigscreen, Inc.)\'
```
This will make a link Dropbox that you can safely use in the bash scripts.

# Jenkins

```
ssh -i "$BIGSCREEN_DROPBOX_PATH/Server_Keys/_Jenkins/JenkinsBuildServerA.pem" ubuntu@ec2-34-217-118-93.us-west-2.compute.amazonaws.com
```



# Copy logs scratch pad

```
scp -i "$BIGSCREEN_DROPBOX_PATH/Server_Keys/_Jenkins/JenkinsBuildServerA.pem" ubuntu@ec2-34-217-118-93.us-west-2.compute.amazonaws.com:/home/ubuntu/networks/main/horse/admin-api-out.log ./admin-api-out.log
scp -i "$BIGSCREEN_DROPBOX_PATH/Server_Keys/_Jenkins/JenkinsBuildServerA.pem" ubuntu@ec2-34-217-118-93.us-west-2.compute.amazonaws.com:/home/ubuntu/networks/main/gem/out.log ./log.out.log
```


# FINANCIAL REPORTS

## BEFORE RUNNING THIS, ensure to subscribe/unsubscribe from exchangeratesapi.io's billing in case previously unsubscribed to save money: https://manage.exchangeratesapi.io/plan

### Running financial reports on the api server
SSH into the build server first, in order to access keys and servers. See the above README to understand why. Example:

`ssh -i JenkinsBuildServerA.pem ubuntu@ec2-35-165-77-91.us-west-2.compute.amazonaws.com`

SSH into the `admin-api` server

`sudo ssh -i networks/main/melon/aws/main_melon.pem ubuntu@ec2-34-213-180-147.us-west-2.compute.amazonaws.com`

cd into the current admin-api build, ex. `cd ~/main_melon_admin_api_34/`

Edit the reporting script to have the correct date range. ex. `nano apps/admin_api/reporting.js` The date range is the last line of index.js. Be sure to run the reporting script from the top level directory of the codebase.
`node apps/admin_api/reporting.js`

### Copy financial reporting from api server

on the Jenkins Build server, first run
`sudo scp -i networks/main/horse/aws/main_horse.pem ubuntu@ec2-18-246-13-7.us-west-2.compute.amazonaws.com:~/report.tsv ./report.tsv`

then on your local machine, run
`scp -i JenkinsBuildServerA.pem ubuntu@ec2-34-217-118-93.us-west-2.compute.amazonaws.com:~/report.tsv ./report.tsv`

### Appending weekly TSVs into a monthly TSV for monthly reporting
Install tsv-append from https://github.com/eBay/tsv-utils

`tsv-append -H report1.tsv report2.tsv report3.tsv > january.tsv` 
This will maintain the headers of each TSV, and output it to january.tsv

# TURN Server

This server is currently aimed at ALPHA/STAGING/BETA

ssh -i "$BIGSCREEN_DROPBOX_PATH/Server_Keys/TurnServer/TurnServer.pem" ubuntu@ec2-34-214-101-195.us-west-2.compute.amazonaws.com


## Metabase 

`echo "Metabase";   ssh -i "$BIGSCREEN_DROPBOX_PATH/Server_Keys/_Metabase/MetabaseV1.pem" ubuntu@ec2-54-186-80-91.us-west-2.compute.amazonaws.com`

scp -i "$BIGSCREEN_DROPBOX_PATH/Server_Keys_Production/Quebec/Quebec.pem" ubuntu@ec2-34-222-201-2.us-west-2.compute.amazonaws.com:/home/ubuntu/.pm2/logs/api-out.log logs.log

NOTE: While almost all EC2 instances are in US-West Oregon, the Metabase EC2 instance is in US-East-1 N. Virginia https://console.aws.amazon.com/ec2/v2/home?region=us-east-1#SecurityGroups:


# DEC 2021 Server Upgrade

```
sudo apt upgrade
sudo apt update

do-release-upgrade

sudo apt autoremove

curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
sudo apt install -y nodejs
sudo apt-get install gcc g++ make
curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | sudo tee /usr/share/keyrings/yarnkey.gpg >/dev/null
echo "deb [signed-by=/usr/share/keyrings/yarnkey.gpg] https://dl.yarnpkg.com/debian stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt-get update && sudo apt-get install yarn

sudo npm install pm2@latest -g
pm2 update
sudo shutdown -r now
```

# Install Python3.10 on Ubuntu 20.04

```
sudo apt update
sudo apt -y upgrade
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get install -y python3.10 python3.10-distutils python3.10-venv
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 2
sudo apt-get install -y python3-opencv
```
