From f832b9b81541cbae8b9f2e0f97e0e29569584c6b Mon Sep 17 00:00:00 2001 From: Steve Hu Date: Mon, 19 Feb 2018 07:05:38 -0500 Subject: [PATCH] fixes #106 generate build.sh for easy docker build with version --- .../codegen/graphql/GraphqlGenerator.java | 7 ++- .../templates/graphql/buildSh.rocker.raw | 52 +++++++++++++++++++ .../src/test/resources/config.json | 3 +- .../codegen/hybrid/HybridServerGenerator.java | 7 ++- .../hybrid/HybridServiceGenerator.java | 8 --- .../hybrid/server/buildSh.rocker.raw | 52 +++++++++++++++++++ .../src/test/resources/serverConfig.json | 3 +- .../codegen/rest/OpenApiGenerator.java | 3 ++ .../codegen/rest/SwaggerGenerator.java | 3 ++ .../templates/rest/buildSh.rocker.raw | 52 +++++++++++++++++++ light-rest-4j/src/test/resources/config.json | 3 +- 11 files changed, 174 insertions(+), 19 deletions(-) create mode 100644 light-graphql-4j/src/main/resources/templates/graphql/buildSh.rocker.raw create mode 100644 light-hybrid-4j/src/main/resources/templates/hybrid/server/buildSh.rocker.raw create mode 100644 light-rest-4j/src/main/resources/templates/rest/buildSh.rocker.raw diff --git a/light-graphql-4j/src/main/java/com/networknt/codegen/graphql/GraphqlGenerator.java b/light-graphql-4j/src/main/java/com/networknt/codegen/graphql/GraphqlGenerator.java index 68393d55d..fdd191e74 100644 --- a/light-graphql-4j/src/main/java/com/networknt/codegen/graphql/GraphqlGenerator.java +++ b/light-graphql-4j/src/main/java/com/networknt/codegen/graphql/GraphqlGenerator.java @@ -41,11 +41,9 @@ public void generate(String targetPath, Object model, Any config) throws IOExcep boolean enableHttps = config.toBoolean("enableHttps"); String httpsPort = config.toString("httpsPort"); boolean enableRegistry = config.toBoolean("enableRegistry"); - boolean supportOracle = config.toBoolean("supportOracle"); - boolean supportMysql = config.toBoolean("supportMysql"); - boolean supportPostgresql = config.toBoolean("supportPostgresql"); - boolean supportH2ForTest = config.toBoolean("supportH2ForTest"); boolean supportClient = config.toBoolean("supportClient"); + String dockerOrganization = config.toString("dockerOrganization"); + if(dockerOrganization == null || dockerOrganization.length() == 0) dockerOrganization = "networknt"; transfer(targetPath, "", "pom.xml", templates.graphql.pom.template(config)); // There is only one port that should be exposed in Dockerfile, otherwise, the service @@ -58,6 +56,7 @@ public void generate(String targetPath, Object model, Any config) throws IOExcep } transfer(targetPath, "docker", "Dockerfile", templates.graphql.dockerfile.template(config, expose)); transfer(targetPath, "docker", "Dockerfile-Redhat", templates.graphql.dockerfileredhat.template(config, expose)); + transfer(targetPath, "", "build.sh", templates.graphql.buildSh.template(dockerOrganization, config.get("groupId") + "." + config.get("artifactId") + "-" + config.get("version"))); transfer(targetPath, "", ".gitignore", templates.graphql.gitignore.template()); transfer(targetPath, "", "README.md", templates.graphql.README.template()); transfer(targetPath, "", "LICENSE", templates.graphql.LICENSE.template()); diff --git a/light-graphql-4j/src/main/resources/templates/graphql/buildSh.rocker.raw b/light-graphql-4j/src/main/resources/templates/graphql/buildSh.rocker.raw new file mode 100644 index 000000000..2792d37a7 --- /dev/null +++ b/light-graphql-4j/src/main/resources/templates/graphql/buildSh.rocker.raw @@ -0,0 +1,52 @@ +@args (String org, String serviceId) +#!/bin/bash + +set -ex + +VERSION=$1 +IMAGE_NAME="@org/@serviceId" + +showHelp() { + echo " " + echo "Error: $1" + echo " " + echo " build.sh [VERSION]" + echo " " + echo " where [VERSION] version of the docker image that you want to publish (example: 0.0.1)" + echo " " + echo " example: ./build.sh 0.0.1" + echo " " +} + +build() { + echo "Building ..." + mvn clean install + echo "Successfully built!" +} + +cleanup() { + if [[ "$(docker images -q $IMAGE_NAME 2> /dev/null)" != "" ]]; then + echo "Removing old $IMAGE_NAME images" + docker images | grep $IMAGE_NAME | awk '{print $3}' | xargs docker rmi -f + echo "Cleanup completed!" + fi +} + +publish() { + echo "Building Docker image with version $VERSION" + docker build -t $IMAGE_NAME:$VERSION -t $IMAGE_NAME:latest -f ./docker/Dockerfile . --no-cache=true + docker build -t $IMAGE_NAME:$VERSION-redhat -f ./docker/Dockerfile-Redhat . --no-cache=true + echo "Images built with version $VERSION" + echo "Pushing image to DockerHub" + docker push $IMAGE_NAME + echo "Image successfully published!" +} + +if [ -z $VERSION ]; then + showHelp "[VERSION] parameter is missing" + exit +fi + +build; +cleanup; +publish; diff --git a/light-graphql-4j/src/test/resources/config.json b/light-graphql-4j/src/test/resources/config.json index c297fbef4..7910c4d7d 100644 --- a/light-graphql-4j/src/test/resources/config.json +++ b/light-graphql-4j/src/test/resources/config.json @@ -20,5 +20,6 @@ "password": "my-secret-pw" }, "supportH2ForTest": false, - "supportClient": false + "supportClient": false, + "dockerOrganization": "networknt" } diff --git a/light-hybrid-4j/src/main/java/com/networknt/codegen/hybrid/HybridServerGenerator.java b/light-hybrid-4j/src/main/java/com/networknt/codegen/hybrid/HybridServerGenerator.java index 2ba4a21ec..c5c4649df 100644 --- a/light-hybrid-4j/src/main/java/com/networknt/codegen/hybrid/HybridServerGenerator.java +++ b/light-hybrid-4j/src/main/java/com/networknt/codegen/hybrid/HybridServerGenerator.java @@ -36,10 +36,8 @@ public void generate(String targetPath, Object model, Any config) throws IOExcep boolean enableHttps = config.toBoolean("enableHttps"); String httpsPort = config.toString("httpsPort"); boolean enableRegistry = config.toBoolean("enableRegistry"); - boolean supportOracle = config.toBoolean("supportOracle"); - boolean supportMysql = config.toBoolean("supportMysql"); - boolean supportPostgresql = config.toBoolean("supportPostgresql"); - boolean supportH2ForTest = config.toBoolean("supportH2ForTest"); + String dockerOrganization = config.toString("dockerOrganization"); + if(dockerOrganization == null || dockerOrganization.length() == 0) dockerOrganization = "networknt"; boolean supportClient = config.toBoolean("supportClient"); @@ -55,6 +53,7 @@ public void generate(String targetPath, Object model, Any config) throws IOExcep } transfer(targetPath, "docker", "Dockerfile", templates.hybrid.server.dockerfile.template(config, expose)); transfer(targetPath, "docker", "Dockerfile-Redhat", templates.hybrid.server.dockerfileredhat.template(config, expose)); + transfer(targetPath, "", "build.sh", templates.hybrid.server.buildSh.template(dockerOrganization, config.get("groupId") + "." + config.get("artifactId") + "-" + config.get("version"))); transfer(targetPath, "", ".gitignore", templates.hybrid.gitignore.template()); transfer(targetPath, "", "README.md", templates.hybrid.server.README.template()); transfer(targetPath, "", "LICENSE", templates.hybrid.LICENSE.template()); diff --git a/light-hybrid-4j/src/main/java/com/networknt/codegen/hybrid/HybridServiceGenerator.java b/light-hybrid-4j/src/main/java/com/networknt/codegen/hybrid/HybridServiceGenerator.java index 770768981..6a03d2872 100644 --- a/light-hybrid-4j/src/main/java/com/networknt/codegen/hybrid/HybridServiceGenerator.java +++ b/light-hybrid-4j/src/main/java/com/networknt/codegen/hybrid/HybridServiceGenerator.java @@ -34,20 +34,12 @@ public String getFramework() { @Override public void generate(String targetPath, Object model, Any config) throws IOException { // whoever is calling this needs to make sure that model is converted to Map - String rootPackage = config.get("rootPackage").toString(); - String modelPackage = config.get("modelPackage").toString(); String handlerPackage = config.get("handlerPackage").toString(); boolean overwriteHandler = config.toBoolean("overwriteHandler"); boolean overwriteHandlerTest = config.toBoolean("overwriteHandlerTest"); boolean enableHttp = config.toBoolean("enableHttp"); - String httpPort = config.toString("httpPort"); boolean enableHttps = config.toBoolean("enableHttps"); - String httpsPort = config.toString("httpsPort"); boolean enableRegistry = config.toBoolean("enableRegistry"); - boolean supportOracle = config.toBoolean("supportOracle"); - boolean supportMysql = config.toBoolean("supportMysql"); - boolean supportPostgresql = config.toBoolean("supportPostgresql"); - boolean supportH2ForTest = config.toBoolean("supportH2ForTest"); boolean supportClient = config.toBoolean("supportClient"); transfer(targetPath, "", "pom.xml", templates.hybrid.service.pom.template(config)); diff --git a/light-hybrid-4j/src/main/resources/templates/hybrid/server/buildSh.rocker.raw b/light-hybrid-4j/src/main/resources/templates/hybrid/server/buildSh.rocker.raw new file mode 100644 index 000000000..2792d37a7 --- /dev/null +++ b/light-hybrid-4j/src/main/resources/templates/hybrid/server/buildSh.rocker.raw @@ -0,0 +1,52 @@ +@args (String org, String serviceId) +#!/bin/bash + +set -ex + +VERSION=$1 +IMAGE_NAME="@org/@serviceId" + +showHelp() { + echo " " + echo "Error: $1" + echo " " + echo " build.sh [VERSION]" + echo " " + echo " where [VERSION] version of the docker image that you want to publish (example: 0.0.1)" + echo " " + echo " example: ./build.sh 0.0.1" + echo " " +} + +build() { + echo "Building ..." + mvn clean install + echo "Successfully built!" +} + +cleanup() { + if [[ "$(docker images -q $IMAGE_NAME 2> /dev/null)" != "" ]]; then + echo "Removing old $IMAGE_NAME images" + docker images | grep $IMAGE_NAME | awk '{print $3}' | xargs docker rmi -f + echo "Cleanup completed!" + fi +} + +publish() { + echo "Building Docker image with version $VERSION" + docker build -t $IMAGE_NAME:$VERSION -t $IMAGE_NAME:latest -f ./docker/Dockerfile . --no-cache=true + docker build -t $IMAGE_NAME:$VERSION-redhat -f ./docker/Dockerfile-Redhat . --no-cache=true + echo "Images built with version $VERSION" + echo "Pushing image to DockerHub" + docker push $IMAGE_NAME + echo "Image successfully published!" +} + +if [ -z $VERSION ]; then + showHelp "[VERSION] parameter is missing" + exit +fi + +build; +cleanup; +publish; diff --git a/light-hybrid-4j/src/test/resources/serverConfig.json b/light-hybrid-4j/src/test/resources/serverConfig.json index 9c433945e..e9adc11e6 100644 --- a/light-hybrid-4j/src/test/resources/serverConfig.json +++ b/light-hybrid-4j/src/test/resources/serverConfig.json @@ -20,5 +20,6 @@ "password": "my-secret-pw" }, "supportH2ForTest": false, - "supportClient": false + "supportClient": false, + "dockerOrganization": "networknt" } diff --git a/light-rest-4j/src/main/java/com/networknt/codegen/rest/OpenApiGenerator.java b/light-rest-4j/src/main/java/com/networknt/codegen/rest/OpenApiGenerator.java index 0d9383a35..5afceef5c 100644 --- a/light-rest-4j/src/main/java/com/networknt/codegen/rest/OpenApiGenerator.java +++ b/light-rest-4j/src/main/java/com/networknt/codegen/rest/OpenApiGenerator.java @@ -84,6 +84,8 @@ public void generate(String targetPath, Object model, Any config) throws IOExcep String httpsPort = config.toString("httpsPort"); boolean enableRegistry = config.toBoolean("enableRegistry"); boolean supportClient = config.toBoolean("supportClient"); + String dockerOrganization = config.toString("dockerOrganization"); + if(dockerOrganization == null || dockerOrganization.length() == 0) dockerOrganization = "networknt"; transfer(targetPath, "", "pom.xml", templates.rest.openapi.pom.template(config)); // There is only one port that should be exposed in Dockerfile, otherwise, the service @@ -96,6 +98,7 @@ public void generate(String targetPath, Object model, Any config) throws IOExcep } transfer(targetPath, "docker", "Dockerfile", templates.rest.dockerfile.template(config, expose)); transfer(targetPath, "docker", "Dockerfile-Redhat", templates.rest.dockerfileredhat.template(config, expose)); + transfer(targetPath, "", "build.sh", templates.rest.buildSh.template(dockerOrganization, config.get("groupId") + "." + config.get("artifactId") + "-" + config.get("version"))); transfer(targetPath, "", ".gitignore", templates.rest.gitignore.template()); transfer(targetPath, "", "README.md", templates.rest.README.template()); transfer(targetPath, "", "LICENSE", templates.rest.LICENSE.template()); diff --git a/light-rest-4j/src/main/java/com/networknt/codegen/rest/SwaggerGenerator.java b/light-rest-4j/src/main/java/com/networknt/codegen/rest/SwaggerGenerator.java index ba59e8d8e..ba049658a 100644 --- a/light-rest-4j/src/main/java/com/networknt/codegen/rest/SwaggerGenerator.java +++ b/light-rest-4j/src/main/java/com/networknt/codegen/rest/SwaggerGenerator.java @@ -82,6 +82,8 @@ public void generate(String targetPath, Object model, Any config) throws IOExcep String httpsPort = config.toString("httpsPort"); boolean enableRegistry = config.toBoolean("enableRegistry"); boolean supportClient = config.toBoolean("supportClient"); + String dockerOrganization = config.toString("dockerOrganization"); + if(dockerOrganization == null || dockerOrganization.length() == 0) dockerOrganization = "networknt"; transfer(targetPath, "", "pom.xml", templates.rest.swagger.pom.template(config)); // There is only one port that should be exposed in Dockerfile, otherwise, the service @@ -95,6 +97,7 @@ public void generate(String targetPath, Object model, Any config) throws IOExcep transfer(targetPath, "docker", "Dockerfile", templates.rest.dockerfile.template(config, expose)); transfer(targetPath, "docker", "Dockerfile-Redhat", templates.rest.dockerfileredhat.template(config, expose)); + transfer(targetPath, "", "build.sh", templates.rest.buildSh.template(dockerOrganization, config.get("groupId") + "." + config.get("artifactId") + "-" + config.get("version"))); transfer(targetPath, "", ".gitignore", templates.rest.gitignore.template()); transfer(targetPath, "", "README.md", templates.rest.README.template()); transfer(targetPath, "", "LICENSE", templates.rest.LICENSE.template()); diff --git a/light-rest-4j/src/main/resources/templates/rest/buildSh.rocker.raw b/light-rest-4j/src/main/resources/templates/rest/buildSh.rocker.raw new file mode 100644 index 000000000..2792d37a7 --- /dev/null +++ b/light-rest-4j/src/main/resources/templates/rest/buildSh.rocker.raw @@ -0,0 +1,52 @@ +@args (String org, String serviceId) +#!/bin/bash + +set -ex + +VERSION=$1 +IMAGE_NAME="@org/@serviceId" + +showHelp() { + echo " " + echo "Error: $1" + echo " " + echo " build.sh [VERSION]" + echo " " + echo " where [VERSION] version of the docker image that you want to publish (example: 0.0.1)" + echo " " + echo " example: ./build.sh 0.0.1" + echo " " +} + +build() { + echo "Building ..." + mvn clean install + echo "Successfully built!" +} + +cleanup() { + if [[ "$(docker images -q $IMAGE_NAME 2> /dev/null)" != "" ]]; then + echo "Removing old $IMAGE_NAME images" + docker images | grep $IMAGE_NAME | awk '{print $3}' | xargs docker rmi -f + echo "Cleanup completed!" + fi +} + +publish() { + echo "Building Docker image with version $VERSION" + docker build -t $IMAGE_NAME:$VERSION -t $IMAGE_NAME:latest -f ./docker/Dockerfile . --no-cache=true + docker build -t $IMAGE_NAME:$VERSION-redhat -f ./docker/Dockerfile-Redhat . --no-cache=true + echo "Images built with version $VERSION" + echo "Pushing image to DockerHub" + docker push $IMAGE_NAME + echo "Image successfully published!" +} + +if [ -z $VERSION ]; then + showHelp "[VERSION] parameter is missing" + exit +fi + +build; +cleanup; +publish; diff --git a/light-rest-4j/src/test/resources/config.json b/light-rest-4j/src/test/resources/config.json index 89e8375e9..89e742956 100644 --- a/light-rest-4j/src/test/resources/config.json +++ b/light-rest-4j/src/test/resources/config.json @@ -23,5 +23,6 @@ "password": "my-secret-pw" }, "supportH2ForTest": false, - "supportClient": false + "supportClient": false, + "dockerOrganization": "networknt" }