コード例 #1
0
ファイル: restart.py プロジェクト: transacid/yadtshell
def restart(protocol=None, uris=None, parallel=None, **kwargs):
    logger.debug("uris: %s" % uris)
    logger.debug("parallel: %s" % parallel)
    logger.debug("kwargs: %s" % kwargs)

    components = restore_current_state()
    service_uris = expand_hosts(uris)
    service_uris = glob_hosts(components, service_uris)

    logging.debug("service uris: %s" % service_uris)

    stop_plan = metalogic(STOP, uris, plan_post_handler=identity)

    service_states = {}
    for action in stop_plan.actions:
        service_states[action.uri] = components[action.uri].state
    logging.debug("current states: %s" % service_states)

    start_uris = [
        uri for uri, state in service_states.iteritems() if state == UP
    ] + uris
    start_uris = set(start_uris)
    logging.info("restarting %s" % ", ".join(start_uris))
    start_plan = metalogic(START, start_uris, plan_post_handler=identity)

    plan = ActionPlan('restart', [stop_plan, start_plan], nr_workers=1)

    for line in plan.dump(include_preconditions=True).splitlines():
        logging.info(line)

    plan = apply_instructions(plan, parallel)
    dump_action_plan('restart', plan)
    return 'restart'
コード例 #2
0
ファイル: restart.py プロジェクト: Woi/yadtshell
def restart(protocol=None, uris=None, parallel=None, **kwargs):
    logger.debug("uris: %s" % uris)
    logger.debug("parallel: %s" % parallel)
    logger.debug("kwargs: %s" % kwargs)

    components = restore_current_state()
    service_uris = expand_hosts(uris)
    service_uris = glob_hosts(components, service_uris)

    logging.debug("service uris: %s" % service_uris)

    stop_plan = metalogic(STOP, uris, plan_post_handler=identity)

    service_states = {}
    for action in stop_plan.actions:
        service_states[action.uri] = components[action.uri].state
    logging.debug("current states: %s" % service_states)

    start_uris = [uri for uri, state in service_states.iteritems()
                  if state == UP] + uris
    start_uris = set(start_uris)
    logging.info("restarting %s" % ", ".join(start_uris))
    start_plan = metalogic(START, start_uris, plan_post_handler=identity)

    plan = ActionPlan('restart', [stop_plan, start_plan], nr_workers=1)

    for line in plan.dump(include_preconditions=True).splitlines():
        logging.info(line)

    plan = apply_instructions(plan, parallel)
    dump_action_plan('restart', plan)
    return 'restart'
コード例 #3
0
    def test_apply_instructions_should_default_to_zero_tolerated_errors(self):
        actions = [Action('sudo service bar stop', 'service://foo/bar'),
                   Action('sudo service baz stop', 'service://foo/baz')]
        original_plan = ActionPlan('update', actions)

        self.assertEqual(original_plan.nr_workers, None)

        actual_plan = apply_instructions(original_plan, None)

        self.assertEqual(actual_plan.nr_errors_tolerated, 0)
コード例 #4
0
    def test_apply_instructions_should_augment_plan_with_no_instructions_to_use_one_worker(self):
        actions = [Action('sudo service bar stop', 'service://foo/bar'),
                   Action('sudo service baz stop', 'service://foo/baz')]
        original_plan = ActionPlan('update', actions)

        self.assertEqual(original_plan.nr_workers, None)

        actual_plan = apply_instructions(original_plan, None)

        self.assertEqual(actual_plan.nr_workers, 1)
コード例 #5
0
    def test_apply_instructions_should_default_to_zero_tolerated_errors(self):
        actions = [Action('sudo service bar stop', 'service://foo/bar'),
                   Action('sudo service baz stop', 'service://foo/baz')]
        original_plan = ActionPlan('update', actions)

        self.assertEqual(original_plan.nr_workers, None)

        actual_plan = apply_instructions(original_plan, None)

        self.assertEqual(actual_plan.nr_errors_tolerated, 0)
コード例 #6
0
    def test_apply_instructions_should_augment_plan_with_no_instructions_to_use_one_worker(self):
        actions = [Action('sudo service bar stop', 'service://foo/bar'),
                   Action('sudo service baz stop', 'service://foo/baz')]
        original_plan = ActionPlan('update', actions)

        self.assertEqual(original_plan.nr_workers, None)

        actual_plan = apply_instructions(original_plan, None)

        self.assertEqual(actual_plan.nr_workers, 1)
コード例 #7
0
    def test_apply_instructions_should_augment_plan_with_workers_and_tolerated_errors(self):
        actions = [Action('sudo service bar stop', 'service://foo/bar'),
                   Action('sudo service baz stop', 'service://foo/baz')]
        original_plan = ActionPlan('update', actions)

        self.assertEqual(original_plan.nr_workers, None)

        actual_plan = apply_instructions(original_plan, 'update=1_1_3')

        self.assertEqual(actual_plan.actions[0].nr_workers, 1)
        self.assertEqual(actual_plan.actions[0].nr_errors_tolerated, '3')
コード例 #8
0
    def test_apply_instructions_should_augment_plan_with_workers_and_tolerated_errors(self):
        actions = [Action('sudo service bar stop', 'service://foo/bar'),
                   Action('sudo service baz stop', 'service://foo/baz')]
        original_plan = ActionPlan('update', actions)

        self.assertEqual(original_plan.nr_workers, None)

        actual_plan = apply_instructions(original_plan, 'update=1_1_3')

        self.assertEqual(actual_plan.actions[0].nr_workers, 1)
        self.assertEqual(actual_plan.actions[0].nr_errors_tolerated, '3')
コード例 #9
0
def reboot(protocol=None, uris=None, parallel=None, **kwargs):
    for uri in uris:
        if not uri.startswith("host://"):
            message = "Cannot reboot %s" % uri
            logger.error(message)
            raise ValueError(message)

    components = restore_current_state()
    host_uris = expand_hosts(uris)
    host_uris = glob_hosts(components, host_uris)

    hosts_to_reboot = uris
    stop_plan = create_plan_to_stop_all_services_on(hosts_to_reboot)

    all_stopped_services = set()
    for action in stop_plan.actions:
        all_stopped_services.add(action.uri)

    start_plan = create_plan_to_start_services_after_rebooting(all_stopped_services,
                                                               hosts_to_reboot,
                                                               components)

    reboot_actions = set([create_reboot_action_for(components[host_uri]) for host_uri in hosts_to_reboot])

    all_actions = set(start_plan.actions) | set(
        stop_plan.actions) | reboot_actions
    all_plan = ActionPlan('all', all_actions)
    all_plan = chop_minimal_related_chunks(all_plan)

    reboot_chunks = set()
    for chunk in all_plan.actions:
        all_chunk_cmds = set(a.cmd for a in chunk.actions)
        if yadtshell.settings.UPDATE in all_chunk_cmds:
            reboot_chunks.add(chunk)
            continue

    prestart_chunks = set()
    for possible_prestart_chunk in all_plan.actions:
        if possible_prestart_chunk in reboot_chunks:
            continue
        if possible_prestart_chunk.is_not_empty:
            prestart_chunks.add(possible_prestart_chunk)

    plan = ActionPlan(
        'update', [ActionPlan('prestart', prestart_chunks),
                   ActionPlan('stoprebootstart', reboot_chunks)
                   ], nr_workers=1)
    plan = apply_instructions(plan, parallel)
    dump_action_plan('reboot', plan)

    return 'reboot'
コード例 #10
0
    def test_apply_instructions_should_split_plan_in_subplans(self):
        actions = [
            Action('sudo service bar stop', 'service://foo/bar'),
            Action('sudo service baz stop', 'service://foo/baz'),
            Action('sudo service baf start', 'service://foo/baf'),
            Action('sudo service bam start', 'service://foo/bam')
        ]
        original_plan = ActionPlan('test', actions)

        actual_plan = apply_instructions(original_plan, 'test=1_1_0:*_*_1')

        first_subplan = actual_plan.actions[0]
        second_subplan = actual_plan.actions[1]

        self.assertEqual(first_subplan.nr_workers, 1)
        self.assertEqual(first_subplan.nr_errors_tolerated, '0')
        self.assertEqual(len(first_subplan.actions), 1)
        self.assertEqual(first_subplan.actions, [actions[0]])

        self.assertEqual(second_subplan.nr_workers, 3)
        self.assertEqual(second_subplan.nr_errors_tolerated, '1')
        self.assertEqual(len(second_subplan.actions), 3)
        self.assertEqual(second_subplan.actions, actions[1:])
コード例 #11
0
    def test_apply_instructions_should_split_plan_in_subplans(self):
        actions = [
            Action('sudo service bar stop', 'service://foo/bar'),
            Action('sudo service baz stop', 'service://foo/baz'),
            Action('sudo service baf start', 'service://foo/baf'),
            Action('sudo service bam start', 'service://foo/bam')
        ]
        original_plan = ActionPlan('test', actions)

        actual_plan = apply_instructions(original_plan, 'test=1_1_0:*_*_1')

        first_subplan = actual_plan.actions[0]
        second_subplan = actual_plan.actions[1]

        self.assertEqual(first_subplan.nr_workers, 1)
        self.assertEqual(first_subplan.nr_errors_tolerated, '0')
        self.assertEqual(len(first_subplan.actions), 1)
        self.assertEqual(first_subplan.actions, [actions[0]])

        self.assertEqual(second_subplan.nr_workers, 3)
        self.assertEqual(second_subplan.nr_errors_tolerated, '1')
        self.assertEqual(len(second_subplan.actions), 3)
        self.assertEqual(second_subplan.actions, actions[1:])