Skip to content

Deploying

hemangandhi edited this page Feb 11, 2019 · 5 revisions

In addition to the step-by-step instructions, this document has a couple of notes on cleaning up deployments at the bottom.

How to deploy?

There are really three major steps: having a DB, hosting the API, and preparing third parties.

The third parties we rely on are MLH, sparkpost, google calendar, and slack. MLH. In the eyes of the backend, is an alternative login that is familiar to many hackers and saves them having to re-enter information. Sparkpost is an emailing API that lets us mass-email hackers. Sparkpost has a nice API that we're integrating (mostly done - just needs testing). Slack is our day-of messaging platform: announcements are posted there. To facilitate workshops, talks, games, and other day-of events, we provide a way for hackers to see slack announcements from a dashboard. Google Calendar lets us show upcoming events on the dashboard as well, letting hackers see events on their own time. Each are detailed further in step 1.

Step 0: the DB

We currently don't have SQL compatibility. This is intentional: hackathons aren't run with enough foresight to justify a rigid schema. Hence, we intend this to be run with Mongo. Hence, the first step is to have a mongo instance up and running. We used mLab for convenience, but host as you wish.

Just remember to host the DB in a way that is always accessible.

Step 1: Preparing AWS

There are two mini-steps: preparing the code and putting it on AWS. Both are straight-forward.

Preparing the Code

On a UNIX-like shell (Linux, Mac, probably Solaris if you roll that way, and certain BSD things I know nothing about), you more or less run the following:

git clone https://github.com/HackRU/LCS.git #or your fork of it
cd LCS #or wherever you clone to
virtualenv venv
cp config.py.example config.py

Make a copy of config.py.example called config.py.

Dependencies:

  • Python 3.6 (AWS wants this)
  • pip for the version of python you have
  • virtualenv also for python 3.6

Be sure to set up the DB URL, user, and password, in the first lines of your copy of config.py.

DB_URI = "ein URL"
DB_USER = "un usuario"
DB_PASS = "el llave secreto"

Setting up the QR code endpoint is a bit more complex. It requires image processing binaries with the python pillow library. There is a blog post, at least, that describes this.

When deploying a file, all you need to do is issue: ./deploy.sh #files here. We'll explain which endpoints need which files in the next section.

Getting the Code on AWS

The general procedure is as follows:

  1. Make a lambda. Be sure to chose python 3 as the language and choose to upload a zip.
  2. Run the deploy script: ./deploy.sh #files here... to obtain a dep.zip file.
  3. Upload the dep.zip and put filemname.function as the function name (so AWS knows what to execute). This is the "Lambda Location" column in the table.
  4. Make the API endpoint. Here, put in the lambda name. We'll tell you about whether or not to lambda proxy integrate and which method to use. Always enable CORS. Also, the methods are recommendations, but we recommend them.
Endpoint URL Endpoint method Lambda Location Proxy integrate? Deploy Command
/authorize POST authorize.authorize No ./deploy.sh authorize.py
/create POST authorize.create_user No ./deploy.sh authorize.py
/mlhcallback GET authorize.mlh_callback Yes ./deploy.sh authorize.py
/validate POST validate.validate No ./deploy.sh validate.py
/update POST validate.update No ./deploy.sh validate.py
/qr POST qru.email2qr Yes See later for more.
/consume POST consume.consumeUrl No ./deploy.sh consume.py validate.py
/createmagiclink POST maglink.genMagicLink No ./deploy.sh maglink.py validate.py use_sparkpost.py read.py
/dayof-events GET cal_announce.google_cal No ./deploy.sh cal_announce.py and ./deploy.sh cal_announce.py calendar-python-quickstart.json after you set up the google calendar API.
/dayof-slack GET cal_announce.slack_announce No ./deploy.sh cal_announce.py
/send-emails POST use_sparkpost.send_emails No ./deploy.sh use_sparkpost.py validate.py read.py
/read POST read.read_info No ./deploy.sh read.py

You'll have to repeat the four steps above for each endpoint in the API. Important: when I say deploy an endpoint I mean that you should follow the 4 steps above for the row corresponding to the endpoint I'm on about.

Step 2: Preparing Third Parties

These can happen in any order. Depending on the way you'd like to run the hackathon, most (if not all) are optional. We'll mention features that you'll miss should you choose not to use a third party.

MLH

Needs and needed for:

  • mlhcallback endpoint.

You have to make an app. Don't fret about the logo - you can modify it whenever. Set the callback URL to the URL of the mlhcallback endpoint that you made above. Then copy the secrets into the fields of the same name in your copy of config.py. The relevant lines are:

MLH = {
    'client_id': 'an ID',
    'client_secret': 'ssh. It's a secret',
    'redirect_uri': 'where do we go now?',
    #As a matter of fact, the below eedn't change.
    'grant_type': 'authorization_code'
}

Deploy the mlhcallback endpoint and feel free to test out your implementation through MLH.

Sparkpost

Needed for:

  • send-emails
  • createmagiclink
    • This is needed to support users having forgotten their passwords.

After you make an account, make an API key. If you want to specify permissions, we need at least "Templates: Read/Write" and "Transmissions: Read/Write". Put this API key in config.py.

Make templates with substitution parameter "link" and IDs:

  • forgot-password so that when users forget passwords
  • judge-invite to invite judges from the frontend. See Sparkpost's docs if you want to know too much about the substitution parameter. We use the link to link users back into you with the extra information that lets us process them correctly.

Redeploy the send-emails and createmagiclink endpoints for everything to work.

Google Calendar

Needed for:

  • dayof-events

First, add the API. You have to do some non-trivial google-API-fu which I'll leave up to Google to poorly document. You only need step 1 in the google quickstart - we did the rest of the copy-pasting already.

On your favorite UNIX-like shell (Mac, Linux, the average in-flight entertainment system, etc.), you basically run the following (assuming you're in the LCS directory and have done the previous step (step 1 - you don't need sparkpost or MLH here.)):

source bin/activate  #enter the virtual environment
python -c 'google_cal({}, None, True)' cal_announce.py
cp /tmp/calendar-python-quickstart.json .
./deploy.sh cal_announce.py calendar-python-quickstart.json

Then upload the dep.zip to your /dayof-events lambda. You'll have to include that JSON file on every redeploy of the dayof-events endpoint.

Slack

Needed for:

  • dayof-slack

I'll let slack's poor docs take over. Once you have a token (xoxp-some-hash-thats-long) and a channel URL (Cthings), you can throw them into the config.py:

SLACK_KEYS = {
    'token': '',
    'channel': ''
}

Re-deploy the dayof-slack endpoint and you should be good to go!

Some Deployment Tricks

Oh no, I lost the configs I used to deploy

Go to the lambda you're interested in on AWS - as if you were updating it. On the top of the page, click the actions drop-down and select "export function". A pop-up pops up - click the orange "Download Deployment Package" button and save the file. Though it isn't labelled as such, it is a zip, so unzip it as you please. The config file will be there. I do not recommend that you just re-zip the zip file, but you can if you want to - git just may miss some changes.