cmlenz@13: # -*- coding: iso8859-1 -*- cmlenz@13: # cmlenz@13: # Copyright (C) 2005 Christopher Lenz cmlenz@13: # cmlenz@13: # Bitten is free software; you can redistribute it and/or cmlenz@13: # modify it under the terms of the GNU General Public License as cmlenz@13: # published by the Free Software Foundation; either version 2 of the cmlenz@13: # License, or (at your option) any later version. cmlenz@13: # cmlenz@13: # Trac is distributed in the hope that it will be useful, cmlenz@13: # but WITHOUT ANY WARRANTY; without even the implied warranty of cmlenz@13: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU cmlenz@13: # General Public License for more details. cmlenz@13: # cmlenz@13: # You should have received a copy of the GNU General Public License cmlenz@13: # along with this program; if not, write to the Free Software cmlenz@13: # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. cmlenz@13: # cmlenz@13: # Author: Christopher Lenz cmlenz@13: cmlenz@15: import logging cmlenz@13: import os cmlenz@13: import sys cmlenz@42: import tempfile cmlenz@13: import time cmlenz@13: cmlenz@19: from bitten import __version__ as VERSION cmlenz@29: from bitten.util import beep, xmlio cmlenz@13: cmlenz@13: cmlenz@13: class Slave(beep.Initiator): cmlenz@13: cmlenz@13: def greeting_received(self, profiles): cmlenz@19: if OrchestrationProfileHandler.URI not in profiles: cmlenz@15: logging.error('Peer does not support Bitten profile') cmlenz@15: raise beep.TerminateSession, 'Peer does not support Bitten profile' cmlenz@29: self.channels[0].profile.send_start([OrchestrationProfileHandler]) cmlenz@13: cmlenz@14: cmlenz@19: class OrchestrationProfileHandler(beep.ProfileHandler): cmlenz@19: """Handler for communication on the Bitten build orchestration profile from cmlenz@19: the perspective of the build slave. cmlenz@13: """ cmlenz@19: URI = 'http://bitten.cmlenz.net/beep/orchestration' cmlenz@13: cmlenz@29: def handle_connect(self, init_elem=None): cmlenz@13: """Register with the build master.""" cmlenz@13: def handle_reply(cmd, msgno, msg): cmlenz@15: if cmd == 'ERR': cmlenz@13: if msg.get_content_type() == beep.BEEP_XML: cmlenz@29: elem = xmlio.parse(msg.get_payload()) cmlenz@13: if elem.tagname == 'error': cmlenz@13: raise beep.TerminateSession, \ cmlenz@29: '%s (%d)' % (elem.gettext(), int(elem.code)) cmlenz@13: raise beep.TerminateSession, 'Registration failed!' cmlenz@15: logging.info('Registration successful') cmlenz@29: cmlenz@29: sysname, nodename, release, version, machine = os.uname() cmlenz@29: logging.info('Registering with build master as %s', nodename) cmlenz@29: xml = xmlio.Element('register', name=nodename)[ cmlenz@29: xmlio.Element('platform')[machine], cmlenz@29: xmlio.Element('os', family=os.name, version=release)[sysname] cmlenz@29: ] cmlenz@29: self.channel.send_msg(beep.MIMEMessage(xml), handle_reply) cmlenz@13: cmlenz@13: def handle_msg(self, msgno, msg): cmlenz@43: if msg.get_content_type() == 'application/tar': cmlenz@42: logging.info('Received snapshot') cmlenz@42: workdir = tempfile.mkdtemp(prefix='bitten') cmlenz@42: archive_name = msg.get('Content-Disposition', 'snapshot.tar.gz') cmlenz@42: archive_path = os.path.join(workdir, archive_name) cmlenz@42: file(archive_path, 'wb').write(msg.get_payload()) cmlenz@42: logging.info('Stored snapshot archive at %s', archive_path) cmlenz@42: cmlenz@42: # TODO: Spawn the build process cmlenz@42: cmlenz@42: xml = xmlio.Element('ok') cmlenz@42: self.channel.send_rpy(msgno, beep.MIMEMessage(xml)) cmlenz@42: logging.info('Sent in reply to build request') cmlenz@42: cmlenz@42: else: cmlenz@42: xml = xmlio.Element('error', code=500)['Sorry, what?'] cmlenz@42: self.channel.send_err(msgno, beep.MIMEMessage(xml)) cmlenz@13: cmlenz@13: cmlenz@31: def main(): cmlenz@19: from optparse import OptionParser cmlenz@19: cmlenz@19: parser = OptionParser(usage='usage: %prog [options] host [port]', cmlenz@19: version='%%prog %s' % VERSION) cmlenz@19: parser.add_option('--debug', action='store_const', dest='loglevel', cmlenz@19: const=logging.DEBUG, help='enable debugging output') cmlenz@19: parser.add_option('-v', '--verbose', action='store_const', dest='loglevel', cmlenz@19: const=logging.INFO, help='print as much as possible') cmlenz@19: parser.add_option('-q', '--quiet', action='store_const', dest='loglevel', cmlenz@19: const=logging.ERROR, help='print as little as possible') cmlenz@19: parser.set_defaults(loglevel=logging.WARNING) cmlenz@19: options, args = parser.parse_args() cmlenz@19: cmlenz@13: if len(args) < 1: cmlenz@19: parser.error('incorrect number of arguments') cmlenz@13: host = args[0] cmlenz@13: if len(args) > 1: cmlenz@18: try: cmlenz@18: port = int(args[1]) cmlenz@19: assert (1 <= port <= 65535), 'port number out of range' cmlenz@19: except AssertionError, ValueError: cmlenz@19: parser.error('port must be an integer in the range 1-65535') cmlenz@13: else: cmlenz@13: port = 7633 cmlenz@13: cmlenz@19: logging.getLogger().setLevel(options.loglevel) cmlenz@13: cmlenz@13: slave = Slave(host, port) cmlenz@34: try: cmlenz@34: slave.run() cmlenz@34: except KeyboardInterrupt: cmlenz@34: slave.quit() cmlenz@31: cmlenz@31: if __name__ == '__main__': cmlenz@33: main()