def add_command(command=None, identifier=None, env=None, user=None, host=None, group=None, order=None, sleep_after=0.25, respawn=True, minimum_lifetime_to_respawn=0.5, log_dir=None, sleep=None, depends=None, try_again_delay=0.25, give_up_after=0, ssh_port=None): """ This is the only function that users use from within the configuration file. It adds a Command instance to the list of commands to run. This function calls the Master.add_command static method, passing to it a L{lunch.commands.Command} object """ # TODO: remove priority and sleep kwargs in a future version log.debug("Adding %s (%s) %s@%s" % (identifier, command, user, host)) # ------------- warnings ------------------ if group is not None: raise RuntimeError("Groups are deprecated. Use dependencies instead.") if identifier is not None: _validate_identifier(identifier) if sleep is not None: raise RuntimeError("The sleep keyword argument has been renamed to sleep_after.") sleep_after = sleep #if priority is not None: # warnings.warn("The priority keyword argument does not exist anymore. Only the order in which add_command calls are done is considered.", DeprecationWarning) if log_dir is None: log_dir = lunch_master.log_dir #TODO: if pid_dir is None: # pid_dir = lunch_master.pid_dir c = commands.Command(command=command, env=env, host=host, user=user, order=order, sleep_after=sleep_after, respawn=respawn, minimum_lifetime_to_respawn=minimum_lifetime_to_respawn, log_dir=log_dir, identifier=identifier, depends=depends, try_again_delay=try_again_delay, give_up_after=give_up_after, ssh_port=ssh_port) lunch_master.add_command(c)
def test_add_commands_with_dependencies(self): COMMAND_A_IDENTIFIER = "test_A" COMMAND_B_IDENTIFIER = "test_B" COMMAND_C_IDENTIFIER = "test_C" COMMAND_LINE = "man man" DELAY = 0.1 self._deferred = defer.Deferred() self._master.add_command( commands.Command(COMMAND_LINE, identifier=COMMAND_A_IDENTIFIER)) self._master.add_command( commands.Command(COMMAND_LINE, identifier=COMMAND_B_IDENTIFIER, depends=[COMMAND_A_IDENTIFIER])) self._master.add_command( commands.Command(COMMAND_LINE, identifier=COMMAND_C_IDENTIFIER, depends=[COMMAND_B_IDENTIFIER])) log.info("added commands") def _cl1(): all = self._master.get_all_commands() self.failUnlessEqual(len(all), 3) _final_cleanup() def _final_cleanup(): log.info("_final_cleanup") d = _tear_down() def _tear_down_cb(result): self._deferred.callback(None) d.addCallback(_tear_down_cb) def _tear_down(): # return a deferred list log.info("_tear_down") # quit all slaves return self._master.cleanup() #_final_cleanup() _cl1() #reactor.callLater(DELAY, _cl1) return self._deferred
def _test(): global has_them global counter if not has_them: print("Adding them again!") m.add_command(commands.Command("xeyes", identifier="xeyes")) m.add_command(commands.Command("xlogo", identifier="xlogo")) m.add_command(commands.Command("xcalc", identifier="xcalc")) m.add_command( commands.Command("xterm -hold -e /bin/bash -c \"echo %d\"" % (counter), identifier="xterm")) counter += 1 has_them = True else: print("Removing them.") m.remove_command("xeyes") m.remove_command("xlogo") m.remove_command("xcalc") m.remove_command("xterm") has_them = False
def test_add_remove_command(self): COMMAND_IDENTIFER = "test" COMMAND_LINE = "man man" _deferred = defer.Deferred() _master = master.Master() self.the_command = None def _later1(): # checks the the command has been added # removes the command self.the_command = _master.get_command(COMMAND_IDENTIFER) log.info("Set self.the_command to %s" % (self.the_command)) _master.remove_command(COMMAND_IDENTIFER) log.info("remove_command") reactor.callLater(0.1, _later2) def _later2(): log.info("_later2") # checks the the command has been removed if len(_master.get_all_commands()) != 0: msg = "The command did not get removed" log.info(msg) _deferred.errback( failure.Failure(failure.DefaultException(msg))) log.info("failed") else: log.info("removing the looping call") if _master._looping_call.running: d = _master._looping_call.deferred _master._looping_call.stop() # FIXME d.addCallback(_cb3) else: _deferred.callback(None) def _cb3(result): # Called when the looping call has been stopped log.info("quit all slaves") for command in _master.get_all_commands(): command.quit_slave() #TODO: use the Deferred if self.the_command.slave_state == STATE_RUNNING: self.the_command.quit_slave() #TODO: use the Deferred reactor.callLater(0.1, _later4) def _later4(): _deferred.callback(None) _master.add_command( commands.Command(COMMAND_LINE, identifier=COMMAND_IDENTIFER)) log.info("added command $ %s" % (COMMAND_LINE)) reactor.callLater(0.1, _later1) return _deferred
def _test(): print("Adding one more!") m.add_command(commands.Command("xeyes"))
This example demonstrate how to use lunch master as a library. """ from twisted.internet import gtk2reactor gtk2reactor.install() # has to be done before importing reactor from twisted.internet import reactor from twisted.internet import task from lunch import commands from lunch import master from lunch import gui if __name__ == "__main__": unique_master_id = "example" log_dir = master.DEFAULT_LOG_DIR pid_file = master.write_master_pid_file(identifier=unique_master_id, directory=log_dir) # XXX add_command here m = master.Master(log_dir=log_dir, pid_file=pid_file) m.add_command(commands.Command("xeyes", identifier="xeyes")) m.add_command(commands.Command("xlogo", identifier="xlogo")) m.add_command(commands.Command("xcalc", identifier="xcalc")) def _test(): print("Adding one more!") m.add_command(commands.Command("xeyes")) looping_call = task.LoopingCall(_test) looping_call.start(1.0, False) gui.start_gui(m) reactor.run()
from lunch import master from lunch import gui has_them = False counter = 0 if __name__ == "__main__": unique_master_id = "example" log_dir = master.DEFAULT_LOG_DIR master.start_logging() pid_file = master.write_master_pid_file(identifier=unique_master_id, directory=log_dir) m = master.Master(log_dir=log_dir, pid_file=pid_file, verbose=True) m.add_command(commands.Command("xeyes", identifier="xeyes")) m.add_command(commands.Command("xlogo", identifier="xlogo")) m.add_command(commands.Command("xcalc", identifier="xcalc")) m.add_command( commands.Command("xterm -hold -e /bin/bash -c \"echo %d\"" % (counter), identifier="xterm")) counter += 1 has_them = True def _test(): global has_them global counter if not has_them: print("Adding them again!") m.add_command(commands.Command("xeyes", identifier="xeyes")) m.add_command(commands.Command("xlogo", identifier="xlogo"))