Python fabric remote operations and deployment examples

Source: Internet
Author: User

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, the depressing one is that each operation is the same, the command is the same, the worst thing is that on multiple machines, it is difficult to deal with one script on the local machine, the main time is wasted on ssh, the command is knocked on, the script is written, it can be executed with one click, it takes two minutes to view the execution result.

Until fabric was found to be able to solidify commands for automated deployment or multi-machine operations into a script, which is similar to some O & M tools. It is mainly used because it is easy to use and 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)

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.
Copy codeThe Code is as follows:
[Ken @~ $] Which fab
/Usr/local/bin/fab

After installation, you can browse the official documents

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
Copy codeThe Code is as follows:
Def hello ():
Print ("Hello world! ")

Command Line execution:
Copy codeThe 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.
Copy codeThe 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:
Copy codeThe Code is as follows:
Def hello (name, value ):
Print ("% s = % s! "% (Name, value ))

Run
Copy codeThe 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:

Copy codeThe Code is as follows:
From fabric. api import local

Def lsfab ():
Local ('CD ~ /Tmp/fab ')
Local ('LS ')

Result:
Copy codeThe 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:
Copy codeThe 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)
Copy codeThe 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.

Copy codeThe 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:
Copy codeThe 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.
Copy codeThe 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:
Copy codeThe 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.
Copy codeThe Code is as follows:
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 ........
Copy codeThe Code is as follows:
Env. hosts = [

'Host1 ',

'Host2'


]
Env. passwords = {
'Host1': "pwdofhost1 ",
'Host2': "pwdofhost2 ",

}

Or

Copy codeThe 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 .....

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.