return result if _user_should_acknowledge_plan(dryrun, flavor, forcedyes): dangerous_actions = remove_harmless_actions( action_plan.list_actions) if dangerous_actions: print( "\n\nThe following actions might be dangerous (i.e. host reboot), please confirm :\n" ) for da in dangerous_actions: print( da.dump(depth=1, include_preconditions=False, include_target_value=False)) print() if not confirm_transaction_by_user(): yadtshell.twisted.stop_and_return( EXIT_CODE_CANCELED_BY_USER) return defer.succeed(None) self.pi = yadtshell.twisted.ProgressIndicator() deferred = None if not dryrun and "lock" not in flavor: deferred = yadtshell.util.start_ssh_multiplexed() try: if deferred: deferred.addCallback(self.handle_cb, action_plan) else: deferred = self.handle(action_plan) except ValueError, ve: reactor.callLater(0, deferred.errback, ve)
pi.finish() return result def filter_dangerous_actions(actions): def is_a_dangerous_action(action): return action.cmd in ['update'] and action.kwargs.get(yadtshell.constants.REBOOT_REQUIRED, False) return filter(is_a_dangerous_action, actions) if _user_should_acknowledge_plan(dryrun, flavor, forcedyes): dangerous_actions = filter_dangerous_actions(action_plan.list_actions) if dangerous_actions: print("\n\nThe following actions might be dangerous (i.e. host reboot), please confirm :\n") for da in dangerous_actions: print(da.dump(depth=1, include_preconditions=False, include_target_value=False)) print() if not confirm_transaction_by_user(): yadtshell.twisted.stop_and_return(EXIT_CODE_CANCELED_BY_USER) return defer.succeed(None) self.pi = yadtshell.twisted.ProgressIndicator() deferred = None if not dryrun: deferred = yadtshell.util.start_ssh_multiplexed() try: if deferred: deferred.addCallback(self.handle_cb, action_plan) else: deferred = self.handle(action_plan) except ValueError, ve: reactor.callLater(0, deferred.errback, ve) return deferred
def test_should_request_input_with_selector(self): self.user_input.return_value = '' confirm_transaction_by_user('y/n', None) self.user_input.assert_called_with('Do you want to continue [y/n]? ')
def test_should_return_default_when_user_provides_no_value(self): self.user_input.return_value = '' self.assertTrue(confirm_transaction_by_user('Y/n', True))
def test_should_return_false_when_user_declines_immediately_with_no(self): self.user_input.return_value = 'nO' self.assertFalse(confirm_transaction_by_user('y/n', None))
def test_should_return_true_when_user_confirms_with_yes(self): self.user_input.return_value = 'Yes' self.assertTrue(confirm_transaction_by_user('y/n', None))
def test_should_return_true_when_user_confirms_immediately(self): self.user_input.return_value = 'y' self.assertTrue(confirm_transaction_by_user('y/n', None))