Python fabric usage notes
This article mainly introduces the python fabric usage notes. fabric is a powerful tool for remote operations and deployment. This article provides multiple instances for use. For more information, see
Fabric
Title is development, but at the same time it is necessary to do development, testing, and O & M ...... For the Mao task * 3, not salary * 3 (o (Clerk □clerk) o)
More and more things have been taken over recently. The release and O & M work is very mechanical, and the frequency is quite high, which leads to a waste of time and many advantages.
Fix bugs or something, test, submit the version Library (2 minutes), ssh to the test environment pull deployment (2 minutes), rsync to online machines A, B, C, D, E (1 minute), ssh to five machines of ABCDE respectively, restart one by one (8-10 minutes) = 13-15 minutes
What is depressing is that each operation is the same, and the command is the same. It is terrible that it is on multiple machines, and it is difficult to handle it in one script on the local machine. The main time is wasted on ssh, the command is typed and written as a script, which can be executed with one click. It takes two minutes to view the execution result.
Until fabric is found.
Function
Powerful tools
The command for automatic deployment or multi-machine operation can be solidified into a script.
Similar to some O & M tools, it is mainly used because python .....
Easy to use
Of course, shell commands can also be combined. The difference between ancient artifacts and modern weapons
Environment Configuration
Install the corresponding package on the local machine and the target machine (note that you must have the package)
The Code is as follows:
Sudo easy_install fabric
The current version is 1.6 (or pip install, the same)
After installation, you can check whether the installation is successful.
The Code is as follows:
[Ken @~ $] Which fab
/Usr/local/bin/fab
After installation, you can browse the official document http://docs.fabfile.org/en/1.6/
Then, you can start it.
Hello world
First, we have a preliminary understanding of the simple operations on the local machine. The example is from the official website.
Create a new py Script: fabfile. py
The Code is as follows:
Def hello ():
Print ("Hello world! ")
Command Line execution:
The Code is as follows:
[Ken @~ /Tmp/fab $] fab hello
Hello world!
Done.
Note: you do not need to use fabfile as the file name, but you must specify the file name during execution.
The Code is as follows:
[Ken @~ /Tmp/fab $] mv fabfile. py test. py
Fabfile. py-> test. py
[Ken @~ /Tmp/fab $] fab hello
Fatal error: Couldn't find any fabfiles!
Remember that-f can be used to specify fabfile path, and use-h for help.
Aborting.
[Ken @~ /Tmp/fab $] fab-f test. py hello
Hello world!
Done.
With parameters:
Modify the fabfile. py script:
The Code is as follows:
Def hello (name, value ):
Print ("% s = % s! "% (Name, value ))
Run
The Code is as follows:
[Ken @~ /Tmp/fab $] fab hello: name = age, value = 20
Age = 20!
Done.
[Ken @~ /Tmp/fab $] fab hello: age, 20
Age = 20!
Done.
Perform local operations
Simple local operations:
The Code is as follows:
From fabric. api import local
Def lsfab ():
Local ('CD ~ /Tmp/fab ')
Local ('LS ')
Result:
The Code is as follows:
[Ken @~ /Tmp/fab $] pwd; ls
/Users/ken/tmp/fab
Fabfile. py fabfile. pyc test. py test. pyc
[Ken @~ /Tmp/fab $] fab-f test. py lsfab
[Localhost] local: cd ~ /Tmp/fab
[Localhost] local: ls
Fabfile. py fabfile. pyc test. py test. pyc
Done.
Start:
Assume that you submit a configuration file settings. py to the version library every day (conflicts are not considered here)
For manual operations:
The Code is as follows:
Cd/home/project/test/conf/
Git add settings. py
Git commit-m'daily update settings. py'
Git pull origin
Git push origin
That is to say, You need to manually repeat these commands every day. The so-called daily job is a mechanical job that repeats every day, let's take a look at how fabric can be implemented with one click: (In fact, it can be done directly using shell scripts, but the advantages of fab are not here. Here we will mainly prepare for local + remote operations, after all, you can write a script for operations in two places to facilitate maintenance)
The Code is as follows:
From fabric. api import local
Def setting_ci ():
Local ("cd/home/project/test/conf /")
Local ("git add settings. py ")
# You know later, you're too lazy to think about it .....
Mix and match integration remote operations
At this time, assume that you want to update the configuration file in the project directory of/home/ken/project of machine.
The Code is as follows:
#! /Usr/bin/env python
# Encoding: UTF-8
From fabric. api import local, cd, run
Env. hosts = ['user @ ip: port',] # parameters to be used by ssh
Env. password = 'pwd'
Def setting_ci ():
Local ('echo "add and commit settings in local "')
# Switch the operation here, you know
Def update_setting_remote ():
Print "remote update"
With cd ('~ /Temp '): # cd is used to enter a directory.
Run ('LS-l | wc-l') # run
Def update ():
Setting_ci ()
Update_setting_remote ()
Then, execute:
The Code is as follows:
[Ken @~ /Tmp/fab $] fab-f deploy. py update
[User @ ip: port] Executing task 'update'
[Localhost] local: echo "add and commit settings in local"
Add and commit settings in local
Remote update
[User @ ip: port] run: ls-l | wc-l
[User @ ip: port] out: 12
[User @ ip: port] out:
Done.
Note: If you do not declare env. password, the interaction requiring the password will jump out when you execute it on the corresponding machine.
Multi-server mashups
To operate multiple servers, you must configure multiple hosts.
The Code is as follows:
#! /Usr/bin/env python
# Encoding: UTF-8
From fabric. api import *
# Servers with the same operation can be placed in one group, and the same operation can be performed in the same group.
Env. roledefs = {
'Testserver': ['user1 @ host1: port1 ',],
'Realserver': ['user2 @ host2: port2',]
}
# Env. password = 'do not use this configuration here. It is impossible to require the same password, and it is not suitable for plaintext writing. Just get through all ssh'
@ Roles ('testserver ')
Def task1 ():
Run ('LS-l | wc-l ')
@ Roles ('realserver ')
Def task2 ():
Run ('ls ~ /Temp/| wc-l ')
Def dotask ():
Execute (task1)
Execute (task2)
Result:
The Code is as follows:
[Ken @~ /Tmp/fab $] fab-f mult. py dotask
[User1 @ host1: port1] Executing task 'task1'
[User1 @ host1: port1] run: ls-l | wc-l
[User1 @ host1: port1] out: 9
[User1 @ host1: port1] out:
[User2 @ host2: port2] Executing task 'task2'
[User2 @ host2: port2] run: ls ~ /Temp/| wc-l
[User2 @ host2: port2] out: 11
[User2 @ host2: port2] out:
Done.
Extension
1. Color
You can print the color to make it more eye-catching and convenient to view the operation result.
From fabric. colors import *
Def show ():
Print green ('success ')
Print red ('fail ')
Print yellow ('yellow ')
# Fab-f color. py show
2. Errors and exceptions
Error Handling
By default, a group of commands will not be executed after the previous command fails to be executed.
After the failure, you can also perform different processing, document
It is currently useless. I will try again later.
3. Password Management
View document
Better password management methods: My brother is more earthy and cannot get through, mainly because the server list changes frequently. My solution is as follows:
1. host, user, port, password configuration list, all written in a file
Or directly get the script. Of course, this is more ........
The Code is as follows:
Env. hosts = [
'Host1 ',
'Host2'
]
Env. passwords = {
'Host1': "pwdofhost1 ",
'Host2': "pwdofhost2 ",
}
Or
The Code is as follows:
Env. roledefs = {
'Testserver': ['host1', 'host2'],
'Realserver': ['host3',]
}
Env. passwords = {
'Host1': "pwdofhost1 ",
'Host2': "pwdofhost2 ",
'Host3': "pwdofhost3 ",
}
2. parse the key into map nesting and put it in deploy.
In addition, the command can also be solidified into a cmds list .....
This is used in the beginning. When there are more requirements in the future, I will get the document again. There are so many good things in the document, that is, too much, and I am dizzy...