本文讲述如何使用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]))

使用Fabric一键批量部署上线/线上环境监控的更多相关文章

  1. Docker + node(koa) + nginx + mysql 线上环境部署

    在上一篇 Docker + node(koa) + nginx + mysql 开发环境搭建,我们进行了本地开发环境搭建 现在我们就来开始线上环境部署 如果本地环境搭建没有什么问题,那么线上部署的配置 ...

  2. (转) 线上环境部署MongoDB的官方建议

    本文主要内容来自MongoDB官方文档http://docs.mongodb.org/manual/administration/production-notes/.并结合了实际工作情况进行分享. 1 ...

  3. express框架开发接口部署线上环境PM2

    1.PM2介绍 PM2是一个线上环境下,用于启动nodejs进程守护的工具,用来保证服务的稳定及分摊服务器进程和压力. 2.下载安装 npm install pm2 -g  => pm2 --v ...

  4. 批量部署Hadoop集群环境(1)

    批量部署Hadoop集群环境(1) 1. 项目简介: 前言:云火的一塌糊涂,加上自大二就跟随一位教授做大数据项目,所以很早就产生了兴趣,随着知识的积累,虚拟机已经不能满足了,这次在服务器上以生产环境来 ...

  5. 使用percona-xtrabackup实现对线上zabbix监控系统数据库mariadb5.5.47的主从同步

    使用percona-xtrabackup实现对线上zabbix监控系统数据库的主从同步 业务背景: zabbix3.0.4是业务的主要监控,部署在一台单机中,为避免数据丢失先对其做数据主从同步,因主数 ...

  6. vue本地和线上环境(域名)配置

    vue本身为运行脚手架项目自家搭载了一个nodejs后台环境,本地可通过proxyTable来处理跨域问题,但是上线(或生产环境)之后改域名真是一件麻烦的事情,所以进行一些配置. config/ind ...

  7. Vue 2.x 3.x 配置项目开发环境跟线上环境

    先找到package.json  (这是nuxt版的vue 可能会跟一般vue不一样  当然总体上差不多的) "scripts": { "dev": " ...

  8. 记一次线上环境 ES 主分片为分配故障

    故障前提 ElasticSearch 版本:5.2 集群节点数:5 索引主分片数:5 索引分片副本数:1 线上环境ES存储的数据量很大,当天由于存储故障,导致一时间 5个节点的 ES 集群,同时有两个 ...

  9. 使用Playbook批量部署多台LAMP环境

    1. 安装ansible yum install epel-release -y yum install ansible -y Playbook是一个不同于使用ansible命令行执行方式的模式,功能 ...

随机推荐

  1. linux下文件和目录

    (1)普通文件(regular file):这是最常用的文件类型,这种文件包含了某种形式的数据,文件内容的解释由处理该文件的应用程序进行. (2)目录文件(directory file):这种文件包含 ...

  2. 关于selenium IDE找不到元素

    selenium IDE ,明明存在元素,却找不到元素 ,报错Element not found 标签: seleniumselenium IDE自动化测试ide 2016-10-31 13:25 1 ...

  3. 使用Eclipse Egit与码云管理你的代码

    总体流程: 建立远程仓库 建立本地仓库并与远程仓库关联 将Eclipse中的项目提交到本地仓库并进而push到远程仓库 一. 配置Eclipse EGit 图解Eclipse中安装及配置EGit插件中 ...

  4. 201521123032 《Java程序设计》第3周学习总结

    1. 本周学习总结 初学面向对象,会学习到很多碎片化的概念与知识.尝试学会使用思维导图将这些碎片化的概念.知识组织起来.请使用纸笔或者下面的工具画出本周学习到的知识点.截图或者拍照上传. 2. 书面作 ...

  5. Java中Collections的min和max方法

    方法一 public static <T extends Object & Comparable<? super T>> T min(Collection<? e ...

  6. vbs读取excel的一个实例

    功能:在excel中对ip与loginType这两列进行遍历读取.本程序依赖于excel文件的"sheet2"表单中具有这两列. dim dictTarget, objExcel ...

  7. linux 编辑文件时 E45: 'readonly' option is set (add ! to override) 隐藏属性 chattr lsattr

    在改一个系统当中的文件参数时, vim config.php 时,提示 E45: 'readonly' option is set (add ! to override) ,同时不能编辑不能删除不能设 ...

  8. Hyperledger Fabric 1.0 从零开始(三)——环境构建(内网/准离线)

    有公网环境的服务器可以直接看 Hyperledger Fabric 1.0 从零开始(二)--环境构建(公网) ,本篇内容与上篇相似,只不过环境搭建需要在内网下,也就是网络被限制的情况下. 1:环境构 ...

  9. UVW源码漫谈(二)

    前一篇发布出来之后,我看着阅读量还是挺多的,就是评论和给意见的一个都没有,或许各位看官就跟我一样,看帖子从不回复,只管看就行了.毕竟大家都有公务在身,没太多时间,可以理解.不过没关系,我是不是可以直接 ...

  10. 基于maven的profile实现动态选择配置文件

    需求 根据选择不同的部署环境自动替换相关配置变量,如连接的数据库等. 最终效果概览 部署环境分为dev和release 工程目录结构 myproject |-profile | |-dev | | | ...