From d0821e65aa3474ce69b6bd64219d9c2082c391b7 Mon Sep 17 00:00:00 2001 From: Tomasz Prus Date: Tue, 15 Jan 2019 07:52:13 +0100 Subject: [PATCH] feat: introduce openapi-generator (#95) * move files related to swagger-codegen to dedicated directory * add script to build clients by openapi-generator * python-asyncio is build by openapi-generator --- openapi/csharp.sh | 2 +- openapi/go.sh | 2 +- openapi/haskell.sh | 2 +- openapi/java.sh | 2 +- openapi/javascript.sh | 2 +- openapi/openapi-generator/Dockerfile | 66 +++++++++++ openapi/openapi-generator/client-generator.sh | 90 +++++++++++++++ .../generate_client_in_container.sh | 103 ++++++++++++++++++ openapi/python-asyncio-api_client.py.patch | 27 ----- openapi/python-asyncio-rest.py.patch | 39 ------- openapi/python-asyncio.sh | 64 +++++------ openapi/python-asyncio.xml | 8 +- openapi/python.sh | 2 +- openapi/ruby.sh | 2 +- openapi/{ => swagger-codegen}/Dockerfile | 2 +- .../{ => swagger-codegen}/client-generator.sh | 7 +- .../generate_client_in_container.sh | 0 openapi/typescript.sh | 2 +- 18 files changed, 301 insertions(+), 121 deletions(-) create mode 100644 openapi/openapi-generator/Dockerfile create mode 100755 openapi/openapi-generator/client-generator.sh create mode 100755 openapi/openapi-generator/generate_client_in_container.sh delete mode 100644 openapi/python-asyncio-api_client.py.patch delete mode 100644 openapi/python-asyncio-rest.py.patch rename openapi/{ => swagger-codegen}/Dockerfile (97%) rename openapi/{ => swagger-codegen}/client-generator.sh (94%) rename openapi/{ => swagger-codegen}/generate_client_in_container.sh (100%) diff --git a/openapi/csharp.sh b/openapi/csharp.sh index 900ffb2..3237629 100755 --- a/openapi/csharp.sh +++ b/openapi/csharp.sh @@ -46,7 +46,7 @@ pushd "${OUTPUT_DIR}" > /dev/null OUTPUT_DIR=`pwd` popd > /dev/null -source "${SCRIPT_ROOT}/client-generator.sh" +source "${SCRIPT_ROOT}/swagger-codegen/client-generator.sh" source "${SETTING_FILE}" # this is to ensure sed after docker build has perm to modify files generated diff --git a/openapi/go.sh b/openapi/go.sh index 608615e..a0d3109 100755 --- a/openapi/go.sh +++ b/openapi/go.sh @@ -43,7 +43,7 @@ pushd "${OUTPUT_DIR}" > /dev/null OUTPUT_DIR=`pwd` popd > /dev/null -source "${SCRIPT_ROOT}/client-generator.sh" +source "${SCRIPT_ROOT}/swagger-codegen/client-generator.sh" source "${SETTING_FILE}" SWAGGER_CODEGEN_COMMIT="${SWAGGER_CODEGEN_COMMIT:-v2.3.0}"; \ diff --git a/openapi/haskell.sh b/openapi/haskell.sh index f21d104..9122b06 100755 --- a/openapi/haskell.sh +++ b/openapi/haskell.sh @@ -43,7 +43,7 @@ pushd "${OUTPUT_DIR}" > /dev/null OUTPUT_DIR=`pwd` popd > /dev/null -source "${SCRIPT_ROOT}/client-generator.sh" +source "${SCRIPT_ROOT}/swagger-codegen/client-generator.sh" source "${SETTING_FILE}" # Latest version of HaskellHttpClientCodegen.java as of Nov 19, 2018 diff --git a/openapi/java.sh b/openapi/java.sh index 607d330..ede8b41 100755 --- a/openapi/java.sh +++ b/openapi/java.sh @@ -46,7 +46,7 @@ pushd "${OUTPUT_DIR}" > /dev/null OUTPUT_DIR=`pwd` popd > /dev/null -source "${SCRIPT_ROOT}/client-generator.sh" +source "${SCRIPT_ROOT}/swagger-codegen/client-generator.sh" source "${SETTING_FILE}" SWAGGER_CODEGEN_COMMIT="${SWAGGER_CODEGEN_COMMIT:-5d263e1c9cdd395d93adf061c63d5ef58a8e9ec5}"; \ diff --git a/openapi/javascript.sh b/openapi/javascript.sh index bbd68d9..a3127b6 100755 --- a/openapi/javascript.sh +++ b/openapi/javascript.sh @@ -43,7 +43,7 @@ pushd "${OUTPUT_DIR}" > /dev/null OUTPUT_DIR=`pwd` popd > /dev/null -source "${SCRIPT_ROOT}/client-generator.sh" +source "${SCRIPT_ROOT}/swagger-codegen/client-generator.sh" source "${SETTING_FILE}" SWAGGER_CODEGEN_COMMIT="${SWAGGER_CODEGEN_COMMIT:-5d263e1c9cdd395d93adf061c63d5ef58a8e9ec5}"; \ diff --git a/openapi/openapi-generator/Dockerfile b/openapi/openapi-generator/Dockerfile new file mode 100644 index 0000000..ba45b41 --- /dev/null +++ b/openapi/openapi-generator/Dockerfile @@ -0,0 +1,66 @@ +# Copyright 2017 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +FROM maven:3.5-jdk-8-slim +ARG OPENAPI_GENERATOR_COMMIT +ARG GENERATION_XML_FILE +ARG OPENAPI_GENERATOR_USER_ORG=OpenAPITools + +# Install preprocessing script requirements +RUN apt-get update && apt-get -y install git python-pip && pip install urllib3 + +# Install Autorest +RUN apt-get update && apt-get -qq -y install libunwind8 libicu57 libssl1.0 liblttng-ust0 libcurl3 libuuid1 libkrb5-3 zlib1g +RUN curl -sL https://deb.nodesource.com/setup_8.x | bash - +RUN apt-get update && apt-get -y install \ + nodejs \ + libunwind8-dev \ + && rm -rf /var/lib/apt/lists/* + +RUN npm install -g autorest + +# Check out specific commit of openapi-generator +RUN mkdir /source && \ + cd /source && \ + git clone -n https://github.com/${OPENAPI_GENERATOR_USER_ORG}/openapi-generator.git && \ + cd openapi-generator && \ + git checkout $OPENAPI_GENERATOR_COMMIT + +# Build it and persist local repository +RUN mkdir /.npm && chmod -R go+rwx /.npm && chmod -R go+rwx /root && umask 0 && cd /source/openapi-generator && \ + mvn install -DskipTests -Dmaven.test.skip=true -pl modules/openapi-generator-maven-plugin -am && \ + cp -r /root/.m2/* /usr/share/maven/ref + +RUN mkdir -p /node_modules && chmod -R go+rwx /node_modules +RUN npm install @microsoft.azure/autorest.csharp \ + @microsoft.azure/autorest.modeler + +RUN curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.asc.gpg +RUN mv microsoft.asc.gpg /etc/apt/trusted.gpg.d/ +RUN curl https://packages.microsoft.com/config/debian/9/prod.list > prod.list +RUN mv prod.list /etc/apt/sources.list.d/microsoft-prod.list +RUN chown root:root /etc/apt/trusted.gpg.d/microsoft.asc.gpg +RUN chown root:root /etc/apt/sources.list.d/microsoft-prod.list + +RUN apt-get update +RUN apt-get install -yy -q dotnet-hosting-2.0.8 + + +# Copy required files +COPY openapi-generator/generate_client_in_container.sh /generate_client.sh +COPY preprocess_spec.py / +COPY custom_objects_spec.json / +COPY ${GENERATION_XML_FILE} /generation_params.xml + +ENTRYPOINT ["mvn-entrypoint.sh", "/generate_client.sh"] diff --git a/openapi/openapi-generator/client-generator.sh b/openapi/openapi-generator/client-generator.sh new file mode 100755 index 0000000..5794913 --- /dev/null +++ b/openapi/openapi-generator/client-generator.sh @@ -0,0 +1,90 @@ +#!/bin/bash + +# Copyright 2015 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Script to fetch latest swagger spec. +# Puts the updated spec at api/swagger-spec/ + +set -o errexit +set -o nounset +set -o pipefail + +# Generates client. +# Required env vars: +# CLEANUP_DIRS: List of directories to cleanup before generation for this language +# KUBERNETES_BRANCH: Kubernetes branch name to get the swagger spec from +# CLIENT_VERSION: Client version. Will be used in the comment sections of the generated code +# PACKAGE_NAME: Name of the client package. +# CLIENT_LANGUAGE: Language of the client. ${CLIENT_LANGUAGE}.xml should exists. +# Optional env vars: +# OPENAPI_GENERATOR_USER_ORG: openapi-generator-user-org +# OPENAPI_GENERATOR_COMMIT: openapi-generator-version +# Input vars: +# $1: output directory +kubeclient::generator::generate_client() { + : "${CLEANUP_DIRS?Must set CLEANUP_DIRS env var}" + : "${KUBERNETES_BRANCH?Must set KUBERNETES_BRANCH env var}" + : "${CLIENT_VERSION?Must set CLIENT_VERSION env var}" + : "${PACKAGE_NAME?Must set PACKAGE_NAME env var}" + : "${CLIENT_LANGUAGE?Must set CLIENT_LANGUAGE env var}" + + OPENAPI_GENERATOR_USER_ORG="${OPENAPI_GENERATOR_USER_ORG:-OpenAPITools}" + OPENAPI_GENERATOR_COMMIT="${OPENAPI_GENERATOR_COMMIT:-v3.3.4}" + USERNAME="${USERNAME:-kubernetes}" + REPOSITORY="${REPOSITORY:-kubernetes}" + + local output_dir=$1 + pushd "${output_dir}" > /dev/null + local output_dir=`pwd` + popd > /dev/null + local SCRIPT_ROOT=$(dirname "${BASH_SOURCE}") + pushd "${SCRIPT_ROOT}" > /dev/null + local SCRIPT_ROOT=`pwd` + popd > /dev/null + + mkdir -p "${output_dir}" + + if [ "${USERNAME}" != "kubernetes" ]; then + image_name="${USERNAME}-${REPOSITORY}-${CLIENT_LANGUAGE}-client-gen-with-openapi-generator:v1" + else + image_name="${REPOSITORY}-${CLIENT_LANGUAGE}-client-gen-with-openapi-generator:v1" + fi + + echo "--- Building docker image ${image_name}..." + docker build "${SCRIPT_ROOT}"/../ -f "${SCRIPT_ROOT}/Dockerfile" -t "${image_name}" \ + --build-arg OPENAPI_GENERATOR_USER_ORG="${OPENAPI_GENERATOR_USER_ORG}" \ + --build-arg OPENAPI_GENERATOR_COMMIT="${OPENAPI_GENERATOR_COMMIT}" \ + --build-arg GENERATION_XML_FILE="${CLIENT_LANGUAGE}.xml" + + # Docker does not support passing arrays, pass the string representation + # of the array instead (space separated) + CLEANUP_DIRS_STRING="${CLEANUP_DIRS[@]}" + + echo "--- Running generator inside container..." + docker run -u $(id -u) \ + -e CLEANUP_DIRS="${CLEANUP_DIRS_STRING}" \ + -e KUBERNETES_BRANCH="${KUBERNETES_BRANCH}" \ + -e CLIENT_VERSION="${CLIENT_VERSION}" \ + -e CLIENT_LANGUAGE="${CLIENT_LANGUAGE}" \ + -e PACKAGE_NAME="${PACKAGE_NAME}" \ + -e OPENAPI_GENERATOR_USER_ORG="${OPENAPI_GENERATOR_USER_ORG}" \ + -e OPENAPI_GENERATOR_COMMIT="${OPENAPI_GENERATOR_COMMIT}" \ + -e USERNAME="${USERNAME}" \ + -e REPOSITORY="${REPOSITORY}" \ + -v "${output_dir}:/output_dir" \ + "${image_name}" "/output_dir" + + echo "---Done." +} diff --git a/openapi/openapi-generator/generate_client_in_container.sh b/openapi/openapi-generator/generate_client_in_container.sh new file mode 100755 index 0000000..dee2674 --- /dev/null +++ b/openapi/openapi-generator/generate_client_in_container.sh @@ -0,0 +1,103 @@ +#!/bin/bash + +# Copyright 2017 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Script to fetch latest swagger spec. +# Puts the updated spec at api/swagger-spec/ + +set -o errexit +set -o nounset +set -o pipefail + +# Generates client. +# Required env vars: +# CLEANUP_DIRS: List of directories (string separated by space) to cleanup before generation for this language +# KUBERNETES_BRANCH: Kubernetes branch name to get the swagger spec from +# CLIENT_VERSION: Client version. Will be used in the comment sections of the generated code +# PACKAGE_NAME: Name of the client package. +# OPENAPI_GENERATOR_COMMIT: openapi-generator commit sha or tag/branch name. Will only be used as a reference in docs. +# Input vars: +# $1: output directory +: "${CLEANUP_DIRS?Must set CLEANUP_DIRS env var}" +: "${KUBERNETES_BRANCH?Must set KUBERNETES_BRANCH env var}" +: "${CLIENT_VERSION?Must set CLIENT_VERSION env var}" +: "${CLIENT_LANGUAGE?Must set CLIENT_LANGUAGE env var}" +: "${PACKAGE_NAME?Must set PACKAGE_NAME env var}" +: "${OPENAPI_GENERATOR_COMMIT?Must set OPENAPI_GENERATOR_COMMIT env var}" + +output_dir=$1 +pushd "${output_dir}" > /dev/null +output_dir=`pwd` +popd > /dev/null +SCRIPT_ROOT=$(dirname "${BASH_SOURCE}") +pushd "${SCRIPT_ROOT}" > /dev/null +SCRIPT_ROOT=`pwd` +popd > /dev/null + +if ! which mvn > /dev/null 2>&1; then + echo "Maven is not installed." + exit +fi + +# There should be only one version of openapi-generator-maven-plugin. +unset PLUGIN_VERSION +shopt -s nullglob +FOLDERS=(/root/.m2/repository/org/openapitools/openapi-generator-maven-plugin/*) +for folder in "${FOLDERS[@]}"; do + if [[ -d "${folder}" ]]; then + folder=$(basename "${folder}") + if [[ ! -z "${PLUGIN_VERSION:-}" ]]; then + echo "Multiple openapi-generator-maven-plugin version exists: ${PLUGIN_VERSION} & ${folder}" + exit 1 + fi + PLUGIN_VERSION="${folder}" + fi +done +if [[ -z "${PLUGIN_VERSION:-}" ]]; then + echo "Cannot find openapi-generator-maven-plugin version" + exit 1 +fi +shopt -u nullglob + +# To make sure we can reproduce generation, we would also log code-gen exact commit +pushd /source/openapi-generator + OPENAPI_GENERATOR_COMMIT_ACTUAL=$(git rev-parse HEAD) +popd + +mkdir -p "${output_dir}" + +echo "--- Downloading and pre-processing OpenAPI spec" +python "${SCRIPT_ROOT}/preprocess_spec.py" "${CLIENT_LANGUAGE}" "${KUBERNETES_BRANCH}" "${output_dir}/swagger.json" "${USERNAME}" "${REPOSITORY}" + +echo "--- Cleaning up previously generated folders" +for i in ${CLEANUP_DIRS}; do + echo "--- Cleaning up ${output_dir}/${i}" + rm -rf "${output_dir}/${i}" +done + +echo "--- Generating client ..." +mvn -f "${SCRIPT_ROOT}/generation_params.xml" clean generate-sources \ + -Dgenerator.spec.path="${output_dir}/swagger.json" \ + -Dgenerator.output.path="${output_dir}" \ + -D=generator.client.version="${CLIENT_VERSION}" \ + -D=generator.package.name="${PACKAGE_NAME}" \ + -D=openapi-generator-version="${PLUGIN_VERSION}" \ + -Duser.home=/root + +mkdir -p "${output_dir}/.openapi-generator" +echo "Requested Commit: ${OPENAPI_GENERATOR_COMMIT}" > "${output_dir}/.openapi-generator/COMMIT" +echo "Actual Commit: ${OPENAPI_GENERATOR_COMMIT_ACTUAL}" >> "${output_dir}/.openapi-generator/COMMIT" + +echo "---Done." diff --git a/openapi/python-asyncio-api_client.py.patch b/openapi/python-asyncio-api_client.py.patch deleted file mode 100644 index 60a6dd6..0000000 --- a/openapi/python-asyncio-api_client.py.patch +++ /dev/null @@ -1,27 +0,0 @@ -61a62 -> _pool = None -64c65 -< cookie=None): ---- -> cookie=None, pool_threads=None): -67a69 -> self.pool_threads = pool_threads -69d70 -< self.pool = ThreadPool() -79,80c80,92 -< self.pool.close() -< self.pool.join() ---- -> if self._pool: -> self._pool.close() -> self._pool.join() -> self._pool = None -> -> @property -> def pool(self): -> """Create thread pool on first request -> avoids instantiating unused threadpool for blocking clients. -> """ -> if self._pool is None: -> self._pool = ThreadPool(self.pool_threads) -> return self._pool diff --git a/openapi/python-asyncio-rest.py.patch b/openapi/python-asyncio-rest.py.patch deleted file mode 100644 index 9183566..0000000 --- a/openapi/python-asyncio-rest.py.patch +++ /dev/null @@ -1,39 +0,0 @@ -21a22,23 -> import asyncio -> -64a67,70 -> if not configuration.verify_ssl: -> ssl_context.check_hostname = False -> ssl_context.verify_mode = ssl.CERT_NONE -> -67,68c73 -< ssl_context=ssl_context, -< verify_ssl=configuration.verify_ssl ---- -> ssl_context=ssl_context -81a87,89 -> def __del__(self): -> asyncio.ensure_future(self.pool_manager.close()) -> -130a139,141 -> if headers['Content-Type'] == 'application/json-patch+json': -> if not isinstance(body, list): -> headers['Content-Type'] = 'application/strategic-merge-patch+json' -164c175,177 -< async with self.pool_manager.request(**args) as r: ---- -> r = await self.pool_manager.request(**args) -> if _preload_content: -> -168,169c181,182 -< # log response body -< logger.debug("response body: %s", r.data) ---- -> # log response body -> logger.debug("response body: %s", r.data) -171,172c184,185 -< if not 200 <= r.status <= 299: -< raise ApiException(http_resp=r) ---- -> if not 200 <= r.status <= 299: -> raise ApiException(http_resp=r) diff --git a/openapi/python-asyncio.sh b/openapi/python-asyncio.sh index f7be85f..e3476ee 100755 --- a/openapi/python-asyncio.sh +++ b/openapi/python-asyncio.sh @@ -41,66 +41,52 @@ pushd "${OUTPUT_DIR}" > /dev/null OUTPUT_DIR=`pwd` popd > /dev/null -source "${SCRIPT_ROOT}/client-generator.sh" source "${SETTING_FILE}" -# Client specific Swagger branch to use. if [ ${PACKAGE_NAME} == "client" ]; then - SWAGGER_CODEGEN_COMMIT=f9b2839a3076f26db1b8fc61655a26662f2552ee -else - SWAGGER_CODEGEN_COMMIT=v2.3.1 -fi -# Build the client library in a Docker container. -CLIENT_LANGUAGE=python-asyncio -CLEANUP_DIRS=(client/apis client/models docs test) -kubeclient::generator::generate_client "${OUTPUT_DIR}" + # use openapi-generator to generate library + source "${SCRIPT_ROOT}/openapi-generator/client-generator.sh" -# Generic patches to the generated Python code, most notably renaming the library. -echo "--- Patching generated code..." + CLIENT_LANGUAGE=python-asyncio + CLEANUP_DIRS=(client/apis client/models docs test) + kubeclient::generator::generate_client "${OUTPUT_DIR}" -# Post-processing of the generated Python wrapper. -if [ ${PACKAGE_NAME} == "client" ]; then + # Generic patches to the generated Python code, most notably renaming the library. + echo "--- Patching generated code..." + + # Post-processing of the generated Python wrapper. find "${OUTPUT_DIR}/test" -type f -name \*.py -exec sed -i 's/\bclient/kubernetes_asyncio.client/g' {} + find "${OUTPUT_DIR}" -path "${OUTPUT_DIR}/base" -prune -o -type f -a -name \*.md -exec sed -i 's/\bclient/kubernetes_asyncio.client/g' {} + find "${OUTPUT_DIR}" -path "${OUTPUT_DIR}/base" -prune -o -type f -a -name \*.md -exec sed -i 's/kubernetes_asyncio.client-python/client-python/g' {} + - # workaround https://github.com/swagger-api/swagger-codegen/pull/7905 - # TODO: Remove this when above merges - find "${OUTPUT_DIR}/client" -type f -name \*.py ! -name '__init__.py' -exec sed -i '/^from .*models.*/d' {} \; - - # workaround https://github.com/swagger-api/swagger-codegen/pull/8204 - # + closing session - # + support application/strategic-merge-patch+json - # workaround https://github.com/swagger-api/swagger-codegen/pull/8797 - # + aiohttp without verify_ssl - # TODO: Remove this when above merges - patch "${OUTPUT_DIR}/client/rest.py" "${SCRIPT_ROOT}/python-asyncio-rest.py.patch" - - # workaround https://github.com/swagger-api/swagger-codegen/pull/8401 - # TODO: Remove this when above merges - - find "${OUTPUT_DIR}/client/" -type f -name \*.py -exec sed -i 's/async=/async_req=/g' {} + - find "${OUTPUT_DIR}/client/" -type f -name \*.py -exec sed -i 's/async bool/async_req bool/g' {} + - find "${OUTPUT_DIR}/client/" -type f -name \*.py -exec sed -i "s/'async'/'async_req'/g" {} + - find "${OUTPUT_DIR}/client/" -type f -name \*.py -exec sed -i "s/async parameter/async_req parameter/g" {} + - find "${OUTPUT_DIR}/client/" -type f -name \*.py -exec sed -i "s/if not async/if not async_req/g" {} + - - # workaround https://github.com/swagger-api/swagger-codegen/pull/8061 (thread pool only on demand) - # TODO: Remove this when above merges - patch "${OUTPUT_DIR}/client/api_client.py" "${SCRIPT_ROOT}/python-asyncio-api_client.py.patch" - # fix imports find "${OUTPUT_DIR}/client/" -type f -name \*.py -exec sed -i 's/import client\./import kubernetes_asyncio.client./g' {} + find "${OUTPUT_DIR}/client/" -type f -name \*.py -exec sed -i 's/from client/from kubernetes_asyncio.client/g' {} + find "${OUTPUT_DIR}/client/" -type f -name \*.py -exec sed -i 's/getattr(client\.models/getattr(kubernetes_asyncio.client.models/g' {} + + else + + # use swagger-codegen to generate library + source "${SCRIPT_ROOT}/swagger-codegen/client-generator.sh" + + SWAGGER_CODEGEN_COMMIT=v2.3.1 + # Build the client library in a Docker container. + CLIENT_LANGUAGE=python-asyncio + CLEANUP_DIRS=(client/apis client/models docs test) + kubeclient::generator::generate_client "${OUTPUT_DIR}" + + # Generic patches to the generated Python code, most notably renaming the library. + echo "--- Patching generated code..." + + # Post-processing of the generated Python wrapper. find "${OUTPUT_DIR}/test" -type f -name \*.py -exec sed -i "s/\\bclient/${PACKAGE_NAME}.client/g" {} + find "${OUTPUT_DIR}" -path "${OUTPUT_DIR}/base" -prune -o -type f -a -name \*.md -exec sed -i "s/\\bclient/${PACKAGE_NAME}.client/g" {} + find "${OUTPUT_DIR}" -path "${OUTPUT_DIR}/base" -prune -o -type f -a -name \*.md -exec sed -i "s/${PACKAGE_NAME}.client-python/client-python/g" {} + # Remove circular imports in `v1beta1_json_schema_props.py`. sed -i "/^from ${PACKAGE_NAME}\.models.*/d" "${PACKAGE_NAME}/${PACKAGE_NAME}/models/v1beta1_json_schema_props.py" + fi echo "---Done." diff --git a/openapi/python-asyncio.xml b/openapi/python-asyncio.xml index 44a6bff..81d9f4f 100644 --- a/openapi/python-asyncio.xml +++ b/openapi/python-asyncio.xml @@ -8,9 +8,9 @@ - io.swagger - swagger-codegen-maven-plugin - ${swagger-codegen-version} + org.openapitools + openapi-generator-maven-plugin + ${openapi-generator-version} @@ -37,7 +37,7 @@ - io.swagger + org.openapitools swagger-annotations ${swagger-annotations-version} diff --git a/openapi/python.sh b/openapi/python.sh index 437570f..342921c 100755 --- a/openapi/python.sh +++ b/openapi/python.sh @@ -43,7 +43,7 @@ pushd "${OUTPUT_DIR}" > /dev/null OUTPUT_DIR=`pwd` popd > /dev/null -source "${SCRIPT_ROOT}/client-generator.sh" +source "${SCRIPT_ROOT}/swagger-codegen/client-generator.sh" source "${SETTING_FILE}" SWAGGER_CODEGEN_COMMIT="${SWAGGER_CODEGEN_COMMIT:-d2b91073e1fc499fea67141ff4c17740d25f8e83}"; \ diff --git a/openapi/ruby.sh b/openapi/ruby.sh index e088315..eaff685 100755 --- a/openapi/ruby.sh +++ b/openapi/ruby.sh @@ -43,7 +43,7 @@ pushd "${OUTPUT_DIR}" > /dev/null OUTPUT_DIR=`pwd` popd > /dev/null -source "${SCRIPT_ROOT}/client-generator.sh" +source "${SCRIPT_ROOT}/swagger-codegen/client-generator.sh" source "${SETTING_FILE}" CLIENT_LANGUAGE=ruby; \ diff --git a/openapi/Dockerfile b/openapi/swagger-codegen/Dockerfile similarity index 97% rename from openapi/Dockerfile rename to openapi/swagger-codegen/Dockerfile index 6923177..942c074 100644 --- a/openapi/Dockerfile +++ b/openapi/swagger-codegen/Dockerfile @@ -58,7 +58,7 @@ RUN apt-get install -yy -q dotnet-hosting-2.0.8 # Copy required files -COPY generate_client_in_container.sh /generate_client.sh +COPY swagger-codegen/generate_client_in_container.sh /generate_client.sh COPY preprocess_spec.py / COPY custom_objects_spec.json / COPY ${GENERATION_XML_FILE} /generation_params.xml diff --git a/openapi/client-generator.sh b/openapi/swagger-codegen/client-generator.sh similarity index 94% rename from openapi/client-generator.sh rename to openapi/swagger-codegen/client-generator.sh index 7542295..c5b6a3c 100755 --- a/openapi/client-generator.sh +++ b/openapi/swagger-codegen/client-generator.sh @@ -49,20 +49,21 @@ kubeclient::generator::generate_client() { pushd "${output_dir}" > /dev/null local output_dir=`pwd` popd > /dev/null - SCRIPT_ROOT=$(dirname "${BASH_SOURCE}") + local SCRIPT_ROOT=$(dirname "${BASH_SOURCE}") pushd "${SCRIPT_ROOT}" > /dev/null local SCRIPT_ROOT=`pwd` popd > /dev/null mkdir -p "${output_dir}" - echo "--- Building docker image..." if [ "${USERNAME}" != "kubernetes" ]; then image_name="${USERNAME}-${REPOSITORY}-${CLIENT_LANGUAGE}-client-gen:v1" else image_name="${REPOSITORY}-${CLIENT_LANGUAGE}-client-gen:v1" fi - docker build "${SCRIPT_ROOT}" -t "${image_name}" \ + + echo "--- Building docker image ${image_name}..." + docker build "${SCRIPT_ROOT}"/../ -f "${SCRIPT_ROOT}/Dockerfile" -t "${image_name}" \ --build-arg SWAGGER_CODEGEN_USER_ORG="${SWAGGER_CODEGEN_USER_ORG}" \ --build-arg SWAGGER_CODEGEN_COMMIT="${SWAGGER_CODEGEN_COMMIT}" \ --build-arg GENERATION_XML_FILE="${CLIENT_LANGUAGE}.xml" diff --git a/openapi/generate_client_in_container.sh b/openapi/swagger-codegen/generate_client_in_container.sh similarity index 100% rename from openapi/generate_client_in_container.sh rename to openapi/swagger-codegen/generate_client_in_container.sh diff --git a/openapi/typescript.sh b/openapi/typescript.sh index 768bea0..91aee99 100755 --- a/openapi/typescript.sh +++ b/openapi/typescript.sh @@ -43,7 +43,7 @@ pushd "${OUTPUT_DIR}" > /dev/null OUTPUT_DIR=`pwd` popd > /dev/null -source "${SCRIPT_ROOT}/client-generator.sh" +source "${SCRIPT_ROOT}/swagger-codegen/client-generator.sh" source "${SETTING_FILE}" CLIENT_LANGUAGE=typescript; \