#!/usr/bin/python
#
# Copyright (c) 2010 Red Hat, Inc.
#
# This program is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 2 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see http://www.gnu.org/licenses/.


import sys, xmlrpclib, syslog, os, time

USAGE_TEXT = """
Usage:
    rhts-test-checkin <lab_server> <hostname> <jobid> <test> <killtime> <testid>
"""
def usage():
    print USAGE_TEXT
    sys.exit(-1)

args = sys.argv[1:]
if len(args) != 6:
  print "ERROR: %s args provided" % (len(args))
  usage()

timetext = time.strftime('%m/%d/%y %H:%M:%S ', time.localtime(time.time()))

lab_server = sys.argv[1]
server = "http://%s/cgi-bin/rhts/scheduler_xmlrpc.cgi" % (lab_server)
hostname = sys.argv[2]
jobid = sys.argv[3]
test = sys.argv[4]
killtime = sys.argv[5]
testid = sys.argv[6]

client = xmlrpclib.Server(server)
while True:
    try:
        resp = client.watchdog.testCheckin(hostname, jobid, test, killtime, testid)
        break
    except:
        print "Unable to connect to server, sleeping 5 seconds..."
        time.sleep(5)

# syslog the message
syslog.syslog("JobID:%s Test:%s Response:%s" % (jobid, test, resp))
# send it to stderr as well
sys.stderr.write("%s JobID:%s Test:%s Response:%s\n" % (timetext, jobid, test, resp))
if resp != 1:
    sys.exit(1)
