Blog moved to: New address (click to go directly)
New blog using markdown maintenance, the offline version of the library, write their own, so will be timed to update synchronization, the same time to provide a better navigation and reading experience
CSDN to Markdown support is not good, so the old version will not take the time to synchronize revisions, sorry
-----------------------------------------------
Fabric
Title is development, but the same time to do the development test and operation of the work ... For Mao Task*3 Not salary * 3 (O (╯-╰) o)
Recently took over more and more things, the publication and operation of the work is quite mechanical, coupled with the frequency is quite high, resulting in time wasted or more advantages.
Fix bugs, test, submit version number library (2 minutes), SSH to test environment pull deployment (2 minutes), rsync to online machine a,b,c,d,e (1 minutes), SSH to ABCDE five machines, one by one reboot (8-10 minutes) = 13-15 minutes
Among the depressed is that each operation is the same, the same command, deadly in a number of machines, very difficult in this machine a script, the main time is wasted in ssh, Knock command, written script, fully able to run a button, spend two minutes to see the results of the run
Until we found the fabric.
Role
A very powerful tool
The ability to solidify the commands of a self-initiated deployment or multi-machine operation into a single script
And some OPS tools are very much like, using it mainly because, Python .....
Easy to use and easy to get started
Of course, shell commands can be combined to make the difference between ancient artifacts and modern weapons.
Environment configuration
Install the appropriate package on the machine and the target machine (note, all must have)
sudo easy_install fabric
This is the 1.6 version number (or the same as Pip install)
After installation, you can see if the installation was successful
[[email protected]~$] which fab/usr/local/bin/fab
After loading, you can browse the official documents
And then, you can do it.
Hello World
First the simple operation of the machine, there is a preliminary understanding, sample source and official website
Create a new PY script: fabfile.py
def hello(): print("Hello world!")
Command line run:
[[email protected]~/tmp/fab$] fab helloHello world!Done.
Note that it is not possible to use Fabfile as a file name, but you need to specify the file at run time
[[email protected]~/tmp/fab$] mv fabfile.py test.pyfabfile.py -> test.py[[email protected]~/tmp/fab$] fab helloFatal error: Couldn‘t find any fabfiles!Remember that -f can be used to specify fabfile path, and use -h for help.Aborting.[[email protected]~/tmp/fab$] fab -f test.py helloHello world!Done.
Number of references:
To change the fabfile.py script:
def hello(name, value): print("%s = %s!" % (name, value))
Run
[[email protected]~/tmp/fab$] fab hello:name=age,value=20age = 20!Done.[[email protected]~/tmp/fab$] fab hello:age,20age = 20!Done.
Run a native operation
Simple local operation:
from fabric.api import localdef lsfab(): local(‘cd ~/tmp/fab‘) local(‘ls‘)
Results:
[[email protected]~/tmp/fab$] pwd;ls/Users/ken/tmp/fabfabfile.py fabfile.pyc test.py test.pyc[[email protected]~/tmp/fab$] fab -f test.py lsfab[localhost] local: cd ~/tmp/fab[localhost] local: lsfabfile.py fabfile.pyc test.py test.pycDone.
Start the actual combat:
If you are submitting a copy of the configuration file settings.py to the version number library every day (there is no conflict case)
Let's say manual operation:
cd /home/project/test/conf/git add settings.pygit commit -m ‘daily update settings.py‘git pull origingit push origin
In other words, these commands you have to manually knock every day, the so-called daily job, is to be repeated every day, mechanized work, let us see how to achieve a key with the fabric: (its useful shell script can be directly done, but the advantage of FAB is not here, Here the main bit behind the local + remote operation to prepare, after all, two places to write a script for easy maintenance)
from fabric.api import localdef setting_ci(): local("cd /home/project/test/conf/") local("git add settings.py") #后面你懂的,懒得敲了…..
Mashup Integration Remote operation
At this time, if you want to go to machine A's/home/ken/project corresponding project folder to update the configuration file
#!/usr/bin/env python# encoding: utf-8from fabric.api import local,cd,runenv.hosts=[‘[email protected]:port‘,] #ssh要用到的參数env.password = ‘pwd‘def setting_ci(): local(‘echo "add and commit settings in local"‘) #刚才的操作换到这里,你懂的def update_setting_remote(): print "remote update" with cd(‘~/temp‘): #cd用于进入某个文件夹 run(‘ls -l | wc -l‘) #远程操作用rundef update(): setting_ci() update_setting_remote()
Then, run it:
[[email protected]~/tmp/fab$] fab -f deploy.py update[[email protected]:port] Executing task ‘update‘[localhost] local: echo "add and commit settings in local"add and commit settings in localremote update[[email protected]:port] run: ls -l | wc -l[[email protected]:port] out: 12[[email protected]:port] out:Done.
Note that, assuming that Env.password is not declared, the interaction that requires a password is taken out when running to the appropriate machine
Multi-server Mashup
Multi-server operation, multiple host configuration required
#!/usr/bin/env python# encoding: utf-8from fabric.api import *#操作一致的服务器能够放在一组,同一组的运行同一套操作env.roledefs = { ‘testserver‘: [‘[email protected]:port1‘,], ‘realserver‘: [‘[email protected]:port2‘, ] }#env.password = ‘这里不要用这样的配置了,不可能要求密码都一致的,明文编写也不合适。打通全部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)
Results:
[[email protected]~/tmp/fab$] fab -f mult.py dotask[[email protected]:port1] Executing task ‘task1‘[[email protected]:port1] run: ls -l | wc -l[[email protected]:port1] out: 9[[email protected]:port1] out:[[email protected]:port2] Executing task ‘task2‘[[email protected]:port2] run: ls ~/temp/ | wc -l[[email protected]:port2] out: 11[[email protected]:port2] out:Done.
Extended
1. Color
Ability to print colors that are more eye-catching and convenient when viewing operational results information
from fabric.colors import *def show(): print green(‘success‘) print red(‘fail‘) print yellow(‘yellow‘)#fab -f color.py show
2. Errors and exceptions
About error Handling
Default, a set of commands that do not run down after the last command fails
Can be processed differently after a failure, the document
It doesn't work right now.
3.password Management
Read the document
Better password management way, brother compared to the soil, not get through, mainly the server list changes frequently, my processing method is:
1.host,user,port,password configuration list, all written in one file
or directly into the script, of course, this is more ...
Env.hosts = [
' Host1 ',
' Host2 '
]
Env.passwords = {
' Host1 ': "Pwdofhost1",
' Host2 ': "Pwdofhost2",
}
or
 
Env.roledefs = {
' TestServer ': [' host1 ', ' host2 '],
' Realserver ': [' host3 ',]
}
Env.passwords = {
' Host1 ': "Pwdofhost1",
' Host2 ': "Pwdofhost2",
' Host3 ': "Pwdofhost3",
}
2. Based on key parsing into map nesting, put into deploy
In addition, the command can actually solidify into a cmds list ....
At the beginning of the use of these, there might be a lot of other requirements when you go to the document, saying that the document is really a lot of good things, is too much, looked dizzy ...
The end!
To be continue ....
Wklken
Email: [Email protected]
Blog:http://www.wklken.com
2013-03-25
Reprint please indicate the source, thank you