阶段性总结:

     跌跌撞撞的用了一周左右的时间做完了网站自动升级功能,中间遇到了很多的问题,也学到了很多,在此做一个总结。
 

1、整体架构:

后台:nginx+uwsgi  #nginx提供web服务,uwsgi对python代码进行解析
前台:flask+bootstrap+html  #flask是python的一套web开发工具,bootstrap是一套现成的模板,html不用解释。
功能:shell+saltstack+svn #saltstack 功能的核心,自动化运维工具。 shell 不解释,svn代码一致性工具。
 

2、用到的技巧:

python相关:

     对于文件的读写
     列表的相关操作
     字典的调用
     如何执行shell命令
     时间模块
     线程的使用

flask相关:

     表单
     session
     flash
其他的貌似想不到了,注释代码的时候一并回忆整理吧。
 

3、下面进行代码的review和注释

 
########################################################################################
########################################################################################
#调用flask相关的模块
from flask import Flask, render_template, session, redirect, url_for, flash
from flask.ext.script import Manager
from flask.ext.bootstrap import Bootstrap
from flask.ext.moment import Moment
from flask.ext.wtf import Form
#调用需要用到的表单类型以及属性
from wtforms import TextAreaField,StringField,SubmitField,SelectField,PasswordField
from wtforms.validators import Required,DataRequired
#调用基础模块
import os,commands,subprocess,time,threading
#调用字典文件并加载
from webpath import webpath
from userdata import userdata
 
app = Flask(__name__)
app.config['SECRET_KEY'] = 'who is your boss'
 
manager = Manager(app)
bootstrap = Bootstrap(app)
moment = Moment(app)
 
#函数:获取指定格式的当前时间
def gettime():
        ISOTIMEFORMAT='%Y-%m-%d %X'     #指定时间格式
        ttime = time.strftime( ISOTIMEFORMAT,time.localtime(time.time()))     #获取当前时间
        return ttime
 
#函数:将输出转换为列表
def make_list(listin,symbol):
        tem = listin.replace(symbol,'')     #将指定内容替换为空
        tem1 = tem.splitlines()     #按照行将输入转换为列表
        return tem1
 
#函数:输出日志
def inputF(con):
        f = open('seos.log','a')     #以追加的模式打开文件,如果没有,创建
        updatetime = gettime()
        uptime = str(updatetime) + '\n'
        f.write(uptime)
        conx = con + '\n'
        f.write(conx)
        f.close()     #关闭文件,释放资源
 
#函数:获取当前存活的web服务器名称,转换为列表
def getweb():
     #执行shell命令,获取saltstack中存活的web服务器
    p = subprocess.Popen('salt-run manage.up | grep web',shell=True,stdout=subprocess.PIPE)
    tem0 = p.stdout.read()     #获取脚本执行结果
    tem2 = make_list(tem0,'- ')
    return tem2
 
#函数:删除指定web的相关模块缓存
def rmcache_Newseos(webname):
        wbapp = 'Wap Weibo'
        tem2 = getweb()
        basicpath = webpath.get(webname)
     #执行脚本,调用saltstack的客户端服务器上的脚本,实现删除缓存功能
        res = subprocess.Popen('salt \'%s\' cmd.run \'sh %scleancache.sh %s\'' % (webname, basicpath, wbapp),shell=True,stdout=subprocess.PIPE)
 
#函数:删除所有web的相关模块缓存
#这个函数是使用线程之前的函数,作用如上,效率比较低
def rmomscache():
        p = subprocess.Popen('salt-run manage.up | grep web',shell=True,stdout=subprocess.PIPE)
        path = 'oms/Runtime/*'
        tem0 = p.stdout.read()
        tem2 = make_list(tem0,'- ')
        for webname in tem2:
                basicpath = webpath.get(webname)
                tpath = '%s%s' % (basicpath, path)
                res = subprocess.Popen('salt \'%s\' cmd.run \'rm -rf %s\'' % (webname, tpath),shell=True,stdout=subprocess.PIPE)
                res_in = res.stdout.read()
                print res_in
                print tpath
        return 'Delete oms cache!'
 
#函数:删除指定web的指定模块缓存
def rmomscache_new(webname):
    path = 'oms/Runtime/*'
    basicpath = webpath.get(webname)
    tpath = '%s%s' % (basicpath, path)
    res = subprocess.Popen('salt \'%s\' cmd.run \'rm -rf %s\'' % (webname, tpath),shell=True,stdout=subprocess.PIPE)
 
#函数:创建线程集合
def getthreads(targets):
        allweb = getweb()
        threads = []
        for webname in allweb:
                t = threading.Thread(target=targets, args=(webname,))
                threads.append(t)
        return threads
 
#函数:升级网站
def update_web(path):
        f = open('resault.txt','a')
        tem2 = getweb()
        for webname in tem2:
                basicpath = webpath.get(webname)
                tpath = '%s%s' % (basicpath, path)
                res = subprocess.Popen('salt \'%s\' cmd.run \'svn update %s\'' % (webname, tpath),shell=True,stdout=subprocess.PIPE)
                res_in = res.stdout.read()
                f.write(str(res_in))
                res_list = make_list(res_in,'    ')
        f.close()
        resault = open('resault.txt').read()
        f.close()
        os.remove('resault.txt')
        return resault
 
#定义表单
class NameForm(Form):
    uppath = SelectField('Path', choices=[('do','do'),('oms','oms')])
    username = SelectField('User', choices=[('admin','admin'),('liujian','liujian')])
    upkey = PasswordField('Password', validators=[DataRequired()])
    submit = SubmitField('Update')
 
@app.errorhandler(404)
def page_not_found(e):
    return render_template('404.html'), 404
 
 
@app.errorhandler(500)
def internal_server_error(e):
    return render_template('500.html'), 500
 
#主程序
@app.route('/', methods=['GET', 'POST'])
def index():
    name = None
    uppath = None
    username = None
    upkey = None
    passwd = None
    rmcache = 'Does it need to delete the cache'
    form = NameForm()
    if form.validate_on_submit():
        uppath = form.uppath.data
        upkey = form.upkey.data
        username = form.username.data
        passwd = userdata.get(username)
        if upkey is None or upkey != passwd:
                flash('Your key is wrong!')
                session['name'] = 'Please input right key.'
                session['rmcache'] = 'Does it need to delete the cache'
                return redirect(url_for('index'))
        else:
                utime = gettime()
                print '%s Start update WEB' % utime
                resaul = update_web(uppath)
                resault = resaul.replace('\n','<br>')
                session['name'] = resault
                if uppath.startswith('Newseos'):
                        Newseostime = gettime()
                        print '%s Newseos is updated,start remove cache.' % Newseostime
                        threads = getthreads(rmcache_Newseos)
                        for t in threads:
                                t.setDaemon(True)
                                t.start()
                        t.join()
                        endtime = gettime()
                        print '%s Newseos upgrade over!' % endtime
                        session['rmcache'] = 'Delete Newseos Cache.'
                        uplog = 'Operator:' + username + '\n' + 'Update Path:' + uppath + '\n' + resaul
                        inputF(uplog)
                        return redirect(url_for('index'))
                elif uppath.startswith('oms'):
                        omstime = gettime()
                        print '%s oms id updated,start remove cache.' % omstime
                        threads = getthreads(rmomscache_new)
                        for t in threads:
                                t.setDaemon(True)
                                t.start()
                        t.join()
                        endtime = gettime()
                        print '%s oms upgrade over!' % endtime
                        session['rmcache'] = 'Delete oms Cache.'
                        uplog = 'Operator:' + username + '\n' + 'Update Path:' + uppath + '\n' + resaul
                        inputF(uplog)
                        return redirect(url_for('index'))
                else:
                        session['rmcache'] = 'No need delete cache.'
                        uplog = 'Operator:' + username + '\n' + 'Update Path:' + uppath + '\n' + resaul
                        inputF(uplog)
                        return redirect(url_for('index'))
    return render_template('index.html', form=form, name=session.get('name'), uppath=uppath, upkey=upkey, username = username, rmcache=session.get('rmcache'))
 
 
if __name__ == '__main__':
    manager.run()
 
结语:模板相关的代码以及整体程序结构没有展示,感觉必要性也不是很大。下边给出我参考和学习的一些资料和链接吧:

书籍:

《Flask Web开发-基于Python的Web应用开发实战》   ---核心书籍
《Python 核心编程》第二版
链接:
linux部署flask+uwsgi+nginx详解
http://www.tuicool.com/articles/zUvqMr
flask学习的博客
http://my.oschina.net/ykbj/blog
 

页面展示:

页面实现功能:

选择更新路径、更新密码验证、自动删除缓存、更新结果反馈、更新日志记录

自动化运维:网站svn代码上线更新(flask+saltstack)的更多相关文章

  1. 自动化运维工具 SaltStack 搭建

    原文地址:https://www.ibm.com/developerworks/cn/opensource/os-devops-saltstack-in-cloud/index.html#N10072 ...

  2. 自动化运维经验谈,以及为什么Docker是革命性的

    互联网+的需要 在信息越来越繁杂的互联网时代,公司所运行的项目越来越多,项目相关服务繁多,服务之间存在复杂的依赖关系,运维与管理任务越来越繁重,手工交付需要花费很多的人力与时间,且安全性和时效性均无法 ...

  3. 企业级自动化运维工具应用实战-ansible

    背景 公司计划在年底做一次大型市场促销活动,全面冲刺下交易额,为明年的上市做准备.公司要求各业务组对年底大促做准备,运维部要求所有业务容量进行三倍的扩容,并搭建出多套环境可以共开发和测试人员做测试,运 ...

  4. ansible自动化运维

    ansible 系统架构 ansible简介 ansible是新出现的自动化运维工具,ansible是一个配置管理和应用部署工具,基于Python开发,集合了众多运维工具(puppet.cfengin ...

  5. 使用Ansible实现数据中心自动化运维管理

    长久以来,IT 运维在企业内部一直是个耗人耗力的事情.随着虚拟化的大量应用.私有云.容器的不断普及,数据中心内部的压力愈发增加.传统的自动化工具,往往是面向于数据中心特定的一类对象,例如操作系统.虚拟 ...

  6. puppet自动化运维

    Puppet实现自动化运维 一.案例分析 1.案例概述: 随着服务器数量的增多,系统管理员任务量也逐渐增加,这时就需要简洁的.强大的框架来完成系统管理任务为实现这一目的,我们将引入一批工具,这批工具是 ...

  7. 企业级自动化运维工具应用实战ansible

    公司计划在年底做一次大型市场促销活动,全面冲刺下交易额,为明年的上市做准备.公司要求各业务组对年底大促做准备,运维部要求所有业务容量进行三倍的扩容,并搭建出多套环境可以共开发和测试人员做测试,运维老大 ...

  8. linux自动化运维工具Ansible saltstack Puppet、Chef、Fabric之间的对比

    发现分布式是一个发展的趋势,无论是大型网站的负载均衡架构还是大数据框架部署,以及云存储计算系统搭建都离不开多台服务器的连续部署和环境搭建. 当我们的基础架构是分散式或者基于云的,并且我们经常需要处理在 ...

  9. CMDB 和自动化运维

    目录 传统运维和自动化运维的对比 CMDB CMDB 的几种实现方式 传统运维和自动化运维的对比 1.企业中,项目的发布流程 产品经理调研需求 -->三方开会讨论(开发,产品,运维,测试) -– ...

随机推荐

  1. 转载——C++控制台贪吃蛇代码

    游戏截图: 以下是3个代码文件: Snake_Class.h文件: 1 #ifndef SNAKE 2 #define SNAKE 3 4 #include<windows.h> 5 #i ...

  2. GSM Hacking Part② :使用SDR捕获GSM网络数据并解密

    0×00 在文章第一部分 GSM Hacking Part① :使用SDR扫描嗅探GSM网络 搭建了嗅探GSM流量的环境,在第二部中,我们来讨论如何捕获发短信以及通话过程中的流量,从捕获到的数据中解密 ...

  3. MFC 屏幕截图方法

    //获取当前屏幕的并且保存图片 LRESULT CFeetScanView::SaveViewBMP(WPARAM wParam, LPARAM lParam) { CRect rect; this- ...

  4. 利用SegmentedControl切换控制器的view

    #import "ViewController.h" #import "OneViewController.h" #import "TwoViewCo ...

  5. angular 滚动

    AngularJs $anchorScroll.$controller.$document $anchorScroll 根据HTML5的规则,当调用这个函数时,它检查当前的url的hash值并且滚动到 ...

  6. java 实现一个简易计算器

    import java.util.Scanner;public class Test { public static void main(String[] args) { count(); } pub ...

  7. VIm 一些常用的设置

    一些常用的vim设置   以下内容皆来源于网络,感谢原作者.如果引用出处错误,请告知以便修改. 1. vim的几种模式和按键映射 转载自:[1] Map是Vim强大的一个重要原因,可以自定义各种快捷键 ...

  8. Chrome和IE中使用window.open函数

    做前端开发的人员经常回遇到使用windows.open这个函数来打开一个新的网页窗口,使用这个函数的时候有些需要注意的地方,在Chrome和IE下该函数还是有一些细节性的区别. 以下是我在项目中使用的 ...

  9. Localization要从第一天开始计划

    最近E3,微软说可以在任何region玩任何语言的游戏了.换一个语言么,听起来没有那么复杂,其实操作起来还得是从软件工程初期就好好计划. Windows在很长一段时间,你安装完了,就不能换语言了.大学 ...

  10. DNS记录类型介绍(A记录、MX记录、NS记录等)

    DNS A记录 NS记录 MX记录 CNAME记录 TXT记录 TTL值 PTR值 建站名词解释:DNS A记录 NS记录 MX记录 CNAME记录 TXT记录 TTL值 PTR值 泛域名 泛解析 域 ...