Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Commit

Permalink
Release/0.0.4 (#2)
Browse files Browse the repository at this point in the history
* updates

* Fixing OCP_PIPELINE_LIB_DIR

* Enable cleanup script

* Adding ignore for system labels

* Adding pods deletion
  • Loading branch information
cvarjao authored Nov 15, 2018
1 parent 822d577 commit 78bf4bd
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 13 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/main/groovy/ca/bc/gov/devops/BasicClean.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
4 changes: 2 additions & 2 deletions src/main/groovy/ca/bc/gov/devops/OpenShiftBuildHelper.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
Expand Down
22 changes: 21 additions & 1 deletion src/main/groovy/ca/bc/gov/devops/OpenShiftHelper.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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?:[:]
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion src/main/groovy/example/cleanup.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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!!'
10 changes: 6 additions & 4 deletions src/main/resources/pipeline-cli
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 78bf4bd

Please sign in to comment.