Fabric管理组件的使用

Fabric的官方网站:
http://www.fabfile.org
帮助文档:
https://fabric-chs.readthedocs.io/zh_CN/chs/tutorial.html

  1. Fabric的安装
yum install -y make gcc gcc++ python-devel python-pip
$ pip install fabric==1.14.0
或者
$ pip3 install fabric2 (没有fabric.api模块)
  1. 验证安装
python
>>> import fabric
或者
python3
>>> import fabric
  1. 命令行入口fab
fab [options]  --  [shell command]

基本用法

Fabric的核心API

核心API类别
带颜色的输出类color output
上下文管理类context managers
装饰类decorators
网络类network
操作类operations
任务类tasks
工具类utils

fabric.api命令集
1720799-20190701235854246-1570029464.jpg

  1. 使用方法
$ cat fabfile.py
----------------------------------
#!/usr/bin/python
# -*- coding: utf-8 -*-

from fabric.api import *
from fabric.colors import *
from fabric.context_managers import *

env.user = 'beeworkshop'
env.hosts = ['192.168.30.66']
env.password = '123456'

@task
def run_remote_cmds():
    print yellow("我要执行命令啦")
    with settings(warn_only=True):  <---错误也继续执行
        local("hostname")
        local("uname -a")
        run("w")
        run("hostname")
        run("ifconfig")
        run("ls -l")
---------------------------------
$ fab -l
Available commands:

    run_remote_cmds

$ fab run_remote_cmds
[192.168.30.66] Executing task 'run_remote_cmds'
我要执行命令啦
[localhost] local: hostname
bee-a
[localhost] local: uname -a
Linux bee-a 3.10.0-693.el7.x86_64 #1 SMP Tue Aug 22 21:09:27 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
[192.168.30.66] run: w
[192.168.30.66] out:  01:04:50 up  1:35,  1 user,  load average: 0.00, 0.00, 0.00
[192.168.30.66] out: USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
[192.168.30.66] out: beeworks pts/0    192.168.30.6     01:04    0.00s  0.02s  0.02s w
[192.168.30.66] out: 

[192.168.30.66] run: hostname
[192.168.30.66] out: sdn-testbed
[192.168.30.66] out: 

[192.168.30.66] run: ifconfig
[192.168.30.66] out: ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
[192.168.30.66] out:         inet 192.168.74.128  netmask 255.255.255.0  broadcast 192.168.74.255
[192.168.30.66] out:         inet6 fe80::4dec:63ce:223b:a7cf  prefixlen 64  scopeid 0x20<link>
[192.168.30.66] out:         ether 00:0c:29:68:44:26  txqueuelen 1000  (Ethernet)
[192.168.30.66] out:         RX packets 89251  bytes 115992608 (115.9 MB)
[192.168.30.66] out:         RX errors 0  dropped 0  overruns 0  frame 0
[192.168.30.66] out:         TX packets 27312  bytes 1844389 (1.8 MB)
[192.168.30.66] out:         TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[192.168.30.66] out: 
[192.168.30.66] out: ens34: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
[192.168.30.66] out:         inet 192.168.30.66  netmask 255.255.255.0  broadcast 192.168.30.255
[192.168.30.66] out:         inet6 fe80::6094:b573:8d2f:dd5  prefixlen 64  scopeid 0x20<link>
[192.168.30.66] out:         ether 00:0c:29:68:44:30  txqueuelen 1000  (Ethernet)
[192.168.30.66] out:         RX packets 305  bytes 39864 (39.8 KB)
[192.168.30.66] out:         RX errors 0  dropped 0  overruns 0  frame 0
[192.168.30.66] out:         TX packets 230  bytes 38470 (38.4 KB)
[192.168.30.66] out:         TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[192.168.30.66] out: 
[192.168.30.66] out: lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
[192.168.30.66] out:         inet 127.0.0.1  netmask 255.0.0.0
[192.168.30.66] out:         inet6 ::1  prefixlen 128  scopeid 0x10<host>
[192.168.30.66] out:         loop  txqueuelen 1000  (Local Loopback)
[192.168.30.66] out:         RX packets 373  bytes 28853 (28.8 KB)
[192.168.30.66] out:         RX errors 0  dropped 0  overruns 0  frame 0
[192.168.30.66] out:         TX packets 373  bytes 28853 (28.8 KB)
[192.168.30.66] out:         TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[192.168.30.66] out: 
[192.168.30.66] out: 

[192.168.30.66] run: ls -l
[192.168.30.66] out: total 48
[192.168.30.66] out: drwxrwxr-x 2 beeworkshop beeworkshop 4096 4月  24 21:20 bak
[192.168.30.66] out: drwxr-xr-x 2 beeworkshop beeworkshop 4096 4月  24 19:40 Desktop
[192.168.30.66] out: drwxr-xr-x 2 beeworkshop beeworkshop 4096 4月  24 19:40 Documents
[192.168.30.66] out: drwxr-xr-x 2 beeworkshop beeworkshop 4096 4月  24 19:40 Downloads
[192.168.30.66] out: drwxr-xr-x 2 beeworkshop beeworkshop 4096 4月  24 19:40 Music
[192.168.30.66] out: drwxr-xr-x 2 beeworkshop beeworkshop 4096 4月  24 19:40 Pictures
[192.168.30.66] out: drwxr-xr-x 2 beeworkshop beeworkshop 4096 4月  24 19:40 Public
[192.168.30.66] out: drwxrwxr-x 5 beeworkshop beeworkshop 4096 4月  25 01:09 sdnsoftware
[192.168.30.66] out: drwxr-xr-x 2 beeworkshop beeworkshop 4096 4月  24 19:40 Templates
[192.168.30.66] out: drwxrwxr-t 2 beeworkshop beeworkshop 4096 4月  25 18:23 thinclient_drives
[192.168.30.66] out: drwxrwxr-x 3 beeworkshop beeworkshop 4096 4月  24 23:33 tools
[192.168.30.66] out: drwxr-xr-x 2 beeworkshop beeworkshop 4096 4月  24 19:40 Videos
[192.168.30.66] out: 


Done.
Disconnecting from 192.168.30.66... done.

如果不是默认的文件fabfile.py,则必须用参数-f指出:

fab -f ~/test.py run_remote_cmds

指出默认的task。此时fab后边可以不必给出函数名,直接fab即可。

@task(default=True)

注意:\cmd 可以取消别名的作用。

例子1

#!/usr/bin/python
# -*- coding: utf-8 -*-

from fabric.api import *
from fabric.colors import *
from fabric.context_managers import *

env.user = 'beeworkshop'
env.hosts = ['192.168.30.66','192.168.30.67','192.168.30.68','192.168.30.69']
env.password = '123456'

@task
def put_hosts_files():
    print yellow("rsync /etc/host file")
    with settings(warn_only=true):
        put("/etc/hosts","/etc/hosts")
        print green("rsync file success")

for host in env.hosts
    env.host_string = host
    put_hosts_files()

这里@task装饰的函数可以多个,然后全部放在for循环中。fab会根据env.host_string的当前值逐个运行函数。

转载于:https://www.cnblogs.com/cerana/p/11117692.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Vue 3中使用Fabric.js需要以下步骤: 1. 安装Fabric.js:首先,你需要在你的Vue 3项目中安装Fabric.js。你可以使用npm或yarn来安装它。打开终端并运行以下命令来安装Fabric.js: ``` npm install fabric ``` 或者 ``` yarn add fabric ``` 2. 创建Vue组件:创建一个Vue组件,用于承载和管理Fabric.js的画布。你可以在组件的模板中添加一个`<canvas>`元素来表示画布。例如: ```vue <template> <canvas ref="canvas"></canvas> </template> <script> import { ref, onMounted } from 'vue'; import { fabric } from 'fabric'; export default { name: 'FabricCanvas', setup() { const canvasRef = ref(null); onMounted(() => { const canvas = new fabric.Canvas(canvasRef.value); // 在这里可以使用Fabric.js的API操作画布和对象 // 例如,创建一个矩形对象并添加到画布上 const rect = new fabric.Rect({ width: 100, height: 100, fill: 'red', }); canvas.add(rect); }); return { canvasRef, }; }, }; </script> ``` 在上面的示例中,我们使用了Vue 3的Composition API来创建了一个名为`FabricCanvas`的组件。在`onMounted`钩子函数中,我们创建了一个新的Fabric.js画布,并通过`canvasRef`引用将其与模板中的`<canvas>`元素关联起来。在这个示例中,我们创建了一个红色的矩形对象并将其添加到画布上。 3. 使用Fabric.js的API:在Vue组件的`setup`函数中,你可以使用Fabric.js的API来操作画布和对象。你可以使用`canvas`对象来添加、删除、移动和更新画布上的对象,以及应用各种效果和变换。 以上就是在Vue 3中使用Fabric.js的基本步骤。你可以根据自己的需求,使用Fabric.js提供的丰富功能来进行更多操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值