使用Fabric一鍵批量部署上線/線上環境監控
阿新 • • 發佈:2022-05-02
本文講述如何使用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]))