Exemplo n.º 1
0
def fetch_service_config(args):
    args.service_config_sets = []
    args.rollout_ids = []

    try:
        # Check service_account_key and non_gcp
        if args.non_gcp and args.service_account_key is None:
            logging.error("[ESP] If --non_gcp is specified, --service_account_key has to be specified");
            sys.exit(3)

        # Get the access token
        if args.service_account_key is None:
            logging.info("Fetching an access token from the metadata service")
            token = fetch.fetch_access_token(args.metadata)
        else:
            token = fetch.make_access_token(args.service_account_key)

        if args.service_config_url is not None:
            # Set the file name to "service.json", if either service
            # config url or version is specified for backward compatibility
            filename = "service.json"
            fetch_and_save_service_config_url(args.config_dir, token, args.service_config_url, filename)
            args.service_config_sets.append({})
            args.service_config_sets[0][args.config_dir + "/" + filename] = 100;
        else:
            # fetch service name, if not specified
            if (args.service is None or not args.service.strip()) and args.check_metadata:
                logging.info(
                    "Fetching the service name from the metadata service")
                args.service = fetch.fetch_service_name(args.metadata)

            # if service name is not specified, display error message and exit
            if args.service is None:
                if args.check_metadata:
                    logging.error("[ESP] Unable to fetch service name from the metadata service");
                else:
                    logging.error("[ESP] Service name is not specified");
                sys.exit(3)

            # fetch service config rollout strategy from metadata, if not specified
            if (args.rollout_strategy is None or not args.rollout_strategy.strip()) and args.check_metadata:
                logging.info(
                    "Fetching the service config rollout strategy from the metadata service")
                args.rollout_strategy = \
                    fetch.fetch_service_config_rollout_strategy(args.metadata);

            if args.rollout_strategy is None or not args.rollout_strategy.strip():
                args.rollout_strategy = DEFAULT_ROLLOUT_STRATEGY

            # fetch service config ID, if not specified
            if (args.version is None or not args.version.strip()) and args.check_metadata:
                logging.info("Fetching the service config ID "\
                             "from the metadata service")
                args.version = fetch.fetch_service_config_id(args.metadata)

            # Fetch api version from latest successful rollouts
            if args.version is None or not args.version.strip():
                services = args.service.split('|')
                args.services = services
                for idx, service in enumerate(services):
                    logging.info(
                        "Fetching the service config ID from the rollouts service")
                    rollout = fetch.fetch_latest_rollout(args.management,
                                                         service, token)
                    args.rollout_ids.append(rollout["rolloutId"])
                    args.service_config_sets.append({})
                    for version, percentage in rollout["trafficPercentStrategy"]["percentages"].iteritems():
                        filename = generate_service_config_filename(version)
                        fetch_and_save_service_config(args.management, service, args.config_dir, token, version, filename)
                        args.service_config_sets[idx][args.config_dir + "/" + filename] = percentage;
            else:
                # Set the file name to "service.json", if either service
                # config url or version is specified for backward compatibility
                filename = "service.json"
                fetch_and_save_service_config(args.management, args.service, args.config_dir, token, args.version, filename)
                args.service_config_sets.append({})
                args.service_config_sets[0][args.config_dir + "/" + filename] = 100;

        if not args.rollout_ids:
            args.rollout_ids.append("")

    except fetch.FetchError as err:
        logging.error(err.message)
        sys.exit(err.code)
Exemplo n.º 2
0
def fetch_service_config(args):
    args.service_configs = {}
    args.rollout_id = ""

    try:
        # Get the access token
        if args.service_account_key is None:
            logging.info("Fetching an access token from the metadata service")
            token = fetch.fetch_access_token(args.metadata)
        else:
            token = fetch.make_access_token(args.service_account_key)

        if args.service_config_url is not None:
            # Set the file name to "service.json", if either service
            # config url or version is specified for backward compatibility
            filename = "service.json"
            fetch_and_save_service_config_url(args, token,
                                              args.service_config_url,
                                              filename)
            args.service_configs[args.config_dir + "/" + filename] = 100
        else:
            # fetch service name, if not specified
            if args.service is None:
                logging.info(
                    "Fetching the service name from the metadata service")
                args.service = fetch.fetch_service_name(args.metadata)

            # if service name is not specified, display error message and exit
            if args.service is None:
                logging.error("Unable to get service name")
                sys.exit(3)

            # fetch service config rollout strategy from metadata, if not specified
            if args.rollout_strategy is None or not args.rollout_strategy.strip(
            ):
                logging.info(
                    "Fetching the service config rollout strategy from the metadata service"
                )
                args.rollout_strategy = \
                    fetch.fetch_service_config_rollout_strategy(args.metadata)

            if args.rollout_strategy is None or not args.rollout_strategy.strip(
            ):
                args.rollout_strategy = DEFAULT_ROLLOUT_STRATEGY

            # fetch service config ID, if not specified
            if args.version is None:
                logging.info("Fetching the service config ID "\
                             "from the metadata service")
                args.version = fetch.fetch_service_config_id(args.metadata)

            # Fetch api version from latest successful rollouts
            if args.version is None or not args.version.strip():
                logging.info(
                    "Fetching the service config ID from the rollouts service")
                rollout = fetch.fetch_latest_rollout(args.management,
                                                     args.service, token)
                args.rollout_id = rollout["rolloutId"]
                for version, percentage in rollout["trafficPercentStrategy"][
                        "percentages"].iteritems():
                    filename = generate_service_config_filename(version)
                    fetch_and_save_service_config(args, token, version,
                                                  filename)
                    args.service_configs[args.config_dir + "/" +
                                         filename] = percentage
            else:
                # Set the file name to "service.json", if either service
                # config url or version is specified for backward compatibility
                filename = "service.json"
                fetch_and_save_service_config(args, token, args.version,
                                              filename)
                args.service_configs[args.config_dir + "/" + filename] = 100

    except fetch.FetchError as err:
        logging.error(err.message)
        sys.exit(err.code)