# Local docker dev environment
This repo hopefully contains all that you need to get your relevant services setup and running
locally on your machine so you can safely and reliably develop and test!

# Table of Contents
* [Setup](#Setup)
  * [Install Dependencies](#Install-Dependencies)
  * [Setting up your environment](#Setting-up-your-environment)
  * [Docker-compose projects structure](#Docker-compose-projects-structure)
    * [Environment Variables](#Environment-variables)
  * [Microservice to microservice communication](#Microservice-to-microservice-communication)
  * [Getting grass working with your local dev ecosystem](#Getting-grass-working-with-your-local-dev-ecosystem)
  * [Getting Frontend-Distribution to build](#Getting-Frontend-Distribution-to-build)

# Setup

## Install Dependencies
You will need to have Docker and Docker-compose installed in your machine.

[Back to table of contents](#Table-of-Contents)

## Setting up your environment
You will then need to edit the docker-compose.yml file to setup which services you want to run on
your machine.

Docker-Compose documentation should help with figuring out what options are available when
configuring your services in the docker-compose file but some options will be explained below.

[Back to table of contents](#Table-of-Contents)

### Docker-compose projects structure
One way to structure your services is to have it look like this:
```
/docker-dev
    docker-compose.yml
    /repos
        /ows-product
        /ows-product-digital
        /ows-grass
        /ows-whatever-you-want
```

#### Environment variables
There are several ways you can set your env vars:

1. Pointing to an env file belonging to the service's repo:
```yaml
version: "3.7"
services:
  grass:
    env_file:
        - ./repos/ows-grass/.env
```

2. explicitly setting env vars for each service like so:
```yaml
version: "3.7"
services:
  product:
    environment:
        - Environment=dev
        - PORT=80
        - LOGGER_DSN=
        - AR_MYSQL_HOST=
        - AR_MYSQL_DATABASE=
        - AR_MYSQL_USER=etan
        - AR_MYSQL_PASSWORD=
```

3. use the `.env` file in this repo to reduce the duplication of env vars and also allows for
interpolation of environment variable values in to the docker-compose file like so:
```bash
$ cat .env
TAG=v1.5

$ cat docker-compose.yml
version: '3'
services:
  web:
    image: "webapp:${TAG}"
```
Above example is taken from the first example [here](https://docs.docker.com/compose/environment-variables/#the-env-file)

[Back to table of contents](#Table-of-Contents)

### Microservice to microservice communication
we can specify an `OWSREQUEST_SERIVCE_MAP` env var in each of our python microservices in the
docker-compose file like so:

```yaml
...
  prodigital:
    build:
        context: ./prodigital
        dockerfile: Dockerfile
        target: dev
    ports:
        - "5002:80"
    environment:
        - Environment=dev
        - PORT=80
        - LOGGER_DSN=
        - AR_MYSQL_HOST=qa.db.qaorch.com
        - AR_MYSQL_DATABASE=art_relations
        - AR_MYSQL_USER=
        - AR_MYSQL_PASSWORD=
        - OWSREQUEST_SERVICE_MAP={"ows-product":"product"} <- service map here
    volumes:
        - ./prodigital:/var/app
    stdin_open: true
    tty: true
  product:
    build:
        context: ./ows-product
        dockerfile: Dockerfile
        target: deploy
    ports:
        - "5003:80"
    environment:
        - Environment=dev
        - PORT=80
        - LOGGER_DSN=
        - OWSREQUEST_SERVICE_MAP={"ows-product-digital":"prodigital"} <- service map here
        - AR_MYSQL_HOST=distro.db.devorch.com
        - AR_MYSQL_PASSWORD=
        - AR_MYSQL_PORT=3306
        - AR_MYSQL_USER=
        - AR_MYSQL_DATABASE=art_relations
        - DATADOG_ENV=
          #     volumes:
          #         - ./ows-product:/var/app
    stdin_open: true
    tty: true
...
```

As you can see, we set a service_map for `ows-product` to point to `ows-product-digital` and
vice versa

[Back to table of contents](#Table-of-Contents)


### Getting grass working with your local dev ecosystem
Checkout https://github.com/theorchard/ows-grass & run `python ./dev.py` (make sure to set up .env file first!)

For ALW User 6918 (vend_contact.id=6918) (works for OA user too)

Look up VAPI Access (really a refresh token) Token for ALW (client_id = 5656789871)

```sql
SELECT oauth_token FROM vectorapi_access_tokens WHERE user_id = 6918 and client_id = 5656789871;
```

Generate a grasssession token for this user

`http://localhost:8080/auth/session/?user=alw%3A6918&token=<oauth_token>&client=5656789871`

Make GRASS calls!

`http://localhost:8080/product-digital/product/audio/2627473`

With the header key:value:
`session:<grasssession>`

Taken from this [**google slide**](https://docs.google.com/presentation/d/1TridG1Z-BQCPEI7nzqhm4Fvym6Q079OsMyEcCR7MfgY/edit#slide=id.g61e6230f18_0_124)

[Back to table of contents](#Table-of-Contents)


### Getting Frontend-Distribution to build
Frontend-Distribution's yarn build requires an SSH key to build properly because it does a git
lookup on the orchard's private repositories.

In order to get the yarn build, you'll have to run this command in your terminal to set an env var
on your machine:
```bash
export PRIVATE_KEY="$(openssl rsa -in ~/.ssh/your_private_key)"
```

Note: At the moment, private keys using RSA encryption is known to work with that export command.

#### Explanation of what this build is doing
The docker build will look for your ssh key in your machine's PRIVATE_KEY env var and inject it in
to the build during build time to do all the git stuff requred in the `yarn install` command.

If you're curious as to how the dockerfile works, you can take a look at the Dockerfile in the Frontend-Distribution repo.

[Back to table of contents](#Table-of-Contents)
