HomeUniteUs icon indicating copy to clipboard operation
HomeUniteUs copied to clipboard

Deploy app to EC2

Open erikguntner opened this issue 2 years ago • 2 comments

Overview

Test out building and deploying client and server to EC2 for different environments (e.g. dev, prod, test, etc.). Possibly setup Github action to do this in PRs.

Action Items

For our existing EC2 instance as of date (2023 July):

Focus is on dev.homeunite.us, the development/test environment

  • [x] Install system-wide python 3.10 for use by the API
  • [x] Make application available to a WSGI server
  • [x] Manual upload of .env containing API configuration until we have another configuration system setup
  • [x] Create directory tree: /opt/homeuniteus-previous-builds/previous and /opt/homeuniteus-previous-builds/archives.
  • [x] Create GitHub actions to install the latest version of main and run it on EC2. Roughly: 1. Build and package API as a "wheel" (a binary distribution of a Python package that can be installed directly into an environment) 2. Upload the package to the EC2 instance 3. Stop the homeunitus service 4. Move /opt/homeuniteus/* to /opt/homeuniteus-previous-builds/previous (the .env file remains) 5. Change directory to /opt/homeuniteus 6. Create a python 3.10 environment in /opt/homeuniteus: python -m venv homeuniteusenv 7. Activate the python environment source homeuniteusenv/bin/activate 8. Install the new API version into the environment: pip install ~/github-deploy/api/openapi_server-*-.whl. 9. Startup the homeuniteus service. If fail, move /opt/homeuniteus-previous-builds/previous to /opt/homeuniteus, start service, exit 1. 10. Archive /opt/homeuniteus-previous-builds/previous directory into /opt/homeuniteus-previous-builds/archives/homeuniteus-<date>.tar.gz and delete contents of /opt/homeuniteus-previous-builds/previous and delete older than 3 (arbitrary choice) archived versions from /opt/homeuniteus-previous-builds/archives/ .

For the frontend app:

  • [x] Upload frontend build to the EC2 instance
  • [x] Make a backup of the previous files
  • [x] move the new frontend build into the directory served by nginx

erikguntner avatar Feb 22 '23 01:02 erikguntner

Install Python 3.10

~The current EC2 instance is Ubuntu 18.04. The system comes with python 2.7 and python 3.6 by default. The deadsnakes PPA repository contains updated versions of Python for Ubuntu 20.04 and 22.04 not Ubuntu 18.04.~ EDIT 2023-07-23: The instance has been updated to Ubuntu 22.04. The system version of Python is 3.10.6. The following is still true.

I used pyenv to install Python 3.10.12 for the ubuntu user using using these steps:

  1. Installed necessary tools to build different Python versions:

    sudo apt install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev lzma python-openssl git
    
  2. Installed pyenv in the ubuntu user directory: curl https://pyenv.run | bash This creates a directory: /home/ubuntu/.pyenv where pyenv is installed.

  3. Added commands to setup ubuntu user's shell by adding the following to the end of /home/ubuntu/.profile:

    export PYENV_ROOT="$HOME/.pyenv"
    command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH" 
    eval "$(pyenv init -)"    
    

    Logged out and back in to active pyenv

  4. Installed Python 3.10.12: pyenv install 3.10.12

  5. Make python 3.10.12 the default version for the user ubuntu. The homeuniteus system service will be run as this user. The command to make this happen: pyenv global 3.10.12.

paulespinosa avatar Jul 19 '23 15:07 paulespinosa

The server is now running Ubuntu 22.04 by upgrading to 20.04 and then to 22.04.

5.19.0-1028-aws # 29~22.04.1-Ubuntu SMP Tue Jun 20 19:12:11 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

Postgresql was upgraded to version 14 by upgrading and migrating data from 10 to 12 to 14.

The system Python version is 3.10.6. The system has pyenv installed. It was used to install Python 3.10.12 and set as the default version for the ubuntu user.

paulespinosa avatar Jul 23 '23 07:07 paulespinosa