예제 #1
0
def run(dry_run=False):
    quay_api_store = get_quay_api_store()

    current_state = fetch_current_state(quay_api_store)
    desired_state = fetch_desired_state()

    # calculate diff
    diff = current_state.diff(desired_state)

    # Ensure all quay org/teams are declared as dependencies:
    # any item that appears in `diff['insert']` means that it's not listed
    # in a `/dependencies/quay-org-1.yml` datafile.
    if len(diff['insert']) > 0:
        unknown_teams = [
            "- {}/{}".format(
                item["params"]["org"],
                item["params"]["team"],
            ) for item in diff['insert']
        ]

        raise RunnerException(("Unknown quay/org/team found:\n"
                               "{}").format("\n".join(unknown_teams)))

    # Run actions
    runner_action = RunnerAction(dry_run, quay_api_store)
    runner = AggregatedDiffRunner(diff)

    runner.register("update-insert", runner_action.add_to_team())
    runner.register("update-delete", runner_action.del_from_team())
    runner.register("delete", runner_action.del_from_team())

    runner.run()
    def test_unknown_diff_on(self):
        left = AggregatedList()
        right = AggregatedList()

        runner = AggregatedDiffRunner(left.diff(right))

        with pytest.raises(Exception):
            runner.register("qwerty", lambda p, i: True, lambda p: True)
    def test_run(self):
        left = AggregatedList()
        right = AggregatedList()

        # test insert
        right.add({'on': 'insert'}, ["i"])

        # test delete
        left.add({'on': 'delete'}, ["d"])

        # test update-insert
        left.add({'on': 'update-insert'}, ["ui1"])
        right.add({'on': 'update-insert'}, ["ui1", "ui2"])

        # test update-delete
        left.add({'on': 'update-delete'}, ["ud1", "ud2"])
        right.add({'on': 'update-delete'}, ["ud1"])

        on_insert = []
        on_delete = []
        on_update_insert = []
        on_update_delete = []

        def recorder(l):
            return lambda p, i: l.append([p, i])

        runner = AggregatedDiffRunner(left.diff(right))

        runner.register("insert", recorder(on_insert))
        runner.register("delete", recorder(on_delete))
        runner.register("update-insert", recorder(on_update_insert))
        runner.register("update-delete", recorder(on_update_delete))

        runner.run()

        assert on_insert == [[{'on': 'insert'}, ['i']]]
        assert on_delete == [[{'on': 'delete'}, ['d']]]
        assert on_update_insert == [[{'on': 'update-insert'}, ['ui2']]]
        assert on_update_delete == [[{'on': 'update-delete'}, ['ud2']]]
    def test_run_cond_false(self):
        left = AggregatedList()
        right = AggregatedList()

        right.add({'on': 'insert'}, ["qwerty"])

        runner = AggregatedDiffRunner(left.diff(right))

        recorder = []
        runner.register("insert", lambda p, i: recorder.append('True'),
                        lambda p: False)

        runner.run()

        assert recorder == []
def run(dry_run=False):
    gqlapi = gql.get_api()

    namespaces = gqlapi.query(NAMESPACES_QUERY)['namespaces']
    roles = gqlapi.query(ROLES_QUERY)['roles']

    cluster_store = ClusterStore(namespaces)

    current_state = fetch_current_state(cluster_store)
    desired_state = fetch_desired_state(roles)

    # calculate diff
    diff = current_state.diff(desired_state)

    # Ensure all namespace/roles are well known.
    # Any item that appears in `diff['insert']` means that it's not listed
    # as a managedCluster in the cluster datafile.
    if len(diff['insert']) > 0:
        unknown_combinations = [
            "- {}/{}/{}".format(
                item["params"]["cluster"],
                item["params"]["namespace"],
                item["params"]["role"],
            ) for item in diff['insert']
        ]

        raise RunnerException(
            ("Unknown cluster/namespace/combinations found:\n"
             "{}").format("\n".join(unknown_combinations)))

    # Run actions
    runner_action = RunnerAction(dry_run, cluster_store)
    runner = AggregatedDiffRunner(diff)

    runner.register("update-insert", runner_action.add_role())
    runner.register("update-delete", runner_action.del_role())
    runner.register("delete", runner_action.del_role())

    status = runner.run()

    if status is False:
        sys.exit(1)
예제 #6
0
def run(dry_run):
    quay_api_store = get_quay_api_store()

    current_state = fetch_current_state(quay_api_store)
    desired_state = fetch_desired_state()

    # calculate diff
    diff = current_state.diff(desired_state)

    # Verify that there are no repeated repo declarations
    for items in diff.values():
        for repo in items:
            assert len(repo['items']) == 1

    # Run actions
    runner_action = RunnerAction(dry_run, quay_api_store)
    runner = AggregatedDiffRunner(diff)

    runner.register("update-insert",
                    runner_action.update_fields(current_state))
    runner.register("insert", runner_action.create_repo())
    runner.register("delete", runner_action.delete_repo())

    runner.run()
예제 #7
0
def run(dry_run=False):
    config = get_config()
    gh_api_store = GHApiStore(config)

    current_state = fetch_current_state(gh_api_store)
    desired_state = fetch_desired_state()

    # Ensure current_state and desired_state match orgs
    current_orgs = set([
        item["params"]["org"]
        for item in current_state.dump()
    ])

    desired_orgs = set([
        item["params"]["org"]
        for item in desired_state.dump()
    ])

    assert current_orgs == desired_orgs, \
        "Current orgs ({}) don't match desired orgs ({})".format(
            current_orgs,
            desired_orgs
        )

    # Calculate diff
    diff = current_state.diff(desired_state)

    # Run actions
    runner_action = RunnerAction(dry_run, gh_api_store)
    runner = AggregatedDiffRunner(diff)

    # insert github-org
    runner.register(
        "insert",
        runner_action.raise_exception("Cannot create a Github Org"),
        service_is("github-org"),
    )

    # insert github-org-team
    runner.register(
        "insert",
        runner_action.create_team(),
        service_is("github-org-team"),
    )
    runner.register(
        "insert",
        runner_action.add_to_team(),
        service_is("github-org-team"),
    )

    # delete github-org
    runner.register(
        "delete",
        runner_action.raise_exception("Cannot delete a Github Org"),
        service_is("github-org"),
    )

    # delete github-org-team
    runner.register(
        "delete",
        runner_action.del_from_team(),
        service_is("github-org-team"),
    )

    # update-insert github-org
    runner.register(
        "update-insert",
        runner_action.add_to_org(),
        service_is("github-org"),
    )

    # update-insert github-org-team
    runner.register(
        "update-insert",
        runner_action.add_to_team(),
        service_is("github-org-team"),
    )

    # update-delete github-org
    runner.register(
        "update-delete",
        runner_action.del_from_org(),
        service_is("github-org"),
    )

    # update-delete github-org-team
    runner.register(
        "update-delete",
        runner_action.del_from_team(),
        service_is("github-org-team"),
    )

    runner.run()