GenweiWu / Blog

个人技术能力提升
MIT License
4 stars 0 forks source link

fabric2 #92

Open GenweiWu opened 1 year ago

GenweiWu commented 1 year ago

参考

https://docs.fabfile.org/en/latest/api/connection.html#

1迁移到2指导

GenweiWu commented 1 year ago

fabric1

前提

- python2
- pip install fabric

样例

fabfile.py


from fabric.api import cd
from fabric.api import env
from fabric.api import lcd
from fabric.api import local
from fabric.api import put
from fabric.api import run
from fabric.api import settings
from fabric.api import sudo

env.hosts = ['1.2.3.4'] env.user = 'ubuntu' env.password = "pwd111"

def _package(): with lcd('D:/code/Test/test-root/'): local('mvn clean install') with lcd('D:/code/Test/test-common/'): local('mvn clean install') with lcd('D:/code/Test/test-control-center/'): local('find -name *.sh -exec dos2unix {{}} 2>&1;') local('mvn clean package')

def _upload(): with settings(user="dave", password="pwd222"): with cd('/home/pict/hhh/saas/helmet/'): put('D:/code/Test/test-control-center/target/test.tar.gz', 'test.tar.gz', mode=0664) run('ls -l test.tar.gz')

def package(): _package() _upload()

def deploy(): sudo("uninstall...") sudo("install...")


### 执行
> 先进入fabfile所在目录 

fab package fab package deploy


## 其他

### local
> 读取返回值
```python 
result =  local('mvn clean install', capture=True)
print("result: %s" % result.stdout)
GenweiWu commented 1 year ago

fabric2

前提

- python3
- pip install fabric2

样例

fabfile.py


from fabric2.connection import Connection
from fabric2.tasks import task

HOST = '1.2.3.4'

def _package(c): with c.cd('D:/code/Test/test-root'): c.run('mvn clean install') with c.cd('D:/code/Test/test-common'): c.run('mvn clean install') with c.cd('D:/code/Test/test-service'): c.run('find -name *.sh -exec dos2unix {{}} 2>&1;') c.run('mvn clean install')

def _upload(): with Connection(host=HOST, user="dave", connect_kwargs={"password": "pwd111"}) as r: r.run('hostname') remote_path = '/paasdata/software/' with r.cd(remote_path): r.run('ls -l |grep test')

注意:r.cd没有对put生效

        r.put('d:/code/Test/test-service/target/test.tar.gz',
              remote_path + 'test.tar.gz')
        r.run('ls -l |grep test')

@task() def package(c): _package(c) _upload()

@task() def deploy(c): with Connection(host=HOST, user="dave", connect_kwargs={"password": "pwd111"}) as r: r.sudo('uninstall...') r.sudo('install...')


### 执行
> 先进入fabfile所在目录  

//查看有哪些任务 $ fab2 -l Available tasks:

deploy package


> 执行

fab2 deploy fab2 deploy package

GenweiWu commented 1 year ago

fabric2

🚗 Connection常用方法:

run        # 执行远程命令,如:run('uname -a')
cd         # 切换远程目录,如:cd('/root'); with conn.cd('/root'),继承这个状态
put        # 上传本地文件到远程主机,如:put('/root/test.py','/root/test.py')
get        # 获取服务器上文件,如:get('/root/test.py')
sudo       # sudo方式执行远程命令,如: sudo('service docker start')
local      # 执行本地命令,如:conn.local('ls'

run、sudo可以继承cd的效果

with c.cd('/passdata/'):
  c.run('ls -l')
  c.sudo('ls -l')
#   c.put无法继承

自动输密码

https://docs.fabfile.org/en/latest/getting-started.html


from fabric2.connection import Connection, Config
from fabric2.tasks import task

HOST = '1.2.3.4' USERNAME = "dave" PWD = "pwd123"

@task() def deploy(c): config = Config(overrides={'sudo': {'password': PWD}})

with Connection(host=HOST, user=USERNAME, connect_kwargs={"password": PWD}, config=config) as r:
    r.sudo('whoami')

### 某步骤执行失败,会停止后续执行
```python
def _upload():
    with Connection(host=HOST, user=USERNAME, connect_kwargs={"password": PWD}) as r:
        r.run('hostname')
        remote_path = '/home/dave'
        with r.cd(remote_path):
            r.run('ls -l |grep xxx')    // 这里grep如果没发现,命令返回的不是0,下面的命令就不会继续执行了;
            //可以这样规避  r.run('ls -l |grep xxx ||true')
            r.run('date')