python fabric implements remote deployment
- 2020-05-19 05:10:03
- OfStack
python fabric implements remote deployment
Requirements describe
In the course of a multi-person collaborative development project, almost every day we had to submit code to the git server and then deploy it to the test server. How to do? Operation and maintenance automation! Now let's talk about fabric. Let's finish some complicated and repetitive work for us. I believe you will like it as much as I do.
Project background
The project we are working on this time is the django framework. Every day after we submit the code to the git server, we have to manually upload the code to the test server, and then execute the command of series 1 django framework. Every day to waste more than 10 minutes of time, doing repetitive labor, these work is not a programmer should do...
The solution
With Python's fabric module, you can solidify commands for automated deployment or multi-machine operations into a single script that can then be executed.
Install fabric
Note: both the native and target servers need to be installed once
sudo easy_install fabric
Or install it with pip:
pip install fabric
Write a script
local is executed natively; run is executed on a remote machine
from fabric.api import hosts, run, env, local, cd, get, lcd
from fabric.tasks import execute
env.hosts = ["fab@192.168.1.101:22", "root@192.168.1.101:22"]
env.passwords = {"fab@192.168.1.101:22": "fab", "root@192.168.1.101:22": "tofabor"}
@hosts("ktv@192.168.1.101:22")
def update():
""" Update the test server code """
with cd("/opt/project/project"): # Enter the project directory of the test server
run("git pull origin master") # from git The server's master Branch down the latest code
run("/usr/local/bin/python2.7 /opt/project/project/manage.py makemigrations") # This is a django A command for the framework to detect database changes
run("/usr/local/bin/python2.7 /opt/project/project/manage.py migrate") # This is a django The framework executes the database change command
@hosts("ktv@192.168.1.101:22")
def restart():
""" Restart the service """
execute('stop')
execute('start')
@hosts("root@192.168.1.101:22")
def start():
""" Start the service """
with cd("/opt/project/project"):
run("supervisorctl start dev")
@hosts("ktv@192.168.1.101:22")
def stop():
""" Stop the service """
pids = run("ps -ef |grep '9001'| awk '{print $2}'")
pid_list = pids.split('\r\n')
for i in pid_list[:-2]:
run('kill -9 %s' % i) # Kill the running service process
Save the above script as fabfile.py (you can also save it as another name, but run the command differently, as described below)
Execute the script
If your script name is fabfile.py, you can enter your fabfile.py directory at the terminal and type the following command enter:
fab update
Next, you will see the terminal prompt you to enter the git account and password, after you successfully enter, will automatically drop down the git server code to the test server.
Then run the following command to restart the service:
fab restart
If your file has a different name, ab.py, then it is wrong to execute fab update /restart. How does the cloud work?
fab -f ab update
fab -f ab restart
Note: fabric is quite powerful. This article just lists 1 small features. If you want to learn more, please see the official document http: / / docs fabfile. org/en / 1.6 /