def main(): """ Run as a utility for launching Turk Core usage: turkctl.py start|stop """ parser = OptionParser("usage: %prog [options] <start|stop|restart|clean>") parser.add_option("-f", "--config-file", dest="config", type="string", default='/etc/turk/turk.yml', help="default configuration file") (options, args) = parser.parse_args() if len(args) != 1: parser.error("incorrect number of arguments") conf = yaml.load(open(options.config, 'rU')) os.environ['TURK_CONF'] = options.config global log log = init_logging('turkctl', conf, debug=get_config('turkctl.debug')) { 'start': start, 'stop': stop, 'restart': lambda conf: (stop(conf), start(conf)), 'clean': clean }[args[0]](conf)
def clean(conf): """Deletes any data associated with improperly stopped Turk Core""" pidfile_path = get_config('turkctl.pidfile', conf) if os.path.exists(pidfile_path): log.debug('Removing old pidfile...') os.unlink(pidfile_path) else: log.debug('No pidfile to remove!')
def run(conf='/etc/turk/turk.yml'): global BUS_NAME if isinstance(conf, basestring): try: conf = yaml.load(open(conf, 'rU'))['xbeed'] except Exception: log.debug( 'failed opening configuration file "%s"' % (conf)) exit(1) log = turk.init_logging('xbeed', conf, debug=get_config('xbeed.debug')) dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) bus = getattr(dbus, get_config('global.bus', conf))() BUS_NAME = dbus.service.BusName(XBEED_SERVICE, bus) daemon = XBeeDaemon(*get_configs(('name', 'port', 'escaping', 'baudrate'), conf, 'xbeed')) mainloop = gobject.MainLoop() mainloop.run()
def run(conf='/etc/turk/turk.yml'): global BUS_NAME if isinstance(conf, basestring): try: conf = yaml.load(open(conf, 'rU'))['xbeed'] except Exception: log.debug('failed opening configuration file "%s"' % (conf)) exit(1) log = turk.init_logging('xbeed', conf, debug=get_config('xbeed.debug')) dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) bus = getattr(dbus, get_config('global.bus', conf))() BUS_NAME = dbus.service.BusName(XBEED_SERVICE, bus) daemon = XBeeDaemon( *get_configs(('name', 'port', 'escaping', 'baudrate'), conf, 'xbeed')) mainloop = gobject.MainLoop() mainloop.run()
def start(conf): """ Starts the Turk Core as a background process. """ pidfile_path = get_config('turkctl.pidfile', conf) if os.path.exists(pidfile_path): print 'File "%s" exists - Is Turk Core already running?' % pidfile_path exit(-1) pidfile = open(pidfile_path, 'w') pid = os.fork() if not pid: # Controller process try: log.debug('starting spawner...') spawner = multiprocessing.Process(target=run_spawner, args=(conf, ), name='spawner') spawner.start() log.debug('starting bridge...') bridge = multiprocessing.Process(target=run_bridge, args=(conf, )) bridge.start() if conf.has_key('xbeed'): log.debug('starting xbeed...') xbeed = multiprocessing.Process(target=run_xbeed, args=(conf, )) xbeed.start() except Exception, e: log.debug('turkctl: error starting Turk: %s' % e) os.unlink(pidfile_path) exit(-1) def finished(*args): log.debug('stopping Turk...') spawner.terminate() bridge.terminate() if conf.has_key('xbeed'): xbeed.terminate() exit(0) signal.signal(signal.SIGTERM, finished) while 1: # Just do nothing until terminated by turkctl sleep(1)
def start(conf): """ Starts the Turk Core as a background process. """ pidfile_path = get_config('turkctl.pidfile', conf) if os.path.exists(pidfile_path): print 'File "%s" exists - Is Turk Core already running?' % pidfile_path exit(-1) pidfile = open(pidfile_path, 'w') pid = os.fork() if not pid: # Controller process try: log.debug('starting spawner...') spawner = multiprocessing.Process(target=run_spawner, args=(conf,), name='spawner') spawner.start() log.debug('starting bridge...') bridge = multiprocessing.Process(target=run_bridge, args=(conf,)) bridge.start() if conf.has_key('xbeed'): log.debug('starting xbeed...') xbeed = multiprocessing.Process(target=run_xbeed, args=(conf,)) xbeed.start() except Exception, e: log.debug('turkctl: error starting Turk: %s' % e) os.unlink(pidfile_path) exit(-1) def finished(*args): log.debug('stopping Turk...') spawner.terminate() bridge.terminate() if conf.has_key('xbeed'): xbeed.terminate() exit(0) signal.signal(signal.SIGTERM, finished) while 1: # Just do nothing until terminated by turkctl sleep(1)
def stop(conf): """ Reads the PID file left by start() and sends SIGTERM to all of the daemon processes that make up the runtime """ print 'stopping Turk...' pidfile_path = get_config('turkctl.pidfile', conf) if not os.path.exists(pidfile_path): print 'Couldn\'t find pidfile! Is Turk REALLY running?' return # Get pids from file pidfile = open(pidfile_path, 'rU') # Kill all the Turk processes (should be one pid per line) [terminate(pid) for pid in pidfile] os.unlink(pidfile_path) pidfile.close()
def main(): """ Run as a utility for launching Turk Core usage: turkctl.py start|stop """ parser = OptionParser("usage: %prog [options] <start|stop|restart|clean>") parser.add_option("-f", "--config-file", dest="config", type="string", default='/etc/turk/turk.yml', help="default configuration file") (options, args) = parser.parse_args() if len(args) != 1: parser.error("incorrect number of arguments") conf = yaml.load(open(options.config, 'rU')) os.environ['TURK_CONF'] = options.config global log log = init_logging('turkctl', conf, debug=get_config('turkctl.debug')) {'start':start, 'stop':stop, 'restart':lambda conf: (stop(conf), start(conf)), 'clean':clean}[args[0]](conf)
self.xbee.SendData(dbus.ByteArray(msg), dbus.UInt64(self.device_addr), 2) except Exception, e: # emit an error signal for bridge self.Error(e.message) print e def run(self): loop = gobject.MainLoop() loop.run() @dbus.service.signal(dbus_interface=TURK_DRIVER_ERROR, signature='s') def Error(self, message): """ Called when an error/exception occurs. Emits a signal for any relevant system management daemons and loggers """ pass # Run as a standalone driver if __name__ == '__main__': import os device_id = int(os.getenv('DEVICE_ID')) device_addr = int(os.getenv('DEVICE_ADDRESS'), 16) bus = os.getenv('BUS', turk.get_config('global.bus')) print "RGB Lamp driver started... driver id: %u, target xbee: 0x%X" % (device_id, device_addr) dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) driver = RGBLamp(device_id, device_addr, getattr(dbus, bus)()) driver.run()
def run(self): loop = gobject.MainLoop() loop.run() @dbus.service.signal(dbus_interface=turk.TURK_DRIVER_ERROR, signature='s') def error(self, message): """ Called when an error/exception occurs. Emits a signal for any relevant system management daemons and loggers """ print message # Run as a standalone driver if __name__ == '__main__': import os try: driver_id = int(os.getenv('DRIVER_ID')) except Exception: #TODO: find better way of returning error (dbus?) print 'TwitterFeed: error parsing environment variables' exit(1) bus = os.getenv('BUS', turk.get_config('global.bus')) print "TwitterFeed driver started... app id: %u" % (driver_id) dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) driver = TwitterFeed(driver_id, getattr(dbus, bus)()) driver.run()