Python fabric remote deployment and pythonfabric deployment
Python fabric Remote Deployment
Requirement Description
In the process of collaborative development projects, code is submitted to the git server almost every day, and then deployed to the test server. It is really boring to repeat several lines of commands every day. What should I do? Automated O & M! Next, let's talk about fabric. We will do some repetitive and complicated work for us. I believe you will like fabric like me!
Background
This project uses the django framework. After code is submitted to the git server every day, you must manually upload the code to the test server and then execute a series of django framework commands. Every day, we have to waste more than 10 minutes doing repetitive work. These jobs are not what a programmer should do...
Solution
With the help of the fabric module of Python, commands for automated deployment or multi-machine operation can be solidified into a script, and then executed through this script.
Install fabric
Note: Install both the local server and the target server.
Sudo easy_install fabric
Or use pip to install:
Pip install fabric
Write scripts
Local is executed on the local machine; run is executed on the remote machine.
From fabric. api import hosts, run, env, local, cd, get, lcdfrom fabric. tasks import executeenv. 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 test server code" with cd ("/opt/project"): # enter the project directory of the test Server run ("git pull origin master ") # Pull down the latest code run ("/usr/local/bin/python2.7/opt/project/manage from the master branch of the git server. py makemigrations ") # This is the django framework command to detect database changes run ("/usr/local/bin/python2.7/opt/project/manage. py migrate ") # This is the django framework to execute Database Change command @ hosts (" ktv@192.168.1.101: 22 ") def restart (): "Restart service" execute ('stop') execute ('start') @ hosts ("root@192.168.1.101: 22") def start (): "start Service" with cd ("/opt/project"): run ("supervisorctl start dev") @ hosts ("ktv@192.168.1.101: 22 ") def stop (): "stop Service" "pids = run (" ps-ef | grep '000000' | 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
The above script is saved as fabfile. py (you can also save it as another name, but the running command is different, which will be detailed below)
Execute scripts
If your script is named fabfile. py, enter the directory of your fabfile. py on the terminal and press enter as follows:
Fab update
Then, you will see the terminal prompts you to enter the git account and password. After you have entered the password, the code of the git server will be automatically pulled down to the test server.
Run the following command to restart the service:
Fab restart
If your file name is another name, such as AB. py, executing fab update/restart is incorrect. How can we run it on the cloud?
Fab-f AB update
Fab-f AB restart
Note: fabric is quite powerful. This article only lists a small feature. For more information, see the official document http://docs.fabfile.org/en/1.6/.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.