Python Development Game open-service script

Source: Internet
Author: User
Tags lua print print

#!/usr/bin/env python#coding:utf-8from Pymongo Import mongoclientimport datetimeimport randomimport Shutilimport Timeimport sysimport reimport osclass Readytostart (object): Def countdown (self): print ("' ########################### ########### # Meet the pack adult a healthy Open service script # ###################################### ") print (' \033[1;31;40m script will begin execution ... \033[0m ') Time.sleep (2) Print for I in range (3,0,-1): Time.sleep (1) Print (' script also has \033[5;31;40m%s\033[0m seconds to start execution, please wait ... '% i) print def P Arameterscheck (self): global opennum,openyear,openmonth,openday try:opennum = sys.argv[1] If not re.match (' ^[0-9]+$ ', Opennum): If Not Re.match (' (^[0-9]+)-([0-9]+$] ', opennum): print (' Please output in the specified format. Example num1-num2,does not decimals ') sys.exit (1) else:parmet1 = Re.match (' (^[0-9]+)-([0-9]+$) ', opennum) if Int ( Parmet1.group (1)) > Int (parmet1.group (2)): Print (' The first opening number%s cannot be greater than the second opening Number%s '% (Parmet1.group (1), Parmet1.group (2))) Sys.exit (1) if Re.match (' ^[0] ', opennum): Print (' You cannot enter numbers starting with 0 ') sys.exit (1) except Exception as E:print e V alues = sys.argv[0] Print (' Please enter at least one parameter ') Exaples = ' Exaples:python {key} 10 | Python {key} 1-10 ' Print (Exaples.format (key=values)) Sys.exit (1) nowdate = Datetime.datetime.now () + Datetime.timedelta (days=1) Openyear = Nowdate.strftime ("%Y") Try:cusdate = sys.argv[2] except Exception as E:cusdate = nowdate Openmonth = Cusdate.strftime ("%m") Openday = Cusdate.strftime ("%d") if Re.match (' 0\d ', openmonth): Openmonth = openmonth[1:] If re.ma TCH (' 0\d ', openday): Openday = openday[1:] else:if not Re.match (' ^[0-9]+\.[ 0-9]+ ', cusdate): print ' The second parameter format is = + Month.day ' sys.exit (1) openmonth = Cusdate.split ('. ') [0] Openday = cusdate.split ('. ') [1] if int (openmonth) < 1 or int (openmonth) > 12:print ' The month entered cannot be greater than twelve and Les s than one ' sys.exit (1) if int (openday) < 1 or int (openday) >31:print ("You can ' t enter more than to or less than 1") Sys.exit (1) openmonth = ' = ' + openmonth openday = ' = ' + OPENDAYCL Deployservice (object): Def __init__ (self): self.toolsdirctory = '/data/tools/new_server/' self.servicedir = '/data/ bz-tw-and/s ' Self.codedir = '/data/bz-tw-and ' self.logdir = '/data/logs/s ' self.userinputspecified = [] Self.userinputall = [] Self.alreadyexist = [] def startdeploy (self): global Inputnum,newservice if not os.path.exists (self . Codedir): Print ('%s not exists,script exit '% self.codedir) sys.exit (0) if not os.path.exists (Self.toolsdirctory + ' confi G.lua ') or not os.path.exists (self.toolsdirctory + ' bin '): Print (' (%s and%s) file or Dirctory not exists,script exit '% (s Elf.toolsdirctory + ' Config.lua ', Self.toolsdirctory + ' bin ')) Sys.exit (1) inputnum = Re.match (' (\d+)-(\d+) ', OpenNum) in Putonenum = Re.match (' (^[1-9][0-9]*$) ', opennum) if Inputonenum:num = 1 for i in Os.listdir (self.codedir): Nowservice = R E.match (' (s) (\d+) ', i) if nowservice:num + = 1Nownum = i[1:] if opennum = = Nownum:print ('%s Service already existsservice already exists '% Opennum) sys.exit () newnum = str (num) + '-' + opennum inputnum = Re.match (' (\d+)-(\d+) ', newnum) if inputnum:onenum = Int (Inputnum.group (1)) Townum = Int (Inputnum.group (2)) for Funum in range (1,townum+1): Self.userInputall.append (Funum) for Funum in range (onenum,townu m+1): Self.userInputSpecified.append (Funum) for I in Os.listdir (self.codedir): Nowservice = Re.match (' (s) (\d+) ', i) if Nowservice:nownum = i[1:] self.alreadyExist.append (int (nownum)) differenceset= (Set (Self.userinputall) ^set ( self.alreadyexist)) intersection= (Set (self.userinputspecified) &set (self.alreadyexist)) NewService=list ( Differenceset) oldservice=list (intersection) if newservice! = []: for i in newservice:gameservice = Self.servicedir + str (i) Os.mkdir (gameservice) shutil.copytree (self.toolsdirctory + ' bin ', Gameservice + '/bin ') shutil.copytree ( Self.servicedir + str (1) + '/scripts ', Self.servicedir + str (i) + '/scripTS ') shutil.copyfile (self.toolsdirctory + ' Config.lua ', Gameservice + '/config.lua ') if not os.path.exists (Self.logdir + STR (i)): Os.mkdir (Self.logdir +str (i)) Os.symlink (Self.logdir+str (i), Self.servicedir + str (i) + '/bin/logs ') if not Os.path.exists (self.toolsdirctory + '/bin/bz-tw-and-s_ '): Print ('%s not exists,please Check '% (self.toolsdirctory + '/ Bin/bz-tw-and-s_ ') shutil.move (Self.servicedir + str (i) + '/bin/bz-tw-and-s_ ', Self.servicedir + str (i) + '/bin/ Bz-tw-and-s ' + str (i)) scriptlist = [' hotup.sh ', ' hotup_client.sh ', ' start.sh ', ' stop.sh '] for scriptname in Scriptlist:if Not os.path.exists (Self.servicedir+str (i) + '/bin/' + scriptname): Print ('%s not exists,please Check '% scriptname) HOTUPL ine = ' Hotupport = 10000 + int (i) for scriptname in [' hotup.sh ', ' hotup_client.sh ']: with open (Self.servicedir + str (i) + '/bin/' + scriptname, ' R ') as F:for line in F.readlines (): If Line.find (' 10000 '): line = Re.sub (' 10000 ', str (hotupport), Line) Hotupline + = line Else:hotupline + = lineWith open (Self.servicedir + str (i) + '/bin/' + scriptname, ' W ') as F:f.write (hotupline) start_stop_line = "for Scriptnam E in [' start.sh ', ' stop.sh ']: with open (Self.servicedir + str (i) + '/bin/' + scriptname, ' R ') as F:for line in F.readlines ( ): If Line.find (' 1 '): line = re.sub (' 1 ', str (i), line) Start_stop_line + = line Else:start_stop_line + line with open (sel F.servicedir + str (i) + '/bin/' + scriptname, ' W ') as F:f.write (start_stop_line) if oldservice! = []: For I in Oldservice: Print (' Already kai fu%s '% i) else:print (' Exaples:python%s | python%s 1-10 '% (Sys.argv[0],sys.argv[0])) class Mon Godabase (object): Def __init__ (self,serverip= ' 192.168.6.184 ', databaseport=27017,user= ' root ', password= ' 123456 ', Admindatabase= ' admin '): self.client = Mongoclient (serverip,databaseport) self.db = Self.client[admindatabase] Self.db.authenticate (User,password) def createpassword (self): password = "For me in Range" (8): current = Random.randrange ( 0,8) If current > i:temp = Random.randint (0,9) Elif Current < I:temp = Chr (Random.randint (65,90)) else:temp = Chr (Random.randint (97,122)) password + = str (temp) r Eturn Password def createUser (self): global passwordlist passwordlist = {} deploy = Deployservice () configfileline = ' PA Sswordpath = '/data/script/' if not os.path.exists (Passwordpath): Os.mkdir (passwordpath) print (' \033[1;31;40m deployment situation is as follows \ 033[0m ') print if newservice! = []: for i in newservice:clientport = 20000 + int (i) Httpport = 10000 + int (i) DbName = ' s ' + str (i) Svrid = str (i) Gameservicename = ' bz-tw-and-s ' + str (i) password = Self.createpassword () passwordlist.update ({i :p Assword}) DbUser = Self.client[gameservicename] Dbuser.command ("CreateUser", "BZ", Pwd=password, roles=["ReadWrite" ] Old = [' 20003 ', ' 10003 ', ' S3 ', ' Zxjryzgi ', ' Sid ', ' 2018 ', ' =5 ', ' =19 '] new = [Clientport,httpport,dbname,password,svrid, Openyear,openmonth,openday] for old,new in Zip (old,new): with open (Deploy.servicedir + str (i) + '/config.lua ', ' R ') as F:fo R line in F.readlines (): If Line.find (StR): line = Re.sub (str), str (new), line) Configfileline + = line Else:configfileline + = line with open (Deploy.servic Edir + str (i) + '/config.lua ', ' W ') as F:f.write (configfileline) Configfileline = ' with open (passwordpath+ ' mongo_pass.tx T ', ' A + ') as F:mongodbname = ' bz-tw-and-' + str (i) f.write (mongodbname+ ' = ' +password+ ' \ n ') os.remove (Deploy.servicedir + STR (i) + '/scripts/config.lua ') shutil.copyfile (Deploy.servicedir + str (i) + '/config.lua ', Deploy.servicedir + str (i) + ' /scripts/config.lua ') print (' \033[31m%s\033[0m service has been deployed ... '% i) print print (' \033[1;31;40m database password file is stored in the location below \033[0m ') Print print ('/data/script/mongo_pass.txt ') class Configcheck (object): Def luaconfigcheck (self): deploy = Deployservice () Month = openmonth[1:] Day = openday[1:] if newservice! = []: For I in Newservice:print ("' ########################## ################## #%s server config file check # ############################################ "% i) data = [' (Game_io_listen_port =) (\ d+) ', ' (game_http_listen_port=) (\d+) ', ' (DBName= "bz-tw-and-" (. *) (") ', ' (dbpwd=") (. *) (") ', ' (Svrid =) (. *) ', ' (activitystartdate={year=) (\d{4}) (, month=) (\d+) (, day=) (\d+) (. *) '] with open (Deploy.servicedir + str (i) + '/scripts/config.lua ', ' R ') as F:for line in F.readlines (): for D In Data:if Re.match (d, line): M = Re.match (d, line) Try:if Int (M.group (2)) = = 10000 + int (i) or int (M.group (2)) = = 20000 + int (i) or int (M.group (2)) = = Int (i): If Int (M.group (2)) = = Int (i): result = ' [\033[32mid correct \033[0m] ' else:result = ' [\ 033[32m Port correct \033[0m] ' Else:result = ' [\033[5;31m configuration error \033[0m] ' if int (M.group (2)) = = Int (openyear) and Int (M.group (4)) = = Int (month) and Int (M.group (6)) = = Int (day): Yresult = ' [\033[32m date correct \033[0m] ' Else:yresult = ' [\033[5;31m year or month or day error \033[0m ] ' except Exception as E:if str (M.group (2)) = = str (' s ' +str (i)) or Str (M.group (2)) = = str (passwordlist[i]): If STR (m.group (2) = = = str (passwordlist[i]): result = ' [\033[32m password correct \033[0m] ' Else:result = ' [\033[32m name correct \033[0m] ' else:result = ' [\ 033[5;31m configuration error \033[0m] ' Try:priNT ('%s\033[31m%s\033[0m%s\033[31m%s\033[0m%s\033[31m%s\033[0m%s%s '% (M.group (1), M.group (2), M.group (3), M.group (4 ), M.group (5), M.group (6), M.group (7), Yresult)) except Exception as E:try:print ('%s\033[31m%s\033[0m%s%s '% (M.group (1 ), M.group (2), M.group (3), result)) except Exception as E:print ('%s\033[31m%s\033[0m%s '% (M.group (1), M.group (2), ResU LT)) If __name__ = = ' __main__ ': start = Readytostart () start. Parameterscheck () start. Countdown () Deploy = Deployservice () deploy.startdeploy () db = Mongodabase () db.createuser () check = Configcheck () check.l Uaconfigcheck ()

Execution results show

Python Development Game open-service script

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.