Python fabric Part2
Errors handling:
command-running operations like run, local etc. can return objects containing their execution details: .failed or .return_code
settings - gives ability to change env variables only for needed chunk of the code
env warn_only setting gives us ability to turn aborts and errors into flexible error handling
abort - used to abort execution
confirm function from contrib.console used for simple `[Y/n]` prompts
prompt used to interactively get information from fab user
validate in run is used as user-input validator (regular expression) and continues prompting if user-input is not valid
execute used to execute already defined fab commands in other fab command (simply calling a task function does not take into account decorators such as roles)
from fabric.api import settings, abort, run, prompt, execute,hosts, task
from fabric.contrib.console import confirm
@task
@hosts('localhost') # if we won't specify this "fab deploy" will return: "No hosts found. Please specify (single) host string for connection" because execute uses attributes set-up in executed command itself
def dmidecode_test():
with settings(warn_only=True):
shell_command = r"sudo dmidecode -t 1 | grep -i 'serial number'"
result = run(shell_command)
if result.failed and not confirm("dmidecode is not installed. Do you need to install"):
abort("Execution aborted. dmidecode is not installed")
elif result.failed:
answer = prompt("Say `y/n` to yum?", validate="[yn]")
run("sudo yum install dmidecode -%s" % answer)
else:
pass
@task
@hosts('localhost')
def dmidecode():
run("sudo dmidecode -t 1 | grep -i 'serial number'")
run("sudo dmidecode -t 16 | grep -i 'maximum capacity'")
@task
@hosts('10.1.1.1')
def deploy():
execute(dmidecode_test)
execute(dmidecode)
[admin@localhost ~]$ fab --list
Available commands:
deploy
dmidecode
dmidecode_test
[admin@localhost ~]$
[admin@localhost ~]$ fab deploy
[10.1.1.1] Executing task 'deploy'
[localhost] Executing task 'dmidecode_test'
[localhost] run: sudo dmidecode -t 1 | grep -i 'serial number'
[localhost] Login password for 'admin':
[localhost] out: [sudo] password for admin:
[localhost] out: sudo: dmidecode: command not found
[localhost] out:
Warning: run() received nonzero return code 1 while executing 'sudo dmidecode -t 1 | grep -i 'serial number''!
dmidecode is not installed. Do you need to install [Y/n] n
Fatal error: Execution aborted. dmidecode is not installed
Aborting.
Disconnecting from localhost... done.
[admin@localhost ~]$
[admin@localhost ~]$ fab deploy
[10.1.1.1] Executing task 'deploy'
[localhost] Executing task 'dmidecode_test'
[localhost] run: sudo dmidecode -t 1 | grep -i 'serial number'
[localhost] Login password for 'admin':
[localhost] out: [sudo] password for admin:
[localhost] out: sudo: dmidecode: command not found
[localhost] out:
Warning: run() received nonzero return code 1 while executing 'sudo dmidecode -t 1 | grep -i 'serial number''!
dmidecode is not installed. Do you need to install [Y/n] y
Say `y/n` to yum? y
[localhost] run: sudo yum install dmidecode -y
...
[localhost] Executing task 'dmidecode'
[localhost] run: sudo dmidecode -t 1 | grep -i 'serial number'
[localhost] out: [sudo] password for admin:
[localhost] out: Serial Number: NXV54ER0246021DE76
[localhost] out:
[localhost] run: sudo dmidecode -t 16 | grep -i 'maximum capacity'
[localhost] out: [sudo] password for admin:
[localhost] out: Maximum Capacity: 16 GB
[localhost] out:
Done.
Disconnecting from localhost... done.
[admin@localhost ~]$
No comments:
Post a Comment