目前就职的公司,架构一直没有稳定下来。运维相关工具也没有完善,有的开发没有登入测试环境服务器的权限,所以就自己写个自动更新测试环境的工具。因为服务器的Python 版本为 python 2.6.6
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>autoRelease</title>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
</head>
<body>
<form method="post" action="/" id="update">
<p><label>环境:<input name="environ" type="text" value="test"/></label></p>
<p><label>分支:<input name="branch" type="text"/></label></p>
<p><input type="submit" value="更新"/></p>
</form>
<script type="text/javascript">
(function ($) {
$(document).ready(function () {
$('#update').submit(function (e) {
e.preventDefault();
$.post('/', $(this).serialize(), function (response) {
console.log(response);
}, 'json');
return false;
});
});
})(jQuery)
</script>
</body>
</html>
import SimpleHTTPServer
import SocketServer
import logging
import cgi
import subprocess
class AutoReleaseRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
logging.warning("======= GET STARTED =======")
logging.warning(self.headers)
self.path = '/index.html'
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
def do_POST(self):
logging.warning("======= POST STARTED =======")
logging.warning(self.headers)
form = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={'REQUEST_METHOD': 'POST',
'CONTENT_TYPE': self.headers['Content-Type'],
})
logging.warning("======= POST VALUES =======")
self.wfile.write('{"status":"OK"')
for item in form.list:
logging.warning(item)
self.wfile.write(',"%s":"%s"\n' % (item.name, item.value))
command = 'cd /home/www/%s/; git stash;git pull --rebase origin %s ' % (
form['environ'].value, form['branch'].value)
self.wfile.write(',"command":"%s"' % command)
self.wfile.write('}')
subprocess.Popen(command, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, shell=True)
logging.warning("\n")
if __name__ == '__main__':
print 'Starting server, use <Ctrl-C> to stop'
server = SocketServer.TCPServer(('0.0.0.0', 1024), AutoReleaseRequestHandler)
server.serve_forever()
nohup python release.py > /dev/null &