This document uses fabric to automatically deploy its own Webtools project, saving time on the Go . Fabric Installation
The fabric installation is straightforward apt-get install fabric, but installing on Windows will cause the following error: Missing Visual C + + 9.0. You only need to download the Microsoft Visual C + + Compiler for Python 2.7 installation. When installed, a Fab.exe file is generated in the scripts directory and added to the environment variable. 0x02. Fabric Basic Syntax
The basic syntax of fabric is simple, and I'll just say a few common functions here.
The first step is to import the module, from FABRIC.API Import run, the Cd,run command is the function that executes the command on the remote server, and the CD is the function of the jump directory. For example, to execute the LS command under the/home directory of the remote server, the code is as follows:
From FABRIC.API import Run, CD
def Exec_code (): With
CD (' Home '):
run (' ls ')
Only the Fab exec_code-h User@remote_host is executed locally, and the program automatically alerts you to enter a password to log in and perform the relevant action. 0x03. Automating the deployment of Django using fabric
My needs are simple, and I need to redeploy the Django project on the Raspberry Pi every time I finish writing, and the settings.py and wsgi.py files are different from the development environment, so they can't be overwritten. So my basic deployment steps are: Fetch from git, jump to the project directory, delete all files except settings.py and wsgi.py to copy the new project over to the settings.py and wsgi.py pip install new Library Perform migrate restart Apache
The code that is summed up is shown below, using the Fab push_code-h Xx@xx.xx.xx.xx,fabric just provides a framework that automatically determines whether a command executes successfully, executes unsuccessfully, stops running, and is able to automatically prompt the program to log in to SSH, which allows for faster batch deployment, such as installing Apache or MySQL software:
# Coding=utf8 from
FABRIC.API import run, CD
def push_code (path= '/home/webtools-rep '): With
CD (path):
run (' Git fetch Origin master ') with
CD ('/home/webtools '):
run (' mv/home/webtools/webtools/settings.py/ Home ') run ('
mv/home/webtools/webtools/wsgi.py/home ') run ('
rm-rf * ')
run (' Cp-r/home/webtools-rep/ webtool/*./') run ('
mv/home/settings.py./webtools ') run ('
mv/home/wsgi.py./webtools ')
run (' Source/ Home/env/bin/activate ')
run (' Pip Install-r/home/webtools/requirements.txt ')
run (' Python manage.py Migrate ')
run ('/etc/init.d/apache2 restart ')