Skip to content

Getting started New Deploy guide

Lucent Fong edited this page Sep 2, 2020 · 3 revisions

Getting started with LCS

Since a serverless application lcs is a little different than your average flask application. Flask, and the standard backend tend to be run on a continuously running server, for LCS we opted to have specific functions run independent of one continuously running server.

The gist of serverless

Functions (and their dependancies) live in aws lambda. We use a service called api gateway to route http requests to those functions and call them.

This may seem a little complicated but we get the benefit of functions independent function scalability and are only charged per function execution instead of for every hour we have a traditional server running.

The serverless framework is a tool that allows us to write a simple yaml file and configure all of these routes and functions in aws lambda. This is done to automate the deployment process, instead of having to deploy each function manually.

In general, since this is a larger repository, it is not recommended to set everything up by hand.

Prerequisites

This guide assumes you have a linux like enviroment or can produce an environment that acts like one. If running on a windows environment, the installation process will likely work the same, but deployment directly to the server will likely not work.

You will need to install the following:

  • python3 (For general running of the functions)
  • pip (For dependencies within the project)
  • virtualenv (To manage your own environment)
  • npm (To install dependencies)
  • serverless (Can be done through npm install -g serverless, for deployment and testing)
  • mongodb (For the database)

Setting up a local python enviroment

The following steps are necessary to set up your local python environment:

You should run the following commands:

This command sets up their virtual environment as the folder "env" with python3

  • virtualenv -p python3 env

This activates your environment, this is necessary to run whenever you need to run LCS functions

  • source env/bin/activate

This is for the dependencies within the python environment

  • pip install -r requirements.txt

And this is for the configurations within LCS itself

  • cp config.example.py config.py

You will need edit your config.py and change DB_URI to point to your local mongodb instance. Another option is to point it to a mongoDB atlas cluster.

As a remind once again, you will need to run source env/bin/activate whenever you want to work on lcs code. You can leave your environment by entering deactivate.

Setting up serverless

Again you need npm and serverless already installed but as long as they are there, to install serverless all you need to do is installed all of the dependencies we list in the package.json file via the command:

  • npm install

Using serverless locally

CURENTLY NOT WORKING - Will require changes

This allows you to run all of the functions as if they were endpoints on a regular server from which you can make requests into and out of. Ideally, to run it like this all you need to do is run:

  • sls offline start (sls may not work for windows computers as an alias)

A local server will be started on localhost:3000

Generating token.pickle

For normal developers this likely will not be necessary. The token.pickle is meant to solve a quirk for our google calendar endpoint.

After ensuring you have activated your environment run:

  • python3 to enter the shell

  • Now that your in the python shell run: import cal_announce

and finally:

  • cal_announce.gen_token()

Now you must visit the link it gives you and login to the rnd email. You can simply copy the code on the screen and paste it into the terminal which will then give you the token.pickle.

Deployment

Deployment does not work on windows machines due to the nature of the deployment process. On windows machines (when not under a linux virtual machine or kernel), it packages windows native python file for certain dependencies. This causes the lambdas to break once deployed.

Setting up access for deploys

First you must get the credentials necessary to send the serverless application to a server.

To secure access to aws credentials either:

  • Signing up for aws free teir and getting your own key
  • or asking someone in rnd to give you an access key/iam account for our servers

Once you have the credentials you can setup aws credentials by either:

  • setting AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY as environment variables
  • or setting up ~/.aws/credentials file

As long as the aws command works in the terminal you should be good to go.

Deploying to our dev instance

  • Ensure sure the current config.py has all the dev config options
  • Ensure sure you have a token.pickle file
  • Simply run: sls deploy

Deploying to our prod instance

  • Ensure sure the current config.py has all the dev config options
  • Ensure sure you have a token.pickle file
  • Simply run: sls deploy --stage prod

Further information on how the deployment works and the yaml file are listed in the "Serverless" section.

General Information

Running tests

To run the tests we use pytest, travis, as explained later, runs these tests automatically with the coverage parameter. To run all of the tests yourself simply run: python -m pytest tests/

Linting

Ideally, we are able to catch issues via the linter, to run the linter simply run: pylint *.py

Travis runs the linter as well for every push to a branch.

Who is Travis? (Our automated testing tool)

Travis is a magical man who lives in the cloud and runs tests/lints the code whenever you push code to a branch or open a pr. If he smiles upon your code you may proceed to everlasting glory.

In all seriousness, Travis is an automated testing tool that sets up the environment automatically and runs the linter and tests specified in /tests on the codebase version you pushed.