InterviewSolution
| 1. |
Deploy Flask application with Fabric. |
|
Answer» Fabric is a Python program that WORKS similarly to Makefiles but also allows you to run commands on a remote server. Flask applications may be easily deployed to other servers when used in conjunction with a well configured Python package (LARGE Applications as Packages) and a good configuration concept (Configuration Handling). Before we get started, here's a quick rundown of what we need to be sure of:
Create a simple Fabfile: Fabric's execution is controlled by a fabfile. It's called fabfile.py, and it's run with the fab program. All of the functions in that file will be AVAILABLE as fab subcommands. They are run on a single or several hosts. These hosts can be specified on the command line or in the fabfile. We'll add them to the fabfile in this scenario. This is a simple initial example that allows you to upload current source code to the server and install it into a virtual environment that already exists: from fabric.api import *[Text Wrapping Break][Text Wrapping Break]# the USER to use for the remote commands[Text Wrapping Break]env.user = 'appuser'[Text Wrapping Break]# the servers where the commands are executed[Text Wrapping Break]env.hosts = ['server1.example.com', 'server2.example.com'][Text Wrapping Break][Text Wrapping Break]def pack():[Text Wrapping Break] # build the package[Text Wrapping Break] local('python setup.py sdist --formats=gztar', capture=False)[Text Wrapping Break][Text Wrapping Break]def deploy():[Text Wrapping Break] # figure out the package name and version[Text Wrapping Break] dist = local('python setup.py --fullname', capture=True).strip()[Text Wrapping Break] filename = f'{dist}.tar.gz'[Text Wrapping Break][Text Wrapping Break] # upload the package to the temporary folder on the server[Text Wrapping Break] put(f'dist/{filename}', f'/tmp/{filename}')[Text Wrapping Break][Text Wrapping Break] # install the package in the application's virtualenv with pip[Text Wrapping Break] run(f'/var/www/yourapplication/env/bin/pip install /tmp/{filename}')[Text Wrapping Break][Text Wrapping Break] # REMOVE the uploaded package[Text Wrapping Break] run(f'rm -r /tmp/{filename}')[Text Wrapping Break][Text Wrapping Break] # touch the .wsgi file to trigger a reload in mod_wsgi[Text Wrapping Break] run('touch /var/www/yourapplication.wsgi')So, how do you put that fabfile into action? The fab command is what you use. This command would be used to deploy the current version of the code to the remote server: $ fab pack deploy |
|