1,重写PYTHON的代码(支持一切无重启在线更新,可惜我那个RUBY版哈)
2,用FLASK写了WEB UI。(JQUERY实时输出执行过程)
3,未完事项(加强的用户认证,BOOTSTRAP结成,支持JAVA打包。。。。)
#!/usr/bin/env python # -*- coding: utf-8 -*- import os,sys,commands,subprocess import re,time,logging from optparse import OptionParser logging.basicConfig(level=logging.INFO, format=‘%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s‘, datefmt=‘%a, %d %b %Y %H:%M:%S‘, filename=‘updateSVN.log‘, filemode=‘a‘) def get_svn_list(name, ip, version): svn_update = {} for line in open("source file"): line_item = line.strip("\n").split("|") if ip == line_item[0] and name == line_item[1]: line_item = line.strip("\n").split("|") svn_update["salt_minion_ip"] = line_item[0] svn_update["svn_name"] = line_item[1] svn_update["salt_master_dir"] = "/srv/salt/%s/" % line_item[0] svn_update["server_dir"] = line_item[2] svn_update["svn_dir"] = line_item[3] svn_update["svn_pre"] = line_item[4] svn_update["svn_version"] = version if len(svn_update) != 7: error_output("database have none item at your xargs.") return svn_update def error_output(error_str): print ‘\033[0;31;40m‘ print error_str + "<br>" print ‘\033[0m‘ sys.exit(1) def exec_cmd(cmd): str_now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) (status,output) = commands.getstatusoutput(cmd) try: if int(status) == 0: print "%s %s is ---OK!<br>" % (str_now, cmd) logging.info(cmd + " ---is OK!") return output else: logging.info(cmd + " cant‘t finish...") print "%s %s can‘t finish...<br>" % (str_now, cmd) sys.exit(1) except: logging.info(cmd + " is Wrong") print "%s %s --- is Wrong<br>" % (str_now, cmd) sys.exit(1) def salt_minion_test(salt_minion_ip): if "True" not in exec_cmd("salt ‘%s‘ test.ping" % salt_minion_ip): error_output("salt minion %s is not active,please restart this minion and execute is again. " % salt_minion_ip) def get_svn_file(svn_dir, svn_pre, svn_version, salt_master_dir): update_svn_A = [] update_svn_D = [] update_svn_M = [] svnLog_cmd ="svn log -v -r %s %s " % (svn_version, svn_dir) output = exec_cmd(svnLog_cmd).split(‘\n‘) for line in output: if line.startswith(" ") and "projects" in line: line_item = line.split() update_item = line_item[1].replace(svn_pre,‘‘) if line_item[0] == "A" : update_svn_A.append(update_item) if line_item[0] == "D" : update_svn_D.append(update_item) if line_item[0] == "M" : update_svn_M.append(update_item) if len(update_svn_A) == 0 and len(update_svn_D) == 0 and len(update_svn_M) == 0 : error_output("no update file in this version ") exec_cmd("mkdir -p %s" % salt_master_dir) exec_cmd("rm -rf %s*" % salt_master_dir) update_svn_all = update_svn_A + update_svn_M for file_item in update_svn_all: file_dir = salt_master_dir + ‘/‘.join(file_item.split(‘/‘)[:-1]) exec_cmd("mkdir -p %s" % file_dir) svn_export_cmd = "svn export %s%s %s%s --force" % (svn_dir, file_item, salt_master_dir, file_item) exec_cmd(svn_export_cmd) return update_svn_A, update_svn_D, update_svn_M def update_back(svn_name, salt_minion_ip,server_dir, svn_update_back): str_now = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime()) server_back_dir = ‘/‘.join(server_dir.split(‘/‘)[:-2]) + "/back/back-" + svn_name +str_now + "/" mkdir_back_cmd = "salt ‘%s‘ cmd.run ‘mkdir -p %s‘" % (salt_minion_ip, server_back_dir) exec_cmd(mkdir_back_cmd) for file_item in svn_update_back: back_dir_cmd = "salt ‘%s‘ cmd.run ‘mkdir -p %s‘" % (salt_minion_ip, ‘/‘.join((server_back_dir + file_item).split(‘/‘)[:-1])) exec_cmd(back_dir_cmd) back_cmd = "salt ‘%s‘ cmd.run ‘cp -p %s %s‘" % (salt_minion_ip, server_dir + file_item, server_back_dir + file_item) exec_cmd(back_cmd) def update_server_file(salt_minion_ip, server_dir, salt_master_dir, svn_update_server): for file_item in svn_update_server: salt_master_file = "salt://%s/%s" % (salt_minion_ip, file_item) salt_minion_file = server_dir + file_item update_file_cmd = "salt ‘%s‘ cp.get_file %s %s makedirs=True" % (salt_minion_ip, salt_master_file, salt_minion_file) #exec_cmd(update_file_cmd) chown_cmd = "salt ‘%s‘ cmd.run ‘chown x:x %s‘" % (salt_minion_ip, salt_minion_file) #exec_cmd(chown_cmd) def main(name, ip, version): svn_update = get_svn_list(name, ip, version) salt_minion_test(svn_update["salt_minion_ip"]) (svn_update_A, svn_update_D, svn_update_M) = get_svn_file(svn_update["svn_dir"], svn_update["svn_pre"], svn_update["svn_version"], svn_update["salt_master_dir"]) svn_update_back = svn_update_D + svn_update_M svn_update_server = svn_update_A + svn_update_M update_back(svn_update["svn_name"], svn_update["salt_minion_ip"], svn_update["server_dir"], svn_update_back) update_server_file(svn_update["salt_minion_ip"], svn_update["server_dir"], svn_update["salt_master_dir"], svn_update_server) if __name__=="__main__": usage = "usage: %prog [options] arg :./update_svn.py -v -n -i " parser = OptionParser(usage) parser.add_option("-v", "--version", dest="version", help="input the version") parser.add_option("-n", "--name", dest="name", help="input the svn name") parser.add_option("-i", "--ip", dest="ip", help="input the ip address") (options, args) = parser.parse_args() version = options.version name = options.name ip = options.ip if version is None or name is None or ip is None : error_output("you must input all args: ./update_svn -i xx -n xx -v xx") main(name, ip, version)
FLASK主框架如下:
import sqlite3 import commands,os from flask import Flask, request, session, g, redirect, url_for, abort, render_template, flash from contextlib import closing #from flask.ext.bootstrap import Bootstrap from flask_bootstrap import Bootstrap from app import app import subprocess bootstrap = Bootstrap(app) @app.route("/update_output") def import_html(): return render_template(‘update_output.html‘) @app.route(‘/update_svn‘, methods=[‘GET‘, ‘POST‘]) def update_svn(): svn_name = request.form[‘svn_name‘] svn_version = request.form[‘svn_version‘] server_ip = request.form[‘server_ip‘] os.system("python update_svn.py -v %s -n %s -i %s > update_output.html" % (svn_version, svn_name, server_ip)) flash(‘Command is %s %s %s ,please wait....‘ % (svn_name, svn_version, server_ip)) return redirect(url_for(‘main_operation_php‘)) @app.route(‘/‘) @app.route(‘/login‘, methods=[‘GET‘, ‘POST‘]) def login(): error = None if request.method == ‘POST‘: if request.form[‘username‘] != app.config[‘USERNAME‘]: error = ‘Invalid password‘ elif request.form[‘password‘] != app.config[‘PASSWORD‘]: error = ‘Invalid password‘ else: session[‘logged_in‘] = True flash(‘You were logged in‘) return redirect(url_for(‘main_operation_php‘)) return render_template(‘login.html‘, error=error) @app.route(‘/main_operation_php‘) def main_operation_php(): if not session.get(‘logged_in‘): abort(401) return render_template(‘main_operation_php.html‘) @app.route(‘/main_operation_java‘) def main_operation_java(): if not session.get(‘logged_in‘): abort(401) return render_template(‘main_operation_java.html‘) @app.route(‘/logout‘) def logout(): session.pop(‘logged_in‘, None) flash(‘You were logged out‘) return redirect(url_for(‘login‘))
来张丑图(以后完了再上漂亮眯的):
时间: 2024-11-05 16:28:21