Esempio n. 1
0
def run_integration(func_container, ctx, *args, **kwargs):
    try:
        int_name = func_container.QONTRACT_INTEGRATION.replace('_', '-')
        running_state = RunningState()
        running_state.integration = int_name
    except AttributeError:
        sys.stderr.write("Integration missing QONTRACT_INTEGRATION.\n")
        sys.exit(ExitCodes.ERROR)

    try:
        gql.init_from_config(sha_url=ctx['gql_sha_url'],
                             integration=int_name,
                             validate_schemas=ctx['validate_schemas'],
                             print_url=ctx['gql_url_print'])
    except GqlApiIntegrationNotFound as e:
        sys.stderr.write(str(e) + "\n")
        sys.exit(ExitCodes.INTEGRATION_NOT_FOUND)

    unleash_feature_state = get_feature_toggle_state(int_name)
    if not unleash_feature_state:
        logging.info('Integration toggle is disabled, skipping integration.')
        sys.exit(ExitCodes.SUCCESS)

    dry_run = ctx.get('dry_run', False)

    try:
        func_container.run(dry_run, *args, **kwargs)
    except RunnerException as e:
        sys.stderr.write(str(e) + "\n")
        sys.exit(ExitCodes.ERROR)
    except GqlApiErrorForbiddenSchema as e:
        sys.stderr.write(str(e) + "\n")
        sys.exit(ExitCodes.FORBIDDEN_SCHEMA)
    except GqlApiError as e:
        if '409' in str(e):
            logging.info(f'Data changed during execution. This is fine.')
            # exit code to relect conflict
            # TODO: document this better
            sys.exit(ExitCodes.DATA_CHANGED)
        else:
            raise e
    finally:
        if ctx.get('dump_schemas_file'):
            gqlapi = gql.get_api()
            with open(ctx.get('dump_schemas_file'), 'w') as f:
                f.write(json.dumps(gqlapi.get_queried_schemas()))
Esempio n. 2
0
    def __new__(cls,
                cluster_name,
                server,
                token,
                jh=None,
                settings=None,
                init_projects=False,
                init_api_resources=False,
                local=False):
        use_native = os.environ.get('USE_NATIVE_CLIENT', '')
        if len(use_native) > 0:
            use_native = use_native.lower() in ['true', 'yes']
        else:
            enable_toggle = 'openshift-resources-native-client'
            strategies = get_feature_toggle_strategies(enable_toggle,
                                                       ['perCluster'])

            # only use the native client if the toggle is enabled and this
            # server is listed in the perCluster strategy
            cluster_in_strategy = False
            if strategies:
                for s in strategies:
                    if cluster_name in s.parameters['cluster_name'].split(','):
                        cluster_in_strategy = True
                        break
            use_native = get_feature_toggle_state(enable_toggle) and \
                not cluster_in_strategy

        if use_native:
            OC.client_status.labels(cluster_name=cluster_name,
                                    native_client=True).inc()
            return OCNative(cluster_name, server, token, jh, settings,
                            init_projects, init_api_resources, local)
        else:
            OC.client_status.labels(cluster_name=cluster_name,
                                    native_client=False).inc()
            return OCDeprecated(cluster_name, server, token, jh, settings,
                                init_projects, init_api_resources, local)