Fabric使用

docker 加入systemctl环境并启动docker

快速安装docker

curl -sSL https://get.daocloud.io/docker | sh
systemctl enable docker
systemctl start docker

docker-compose 安装

走外网或者 github 太慢,可以使用内部加速

 curl -L "https://github.com/docker/compose/releases/download/X.XX.X/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
 curl -L https://get.daocloud.io/docker/compose/releases/download/1.26.2/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose

fabric 自动运维

python 虚拟环境安装

参考

创建一个独立的虚拟环境

cd 目标目录
virtualenv --no-site-packages venv

激活虚拟环境

source venv/bin/activate

python pip 安装 Fabric

pip install fabric3

python pip 导出依赖

pip freeze > requirements.txt

其他python pip 导入安装

pip install -r requirements.txt

Fabric 文档 fabfiles 文档

# encoding=utf-8
from fabric.api import *
from fabric.context_managers import *

env.user = 'root'

env.gateway = '192.168.1.118'

env.hosts = ['10.10.2.6','10.10.2.7']

env.passwords = {
    'root@10.10.2.6:22':'PASSWORD_2',
    'root@10.10.2.7:22':'PASSWORD_2',
    'root@192.168.1.118:22':'PASSWORD_1'
}

jump_server = 'jump_server'
astra_server = 'astra_server'

# 指定 role 角色组
env.roledefs= {
    jump_server: ['192.168.1.118'],
    astra_server: ['10.10.2.6','10.10.2.7']
}

# 测试
@task
@roles(jump_server)
def test():
    run('ls -la && pwd')
 
# 登录私有仓库
@task
@roles(astra_server)
def docker_login():
    run('docker login -u USERNAME -p PASSWORD docker-registery.xxx.com')

# 测试
@task
@roles(astra_server)
def docker_ps():
    run('docker ps')


# 上传docker-compose
@task
@roles(astra_server)
def put_docker_compose():
# 上传失败,照样往下执行 with settings(warn_only=True)
    with settings(warn_only=True):
        result = put('deploy/prod/docker-compose.yml','~/docker-compose.yml')
    if result.failed:
        abort('Aborting file put task!')

# 重新docker-compose部署
@task
@roles(astra_server)
def restart_docker_compose():
    run('docker-compose stop && docker-compose up -d')

@task
def deploy():
    docker_login()
    docker_ps()
    put_docker_compose()
    restart_docker_compose()
comments powered by Disqus