代码拉取完成,页面将自动刷新
From a3cbed8edcadd0e175f52b304f46550f92cbbee8 Mon Sep 17 00:00:00 2001
From: peanut_huang <huangliming5@huawei.com>
Date: Thu, 19 Nov 2020 08:02:33 +0000
Subject: [PATCH] add polycube-codegen tools
Signed-off-by: peanut_huang <huangliming5@huawei.com>
---
tools/install.sh | 77 ++++++++++++++++++++++
tools/polycube-codegen.sh | 130 ++++++++++++++++++++++++++++++++++++++
2 files changed, 207 insertions(+)
create mode 100644 tools/install.sh
create mode 100644 tools/polycube-codegen.sh
diff --git a/tools/install.sh b/tools/install.sh
new file mode 100644
index 0000000..c423e0f
--- /dev/null
+++ b/tools/install.sh
@@ -0,0 +1,77 @@
+#!/bin/bash
+
+# This script installs the tools needed to automatically generate the
+# code for a new service, together with their dependencies.
+# In particular, it installs the pyang-swagger tool that is used to
+# generate the swagger-compatible REST APIs and the swagger-codegen
+# tool that is used to generate the service stub.
+#
+# In addition, this script installs the polycube-codegen application that
+# can be used to interact with the aforementioned tools and generate the
+# service stub and/or the REST APIs given the YANG model of the new service.
+#
+# Run the polycube-codegen application form the terminal after executing this
+# script in order to get the needed information.
+#
+# The script will download the tools if they are not installed, while it will
+# update those tools otherwise.
+
+function success_message {
+ set +x
+ echo
+ echo 'Installation completed successfully'
+}
+
+_pwd=$(pwd)
+
+set -e
+
+[ -z ${SUDO+x} ] && SUDO='sudo'
+
+echo "Installing polycube-codegen"
+
+APT_CMD="apt"
+CONFIG_PATH=$HOME/.config/polycube/
+SWAGGER_CODEGEN_CONFIG_FILENAME=swagger_codegen_config.json
+
+if hash apt 2>/dev/null; then
+ APT_CMD="apt"
+fi
+
+$SUDO $APT_CMD update -y
+
+# Install python dependencies
+PACKAGES=""
+PACKAGES+=" sudo git wget"
+PACKAGES+=" python-minimal python-pip python-setuptools" # dependencies for pyang swagger
+PACKAGES+=" default-jre default-jdk maven" # dependencies for swaggercodegen
+PACKAGES+=" jq" # dependencies for json parsing on bash scripts
+
+$SUDO $APT_CMD install -y $PACKAGES
+
+cd pyang-swagger/
+./install.sh
+
+GIT_SWAGGER_CODEGEN_BRANCH="$(git rev-parse --abbrev-ref HEAD)"
+GIT_SWAGGER_CODEGEN_COMMIT_HASH="$(git log -1 --format=%h)"
+
+mkdir -p $CONFIG_PATH
+
+cd ../swagger-codegen/
+mvn -T $(getconf _NPROCESSORS_ONLN) -am -pl "modules/swagger-codegen-cli" package -DskipTests
+
+$SUDO cp modules/swagger-codegen-cli/target/swagger-codegen-cli.jar /usr/local/bin/
+cd ..
+$SUDO cp polycube-codegen.sh /usr/local/bin/polycube-codegen
+
+#Create configuration file for swagger-codegen
+cat > ${CONFIG_PATH}${SWAGGER_CODEGEN_CONFIG_FILENAME} << EOF
+{
+ "gitUserId" : "polycube-network",
+ "gitRepoId" : "${GIT_SWAGGER_CODEGEN_BRANCH}/${GIT_SWAGGER_CODEGEN_COMMIT_HASH}"
+}
+EOF
+
+success_message
+
+cd $_pwd
diff --git a/tools/polycube-codegen.sh b/tools/polycube-codegen.sh
new file mode 100644
index 0000000..d27c050
--- /dev/null
+++ b/tools/polycube-codegen.sh
@@ -0,0 +1,130 @@
+#!/bin/bash
+
+_pwd=$(pwd)
+
+set -e
+
+SWAGGER_CONFIG_FILE=$HOME/.config/polycube/swagger_codegen_config.json
+POLYCUBE_CODEGEN_LOG=/dev/null
+SWAGGER_CODEGEN_CLI=/usr/local/bin/swagger-codegen-cli.jar
+CLIENT_LANG=polycube
+ONLINE_GENERATOR_URL=https://generator.swagger.io/api/gen/clients
+
+function exit_error() {
+ if [ "$?" -ne "0" ]; then
+ echo "Failed to create the C++ stub"
+ cat $POLYCUBE_CODEGEN_LOG
+ fi
+ exit 0
+}
+
+function show_help() {
+usage="$(basename "$0") [-h] [-i input_yang] [-o output_folder] [-s output_swagger_file] [-l client_language]
+Polycube code generator that translates a YANG file into an polycube C++ service stub
+
+where:
+ -h show this help text
+ -i path to the input YANG file
+ -o path to the destination folder where the service stub will be placed
+ -s path to the destination swagger file (optional)
+ -l language used to generate service's client library (optional)"
+
+echo "$usage"
+}
+
+
+trap exit_error EXIT
+
+while getopts :i:o:s:l:h option; do
+ case "${option}" in
+ h|\?)
+ show_help
+ exit 0
+ ;;
+ i) YANG_PATH=${OPTARG}
+ ;;
+ o) OUT_FOLDER=${OPTARG}
+ ;;
+ s) OUT_SWAGGER_PATH=${OPTARG}
+ ;;
+ l) CLIENT_LANG=${OPTARG}
+ ;;
+ :)
+ echo "Option -$OPTARG requires an argument." >&2
+ show_help
+ exit 0
+ ;;
+ esac
+done
+
+if [ -f $POLYCUBE_CODEGEN_LOG ]; then
+ rm $POLYCUBE_CODEGEN_LOG
+fi
+
+if [ -z ${YANG_PATH+x} ]; then
+ echo "You should specify the YANG file with the -i option" >&2;
+ show_help
+ exit 0
+fi
+
+if [ ! -f "$YANG_PATH" ]; then
+ echo "$YANG_PATH does not exist" >&2;
+ exit 0
+fi
+
+if [ -z ${OUT_FOLDER+x} ] && [ -z ${OUT_SWAGGER_PATH+x} ]; then
+ echo "You should specify the output folder file with the -o option" >&2;
+ show_help
+ exit 0
+fi
+
+if [[ "$POLYCUBE_IN_DOCKER" = "true" ]]; then
+ POLYCUBE_BASE_DATAMODELS_FOLDER="/polycube-base-datamodels"
+fi
+
+if [ -z ${POLYCUBE_BASE_DATAMODELS_FOLDER+x} ]; then
+ POLYCUBE_BASE_DATAMODELS_FOLDER='/usr/local/include/polycube/datamodel-common/'
+fi
+
+now="$(date '+%Y_%m_%d_%H_%M_%S')"
+json_filename="$now"_api.json
+#echo "$now"
+
+pyang -f swagger -p $POLYCUBE_BASE_DATAMODELS_FOLDER $YANG_PATH -o /tmp/"$json_filename" > $POLYCUBE_CODEGEN_LOG 2>&1
+
+if [ -n "${OUT_SWAGGER_PATH+set}" ]; then
+ cp /tmp/"$json_filename" $OUT_SWAGGER_PATH
+ echo "Swagger file saved in $OUT_SWAGGER_PATH"
+ exit 0
+fi
+
+if [ "$CLIENT_LANG" == "polycube" ]; then
+ if [ -f $SWAGGER_CONFIG_FILE ]; then
+ java -jar $SWAGGER_CODEGEN_CLI generate -l polycube -i /tmp/"$json_filename" \
+ -o $OUT_FOLDER --config $SWAGGER_CONFIG_FILE > $POLYCUBE_CODEGEN_LOG 2>&1
+ else
+ java -jar $SWAGGER_CODEGEN_CLI generate -l polycube -i /tmp/"$json_filename" \
+ -o $OUT_FOLDER > $POLYCUBE_CODEGEN_LOG 2>&1
+ fi
+else
+ # Let's use the online generator to generate the service clients
+ swagger_file=$(</tmp/"$json_filename")
+ json_body=' { "spec": '${swagger_file}' }'
+ res=$(curl -H "Content-type: application/json" -X POST -d "$json_body" ${ONLINE_GENERATOR_URL}/${CLIENT_LANG})
+ download_url=$(jq -r '.link' <<< "$res")
+ if [ $? -ne 0 ] || [ -z "$download_url" ] || [ "$download_url" == "null" ]; then
+ echo "Unable to generate the clients stub for ${CLIENT_LANG}"
+ exit 1
+ fi
+
+ wget -O "$OUT_FOLDER/${CLIENT_LANG}_client_api.zip" $download_url
+fi
+
+rm /tmp/"$json_filename"
+
+cd $_pwd
+
+echo "$CLIENT_LANG output generated under $OUT_FOLDER"
+exit 0
+
+
--
2.23.0
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。