### Setup

1. If you don't have one already, create a directory which you will check out all orchard projects into.


    ```bash
    $ mkdir ~/orchard
    ```
    
1.  Check out this repo into that directory.

	```bash
	$ cd ~/orchard
	$ git clone git@github.com:theorchard/dev-microservices-setup.git
	```

1. Go into the source directory, create a virtualenv, and install the package. This was developed using python 3, so use that.

	```bash
	$ cd ~/orchard/dev-microservices-setup
	$ python3 -m venv env
	$ source env/bin/activate
	$ pip install -r requirements.txt
	```

1. (optional) The above installation will install a command-line script, `owsdev`, in `env/bin`. You can add an alias to your .bash_profile or equivalent.

    ```bash
    # ~/.bash_profile
    alias owsdev='~/orchard/dev-microservices-setup/env/bin/owsdev' 
    ```
    
    Some commands require sudo use (for binding to low ports or manipulating the hosts file). A little alias trickery will allow you
    to use sudo on aliased commands [(reference)](https://wiki.archlinux.org/index.php/Sudo#Passing_aliases).
    
    ```bash
    # ~/.bash_profile
    alias sudo='sudo '
    ```

1. Next, you'll need to create and update a configuration file for owsdev. There are comments in this file to help you. 

	```bash
	$ cp ~/orchard/dev-microservices-setup/.owsdev.config.yml.sample ~/.owsdev.config.yml
	```
    
    The location of this file can also be specified by the `OWSDEV_CONFIG` environment variable.

1. You can run `owsdev doctor` now, which will likely tell you that you're missing directories for some projects. Go ahead and check those out and set them up (i.e. create virtualenv, pip install, etc.) as you normally would, then come back to these instructions. Note that you typically shouldn't have to write .env files for each project.

1. You'll now need to set up a handful of supporting services, starting with DynamoDbLocal. DynamoDb is used to secure microservice-to-microservice communications. See python-owsrequest for more details.

	Download the distribution from amazon and unpack it into the directory you specified in ~/.owsdev.config.yml.
	
	```bash
	$ mkdir -p ~/orchard/owsdev/DynamoDBLocal
	$ cd ~/orchard/owsdev/DynamoDBLocal
	$ curl https://s3-us-west-2.amazonaws.com/dynamodb-local/dynamodb_local_latest.zip -o dynamodb_local_latest.zip
	$ unzip dynamodb_local_latest.zip
	```
	
	You can now use `owsdev dynamodb start` to test that it is setup correctly. When the dynamodb instance is started, owsdev checks that the necessary table is available and creates it if necessary.


	```bash
	$ owsdev dynamodb start
    Checking for table.
    Table not found, creating.
    Table created.
    Started server with PID 18528.
	```
	
	This may bark at you about java, in which case you should install java.  Also if you have any issues check out owsdev/log/dynamodb.log

1. Next, you'll need to install redis. I used homebrew.

	```bash
	$ brew install redis
	$ brew services start redis
	```
	Once it's installed and running, you can inject a very long-lived grass session token for use with grass-mediated requests (i.e. running frontend-distribution)
	
	```bash
	$ owsdev redis_setup
	```

1. If you don't already have it, install tmux.

	```bash
	$ brew install tmux
	```

1. When you run the dev setup, you'll be replacing your /etc/hosts file with a customized one. You'll need to make a copy of it to /etc/hosts.orig so that it can be swapped out.

	```bash
	$ sudo cp /etc/hosts /etc/hosts.orig
	```

Now you're ready to run things. See [running.md](running.md)


