Skip to content

Commit b3756a2

Browse files
committed
Add gen-client.sh
This script focusses on generating the client from an api spec file. It does not require access to a running pulp instance. [noissue]
1 parent 6bc3825 commit b3756a2

File tree

6 files changed

+155
-167
lines changed

6 files changed

+155
-167
lines changed

.ci/ansible/start_container.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
follow_redirects: none
5959
register: result
6060
until: result.status == 200
61-
retries: 6
61+
retries: 20
6262
delay: 5
6363
rescue:
6464
- name: "Output pulp container log"

.github/workflows/scripts/install.sh

+9-35
Original file line numberDiff line numberDiff line change
@@ -9,52 +9,26 @@
99

1010
set -euv
1111

12-
mkdir .ci/ansible/vars || true
13-
echo "---" > .ci/ansible/vars/main.yaml
12+
# make sure this script runs at the repo root
13+
cd "$(dirname "$(realpath -e "$0")")"/../../..
1414

15+
TAG="${TAG:-latest}"
16+
17+
mkdir -p .ci/ansible/vars
1518
cd .ci/ansible/
1619

17-
TAG=ci_build
18-
19-
if [ -n "${GITHUB_REF##*/}" ]; then
20-
# Install the plugin only and use published PyPI packages for the rest
21-
# Quoting ${TAG} ensures Ansible casts the tag as a string.
22-
cat >> vars/main.yaml << VARSYAML
23-
image:
24-
name: pulp
25-
tag: "${TAG}"
26-
plugins:
27-
- name: pulpcore
28-
source: pulpcore
29-
- name: pulp_file
30-
source: "pulp_file"
31-
services:
32-
- name: pulp
33-
image: "pulp:${TAG}"
34-
volumes:
35-
- ./settings:/etc/pulp
36-
VARSYAML
37-
else
38-
cat >> vars/main.yaml << VARSYAML
39-
image:
40-
name: pulp
41-
tag: "${TAG}"
42-
plugins:
43-
- name: pulp_file
44-
source: "pulp_file"
45-
- name: pulpcore
46-
source: ./pulpcore
20+
cat >> vars/main.yaml << VARSYAML
21+
---
4722
services:
4823
- name: pulp
49-
image: "pulp:${TAG}"
24+
image: "ghcr.io/pulp/pulp:${TAG}"
5025
volumes:
5126
- ./settings:/etc/pulp
5227
VARSYAML
53-
fi
5428

5529
cat >> vars/main.yaml << VARSYAML
5630
pulp_settings: {"allowed_content_checksums": ["sha1", "sha224", "sha256", "sha384", "sha512"], "allowed_export_paths": ["/tmp"], "allowed_import_paths": ["/tmp"]}
5731
VARSYAML
5832

59-
ansible-playbook build_container.yaml
33+
# ansible-playbook build_container.yaml
6034
ansible-playbook start_container.yaml

.github/workflows/scripts/script.sh

+16-20
Original file line numberDiff line numberDiff line change
@@ -10,39 +10,35 @@
1010

1111
set -mveuo pipefail
1212

13+
# make sure this script runs at the repo root
14+
cd "$(dirname "$(realpath -e "$0")")"/../../..
15+
1316
# Needed for both starting the service and building the docs.
1417
# Gets set in .github/settings.yml, but doesn't seem to inherited by
1518
# this script.
16-
export DJANGO_SETTINGS_MODULE=pulpcore.app.settings
17-
export PULP_SETTINGS=$GITHUB_WORKSPACE/.ci/ansible/settings/settings.py
18-
19+
export DJANGO_SETTINGS_MODULE="pulpcore.app.settings"
20+
export PULP_SETTINGS="$PWD/.ci/ansible/settings/settings.py"
1921
export PULP_URL="http://pulp"
2022

21-
cd $GITHUB_WORKSPACE
22-
2323
./generate.sh pulpcore python
2424
pip install ./pulpcore-client
2525
./generate.sh pulp_file python
2626
pip install ./pulp_file-client
2727

28-
python $GITHUB_WORKSPACE/.github/workflows/scripts/test_bindings.py
29-
cd ../pulp-openapi-generator
28+
python .github/workflows/scripts/test_bindings.py
3029

3130
rm -rf ./pulpcore-client
31+
./generate.sh pulpcore ruby
32+
pushd pulpcore-client
33+
gem build pulpcore_client
34+
gem install --both ./pulpcore_client-*.gem
35+
popd
3236

33-
./generate.sh pulpcore ruby 0
34-
cd pulpcore-client
35-
gem build pulpcore_client
36-
gem install --both ./pulpcore_client-0.gem
37-
cd ..
3837
rm -rf ./pulp_file-client
39-
40-
./generate.sh pulp_file ruby 0
41-
42-
cd pulp_file-client
38+
./generate.sh pulp_file ruby
39+
pushd pulp_file-client
4340
gem build pulp_file_client
44-
gem install --both ./pulp_file_client-0.gem
45-
cd ..
46-
ruby $GITHUB_WORKSPACE/.github/workflows/scripts/test_bindings.rb
41+
gem install --both ./pulp_file_client-*.gem
42+
popd
4743

48-
exit
44+
ruby .github/workflows/scripts/test_bindings.rb

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,6 @@ target/
6565

6666
# written in generate.sh
6767
.openapi-generator-ignore
68+
69+
# generated client packages
70+
/*-client/

gen-client.sh

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
#!/bin/bash
2+
3+
set -eu
4+
5+
if [ $# -eq 0 ]
6+
then
7+
echo "No arguments provided"
8+
echo "Usage: $0 <api_spec> <component> [<language> [<package>]]"
9+
exit 1
10+
fi
11+
12+
API_SPEC="$1"
13+
COMPONENT="$2"
14+
LANGUAGE="${3:-python}"
15+
PACKAGE="${4:-pulp_${COMPONENT//-/_}}"
16+
17+
DOMAIN_ENABLED="$(jq -r '.info."x-pulp-domain-enabled" // false' < "${API_SPEC}")"
18+
VERSION="$(jq -r --arg component "$COMPONENT" '.info."x-pulp-app-versions"[$component]' < "${API_SPEC}")"
19+
20+
echo "Version: ${VERSION}"
21+
[ "${VERSION}" != "none" ]
22+
23+
OPENAPI_PYTHON_IMAGE="${OPENAPI_PYTHON_IMAGE:-docker.io/openapitools/openapi-generator-cli:v4.3.1}"
24+
OPENAPI_RUBY_IMAGE="${OPENAPI_RUBY_IMAGE:-docker.io/openapitools/openapi-generator-cli:v4.3.1}"
25+
OPENAPI_TYPESCRIPT_IMAGE="${OPENAPI_TYPESCRIPT_IMAGE:-docker.io/openapitools/openapi-generator-cli:v5.2.1}"
26+
27+
if command -v podman > /dev/null
28+
then
29+
CONTAINER_EXEC=podman
30+
if [[ -n "${PULP_MCS_LABEL:-}" ]]
31+
then
32+
USER_COMMAND=("--userns=keep-id" "--security-opt" "label=level:$PULP_MCS_LABEL")
33+
else
34+
USER_COMMAND=("--userns=keep-id")
35+
fi
36+
ULIMIT_COMMAND=()
37+
else
38+
CONTAINER_EXEC=docker
39+
if [[ -n "${PULP_MCS_LABEL:-}" ]]
40+
then
41+
USER_COMMAND=("-u" "$(id -u)" "--security-opt" "label=level:$PULP_MCS_LABEL")
42+
else
43+
USER_COMMAND=("-u" "$(id -u)")
44+
fi
45+
ULIMIT_COMMAND=("--ulimit" "nofile=122880:122880")
46+
fi
47+
48+
# Mount volumes from parent container with `--volumes-from` option if `PARENT_CONTAINER_ID` is set.
49+
if [ -z "${PARENT_CONTAINER_ID:-}" ]
50+
then
51+
if command -v getenforce > /dev/null && [ "$(getenforce)" == "Enforcing" ]
52+
then
53+
VOLUME_NAME="/local:Z"
54+
else
55+
VOLUME_NAME="/local"
56+
fi
57+
VOLUME_OPTION=("--volume" "${PWD}:${VOLUME_NAME}")
58+
VOLUME_DIR="/local"
59+
else
60+
VOLUME_OPTION=("--volumes-from" "${PARENT_CONTAINER_ID}:rw")
61+
VOLUME_DIR="${PWD}"
62+
fi
63+
64+
if [ "$LANGUAGE" = "python" ]
65+
then
66+
$CONTAINER_EXEC run \
67+
"${ULIMIT_COMMAND[@]}" \
68+
"${USER_COMMAND[@]}" \
69+
--rm \
70+
"${VOLUME_OPTION[@]}" \
71+
"$OPENAPI_PYTHON_IMAGE" generate \
72+
-i "${VOLUME_DIR}/${API_SPEC}" \
73+
-g python \
74+
-o "${VOLUME_DIR}/${PACKAGE}-client" \
75+
"--additional-properties=packageName=pulpcore.client.${PACKAGE},projectName=${PACKAGE}-client,packageVersion=${VERSION},domainEnabled=${DOMAIN_ENABLED}" \
76+
-t "${VOLUME_DIR}/templates/python" \
77+
--skip-validate-spec \
78+
--strict-spec=false
79+
cp python/__init__.py "${PACKAGE}-client/pulpcore/"
80+
cp python/__init__.py "${PACKAGE}-client/pulpcore/client/"
81+
fi
82+
83+
if [ "$LANGUAGE" = "ruby" ]
84+
then
85+
# https://github.com/OpenAPITools/openapi-generator/wiki/FAQ#how-to-skip-certain-files-during-code-generation
86+
mkdir -p "${PACKAGE}-client"
87+
echo git_push.sh > "${PACKAGE}-client/.openapi-generator-ignore"
88+
89+
python3 remove-cookie-auth.py
90+
$CONTAINER_EXEC run \
91+
"${ULIMIT_COMMAND[@]}" \
92+
"${USER_COMMAND[@]}" \
93+
--rm \
94+
"${VOLUME_OPTION[@]}" \
95+
"$OPENAPI_RUBY_IMAGE" generate \
96+
-i "${VOLUME_DIR}/${API_SPEC}" \
97+
-g ruby \
98+
-o "${VOLUME_DIR}/${PACKAGE}-client" \
99+
"--additional-properties=gemName=${PACKAGE}_client,gemLicense="GPLv2+",gemVersion=${VERSION},gemHomepage=https://github.com/pulp/${PACKAGE}" \
100+
--library=faraday \
101+
-t "${VOLUME_DIR}/templates/ruby" \
102+
--skip-validate-spec \
103+
--strict-spec=false
104+
fi
105+
106+
if [ "$LANGUAGE" = "typescript" ]
107+
then
108+
$CONTAINER_EXEC run \
109+
"${ULIMIT_COMMAND[@]}" \
110+
"${USER_COMMAND[@]}" \
111+
--rm \
112+
"${VOLUME_OPTION[@]}" \
113+
"$OPENAPI_TYPESCRIPT_IMAGE" generate \
114+
-i "${VOLUME_DIR}/${API_SPEC}" \
115+
-g typescript-axios \
116+
-o "${VOLUME_DIR}/${PACKAGE}-client" \
117+
-t "${VOLUME_DIR}/templates/typescript-axios" \
118+
--skip-validate-spec \
119+
--strict-spec=false
120+
fi

generate.sh

+6-111
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,12 @@ then
88
exit 1
99
fi
1010

11-
if command -v podman > /dev/null
11+
# match the component name by removing the "pulp/pulp_" prefix
12+
if [ "$1" = "pulpcore" ]
1213
then
13-
CONTAINER_EXEC=podman
14-
if [[ -n "${PULP_MCS_LABEL:-}" ]]
15-
then
16-
USER_COMMAND=("--userns=keep-id" "--security-opt" "label=level:$PULP_MCS_LABEL")
17-
else
18-
USER_COMMAND=("--userns=keep-id")
19-
fi
20-
ULIMIT_COMMAND=()
14+
COMPONENT="core"
2115
else
22-
CONTAINER_EXEC=docker
23-
if [[ -n "${PULP_MCS_LABEL:-}" ]]
24-
then
25-
USER_COMMAND=("-u" "$(id -u)" "--security-opt" "label=level:$PULP_MCS_LABEL")
26-
else
27-
USER_COMMAND=("-u" "$(id -u)")
28-
fi
29-
ULIMIT_COMMAND=("--ulimit" "nofile=122880:122880")
30-
fi
31-
32-
if command -v getenforce > /dev/null && [ "$(getenforce)" == "Enforcing" ]
33-
then
34-
VOLUME_NAME="/local:Z"
35-
else
36-
VOLUME_NAME="/local"
16+
COMPONENT=${1#"pulp_"}
3717
fi
3818

3919
# Skip downloading the api.json if `USE_LOCAL_API_JSON` is set.
@@ -45,7 +25,7 @@ then
4525

4626
# Download the schema
4727
RETRY_COUNT=0
48-
until curl --fail-with-body -k -o api.json "${PULP_URL}docs/api.json?bindings&plugin=$1"
28+
until curl --fail-with-body -k -o api.json "${PULP_URL}docs/api.json?bindings&component=${COMPONENT}"
4929
do
5030
if [ $RETRY_COUNT -eq 10 ]
5131
then
@@ -57,94 +37,9 @@ then
5737
# Get the version of the pulpcore or plugin as reported by status API
5838
fi
5939

60-
DOMAIN_ENABLED="$(jq -r '.info | ."x-pulp-domain-enabled" // false' < api.json)"
61-
62-
if [ $# -gt 2 ]
63-
then
64-
export VERSION=$3
65-
else
66-
# match the component name by removing the "pulp/pulp_" prefix
67-
if [ "$1" = "pulpcore" ]
68-
then
69-
COMPONENT="core"
70-
else
71-
COMPONENT=${1#"pulp_"}
72-
fi
73-
74-
VERSION="$(curl -k "${PULP_URL}status/" | jq --arg component "$COMPONENT" -r '.versions[] | select(.component == $component) | .version')"
75-
fi
76-
77-
# Mount volumes from parent container with `--volumes-from` option if the
78-
# `PARENT_CONTAINER_ID` is set.
79-
if [ -z "${PARENT_CONTAINER_ID:-}" ]
80-
then
81-
VOLUME_OPTION=("--volume" "${PWD}:${VOLUME_NAME}")
82-
VOLUME_DIR="/local"
83-
else
84-
VOLUME_OPTION=("--volumes-from" "${PARENT_CONTAINER_ID}:rw")
85-
VOLUME_DIR="${PWD}"
86-
fi
87-
88-
OPENAPI_PYTHON_IMAGE="${OPENAPI_PYTHON_IMAGE:-docker.io/openapitools/openapi-generator-cli:v4.3.1}"
89-
OPENAPI_RUBY_IMAGE="${OPENAPI_RUBY_IMAGE:-docker.io/openapitools/openapi-generator-cli:v4.3.1}"
90-
OPENAPI_TYPESCRIPT_IMAGE="${OPENAPI_TYPESCRIPT_IMAGE:-docker.io/openapitools/openapi-generator-cli:v5.2.1}"
91-
9240
echo ::group::BINDINGS
93-
if [ "$2" = "python" ]
94-
then
95-
$CONTAINER_EXEC run \
96-
"${ULIMIT_COMMAND[@]}" \
97-
"${USER_COMMAND[@]}" \
98-
--rm \
99-
"${VOLUME_OPTION[@]}" \
100-
"$OPENAPI_PYTHON_IMAGE" generate \
101-
-i "${VOLUME_DIR}/api.json" \
102-
-g python \
103-
-o "${VOLUME_DIR}/$1-client" \
104-
"--additional-properties=packageName=pulpcore.client.$1,projectName=$1-client,packageVersion=${VERSION},domainEnabled=${DOMAIN_ENABLED}" \
105-
-t "${VOLUME_DIR}/templates/python" \
106-
--skip-validate-spec \
107-
--strict-spec=false
108-
cp python/__init__.py "$1-client/pulpcore/"
109-
cp python/__init__.py "$1-client/pulpcore/client/"
110-
fi
111-
if [ "$2" = 'ruby' ]
112-
then
113-
# https://github.com/OpenAPITools/openapi-generator/wiki/FAQ#how-to-skip-certain-files-during-code-generation
114-
mkdir -p "$1-client"
115-
echo git_push.sh > "$1-client/.openapi-generator-ignore"
11641

117-
python3 remove-cookie-auth.py
118-
$CONTAINER_EXEC run \
119-
"${ULIMIT_COMMAND[@]}" \
120-
"${USER_COMMAND[@]}" \
121-
--rm \
122-
"${VOLUME_OPTION[@]}" \
123-
"$OPENAPI_RUBY_IMAGE" generate \
124-
-i "${VOLUME_DIR}/api.json" \
125-
-g ruby \
126-
-o "${VOLUME_DIR}/$1-client" \
127-
"--additional-properties=gemName=$1_client,gemLicense="GPLv2+",gemVersion=${VERSION},gemHomepage=https://github.com/pulp/$1" \
128-
--library=faraday \
129-
-t "${VOLUME_DIR}/templates/ruby" \
130-
--skip-validate-spec \
131-
--strict-spec=false
132-
fi
133-
if [ "$2" = 'typescript' ]
134-
then
135-
$CONTAINER_EXEC run \
136-
"${ULIMIT_COMMAND[@]}" \
137-
"${USER_COMMAND[@]}" \
138-
--rm \
139-
"${VOLUME_OPTION[@]}" \
140-
"$OPENAPI_TYPESCRIPT_IMAGE" generate \
141-
-i "${VOLUME_DIR}/api.json" \
142-
-g typescript-axios \
143-
-o "${VOLUME_DIR}/$1-client" \
144-
-t "${VOLUME_DIR}/templates/typescript-axios" \
145-
--skip-validate-spec \
146-
--strict-spec=false
147-
fi
42+
./gen-client.sh api.json "${COMPONENT}" "${2:-python}" "${1}"
14843

14944
echo ::endgroup::
15045
if [[ -z "${USE_LOCAL_API_JSON:-}" ]]

0 commit comments

Comments
 (0)