本文讲述如何使用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. Java学习记录:文件的输入输出流

    Java中的输入.输出流中可以用于文件的读写,拷贝. 由于文件都是由字节组成的,可以将文件中的内容以字节的方式读取出来. 输入流还可以直接转换为图片来使用.其实ImageIcon提供了方法可以直接打开 ...

  2. [我所理解的REST] 2.REST用来干什么的?

    笔者每当遇到一个新事物的想去了解的时候,总是会问上自己第一个问题,这个新事物是干什么用的?在解释我所理解的REST这个过程中也不例外,这篇博客我们先关注一下REST是干什么用的,然后后续再解释REST ...

  3. C# 模拟网站登陆并截图

    1.在窗体上加一个按钮,为按钮添加点击事件 private void button1_Click(object sender, EventArgs e) { Bitmap m_Bitmap = Web ...

  4. 201521123107 《Java程序设计》第11周学习总结

    第11周作业-多线程 1.本周学习总结 2.书面作业 本次PTA作业题集多线程 1.互斥访问与同步访问 完成题集4-4(互斥访问)与4-5(同步访问) 1.1 除了使用synchronized修饰方法 ...

  5. 第二次项目冲刺(Beta阶段)--第五天

    一.站立式会议照片 二.项目燃尽图 三.项目进展 - 今天任务是改进程序使程序能完成docx文件的读取,但是并没有成功实现解决该问题,所以燃尽图没有前进. -遇到的问题:不支持docx最早认为是jar ...

  6. 201521123110 《Java程序设计》第7周学习总结

    1. 本章学习总结 2. 书面作业 1.ArrayList代码分析 1.1 解释ArrayList的contains源代码 public boolean contains(Object o) { re ...

  7. 201521123113 《Java程序设计》第5周学习总结

    1. 本周学习总结 1.1 尝试使用思维导图总结有关多态与接口的知识点. 1.2 可选:使用常规方法总结其他上课内容. -继承设计的技巧 1.将公共操作和属性放在父类 2.不要使用protected修 ...

  8. java课程设计(个人)--五子棋

    1.团队课程设计博客链接 http://www.cnblogs.com/mz201521044152/p/7065575.html 2.个人负责模块说明 棋盘类,绘制棋盘,绘制棋子,按钮设置,鼠标监听 ...

  9. 201521123028 《Java程序设计》第14周学习总结

    1. 本周学习总结 2. 书面作业 1. MySQL数据库基本操作 建立数据库,将自己的姓名.学号作为一条记录插入.(截图,需出现自己的学号.姓名) 在自己建立的数据库上执行常见SQL语句(截图) 2 ...

  10. ORACLE PROC开发(转载)

    Proc也就是嵌入式C,与informix的ESQ/C有类似之处,本部分主要列出Proc与Esql的区别,相同部分请参见informix部分. 1.数组功能 Proc中支持使用宿主变量数组一次查询SE ...