A glance of Fabric
Posted on Wed 26 August 2015 in tips
Overview
Fabric is a Python (2.5-2.7) library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks.
Installation
sudo pip install Fabric sudo pip install fabtools
Usage
fab method_name
e.g.
fab upload
fab download
Sample: fabfile.py
from fabric.api import *
from fabric.context_managers import *
from fabric.contrib.console import confirm
env.user='root'
env.hosts=['10.224.64.46']
env.passwords = {
'root@10.224.64.46:22': 'password'
}
@task
def upload(): #upload file task
local_dir='/workspace/cpp'
remote_dir = '/home/walter/codelab/cpp'
file_list = [
'server/src/main/resources/templates/webmonitor.ftl'
]
with cd(remote_dir) :
for filename in file_list:
local_file = local_dir + "/" + filename
remote_file = remote_dir + "/" + filename
#print local_file, " to ", remote_file
with settings(warn_only=True): #when upload error,continue
result = put(local_file, remote_file)
if result.failed and not confirm("put file failed,Continue[Y/N]?"):
abort("Aborting file put task!")
@task
def download(): #upload file task
local_dir='/workspace/cpp/server/target'
remote_dir = '/home/walter/codelab/cpp/server/target'
file_list = [
'index.html'
]
with cd(remote_dir) :
for filename in file_list:
local_file = local_dir + "/" + filename
remote_file = remote_dir + "/" + filename
#print local_file, " to ", remote_file
with settings(warn_only=True): #when upload error,continue
result = get(remote_file,local_file)
if result.failed and not confirm("put file failed,Continue[Y/N]?"):
abort("Aborting file put task!")