Skip to content

Instantly share code, notes, and snippets.

@welshstew
Created September 25, 2016 08:27
Show Gist options
  • Save welshstew/8cf0ef53903a0360c7d4fa050e46d83c to your computer and use it in GitHub Desktop.
Save welshstew/8cf0ef53903a0360c7d4fa050e46d83c to your computer and use it in GitHub Desktop.
simple bit of groovy to show how to execute an "oc patch"
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
//Simple example to show patching a container with new environment variables - need to have oc login before... :)
def dcToUpdate = "kie-app-postgresql"
def newEnvVars = [[name:"GROOVY_ROCKS", value: "true"], [name: "LOVE_OPENSHIFT", value: "of course"]]
def currentDC = new JsonSlurper().parseText(["oc", "get" , "dc" , dcToUpdate ,"-o" ,"json"].execute().text)
def containers = currentDC.spec.template.spec.containers
//add new env vars to container definition
newEnvVars.each { containers[0].env.add(it) }
def patcher = [spec:[template:[spec:["containers":containers]]]]
//generate patch command
def patchCmd = ["oc", "patch", "dc", dcToUpdate, "-p", JsonOutput.toJson(patcher)]
println patchCmd.join(" ")
def patchProc = patchCmd.execute()
def patchProcStream = new StringBuffer();
//different execution style in case of err
patchProc.waitForProcessOutput(patchProcStream, System.err);
println(patchProcStream.toString());
@chrisbsmith
Copy link

Thanks! This was an immense help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment