Skip to content

Commit

Permalink
Merge pull request #84 from ATIX-AG/docker_in_docker_with_local_opena…
Browse files Browse the repository at this point in the history
…pi_schema

Add support for Docker in Docker and local openapi schema
  • Loading branch information
dkliban authored Nov 21, 2023
2 parents 30e72b6 + 4510907 commit c393756
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 18 deletions.
25 changes: 25 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,31 @@ variable to instruct the bindings generator where the root of the API is located
default ``export PULP_API_ROOT="/pulp/"`` is the default root, which then serves the api.json at
``/pulp/api/v3/docs/api.json``.

Generating Bindings Against Remote Systems
------------------------------------------

During bindings generation the openapi schema is fetched. Use the ``PULP_API`` environment
variable to instruct the bindings generator to use a Pulp API on a different host and/or port.
For example, ``export PULP_API="http://localhost:24817"`` are the default host and port, which
results in the bindings generator talking to the Pulp API at
``http://localhost:24817/pulp/api/v3/docs/api.json``.

Generating Bindings Using a Local Openapi Schema
-----------------------------------------------

If you want to use a locally present openapi schema, you can skip fetching the openapi schema
by setting the ``USE_LOCAL_API_JSON`` environment variable. Doing so you have to manually provide the
``api.json`` file containing the openapi schema in the current working directory.

Generate Bindings Using Docker in Docker (dind)
-----------------------------------------------

Bindings are generated using the openapi-generator-cli docker container. If your environment itself runs in
a docker container, the openapi-generator-cli container has to be started as a sibling container. For
sibling containers, volumes cannot be mounted as usual. They have to be passed through from the parent
container. For this to work you have to set the ``PARENT_CONRAINER_ID`` environment variable to specify the
parent container in a dind environment.

Generating Bindings on a Filesystem Shared With Another Container
-----------------------------------------------------------------

Expand Down
52 changes: 34 additions & 18 deletions generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,20 @@ else
volume_name="/local"
fi

PULP_URL="${PULP_URL:-http://localhost:24817}"
# Skip downloading the api.json if `USE_LOCAL_API_JSON` is set.
if [[ -z $USE_LOCAL_API_JSON ]]
then
PULP_URL="${PULP_URL:-http://localhost:24817}"

PULP_API_ROOT="${PULP_API_ROOT:-/pulp/}"

PULP_API_ROOT="${PULP_API_ROOT:-/pulp/}"
PULP_URL="${PULP_URL}${PULP_API_ROOT}api/v3/"

PULP_URL="${PULP_URL}${PULP_API_ROOT}api/v3/"
# Download the schema
curl -k -o api.json "${PULP_URL}docs/api.json?bindings&plugin=$1"
# Get the version of the pulpcore or plugin as reported by status API
fi

# Download the schema
curl -k -o api.json "${PULP_URL}docs/api.json?bindings&plugin=$1"
# Get the version of the pulpcore or plugin as reported by status API
export DOMAIN_ENABLED=$(jq -r '.info | ."x-pulp-domain-enabled" // false' < api.json)

if [ $# -gt 2 ];
Expand All @@ -64,20 +69,31 @@ else
export VERSION=$(http ${PULP_URL}status/ | jq --arg plugin $COMPONENT_NAME -r '.versions[] | select(.component == $plugin) | .version')
fi

# Mount volumes from parent container with `--volumes-from` option if the
# `PARENT_CONTAINER_ID` is set.
if [ -z $PARENT_CONTAINER_ID ]
then
VOLUME_OPTION="--volume ${PWD}:${volume_name}"
VOLUME_DIR="/local"
else
VOLUME_OPTION="--volumes-from ${PARENT_CONTAINER_ID}:rw"
VOLUME_DIR="${PWD}"
fi

echo ::group::BINDINGS
if [ $2 = 'python' ]
then
$container_exec run \
$ULIMIT_COMMAND \
$USER_COMMAND \
--rm \
-v ${PWD}:$volume_name \
${VOLUME_OPTION} \
docker.io/openapitools/openapi-generator-cli:v4.3.1 generate \
-i /local/api.json \
-i ${VOLUME_DIR}/api.json \
-g python \
-o /local/$1-client \
-o ${VOLUME_DIR}/$1-client \
--additional-properties=packageName=pulpcore.client.$1,projectName=$1-client,packageVersion=${VERSION},domainEnabled=${DOMAIN_ENABLED} \
-t /local/templates/python \
-t ${VOLUME_DIR}/templates/python \
--skip-validate-spec \
--strict-spec=false
cp python/__init__.py $1-client/pulpcore/
Expand All @@ -93,14 +109,14 @@ then
$container_exec run \
$ULIMIT_COMMAND \
$USER_COMMAND \
--rm -v ${PWD}:$volume_name \
--rm ${VOLUME_OPTION} \
docker.io/openapitools/openapi-generator-cli:v4.3.1 generate \
-i /local/api.json \
-i ${VOLUME_DIR}/api.json \
-g ruby \
-o /local/$1-client \
-o ${VOLUME_DIR}/$1-client \
--additional-properties=gemName=$1_client,gemLicense="GPLv2+",gemVersion=${VERSION},gemHomepage=https://github.com/pulp/$1 \
--library=faraday \
-t /local/templates/ruby \
-t ${VOLUME_DIR}/templates/ruby \
--skip-validate-spec \
--strict-spec=false
fi
Expand All @@ -109,12 +125,12 @@ then
$container_exec run \
$ULIMIT_COMMAND \
$USER_COMMAND \
--rm -v ${PWD}:$volume_name \
--rm ${VOLUME_OPTION} \
docker.io/openapitools/openapi-generator-cli:v5.2.1 generate \
-i /local/api.json \
-i ${VOLUME_DIR}/api.json \
-g typescript-axios \
-o /local/$1-client \
-t /local/templates/typescript-axios \
-o ${VOLUME_DIR}/$1-client \
-t ${VOLUME_DIR}/templates/typescript-axios \
--skip-validate-spec \
--strict-spec=false
fi
Expand Down

0 comments on commit c393756

Please sign in to comment.