Improved implementation of pretty printing.
Change-Id: Iac4b6629a384bfe55ed2983ec00ab8b1ba9f54e1
diff --git a/src/com/mirantis/mk/Common.groovy b/src/com/mirantis/mk/Common.groovy
index ee6559e..ebc80c1 100644
--- a/src/com/mirantis/mk/Common.groovy
+++ b/src/com/mirantis/mk/Common.groovy
@@ -1,7 +1,7 @@
package com.mirantis.mk
-
import static groovy.json.JsonOutput.prettyPrint
import static groovy.json.JsonOutput.toJson
+import com.cloudbees.groovy.cps.NonCPS
import groovy.json.JsonSlurperClassic
/**
*
@@ -90,12 +90,20 @@
}
/**
+ * Print pretty-printed string representation of given item
+ * @param item item to be pretty-printed (list, map, whatever)
+ */
+def prettyPrint(item){
+ println prettify(item)
+}
+
+/**
* Return pretty-printed string representation of given item
* @param item item to be pretty-printed (list, map, whatever)
* @return pretty-printed string
*/
-def prettyPrint(item){
- return prettyPrint(toJson(item)).replace('\\n', System.getProperty('line.separator'))
+def prettify(item){
+ return groovy.json.JsonOutput.prettyPrint(toJson(item)).replace('\\n', System.getProperty('line.separator'))
}
/**
diff --git a/src/com/mirantis/mk/Orchestrate.groovy b/src/com/mirantis/mk/Orchestrate.groovy
index 77e3eaf..4e16b83 100644
--- a/src/com/mirantis/mk/Orchestrate.groovy
+++ b/src/com/mirantis/mk/Orchestrate.groovy
@@ -388,7 +388,7 @@
//vip=$(salt-call pillar.data _param:stacklight_monitor_address --out key|grep _param: |awk '{print $2}')
//vip=${vip:=172.16.10.253}
def pillar = salt.getPillar(master, 'ctl01*', '_param:stacklight_monitor_address')
- print(common.prettyPrint(pillar))
+ common.prettyPrint(pillar)
def stacklight_vip = pillar['return'][0].values()[0]
if (stacklight_vip) {
diff --git a/src/com/mirantis/mk/Salt.groovy b/src/com/mirantis/mk/Salt.groovy
index ad77e2b..75349a4 100644
--- a/src/com/mirantis/mk/Salt.groovy
+++ b/src/com/mirantis/mk/Salt.groovy
@@ -1,5 +1,6 @@
package com.mirantis.mk
+import com.cloudbees.groovy.cps.NonCPS
import java.util.stream.Collectors
/**
* Salt functions
@@ -333,22 +334,22 @@
}
if(!resource["result"] || (resource["result"] instanceof String && resource["result"] != "true")){
if(resource["result"] != null){
- outputResources.add(String.format("Resource: %s\n\u001B[31m%s\u001B[0m", resKey, common.prettyPrint(resource)))
+ outputResources.add(String.format("Resource: %s\n\u001B[31m%s\u001B[0m", resKey, common.prettify(resource)))
}else{
- outputResources.add(String.format("Resource: %s\n\u001B[33m%s\u001B[0m", resKey, common.prettyPrint(resource)))
+ outputResources.add(String.format("Resource: %s\n\u001B[33m%s\u001B[0m", resKey, common.prettify(resource)))
}
}else{
if(!printOnlyChanges || resource.changes.size() > 0){
- outputResources.add(String.format("Resource: %s\n\u001B[32m%s\u001B[0m", resKey, common.prettyPrint(resource)))
+ outputResources.add(String.format("Resource: %s\n\u001B[32m%s\u001B[0m", resKey, common.prettify(resource)))
}
}
}else{
- outputResources.add(String.format("Resource: %s\n\u001B[36m%s\u001B[0m", resKey, common.prettyPrint(resource)))
+ outputResources.add(String.format("Resource: %s\n\u001B[36m%s\u001B[0m", resKey, common.prettify(resource)))
}
}
common.debugMsg("checkResult: checking resource: ${resource}")
if(resource instanceof String || (resource["result"] != null && !resource["result"]) || (resource["result"] instanceof String && resource["result"] == "false")){
- def prettyResource = common.prettyPrint(resource)
+ def prettyResource = common.prettify(resource)
if(env["ASK_ON_ERROR"] && env["ASK_ON_ERROR"] == "true"){
timeout(time:1, unit:'HOURS') {
input message: "False result on ${nodeKey} found, resource ${prettyResource}. \nDo you want to continue?"
@@ -365,7 +366,7 @@
}
}
}else if(node!=null && node!=""){
- outputResources.add(String.format("Resource: %s\n\u001B[36m%s\u001B[0m", resKey, common.prettyPrint(node)))
+ outputResources.add(String.format("Resource: %s\n\u001B[36m%s\u001B[0m", resKey, common.prettify(node)))
}
if(printResults && !outputResources.isEmpty()){
wrap([$class: 'AnsiColorBuildWrapper']) {
@@ -397,7 +398,7 @@
common.debugMsg("printSaltCommandResult: printing salt command entry: ${entry}")
def nodeKey = entry.keySet()[j]
def node=entry[nodeKey]
- common.infoMsg(String.format("Node %s changes:\n%s",nodeKey, common.prettyPrint(node)))
+ common.infoMsg(String.format("Node %s changes:\n%s",nodeKey, common.prettify(node)))
}
}
}else{