This script is used to automatically update the game server. The main process is to put the configuration files of the update package and script in the center. The client of the branch data center downloads the update package from the center, verify the MD5 value. The game server in the branch data center downloads the corresponding update package through rsync as needed. The game server ensures that the update is completed correctly by extracting, copying, and verifying the MD5 value of some important files, currently, this script has been used to update multiple games of the company. It is compatible with windows and linux systems (based on their own needs to change the relevant path) and can be called on pages. The post page return value has been written.
#! /Usr/bin/env python #-*-coding: UTF-8-*-# Used update game path to server import OS, re, sys, urllib, urllib2, hashlib, time, shutil, platform def post (status, type, info, err_info = ""): post_url = args ["post_url"] aid = args ["aid"] data = {"aid": aid, "status": status, "type": type, "info": info} if err_info: data = {"aid": aid, "status": status, "type": type, "info": info, "err_info ": err_info} print d Ata f = urllib2.urlopen (url = post_url, data = urllib. urlencode (data) def md5sum (file_name): if OS. path. isfile (file_name): f = open (file_name, 'rb') py_ver = sys. version [: 3] if py_ver = "2.4": import md5 as hashlib else: import hashlib md5 = hashlib. md5 (f. read ()). hexdigest () f. close () return md5 else: return 0 def config (args, files): try: game = args ["main_prefix"] url = "http://208.asktao.com/autoup Date/% s/config. ini "% game get = urllib. urlopen (url) aa = get. readlines () w ={} for I in aa: a = I. strip (). split () [0] if "[" in a: x =. strip ("[]") w [x] ={} continue w [x] [I. strip (). split () [1]. strip ()] = I. strip (). split () [0]. strip () for key in w: if key = files: return w [key] failed t Exception, e: return 0 class down_start (): def work (self, args ): aa = config (args, "path_md5") game = args ["main_pre Fix "] if aa = 0: post (2," read update config "," Not find % s config file "% game) sys. exit () local = '/data/autoupdate/' url = "http://208.asktao.com/autoupdate/%s/" % game for f in aa: md5_r = f. strip (). split () [0] pkg_name = f. strip (). split () [1] get = urllib. urlopen (OS. path. join (url, pkg_name) status = get. getcode () if status = 200: # verify MD5. if MD5 is incorrect, download it again. if it is incorrect again, the system prompts failure and exits the urllib program. urlretrieve (OS. path. joi N (url, pkg_name), OS. path. join (local, pkg_name),) md5_l = md5sum (OS. path. join (local, pkg_name) if md5_l = md5_r: post (1, "down", pkg_name) else: urllib. urlretrieve (OS. path. join (url, pkg_name), OS. path. join (local, pkg_name),) md5_l = md5sum (OS. path. join (local, pkg_name) if md5_l = md5_r: post (1, "down", pkg_name) else: post (2, "down", pkg_name, "Download % s, MD5 not right "% pkg_name) sys. exit () else: post (2 ," Down ", pkg_name," Not find % s path file "% pkg_name) # Check whether the rsync service starts while True: pid = OS. popen ("ps auxww | grep 'rsync -- daemon' | grep-v grep "). read () if not pid: getso ("/usr/bin/rsync -- daemon") continue break # all completed, update center status post (1, "down ", "down_done") class sync_start (): def rsync (self, update_pkg, args): node_ip = args ["node_ip"] game = args ["main_prefix"] OS _type = platform. system () if OS _type = "Windows": local = "C: \ update \" elif OS _type = "Linux": local = "/home/update/tmp/" if not OS. path. isdir (local): OS. mkdir (local) for f in OS. listdir (local): if OS. path. isfile (f): OS. remove (OS. path. join (local, f) elif OS. path. isdir (f): shutil. rmtree (OS. path. join (local, f) aa = config (args, "path") if aa = 0: post (2, "read update config ", "Not find % s config file" % game) sys. exit () for f in aa: if Update_pkg in f: md5_r = aa [f] pkg_name = f if OS _type = "Windows": sync = "rsync-avz % s :: update/* % s */cygdrive/c/update/"% (node_ip, update_pkg) elif OS _type =" Linux ": sync =" rsync-avz % s :: update/* % s */home/update/tmp/"% (node_ip, update_pkg) result = OS. popen (sync ). readlines () md5_l = md5sum (OS. path. join (local, pkg_name) if md5_l = md5_r: post (1, "sync", pkg_name) else: if OS _type = "Wind Ows ": sync =" rsync-avz % s: update/* % s */cygdrive/c/update/"% (node_ip, update_pkg) elif OS _type = "Linux": sync = "rsync-avz % s: update/* % s */home/update/tmp/" % (node_ip, update_pkg) result = OS. popen (sync ). readlines () md5_l = md5sum (OS. path. join (local, pkg_name) if md5_l = md5_r: post (1, "sync", pkg_name) else: post (2, "rsync", pkg_name, "sync Error, % s not find or file's md5 wrong "% pkg_name) sy S. exit () def work (self, args): update_pkg = args ["update_pkg"]. split (',') for I in range (len (update_pkg): self. rsync (update_pkg [I], args) post (1, "sync", "sync_done") class update_start (): def copy (self, src, dst): if OS. path. isdir (src): base = OS. path. basename (src) if OS. path. exists (dst): dst = OS. path. join (dst, base) if not OS. path. exists (dst): OS. makedirs (dst) names = OS. listdir (src) for name in na Mes: srcname = OS. path. join (src, name) self. copy (srcname, dst) else: shutil. copy2 (src, dst) def unrar (self, src, dst, args): OS _type = platform. system () try: if not OS. path. exists (dst) or not OS. path. exists (src): raise Exception, "% s or % s not exist! "% (Src, dst) if OS _type =" Windows ": OS. system (r'c: \ Progra ~ 1 \ WinRAR \ rar x-o +-inul % s % s' % (src, dst) elif OS _type = "Linux": if OS. path. splitext (src) [1] = ". tgz ": OS. system ("tar-zxf % s-C % s" % (src, dst) elif OS. path. splitext (src) [1] = ". zip ": OS. system ("unzip-oq % s-d % s" % (src, dst) return 0 failed t Exception, e: return e def work (self, args ): OS _type = platform. system () if OS _type = "Windows": update = "C: \ update \" elif OS _type = "Linux ": update = "/home/update/tmp/" server_dir = {"tmcs": "C: \ Server \", "wd ": "/home/asktao/"} server = server_dir [args ["main_prefix"] update_pkg = args ["update_pkg"]. split (',') game = args ["main_prefix"] for I in range (len (update_pkg )): #################### decompress the update package for tgz in OS. listdir (update): if update_pkg [I] in tgz: src = OS. path. join (update, tgz) r = self. unrar (src, update, args) if r = 0: post (1, "update", "% s unzip success" % src) else: post (2, "update", src, "unzip fail: % s" % r) #################### copy the updated file to the game directory ser_list = OS. listdir (server) up_list = OS. listdir (update) for dir in up_list: for line in ser_list: filepath = OS. path. join (update, dir + "\") serv = OS. path. join (server, line + "\") if dir in line: self. copy (filepath, serv) post (1, "update", "% s files copy success" % line) #################### verify the MD5 of important files aa = config (args, "files ") if aa = 0: post (2, "read update config", "Not find % s config file" % game) sys. exit () for f in aa: md5_r = aa [f] pkg_name = f if OS. path. exists (pkg_name): md5_s = md5sum (pkg_name) if md5_r = md5_s: post (1, "update", "% s md5 OK, update success" % pkg_name) else: post (2, "update", "% s md5 Error, update fail" % pkg_name) sys. exit () shutil. rmtree (update) OS. mkdir (update) post (1, "update", "All_done") if _ name _ = "_ main _": args = {"pack ": "auto_update", "func1": "down_start", "url": "http://208.2222.com/manage/auto_update", "post_url": "http: // 192.168.50.209/reg. php "," update_pkg ":" up, up "," node_ip ":" 192.168.50.208 "," main_prefix ":" tmcs "," aid ": "123" }# down = down_start () # down. work (args) sync = sync_start () sync. work (args) update = update_start () update. work (args) ''' config. ini content [files] # absolute path of the important MD5 files to be verified and MD5 15a6605156e29f68fd637e73a889d4 C: \ Server \ Line1 \ SAFlashPlayer.exe 15a6605156e29f68fdfd637e73a889d4 C: \ Server \ Line2 \ SAFlashPlayer.exe [path] # update the package name and MD5 32fcb8932799aa553db13b5b9b41e5e9 auto.rar 32fcb8932799aa553db13b5b9b41e5e9 update.rar '''