Automated O & M tool Fabric-intelligent task execution (roles, execute)
This article is from the official Fabric document. The original Article is Intelligently executing tasks with execute.
Note: This function is only valid in Fabric 1.3 and mainly uses the execute function.
In Fabric 1.3, you can use roles to define a group of roles for the server and execute different operations based on the role.
Code 1
from fabric.api import run, rolesenv.roledefs = { 'db': ['db1', 'db2'], 'web': ['web1', 'web2', 'web3'],}@roles('db')def migrate(): # Database stuff here. pass@roles('web')def update(): # Code updates here. pass
In Fabric <= 1.2, this is the only way to make the migrate operation take effect on the db Group server. The update operation takes effect on the web group server as follows:
$ fab migrate update
Code 2
In Fabric 1.3, you can use execute to start a metatask. You can modify the Code as follows:
From fabric. api import run, roles, executeenv. roledefs = {'db': ['db1', 'db2 '], 'web': ['web1', 'web2', 'web3'],} @ roles ('db') def migrate (): # Database stuff here. pass @ roles ('web') def update (): # Code updates here. pass # New execute module def deploy (): execute (migrate) execute (update)
Run the following command:
fab deploy
In this way, the roles modifier will take effect as expected. The execution result is as follows:
migrate on db1migrate on db2update on web1update on web2update on web3
Note:
This technique allows tasks to run only once because they do not have a host list (including global host list settings). If a 'regular 'task is run on multiple hosts, execute is called multiple times, and the result is the increase in the number of sub-task calls multiplier-be careful
Note: The number of hosts is large, which may cause an "execution Storm "? Multiple repeated executions? Killing the machine? Or the client tasks will be repeated. You need to find a set of testing machines for testing, but not yet. Students who have passed the test can give a final answer.
Note: The reguar translation is normal? Regular? Qualified. Thank you for your attention.
If you want your exeute call to be executed only once, you can use the runs_once modifier.
This technique works because tasks that themselves have no host list (this includes des the global host list settings) only run one time. if used inside a "regular" task that is going to run on multiple hosts, callto execute will also run multiple times, resulting in multiplicative numbers of subtask cballs-be careful!
If you wowould like your execute callto only be called once, you may use the runs_once decorator.
Use Vagrant and Fabric for Integration Testing
Fabric: Python Remote Deployment Tool
This article permanently updates the link address: