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',
'passwd': '/var/lib/ctf/passwd',
'team_colors': team_colors,
'poll_interval': 60,
'poll_timeout': 0.5,
'heartbeat_dir': '/var/lib/pollster',
'poll_dir': '/var/lib/www',
},
'puzzler':
{'dir': '/usr/lib/www/puzzler',

View File

@ -6,16 +6,37 @@ import sys
import time
import socket
import traceback
import threading
import queue
# TODO:
# scoring interface
# config interface
from ctf import config
from ctf import pointscli
DEBUG = False
POLL_INTERVAL = 60
IP_DIR = 'iptest/'
REPORT_PATH = 'iptest/pollster.html'
SOCK_TIMEOUT = 0.5
POLL_INTERVAL = config.get('poll_interval')
IP_DIR = config.get('heartbeat_dir')
REPORT_PATH = config.get('poll_dir')
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):
''' 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}')
# start point submitter thread
point_queue = queue.Queue()
t = PointSubmitter(point_queue)
t.start()
# loop forever
while True:
@ -154,11 +180,17 @@ while True:
except Exception as e:
pass
out = open(REPORT_PATH, 'w')
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')
try:
out = open(REPORT_PATH, 'w')
except Exception as e:
out = None
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:
@ -178,9 +210,10 @@ while True:
if DEBUG is True:
print('ip: %s' % ip)
out.write('<h2>%s</h2>\n' % ip)
out.write('<table class="pollster">\n<thead><tr><td>Service Name</td></td>')
out.write('<td>Flag Holder</td></tr></thead>\n')
if out is not None:
out.write('<h2>%s</h2>\n' % ip)
out.write('<table class="pollster">\n<thead><tr><td>Service Name</td></td>')
out.write('<td>Flag Holder</td></tr></thead>\n')
# perform polls
for service,func in POLLS.items():
@ -191,9 +224,13 @@ while True:
if DEBUG is True:
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:
print('+-----------------------------------------+')
@ -202,9 +239,10 @@ while True:
exec_time = int(t_end - t_start)
sleep_time = POLL_INTERVAL - exec_time
out.write('<p><b>Next poll in: %ds</b></p>\n' % sleep_time)
out.write('</body>\n</html>\n')
out.close()
if out is not None:
out.write('<p><b>Next poll in: %ds</b></p>\n' % sleep_time)
out.write('</body>\n</html>\n')
out.close()
# sleep until its time to poll again
time.sleep(sleep_time)