Python fabric for remote operations and deployment examples

Source: Internet
Author: User
Recently took over more and more things, release and operation of the work is quite mechanical, coupled with the frequency is quite high, resulting in time wasted or a lot of advantages. Fix bugs What, test, submit repository (2 minutes), SSH to test environment pull deployment (2 minutes), rsync to online machine a,b,c,d,e (1 min), ssh to ABCDE five machines, one restart (8-10 minutes) = 13-15 minutes of which depressed is , each operation is the same, the same command, the killer is on multiple machines, it is difficult to do a script in this machine, the main time is wasted in ssh, Knock command, written script, can be executed in one click, spend two minutes to see the results of execution

Until, it was found that the fabric can be automated deployment or multi-machine operation of the command cured into a script and some operations tools very much like, with it mainly because, easy to use easy to get started, of course, the shell of a variety of commands can be combined, the ancient artifact and modern weapon differences

Environment configuration

Install the corresponding package on the machine and the target machine (note, all must have)

sudo easy_install fabric

Currently 1.6 version (or with Pip install, same)

After installation, you can see if the installation was successful

[ken@~$] which fab/usr/local/bin/fab

After loading, you can browse the official documents

Then, you can do it.

Hello World
First, the simple operation of the machine, there is a preliminary understanding, examples of sources and official website

Create a new PY script: fabfile.py

def hello ():    print ("Hello world!")

Command line execution:

[ken@~/tmp/fab$] Fab Hellohello world!

Done.
Note that you can use Fabfile as the file name, but you need to specify the file when executing

[ken@~/tmp/fab$] MV fabfile.py test.pyfabfile.py-test.py[ken@~/tmp/fab$] fab hellofatal error:couldn ' t find any fab files! Remember That-f can is used to specify Fabfile path, and use-h for help. Aborting. [ken@~/tmp/fab$] fab-f test.py Hellohello world!

Done.
With parameters:

To modify the fabfile.py script:

def hello (name, value):    print ("%s =%s!"% (name, value))

Perform

[ken@~/tmp/fab$] Fab hello:name=age,value=20age = 20! Done. [ken@~/tmp/fab$] Fab hello:age,20age = 20!

Done.
Perform a native operation
Simple local operation:

From Fabric.api import Localdef lsfab ():    local (' CD ~/tmp/fab ')    local (' ls ')

Results:

[ken@~/tmp/fab$] pwd;ls/users/ken/tmp/fabfabfile.py   fabfile.pyc  test.py      test.pyc[ken@~/tmp/fab$] Fab -F test.py Lsfab[localhost] local:cd ~/tmp/fab[localhost] local:lsfabfile.py  fabfile.pyc test.py     test.pyc

Done.
Practical Start:

Suppose you are submitting a copy of the configuration file settings.py to the repository every day (there is no conflict case)

If this is done manually:

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 repeat every day, mechanized work, let us see how to achieve a key with fabric: (in fact, with 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    #后面你懂的, too lazy to knock ....

Mashup Integration Remote operation
At this point, suppose you want to update the configuration file to the/home/ken/project corresponding project directory of machine A.

#!/usr/bin/env python# encoding:utf-8from fabric.api import local,cd,runenv.hosts=[' User@ip:port ',] # SSH to use the parameters Env.password = ' pwd ' def setting_ci ():    local (' echo ' Add and commit settings in local "')    #刚才的操作换到这里, You know that. def update_setting_remote ():    print "Remote Update" with    CD (' ~/temp '):   #cd用于进入某个目录        Run (' ls-l | WC- L ')  #远程操作用rundef update ():    setting_ci ()    update_setting_remote ()

Then, do it:

[ken@~/tmp/fab$] fab-f deploy.py update[user@ip:port] Executing task ' update ' [localhost] local:echo ' Add and commit sett Ings in local "Add and commit settings in Localremote Update[user@ip:port] run:ls-l | Wc-l[user@ip:port] Out:12[user@ip:port] out:

Done.
Note that if you do not declare env.password, the interaction that is required to enter the password is taken out of the corresponding machine.

Multi-server Mashup
To operate multiple servers, you need to configure more than one host

#!/usr/bin/env python# encoding:utf-8from Fabric.api import * #操作一致的服务器可以放在一组, same set of operations Env.roledefs = {            ' TestServer ': [' user1@host1:port1 ',],              ' realserver ': [' user2@host2:port2 ',]            } #env. Password = ' Don't use this configuration here, It is not possible to require passwords to be consistent, nor is it appropriate to write in plaintext. Get through all SSH on the line ' @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:

[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.
Extended
1. Color

Can print color, more visible and convenient when viewing operation result 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

By default, a set of commands, after which the last command fails, does not proceed down

A different process can be done after a failure, the document

At the moment, the follow-up use to see

3. Password Management

Read the document

Better password management, elder brother compared to soil, not get through, the main is 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. Parse into map nesting according to key and put in deploy

In addition, the command can actually solidify into a cmds list ...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.