def update(self, reboot_required=False, upgrade_packages=True): next_artefacts = [uri.replace('/', '-', 1) for uri in self.next_artefacts] if not reboot_required: return self.remote_call('yadt-host-update %s' % ' '.join(next_artefacts), '%s_%s' % (self.hostname, yadtshell.settings.UPDATE)) update_and_reboot_command = self.remote_call( 'yadt-host-update -r %s' % ' '.join(next_artefacts if upgrade_packages else []), '%s_%s' % (self.hostname, yadtshell.settings.UPDATE)) p = YadtProcessProtocol(self, update_and_reboot_command, out_log_level=logging.INFO) p.target_state = yadtshell.settings.UPTODATE p.state = yadtshell.settings.UNKNOWN def handle_rebooting_machine(failure, ssh_poll_max_seconds): if failure.value.exitCode == 152: raise yadtshell.actions.ActionException( 'Timed out while waiting for %s to reboot' % self.uri, 152) elif failure.value.exitCode == 255: logger.info("%s: rebooting now" % self.uri) return poll_rebooting_machine() return failure def poll_rebooting_machine(count=1): max_tries = calculate_max_tries_for_interval_and_delay(interval=self.ssh_poll_max_seconds, delay=yadtshell.constants.SSH_POLL_DELAY) logger.info("%s: polling for ssh connect, try %i of %i" % (self.uri, count, max_tries)) poll_command = self.remote_call('uptime', '%s_poll' % self.hostname) poll_protocol = YadtProcessProtocol(self, poll_command, out_log_level=logging.INFO) poll_protocol.ssh_poll_count = count if (count * yadtshell.constants.SSH_POLL_DELAY) < self.ssh_poll_max_seconds: poll_protocol.deferred.addErrback( lambda x: task.deferLater(reactor, yadtshell.constants.SSH_POLL_DELAY, poll_rebooting_machine, count + 1) ) cmdline = shlex.split(poll_protocol.cmd) reactor.spawnProcess(poll_protocol, cmdline[0], cmdline, None) return poll_protocol.deferred p.deferred.addErrback(handle_rebooting_machine, self.ssh_poll_max_seconds) def display_reboot_info(protocol): if hasattr(protocol, 'ssh_poll_count'): logger.info('%s: reboot took %d seconds' % (self.uri, protocol.ssh_poll_count * yadtshell.constants.SSH_POLL_DELAY)) return protocol p.deferred.addCallback(display_reboot_info) cmdline = shlex.split(p.cmd.encode('ascii')) reactor.spawnProcess(p, cmdline[0], cmdline, None) return p.deferred
def test_out_received_should_append_data(self): mock_process_protocol = Mock(YadtProcessProtocol) mock_process_protocol.data = 'some-data-' mock_process_protocol.component = 'component' mock_process_protocol.out_log_level = 'info' mock_process_protocol.pi = None mock_logger = Mock() mock_process_protocol.logger = mock_logger YadtProcessProtocol.outReceived(mock_process_protocol, '-more-data') self.assertEqual(mock_process_protocol.data, 'some-data--more-data')
def test_stderr_should_update_progress_indicator_with_command_and_component(self): mock_progress_indicator = Mock() mock_process_protocol = Mock(YadtProcessProtocol) mock_process_protocol.data = '' mock_process_protocol.cmd = 'command' mock_process_protocol.component = 'component' mock_process_protocol.err_log_level = 'info' mock_process_protocol.pi = mock_progress_indicator mock_logger = Mock() mock_process_protocol.logger = mock_logger YadtProcessProtocol.errReceived(mock_process_protocol, 'data') mock_progress_indicator.update.assert_called_with(('command', 'component'))
def test_stderr_should_update_progress_indicator_with_command_and_component( self): mock_progress_indicator = Mock() mock_process_protocol = Mock(YadtProcessProtocol) mock_process_protocol.data = '' mock_process_protocol.cmd = 'command' mock_process_protocol.component = 'component' mock_process_protocol.err_log_level = 'info' mock_process_protocol.pi = mock_progress_indicator mock_logger = Mock() mock_process_protocol.logger = mock_logger YadtProcessProtocol.errReceived(mock_process_protocol, 'data') mock_progress_indicator.update.assert_called_with( ('command', 'component'))
def poll_rebooting_machine(count=1): max_tries = calculate_max_tries_for_interval_and_delay(interval=self.ssh_poll_max_seconds, delay=yadtshell.constants.SSH_POLL_DELAY) logger.info("%s: polling for ssh connect, try %i of %i" % (self.uri, count, max_tries)) poll_command = self.remote_call('uptime', '%s_poll' % self.hostname) poll_protocol = YadtProcessProtocol(self, poll_command, out_log_level=logging.INFO) poll_protocol.ssh_poll_count = count if (count * yadtshell.constants.SSH_POLL_DELAY) < self.ssh_poll_max_seconds: poll_protocol.deferred.addErrback( lambda x: task.deferLater(reactor, yadtshell.constants.SSH_POLL_DELAY, poll_rebooting_machine, count + 1) ) cmdline = shlex.split(poll_protocol.cmd) reactor.spawnProcess(poll_protocol, cmdline[0], cmdline, None) return poll_protocol.deferred
def status(self): status_command = self.remote_call( 'yadt-service-%s %s' % (yadtshell.settings.STATUS, self.name), tag='%s_%s' % (self.name, yadtshell.settings.STATUS)) status_protocol = YadtProcessProtocol(self, status_command, out_log_level=logging.DEBUG) cmdline = shlex.split(status_protocol.cmd) reactor.spawnProcess(status_protocol, cmdline[0], cmdline, None) return status_protocol.deferred
def poll_rebooting_machine(count=1): max_tries = calculate_max_tries_for_interval_and_delay( interval=self.ssh_poll_max_seconds, delay=yadtshell.constants.SSH_POLL_DELAY) logger.info("%s: polling for ssh connect, try %i of %i" % (self.uri, count, max_tries)) poll_command = self.remote_call('uptime', '%s_poll' % self.hostname) poll_protocol = YadtProcessProtocol(self, poll_command, out_log_level=logging.INFO) poll_protocol.ssh_poll_count = count if (count * yadtshell.constants.SSH_POLL_DELAY ) < self.ssh_poll_max_seconds: poll_protocol.deferred.addErrback(lambda x: task.deferLater( reactor, yadtshell.constants.SSH_POLL_DELAY, poll_rebooting_machine, count + 1)) cmdline = shlex.split(poll_protocol.cmd) reactor.spawnProcess(poll_protocol, cmdline[0], cmdline, None) return poll_protocol.deferred
def update(self, reboot_required=False, upgrade_packages=True): next_artefacts = [ uri.replace('/', '-', 1) for uri in self.next_artefacts ] if not reboot_required: return self.remote_call( 'yadt-host-update %s' % ' '.join(next_artefacts), '%s_%s' % (self.hostname, yadtshell.settings.UPDATE)) update_and_reboot_command = self.remote_call( 'yadt-host-update -r %s' % ' '.join(next_artefacts if upgrade_packages else []), '%s_%s' % (self.hostname, yadtshell.settings.UPDATE)) p = YadtProcessProtocol(self, update_and_reboot_command, out_log_level=logging.INFO) p.target_state = yadtshell.settings.UPTODATE p.state = yadtshell.settings.UNKNOWN def handle_rebooting_machine(failure, ssh_poll_max_seconds): if failure.value.exitCode == 152: raise yadtshell.actions.ActionException( 'Timed out while waiting for %s to reboot' % self.uri, 152) elif failure.value.exitCode == 255: logger.info("%s: rebooting now" % self.uri) return poll_rebooting_machine() return failure def poll_rebooting_machine(count=1): max_tries = calculate_max_tries_for_interval_and_delay( interval=self.ssh_poll_max_seconds, delay=yadtshell.constants.SSH_POLL_DELAY) logger.info("%s: polling for ssh connect, try %i of %i" % (self.uri, count, max_tries)) poll_command = self.remote_call('uptime', '%s_poll' % self.hostname) poll_protocol = YadtProcessProtocol(self, poll_command, out_log_level=logging.INFO) poll_protocol.ssh_poll_count = count if (count * yadtshell.constants.SSH_POLL_DELAY ) < self.ssh_poll_max_seconds: poll_protocol.deferred.addErrback(lambda x: task.deferLater( reactor, yadtshell.constants.SSH_POLL_DELAY, poll_rebooting_machine, count + 1)) cmdline = shlex.split(poll_protocol.cmd) reactor.spawnProcess(poll_protocol, cmdline[0], cmdline, None) return poll_protocol.deferred p.deferred.addErrback(handle_rebooting_machine, self.ssh_poll_max_seconds) def display_reboot_info(protocol): if hasattr(protocol, 'ssh_poll_count'): logger.info('%s: reboot took %d seconds' % (self.uri, protocol.ssh_poll_count * yadtshell.constants.SSH_POLL_DELAY)) return protocol p.deferred.addCallback(display_reboot_info) cmdline = shlex.split(p.cmd.encode('ascii')) reactor.spawnProcess(p, cmdline[0], cmdline, None) return p.deferred