前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用Fabric一键批量部署上线/线上环境监控

使用Fabric一键批量部署上线/线上环境监控

作者头像
用户1225216
发布2018-03-05 14:20:24
9060
发布2018-03-05 14:20:24
举报
文章被收录于专栏:扎心了老铁扎心了老铁

本文讲述如何使用fabric进行批量部署上线的功能

这个功能对于小应用,可以避免开发部署上线的平台,或者使用linux expect开发不优雅的代码。

前提条件:

1、运行fabric脚本的机器和其他机器tcp_port=22端口通

2、ssh可以登录,你有账号密码

一、先说批量部署上线

先上代码,再仔细讲解,脚本如下

# -*- coding:utf-8 -*-
from fabric.colors import *
from fabric.api import *
from contextlib import contextmanager as _contextmanager

# 自动载入
env.user='data_monitor'
env.hosts=['10.93.21.21', '10.93.18.34', '10.93.18.35']
env.password='datamonitor@123'
# 手动加入
env.activate = 'source /home/data_monitor/.bash_profile'
env.directory = '/home/data_monitor/dmonitor/dmonitor'


@_contextmanager
def virtualenv():
    with cd(env.directory):
        with prefix(env.activate):
            yield


@task
def update():
    with virtualenv():
        run("git pull origin master")


@task
def start():
    with virtualenv():
        run("$(nohup gunicorn --worker-class=gevent dmonitor.wsgi:application -b 0.0.0.0:8009 -w 4 &> /dev/null &) && sleep 1", warn_only=True)
        run("$(nohup python manage.py celery worker -Q high -c 30 &> /dev/null &) && sleep 1 ", warn_only=True)
        run("$(nohup python manage.py celery worker -Q mid -c 30 &> /dev/null &) && sleep 1 ", warn_only=True)
        run("$(nohup python manage.py celery worker -Q low -c 30 &> /dev/null &) && sleep 1", warn_only=True)


@task
def stop():
    with virtualenv():
        run("ps -ef | grep gunicorn | grep -v grep | awk '{print $2}'| xargs kill -9", warn_only=True)
        run("ps -ef | grep celery | grep worker | grep -v grep | awk '{print $2}' | xargs kill -9", warn_only=True)


@task
def deploy():
    update()
    stop()
    start()

2、线上环境监控

当然一般线上环境没有用fabric监控的,但是开发环境和测试环境的话,一般都是虚拟机,没有人管你。

所以自己开发一个小型监控程序,监控一下硬盘cpu内存,或者是一些进程(redis/mysql...),还是挺有用的。

先上代码

这个文件是各种task

import logging

from fabric.api import *
from fabric.context_managers import *
from fabric.colors import red, yellow, green
from common.redis import Redis
from common.config import redis as redis_config

logger = logging.getLogger(__name__)
redis = Redis(redis_config.get('ip'), redis_config.get('port'))


# hard_disk_monitor, item_name=hard_disk
@task
def hard_disk_monitor(item_group, item_name, threshold):
    with settings(hide('warnings', 'running', 'stdout', 'stderr'), parallel=True, warn_only=True):
        host = run('hostname -i')
        hard_disk = run("df -hl | grep /dev/vda3 | awk -F ' ' '{print $5}'")
        print green(host + ':' + hard_disk)
        if int(hard_disk.strip('%')) > threshold:
            redis("lpush %s %s" % (':'.join(['machine', item_group, item_name]), host))


# memory_monitor, item_name=memory
@task
def memory_monitor(item_group, item_name, threshold):
    with settings(hide('warnings', 'running', 'stdout', 'stderr'), parallel=True, warn_only=True):
        host = run('hostname -i')
        memory = run("cat /proc/meminfo | grep MemFree | awk -F ' ' '{print $2}'")
        print yellow(host + ':' + memory)
        if int(memory.strip()) < threshold:
            redis("lpush %s %s" % (':'.join(['machine', item_group, item_name]), host))


# base_services_monitor, item_name != hard_disk or item_name != memory
@task
def base_services_monitor(item_group, item_name, threshold):
    with settings(hide('warnings','running','stdout','stderr'),parallel=True,warn_only=True):
        host = run('hostname -i')
        count = run("ps -ef | grep %s | grep -v grep | wc -l" % item_name)
        print red(host + ':' + count)
        if int(count.strip()) != threshold:
            redis("hset %s %s %s" % (':'.join(['machine', item_group, item_name]), host, count))
            redis('incr %s' % ':'.join(['machine', item_group, item_name, host]))
            redis('expire %s 1800' % ':'.join(['machine', item_group, item_name, host]))


# restart_services_monitor, item_name = tomcat-7.0.57-mis or item_name = tomcat-httpapi
@task
def restart_services_monitor(item_start):
    with settings(hide('warnings', 'running', 'stdout', 'stderr'), parallel=True,warn_only=True):
        host = run('hostname -i')
        run(item_start)
        print green(host + ':' + item_start)

这个文件是执行task

# -*- coding:utf-8 -*-

from fabric.api import *
from fabric.context_managers import *
execute(monitors.hard_disk_monitor, item_group, item_name, item_threshold,
                        hosts=json.loads(item_param.get('item_hosts')))
                hosts = self.redis('lrange %s 0 -1' % ':'.join(['machine', item_group, item_name]))
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-07-19 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 Redis
腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档