diff --git a/base/local_scripts/generate_client.sh b/base/local_scripts/generate_client.sh index 0571a6f..7aa29c8 100755 --- a/base/local_scripts/generate_client.sh +++ b/base/local_scripts/generate_client.sh @@ -24,4 +24,40 @@ export PULP_URL="${API_PROTOCOL}://${API_HOST}:${API_PORT}" CONTAINER_LABEL=$("${COMPOSE_BINARY}" container inspect "${CONTAINER_NAME}" | jq -r ".[0].ProcessLabel") export PULP_MCS_LABEL="${CONTAINER_LABEL#'system_u:system_r:container_t:'}" -./generate.sh "${PROJECT}" "${LANGUAGE}" "${API_VERSION}" +if [ ! -x ./gen-client.sh ] +then + echo "The legacy generate.sh interface is deprecated; update pulp-openapi-generator to a checkout providing gen-client.sh." >&2 + exit 1 +fi + +if [ "${PROJECT}" = "pulpcore" ] +then + COMPONENT="core" +else + COMPONENT="${PROJECT#pulp_}" +fi + +API_SPEC="$(mktemp "${PWD}/api-spec.XXXXXX.json")" +cleanup() { + rm -f "${API_SPEC}" +} +trap cleanup EXIT + +API_ROOT="${PULP_API_ROOT:-/pulp/}" +API_ROOT="/${API_ROOT#/}" +API_ROOT="${API_ROOT%/}/" +API_URL="${API_PROTOCOL}://${API_HOST}:${API_PORT}${API_ROOT}api/${API_VERSION}/" +echo "retrieving api.json for version ${API_VERSION}" +RETRY_COUNT=0 +until curl --fail-with-body -k -o "${API_SPEC}" \ + "${API_URL}docs/api.json?bindings&component=${COMPONENT}" +do + if [ "${RETRY_COUNT}" -eq 10 ] + then + exit 1 + fi + sleep 2 + RETRY_COUNT=$((RETRY_COUNT + 1)) +done + +./gen-client.sh "${API_SPEC}" "${COMPONENT}" "${LANGUAGE}" "${PROJECT}" diff --git a/client/oci_env/agent.py b/client/oci_env/agent.py index 1c0fd04..6e79a7a 100644 --- a/client/oci_env/agent.py +++ b/client/oci_env/agent.py @@ -5,6 +5,7 @@ import shutil import socket import subprocess +import tempfile from oci_env.logger import logger from oci_env.utils import ( @@ -281,7 +282,7 @@ def agent_generator_dir(src_dir): def agent_has_generator_copy(src_dir): """True if create copied pulp-openapi-generator into the agent SRC_DIR.""" - return os.path.isfile(os.path.join(agent_generator_dir(src_dir), "generate.sh")) + return os.path.isfile(os.path.join(agent_generator_dir(src_dir), "gen-client.sh")) def copy_generator_checkout(host_src_dir, compiled_dir): @@ -385,41 +386,45 @@ def agent_create(args): project_name = agent_project_name(args.agent_id) compose_profile = env_overrides.get("COMPOSE_PROFILE") or "lean" - os.makedirs(compiled_dir, exist_ok=True) - overlay = write_plugin_volume_overlay( - agent_volume_overlay_path(oci_env_path, args.agent_id), - plugin_paths, - ) - generator = copy_generator_checkout(host_src_dir, compiled_dir) - if not generator: - logger.warning( - f"No {GENERATOR_REPO} checkout at " - f"{os.path.join(host_src_dir, GENERATOR_REPO)}. " - "oci-env agent generate-client will fail until you clone it and " - "destroy/recreate this agent." + compiled_root = os.path.dirname(compiled_dir) + temp_dir = tempfile.mkdtemp(prefix=f".{project_name}.", dir=compiled_root) + os.chmod(temp_dir, 0o755) + try: + overlay = write_plugin_volume_overlay( + os.path.join(temp_dir, AGENT_OVERLAY_FILENAME), plugin_paths ) + generator = copy_generator_checkout(host_src_dir, temp_dir) + if not generator: + logger.warning( + f"No {GENERATOR_REPO} checkout at " + f"{os.path.join(host_src_dir, GENERATOR_REPO)}. " + "oci-env agent generate-client will fail until you clone it and " + "destroy/recreate this agent." + ) - values = { - "OCI_AGENT_ID": args.agent_id, - "COMPOSE_PROFILE": compose_profile, - "DEV_SOURCE_PATH": ":".join(plugins), - "COMPOSE_PROJECT_NAME": project_name, - "API_PORT": str(api_port), - "SRC_DIR": compiled_dir, - "COMPOSE_BINARY": AGENT_COMPOSE_BINARY, - "API_HOST": "localhost", - "API_PROTOCOL": "http", - # Pulp refuses to start without this; --env can override. - "PULP_SECRET_KEY": "dummy", - "OCI_AGENT_HOST_SRC_DIR": host_src_dir, - "OCI_AGENT_PLUGIN_PATHS": encode_plugin_paths(plugin_paths), - "OCI_AGENT_VOLUME_OVERLAY": overlay, - } - - # Apply user --env overrides (locked keys already rejected). - values.update(env_overrides) - - write_env_file(env_path, values) + values = { + "OCI_AGENT_ID": args.agent_id, + "COMPOSE_PROFILE": compose_profile, + "DEV_SOURCE_PATH": ":".join(plugins), + "COMPOSE_PROJECT_NAME": project_name, + "API_PORT": str(api_port), + "SRC_DIR": compiled_dir, + "COMPOSE_BINARY": AGENT_COMPOSE_BINARY, + "API_HOST": "localhost", + "API_PROTOCOL": "http", + "PULP_SECRET_KEY": "dummy", + "OCI_AGENT_HOST_SRC_DIR": host_src_dir, + "OCI_AGENT_PLUGIN_PATHS": encode_plugin_paths(plugin_paths), + "OCI_AGENT_VOLUME_OVERLAY": agent_volume_overlay_path( + oci_env_path, args.agent_id + ), + } + values.update(env_overrides) + write_env_file(os.path.join(temp_dir, AGENT_ENV_FILENAME), values) + os.replace(temp_dir, compiled_dir) + except BaseException: + shutil.rmtree(temp_dir, ignore_errors=True) + raise print(f"Created agent {args.agent_id}") print(f" env: {env_path}") print(f" project: {project_name}") @@ -590,6 +595,7 @@ def agent_generate_client(args): ) client = compose_for_agent(args.is_verbose, env_path) + client.poll(args.attempts, args.wait) from oci_env.commands import generate_client @@ -602,6 +608,17 @@ class _Args: gargs.install_client = args.install_client gargs.is_verbose = args.is_verbose gargs.api_version = getattr(args, "api_version", "v3") + + requested_plugins = ( + [args.plugin] + if args.plugin + else [p for p in env_data.get("DEV_SOURCE_PATH", "").split(":") if p] + ) + if args.language == "python" and "pulpcore" not in requested_plugins: + gargs.plugin = "pulpcore" + generate_client(gargs, client) + + gargs.plugin = args.plugin generate_client(gargs, client) diff --git a/client/oci_env/main.py b/client/oci_env/main.py index 75c9a9b..8b14d61 100644 --- a/client/oci_env/main.py +++ b/client/oci_env/main.py @@ -281,6 +281,8 @@ def parse_agent_command(subparsers): dest="api_version", help="API version to generate the clients for (default: v3).", ) + gen.add_argument("--attempts", type=int, default=30, help="API readiness poll attempts.") + gen.add_argument("--wait", type=int, default=10, help="Seconds between readiness polls.") gen.set_defaults(func=agent_dispatch, agent_action="generate-client")