Deploying - Aimlackies/Reporter GitHub Wiki

To host the Flask app we are using Google Cloud but the same set-up instructions can be applied with minor modifications to any Virtual Private Server (VPS) and MySQL host.

VPS set-up

  1. Click on create instance in Google Compute Engine
  2. Select the type of server required for hosting. At present the Flask App only uses a single server so the size of this will set how much load the application can handle. For testing the smallest size will work fine. Make a note of the region and zone as these should be the same for the MySQL server to minimise latency.
  3. The image should be set to Ubuntu with the version set to the latest Long Term Support (LTS). Access scopes can either be Allow default access or Allow full access to all Cloud APIs
  4. The firewall should allow both HTTP and HTTPs traffic if testing but HTTP traffic should be disabled at a later date when the site is made live or is used in production.
  5. Click create. Once this is done make a note of the external IP address.
  6. Start the VPS
  7. Create a Firewall rule to allow tcp on port 8080 (If the site should be accessable on that port)

MySQL set-up

  1. Click on create instance in Google SQL
  2. Click MySQL
  3. Set an instance ID and password. Write the password down
  4. Set the database version to 8
  5. Select the region that matches the VPS, zone can be multiple or the same as the VPS (selecting a single zone should only be selected when testing)
  6. Select an appropriate machine type and storage amount
  7. click create instance
  8. Inside the instance navigate to Databases and create a database for the Flask app. Give it an appropriate name.
  9. Inside the instance navigate to Users and create a use for the Flask app to access the database. Note down the username and password
  10. Inside the instance navage to Connections and add the compute VPS to Authorized networks with the IP address you saved when setting up the VPS (you can find this again by navigating back to the Compute Engine). If the Flask app does not have an SSL certificate Allow only SSL connections will need to be disabled but this should NOT be done on production.
  11. Start the MySQL server

Configuring the Flask app on the VPS

  1. Make sure both the VPS and MySQL servers are running

  2. Connect to the VPS via SSL

  3. Download the latest version of Anaconda e.g. wget https://repo.anaconda.com/archive/Anaconda3-2021.05-Linux-x86_64.sh

  4. Install Anaconda: bash Anaconda3-2021.05-Linux-x86_64.sh

  5. Restart the VPS and reconnect once this is complete

  6. Update `~/.bashrc to load conda for non interactive users. This allows cron jobs to use it

    i. Open ~/.bashrc

     nano ~/.bashrc
    

    ii. Cut the content for Conda to the top of the file. This will be marked by comments stating when Conda in initilised. Once done the original Conda commands can be deleted. Save and exit nano.

    iii. Reload .bashrc (This will also need to be re-run on every reboot of the system / reconnect via SSH)

    source ~/.bashrc
    
  7. Install postfix and configure to allow local messaging. This is used for logging cron jobs. If it is not installed cron jobs will still function but logging will not.

    sudo apt-get install postfix
    
  8. Set the timezone on the VSP to Europe/London

    sudo timedatectl set-timezone Europe/London
    
  9. Clone repository to your local machine.

    git clone https://git.cardiff.ac.uk/aimlackies/reporter.git
    
  10. Create a Conda environment and install Python packages. The Conda enviroemtn is set to use Python 3.8 as, at the time of creation, that was the latest version PyTorch supported. Ensure compatibility with dependencies before upgrading.

    conda create -n aimlacReporter python=3.8
    conda activate aimlacReporter
    pip install -r requirements.txt
    
  11. Set-up environmental variables for config.py
    For some aspects of the web application to function we will need to provide a variable value that should NOT be added to the repository. We will therefore set these up as environmental variables that the web application can access, but is not set as part of the application. i.

    nano ~/.bash_profile
    
    

    ii. Enter the following variables

    export AIMLAC_CC_MACHINE=34.72.51.59
    export AIMLAC_API_KEY=AIMLACkies275001901
    export FLASK_APP=reporter.py
    export AIMLACKIES_REPORTER_SECRET_KEY= # Enter your generated secret key (e.g. openssl rand -base64 64)
    export AIMLACKIES_REPORTER_SECURITY_PASSWORD_SALT= # Enter your generated password salt (e.g. secrets.SystemRandom().getrandbits(128))
    export AIMLACKIES_REPORTER_DATABASE_URL=mysql+pymysql://reporter_db_user:reporter_db_password@MySQL_IP/local_reporter
    
    # reporter_db_user = database user name
    # reporter_db_password = database user password
    # MySQL_IP = MySQL server public IP address
    # local_reporter = local database name
    
    # generate secure random varables on your local machine. This can be done use command line utilities such as `openssl rand -base64 64`
    # or in python with `secrets.SystemRandom().getrandbits(128)`
    
    

    iii. Save the variables

    source ~/.bash_profile
    
  12. Initiate, create and seed your local database with a default admin user. The commands to migrate and upgrade the database will also be used to make updates to the database schema. The default admin user will have the email [email protected] and the password password. The password should be changed straight away. sh flask db init flask db upgrade flask seed

  13. Load in cron tasks from the backup file

    crontab ./crontab.bak
    
  14. Start the application You done! The web application will now run. The first command will start the Flask app but will only keep it running as your terminal is running. The second command will start this in a blind process which will stay running until it is stopped. gunicorn -b 0.0.0.0:8080 reporter:app