config and points submission stuff added to polster

This commit is contained in:
Curt Hash 2009-10-08 11:11:41 -06:00
parent 04893b33bd
commit 7c07f13117
2 changed files with 63 additions and 21 deletions

View File

@ -37,6 +37,10 @@ else:
'house_team': 'dirtbags', 'house_team': 'dirtbags',
'passwd': '/var/lib/ctf/passwd', 'passwd': '/var/lib/ctf/passwd',
'team_colors': team_colors, 'team_colors': team_colors,
'poll_interval': 60,
'poll_timeout': 0.5,
'heartbeat_dir': '/var/lib/pollster',
'poll_dir': '/var/lib/www',
}, },
'puzzler': 'puzzler':
{'dir': '/usr/lib/www/puzzler', {'dir': '/usr/lib/www/puzzler',

View File

@ -6,16 +6,37 @@ import sys
import time import time
import socket import socket
import traceback import traceback
import threading
import queue
# TODO: from ctf import config
# scoring interface from ctf import pointscli
# config interface
DEBUG = False DEBUG = False
POLL_INTERVAL = 60 POLL_INTERVAL = config.get('poll_interval')
IP_DIR = 'iptest/' IP_DIR = config.get('heartbeat_dir')
REPORT_PATH = 'iptest/pollster.html' REPORT_PATH = config.get('poll_dir')
SOCK_TIMEOUT = 0.5 SOCK_TIMEOUT = config.get('poll_timeout')
class PointSubmitter(threading.Thread):
''' Pulls point allocations from the queue and submits them. '''
def __init__(self, point_queue):
threading.Thread.__init__(self)
self.point_queue = point_queue
self.sock = pointscli.makesock('localhost')
def run(self):
# loop forever
while(True):
cat, team, score = self.point_queue.get()
if None in [cat, team, score]:
continue
try:
pointscli.submit(cat, team, score, sock=self.sock)
except ValueError:
print('pollster: error submitting score (%s, %s, %d)' % (cat, team, score))
traceback.print_exc()
def socket_poll(ip, port, msg, prot, max_recv=1): def socket_poll(ip, port, msg, prot, max_recv=1):
''' Connect via socket to the specified <ip>:<port> using the ''' Connect via socket to the specified <ip>:<port> using the
@ -137,6 +158,11 @@ POLLS = {
ip_re = re.compile('(\d{1,3}\.){3}\d{1,3}') ip_re = re.compile('(\d{1,3}\.){3}\d{1,3}')
# start point submitter thread
point_queue = queue.Queue()
t = PointSubmitter(point_queue)
t.start()
# loop forever # loop forever
while True: while True:
@ -154,11 +180,17 @@ while True:
except Exception as e: except Exception as e:
pass pass
out = open(REPORT_PATH, 'w') try:
out.write('<html>\n<head>\n') out = open(REPORT_PATH, 'w')
out.write('<title>Pollster Results</title>\n') except Exception as e:
out.write('<link rel="stylesheet" href="ctf.css" type="text/css" media="all" />\n') out = None
out.write('</head><body>\n<h1>Polling Results</h1>\n') pass
if out is not None:
out.write('<html>\n<head>\n')
out.write('<title>Pollster Results</title>\n')
out.write('<link rel="stylesheet" href="ctf.css" type="text/css" media="all" />\n')
out.write('</head><body>\n<h1>Polling Results</h1>\n')
for ip in ips: for ip in ips:
@ -178,9 +210,10 @@ while True:
if DEBUG is True: if DEBUG is True:
print('ip: %s' % ip) print('ip: %s' % ip)
out.write('<h2>%s</h2>\n' % ip) if out is not None:
out.write('<table class="pollster">\n<thead><tr><td>Service Name</td></td>') out.write('<h2>%s</h2>\n' % ip)
out.write('<td>Flag Holder</td></tr></thead>\n') out.write('<table class="pollster">\n<thead><tr><td>Service Name</td></td>')
out.write('<td>Flag Holder</td></tr></thead>\n')
# perform polls # perform polls
for service,func in POLLS.items(): for service,func in POLLS.items():
@ -191,9 +224,13 @@ while True:
if DEBUG is True: if DEBUG is True:
print('\t%s - %s' % (service, team)) print('\t%s - %s' % (service, team))
out.write('<tr><td>%s</td><td>%s</td>\n' % (service, team)) if out is not None:
out.write('<tr><td>%s</td><td>%s</td>\n' % (service, team))
out.write('</table>\n')
point_queue.put((service, team, 1))
if out is not None:
out.write('</table>\n')
if DEBUG is True: if DEBUG is True:
print('+-----------------------------------------+') print('+-----------------------------------------+')
@ -202,9 +239,10 @@ while True:
exec_time = int(t_end - t_start) exec_time = int(t_end - t_start)
sleep_time = POLL_INTERVAL - exec_time sleep_time = POLL_INTERVAL - exec_time
out.write('<p><b>Next poll in: %ds</b></p>\n' % sleep_time) if out is not None:
out.write('</body>\n</html>\n') out.write('<p><b>Next poll in: %ds</b></p>\n' % sleep_time)
out.close() out.write('</body>\n</html>\n')
out.close()
# sleep until its time to poll again # sleep until its time to poll again
time.sleep(sleep_time) time.sleep(sleep_time)