From 78bf4bd0d271fee8d3bdf597b4fcb737347fcaf1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9cio=20Varj=C3=A3o?= <1348549+cvarjao@users.noreply.github.com> Date: Wed, 14 Nov 2018 16:01:27 -0800 Subject: [PATCH] Release/0.0.4 (#2) * updates * Fixing OCP_PIPELINE_LIB_DIR * Enable cleanup script * Adding ignore for system labels * Adding pods deletion --- README.md | 12 +++++++--- .../groovy/ca/bc/gov/devops/BasicClean.groovy | 2 +- .../bc/gov/devops/OpenShiftBuildHelper.groovy | 4 ++-- .../devops/OpenShiftDeploymentHelper.groovy | 2 +- .../ca/bc/gov/devops/OpenShiftHelper.groovy | 22 ++++++++++++++++++- src/main/groovy/example/cleanup.groovy | 2 +- src/main/resources/pipeline-cli | 10 +++++---- 7 files changed, 41 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 94f4854..c28eae9 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # ocp-cd-pipeline -This is a groovy library (provided as jar file) for helping orchestrating the creation of OpenShift objects required for building and deploying applications. +This is a groovy library/utility (provided as jar file) for helping orchestrating the creation of OpenShift objects required for building and deploying applications. # Motivation @@ -10,7 +10,7 @@ The [oc](https://github.com/openshift/origin/blob/master/docs/cli.md) command-li Developers are encouraged to create [templates](https://docs.okd.io/latest/dev_guide/templates.html) and use the template to create/update objects by using the oc command-line. However, we have observed that those templates are not kept up-to-date, and it is not consistently and continuously tested. -In the spirit of [Infrastructue as Code](https://en.wikipedia.org/wiki/Infrastructure_as_Code), and [Continuous Delivery](https://en.wikipedia.org/wiki/Continuous_delivery) Practices, we wanted to (1) test the template as often and as thoroughly as possible, (2) be able to spin up a new environment as quick and easy as possible, and (3) doing so consistently as part of a [deployment pipeline](https://en.wikipedia.org/wiki/Continuous_delivery#Deployment_pipeline). +In the spirit of [Infrastructue as Code](https://en.wikipedia.org/wiki/Infrastructure_as_Code), and [Continuous Delivery](https://en.wikipedia.org/wiki/Continuous_delivery) Practices, we wanted to (1) test the template as often and as thoroughly as possible, (2) be able to spin up a new environment as quickly and easily as possible, and (3) doing so consistently as part of a [deployment pipeline](https://en.wikipedia.org/wiki/Continuous_delivery#Deployment_pipeline). ## Bye Jenkins Shared Library @@ -54,7 +54,13 @@ Create a config file using Groovy [ConfigSlurper](http://docs.groovy-lang.org/la ## Build ``` -groovy -cp src/main/groovy/ src/main/groovy/example/build.groovy --config= +# MY_APP_DIR +# OCP_CD_PIPELINE_DIR + +cd $MY_APP_DIR +groovy -cp target/ocp-pipeline-0.0.4.jar 'jar:file:target/ocp-pipeline-0.0.4.jar!/example/build.groovy' + +groovy -cp $OCP_CD_PIPELINE_DIR/src/main/groovy/ $OCP_CD_PIPELINE_DIR/src/main/groovy/example/build.groovy --config=openshift/config.groovy ``` # Build diff --git a/src/main/groovy/ca/bc/gov/devops/BasicClean.groovy b/src/main/groovy/ca/bc/gov/devops/BasicClean.groovy index 48ad766..52d1d99 100644 --- a/src/main/groovy/ca/bc/gov/devops/BasicClean.groovy +++ b/src/main/groovy/ca/bc/gov/devops/BasicClean.groovy @@ -59,7 +59,7 @@ abstract class BasicClean extends Script { appVersion=deploymentConfig.app.build.version } - println OpenShiftHelper.oc(['delete','builds,bc,dc,rc,svc,routes,secret,pvc,ServiceAccount,RoleBinding', '-l', "app=${appId},!image-stream.name", '-n', appNamespace]).out.toString().trim() + println OpenShiftHelper.oc(['delete','build,bc,dc,rc,svc,routes,pods,secret,pvc,ServiceAccount,RoleBinding', '-l', "app=${appId},!image-stream.name,!shared,!template,!system", '-n', appNamespace]).out.toString().trim() Map ret = OpenShiftHelper.ocGet(['is', '-l', "app-name=${appName}", '-n', appNamespace]) for(Map imageStream in ret.items){ OpenShiftHelper.oc(['delete', 'istag', "${imageStream.metadata.name}:${appVersion}", '--ignore-not-found=true', '-n',appNamespace]) diff --git a/src/main/groovy/ca/bc/gov/devops/OpenShiftBuildHelper.groovy b/src/main/groovy/ca/bc/gov/devops/OpenShiftBuildHelper.groovy index a57dfdf..fc9344f 100644 --- a/src/main/groovy/ca/bc/gov/devops/OpenShiftBuildHelper.groovy +++ b/src/main/groovy/ca/bc/gov/devops/OpenShiftBuildHelper.groovy @@ -406,12 +406,12 @@ class OpenShiftBuildHelper extends OpenShiftHelper{ if ('Cancelled' == item.phase) continue if ('Failed' == item.phase) continue - //println "Processing - ${OpenShiftHelper.guid(object)} status:${item.phase} attempts:${item.attempts?:0}" + println "Processing - ${OpenShiftHelper.guid(object)} status:${item.phase} attempts:${item.attempts?:0}" pendingItems++ boolean dependenciesMet=true for (Map dependency:item.dependencies){ - //println " > ${OpenShiftHelper.guid(dependency.object)} - status:${dependency.phase}" + println " > ${OpenShiftHelper.guid(dependency.object)} - status:${dependency.phase}" if ('Complete' != dependency.phase){ dependenciesMet = false } diff --git a/src/main/groovy/ca/bc/gov/devops/OpenShiftDeploymentHelper.groovy b/src/main/groovy/ca/bc/gov/devops/OpenShiftDeploymentHelper.groovy index b75fd8b..dd53daa 100644 --- a/src/main/groovy/ca/bc/gov/devops/OpenShiftDeploymentHelper.groovy +++ b/src/main/groovy/ca/bc/gov/devops/OpenShiftDeploymentHelper.groovy @@ -79,7 +79,7 @@ class OpenShiftDeploymentHelper extends OpenShiftHelper{ if (deployImageStreamTag == null){ //Creating ImageStreamTag oc(['tag', "${config.app.build.namespace}/${buildImageStreamTagName}", "${object.metadata.namespace}/${deploymageStreamTagName}", '-n', object.metadata.namespace]) - }else if (buildImageStreamTag.image.metadata.name != deployImageStreamTag.image.metadata.name ){ + }else if (buildImageStreamTag!=null && buildImageStreamTag.image.metadata.name != deployImageStreamTag.image.metadata.name ){ //Updating ImageStreamTag oc(['tag', "${config.app.build.namespace}/${buildImageStreamTagName}", "${object.metadata.namespace}/${deploymageStreamTagName}", '-n', object.metadata.namespace]) } diff --git a/src/main/groovy/ca/bc/gov/devops/OpenShiftHelper.groovy b/src/main/groovy/ca/bc/gov/devops/OpenShiftHelper.groovy index ddc971e..8762144 100644 --- a/src/main/groovy/ca/bc/gov/devops/OpenShiftHelper.groovy +++ b/src/main/groovy/ca/bc/gov/devops/OpenShiftHelper.groovy @@ -66,7 +66,7 @@ class OpenShiftHelper{ if (getVerboseLevel() >= 2) println template.file //Load Template - Map templateObject = new groovy.json.JsonSlurper().parseFile(new File(template.file), 'UTF-8') + Map templateObject = loadTemplateAsJson(template.file) //Normalize template and calculate hash templateObject.objects.each { Map it -> it.metadata.labels=it.metadata.labels?:[:] @@ -294,6 +294,26 @@ class OpenShiftHelper{ return ret } + public static Map loadTemplateAsJson(String file){ + List _args = ['oc', 'create', '-f', file, '--dry-run', '-o', 'json'] + + Map ret = _exec(_args, new StringBuffer(), new StringBuffer()) + + if (ret.status !=0){ + throw new RuntimeException("oc returned an error code: ${ret}") + } + + if (ret.out!=null && ret.out.length() > 0){ + + Map template= toJson(ret.out) + template.metadata.remove('namespace') + //println template + return template + } + + return null + } + public static Map ocApply(List items, List args){ List _args = ['oc', 'apply', '-f', '-'] + args String json=new groovy.json.JsonBuilder(['kind':'List', 'apiVersion':'v1', 'items':items]).toPrettyString() diff --git a/src/main/groovy/example/cleanup.groovy b/src/main/groovy/example/cleanup.groovy index cc19379..ca82c02 100644 --- a/src/main/groovy/example/cleanup.groovy +++ b/src/main/groovy/example/cleanup.groovy @@ -7,7 +7,7 @@ import ca.bc.gov.devops.OpenShiftBuildHelper @groovy.transform.SourceURI URI sourceURI -//runScript(sourceURI) +runScript(sourceURI) println 'Work in progress!!' println 'Done!!' diff --git a/src/main/resources/pipeline-cli b/src/main/resources/pipeline-cli index 6f6e991..f7fdaa9 100644 --- a/src/main/resources/pipeline-cli +++ b/src/main/resources/pipeline-cli @@ -128,11 +128,13 @@ if [ ! -d "${OCP_PIPELINE_DIR}" ] ; then unzip -oq "${OCP_PIPELINE_DOWNLOAD_FILE}" -d "${OCP_PIPELINE_DIR}" fi +if [[ -z "${OCP_PIPELINE_LIB_DIR}" ]]; then + OCP_PIPELINE_LIB_DIR="${OCP_PIPELINE_DIR}/ocp-cd-pipeline-${OCP_PIPELINE_VERSION}/src/main/groovy" +fi -OCP_PIPELINE_LIB_DIR="${OCP_PIPELINE_DIR}/ocp-cd-pipeline-${OCP_PIPELINE_VERSION}/src/main/groovy" - -#echo "OCP_PIPELINE_VERSION=${OCP_PIPELINE_VERSION}" -#echo "OCP_PIPELINE_DIR=${OCP_PIPELINE_DIR}" +echo "OCP_PIPELINE_VERSION=${OCP_PIPELINE_VERSION}" +echo "OCP_PIPELINE_DIR=${OCP_PIPELINE_DIR}" +echo "OCP_PIPELINE_LIB_DIR=${OCP_PIPELINE_LIB_DIR}" PIPELINE_STAGE="$1"; shift