This code is used for the purposes of COMS 4156 - Advanced Software Engineering course at Columbia University, New York. This is a boilerplate python flask code along with configurations for Circle CI and Google Cloud.
Please log into github before progressing with the next steps.
Grab your free google credits by following this post in piazza.
Fork this repository into your github. From now on, we will be working on your forked repository.
How to fork ?
Look out for a button called fork
in the top right of this page. It will create a replica of this repository and put it in your github account.
Open termnal -> go to any folder -> clone your forked repository into your local folder.
How to clone?
Look out for a button called clone
in the top right of the repository. Once you click that you will get a link like this https://github.com/<your-name>/<your-repository-name>.git
Copy that and go to your folder and type the command:
git clone <your-forked-repository-GIT-URL>
Now set this repository as your origin.
How?
git remote add origin <your-forked-repository-GIT-URL>
Login into Google Cloud using the account that you used for free credits. (Note: If you signed in using another account, switch account to the one that you used to grab free credits.)
- If you are using google cloud first time, it will ask you to accept some terms, please do so.
- On the top left -> click on
select a project
. If you have already used google cloud before and you have also created a project before, then you may see another project name there, even then click on it. - It will open a small pane -> Ensure that organization is
columbia.edu
, if not then select that. - Then, click on
+
button - Give a project name say in this case
ase-boilerplate
. - Also, edit the Project ID and set it also to
ase-boilerplate
other wise, google adds some randome digits to the Project ID and we need this Project ID through out. So it is better if Project ID and Project Names are same. - click
Create
. - Search for
App Engine
-> Ensure that Google App Engine Services are available. Upon seeing the home page of App Engine and choosing a language is taking you to a tutorial then it means the services are available.
- Go to CircleCI
- Sign up for a free account. You can login using github (easy way). Else, you can signup and later authorize github account.
- Click on
projects
on the left pane ->Add Project
->Setup Project
(Note: it might take sometime to sync the projects in your github into the Circle CI.) - Choose the following:
- Operating System - Linux
- Platform - 2.0
- Language - Python
- click
start building
- click on
Console
- In the Search Bar -> search for
Google App Engine Admin API
-> clickEnable
- Click on "Credentials" -> click on
Create Credential
-> selectService Account Key
- From the drop down of Service Account -> select
Compute Engine default service account
- For the Key Type: select JSON
- click
create
. - A json file with name similar to
<ase-boilerplate-some-number>.json
will be downloaded on to your system. Remember the folder it is downloaded to, we need this later.
- Go to CircleCI -> click on
app
- Click on
Projects
- Click on little gear (notation for settings!) that is next to your project which is
ase-boilerplate
- Click on
Environment Variables
- Click on
Add Variable
- For Name:
CLIENT_SECRET
- Now, go to the folder where JSON file is downloaded previously and run this command:
base64 <ase-boilerplate-some-number>.json | pbcopy
It encodes the file into base64 format and copies into your clipboard. - Now go back to browser and for value: press the paste buttons (CMD + V on Mac / CTRL + V on windows)
- Again Click on
Add Variable
- For Name:
GCLOUD_PROJECT_ID
- For Value: put your your Project ID from google cloud.
How do I find my project Id?
- Go to Google Cloud and on top left select the project.
- It opens a new pane, in that look for the Project ID corresponding to the project name you gave. In our case, it is
ase-boilerplate
Step 8 would trigger a new build and release in Circle CI. To verify:
- Go to
projects
->your project
-> open the lates build which will be likemaster #some-number
- If everything is good -> build will succeed and the app will be deployed into google app engine.
- you can also verify that by going to
https://<your-gcloud-project-id>.appspot.com
which will show a message like starting withhello
- Home Page -> Menu -> SQL
- Create Instance -> Select MySQL -> Select Second Generation
- give a name (may be "mysql1" )-> and give a simple password (may be "root").
- Next, wait for couple of minutes for google cloud to process your request and create an instance
- Once, it is ready, you will get a green tick next to it.
- Click on "mysql1" (or your instance name) once you see a green tick next to it.
- There will be a pane called
Connect to this instance
. In that copy the value ofInstance connection name
.
- Go to your code
- go to app.yaml -> update
<YOUR_CONNECTION_NAME>
withInstance connection name
you copied in previous step - likewise update
<YOUR_USERNAME>
with username and<YOUR_PASSWORD>
with password. - Commit your code and push it to repo. It will trigger a build in Circle CI.
- Once your build has succeeded, then go to
https://<your-gcloud-project-id>.appspot.com/databases
and you should see a list of schemas/databases that come by default in your mysql instance.
- Go to Home Page -> Menu -> SQL -> "mysql1" (or your instance name)
- Click on Authorization tab -> For
name
put something like "HOME IP" -> forNetwork
put your public IP address. This is called whitelisting IP address. Google cloud or any cloud providers by default block access from new IP addresses to their services. How do you find your public IP ? Google Search -> "myip" - Then click save.
- Go to
overview
tab ->Connect to this instance
->IPv4 address
-> copy IP Address. You need this to connect to mysql from you local computer. - Open Terminal ->
mysql --host=<IP_ADRESS_YOU_COPIED_PREIVOUSLY> --user=root --password=<YOUR_MYSQL_INSTANCE_PASSWORD>
- Once you login succefully: try
show schemas
- That will give you the same result as going to
https://<your-gcloud-project-id>.appspot.com/databases
If you don't have Python 2.7 in your system, then follow along:
- Download Anaconda with Python version 3.6 (Don't get confused about 3.6 here!! We will be creating virtual environment with 2.7 instead of using Anaconda with Python 2.7 version which is a good practice!)
- Install Anaconda by double clicking the .dmg file
- After installation -> Terminal ->
conda create -n python2env python=2.7
source activate python2env
- At this point you have activated virtual environment named
python2env
with packagespython=2.7
. So now if you testpython --version
you will seepython 2.7
as output - At this point, continue your previous work with this virtual environment active.
- run
easy_install -U pip
- Once you are done, deactivate the environment with
source deactivate
- Fun Check: try
python --version
now! (it will show your default python version installed in your system) - Before moving to next step do
source activate python2env
as running the system locally requires python2.7 and also we will be installing packages mentioned in requirements.txt in this virtual environment instead of your local base installation.
-
Following this link and perform the 3 steps under the
Interactive Installer
section corresponding to your operating system. For our case it is Mac OS (vice versa you can choose Linux / windows based on your OS):
Google Cloud SDK -
Following this link App Engine extension for Python and perform the steps below (and skip if you already have it):
- 1st step (Check your python version by typing
python --version
in you terminal / shell. If it is python 2.7, then skip the step, else check out optional Step 12 in this file.) - 3rd step (perform this step)
- 4th step (Check if you have git installed by typing
git
. If yes, then skip, else perfrom this step. ) - 5th step (perform this step)
- 1st step (Check your python version by typing
-
In your local system ->
cd <your-repository>
in our casecd ase-boilerplate
-
mkdir -p lib
-
pip install -r requirements.txt -t lib
-
pip install -r requirements.txt
. (This is extra step if you are using database setup). -
dev_appserver.py ./
-
Now, you will be running the application locally (in locally mimicked App Engine framework!). So now checkout localhost:8080. You should be greeted with
hello
message -
After you are done, press
ctrl+c
to stop the local server.
Copyright (C) 2018 Columbia University.