Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import os
- import sys
- import subprocess
- import MySQLdb
- from threading import Thread
- from time import time, sleep
- import games
- spawn_dir = '/root/serverspawns'
- class ConnHelper:
- def __init__(self):
- self.mysql_host = 'localhost'
- self.mysql_user = 'sbuser'
- self.mysql_pwd = 'serve$Moskv4_Sql'
- self.mysql_db = 'sbdb'
- def _connect(self):
- self.conn = MySQLdb.connect(host = self.mysql_host,
- user = self.mysql_user,
- passwd = self.mysql_pwd,
- db = self.mysql_db)
- self.cursor = self.conn.cursor()
- def free_server(self, serv_port):
- self._connect()
- self.cursor.execute("UPDATE booking_server SET in_use = '0' WHERE port = '%s'" % serv_port)
- self.cursor.close()
- self.conn.close()
- class WatchFolder(Thread):
- def __init__(self):
- global spawn_dir
- self.path_to_watch = spawn_dir
- self.servers = []
- Thread.__init__(self)
- def checkfile(self, file_name):
- spawn_file = '%s/%s' % (self.path_to_watch, file_name)
- f = open(spawn_file)
- lines = f.readlines()
- values = {}
- values['port'] = '27015'
- values['sname'] = 'Serverbooking.com'
- values['map'] = 'de_dust2'
- values['players'] = '12'
- values['rcon_pwd'] = 'funkypowa'
- values['pwd'] = 'lol'
- values['book_time'] = '1'
- values['book_game'] = 'cstrike'
- if '!SB786' in lines[0]:
- print 'input file is valid. fetching configuration ..'
- for line in lines[1:]:
- print line
- line = line.replace('\n', '')
- val = line.split('=')
- values[val[0]] = val[1]
- print 'starting new server ..'
- self.servers.append(Server(values))
- f.close()
- os.remove(spawn_file)
- print 'input file removed.'
- return
- def run(self):
- while 1:
- for f in os.listdir(self.path_to_watch):
- self.checkfile(f)
- for server in self.servers:
- if not server.started:
- server.start()
- if server.expired():
- print 'server expired ..'
- server.stopserver()
- self.servers.remove(server)
- sleep(20)
- class Server(Thread):
- def __init__(self, values):
- reload(games)
- game = games.games_list(values['book_game'])
- self.game_dir = game[0]
- self.game_file = game[1]
- self.port = values['port']
- self.hostname = values['sname']
- self.map = values['map']
- self.players = values['players']
- self.rcon_password = values['rcon_pwd']
- self.password = values['pwd']
- self.game = values['book_game']
- self.start_cmd = games.game_cmd(self)
- self.expire_time = time() + (int(values['book_time']) * (60 * 60))
- self.started = 0
- self.p = None
- self.screen = None
- Thread.__init__(self)
- def expired(self):
- #print '%s -> %s' % (self.expire_time, time())
- if self.expire_time < time():
- return 1
- else:
- return 0
- def started(self):
- return self.started
- def stopserver(self):
- print 'stopping server .. (%s)' % self.port
- kill_line = 'kill $(ps -ef | grep "SCREEN -dmS p%s" | cut -d" " -f7)' % self.port
- print kill_line
- os.system(kill_line)
- # screen -dRR p27015 -X kill
- helper = ConnHelper()
- helper.free_server(self.port)
- return
- def run(self):
- self.started = 1
- print 'server is booting ..'
- # LINE BELOW IS THE COMMAND TO START THE SERVER PROCESS, PLEASE EDIT WHEN NESCESSARY!!
- print self.game_dir
- print self.game_file
- self.p = subprocess.Popen(self.start_cmd, shell=True, stdout=subprocess.PIPE)
- #self.screen = subprocess.Popen('screen -r p%s' % self.port, shell=True, stdout=subprocess.PIPE)
- '''
- Server output logger
- Things that should be done to interact with server in any way,
- should be written in loop below:
- '''
- while 1:
- #line = self.screen.communicate()[0]
- #print line
- sleep(300) # sleep for 5 minutes.
- WatchFolder().start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement