Commit 3787c955 authored by Spiros Koulouzis's avatar Spiros Koulouzis

fixed all parsing errors

parent 3882fb2b
...@@ -597,7 +597,7 @@ public class DeployService { ...@@ -597,7 +597,7 @@ public class DeployService {
return newJa.toString(); return newJa.toString();
} }
private Map<String, Object> buildSwarmInfo(List<MessageParameter> params) throws JSONException { private Map<String, Object> buildSwarmInfo(List<MessageParameter> params) throws JSONException, IOException {
Map<String, Object> info = new HashMap(); Map<String, Object> info = new HashMap();
for (MessageParameter param : params) { for (MessageParameter param : params) {
String jsonResp = param.getValue().replaceAll("^\"|\"$", ""); String jsonResp = param.getValue().replaceAll("^\"|\"$", "");
......
...@@ -82,52 +82,61 @@ public class Converter { ...@@ -82,52 +82,61 @@ public class Converter {
return jsonObject.toString(); return jsonObject.toString();
} }
public static Map<String, Object> jsonString2Map(String jsonString) throws JSONException { public static Map<String, Object> jsonString2Map(String jsonString) throws JSONException, IOException {
JSONObject jsonObject = new JSONObject(jsonString); // JSONObject jsonObject = new JSONObject(jsonString);
return jsonObject2Map(jsonObject); // return jsonObject2Map(jsonObject);
Map<String, Object> result
= new ObjectMapper().readValue(jsonString, HashMap.class);
return result;
} }
public static List<Object> jsonString2List(String jsonString) throws JSONException { public static List<Object> jsonString2List(String jsonString) throws JSONException, IOException {
JSONArray jSONArray = new JSONArray(jsonString); JSONArray jSONArray = new JSONArray(jsonString);
return jsonArray2List(jSONArray); return jsonArray2List(jSONArray);
} }
public static Map<String, Object> jsonObject2Map(JSONObject object) throws JSONException { public static Map<String, Object> jsonObject2Map(JSONObject object) throws JSONException, IOException {
Map<String, Object> map = new HashMap(); return new ObjectMapper().readValue(object.toString(), HashMap.class);
// Map<String, Object> map = new HashMap();
Iterator<String> keysItr = object.keys(); //
while (keysItr.hasNext()) { // Iterator<String> keysItr = object.keys();
String key = keysItr.next(); // while (keysItr.hasNext()) {
Object value = object.get(key); // String key = keysItr.next();
// Object value = object.get(key);
if (value instanceof JSONArray) { //
value = jsonArray2List((JSONArray) value); // if (value instanceof JSONArray) {
} else if (value instanceof JSONObject) { // value = jsonArray2List((JSONArray) value);
value = jsonObject2Map((JSONObject) value); // } else if (value instanceof JSONObject) {
} else if (value.equals("[]")) { // value = new ObjectMapper().readValue(((JSONObject) value).toString(), HashMap.class);
value = new ArrayList(); // } else if (value.equals("[]")) {
} else if (value.getClass().getName().equals("org.json.JSONObject$Null")) { // value = new ArrayList();
value = new String(); // } else if (value.getClass().getName().equals("org.json.JSONObject$Null")) {
} // value = new String();
map.put(key, value); // }
} // map.put(key, value);
return map; // }
} // return map;
}
public static List<Object> jsonArray2List(JSONArray array) throws JSONException {
public static List<Object> jsonArray2List(JSONArray array) throws JSONException, IOException {
List<Object> list = new ArrayList(); List<Object> list = new ArrayList();
if (array != null) {
for (int i = 0; i < array.length(); i++) { for (int i = 0; i < array.length(); i++) {
Object value = array.get(i); list.add(array.getString(i));
if (value instanceof JSONArray) { }
value = jsonArray2List((JSONArray) value); }
} else if (value instanceof JSONObject) { // for (int i = 0; i < array.length(); i++) {
value = jsonObject2Map((JSONObject) value); // Object value = array.get(i);
} // if (value instanceof JSONArray) {
if (value instanceof String) { // value = jsonArray2List((JSONArray) value);
System.err.println(value); // } else if (value instanceof JSONObject) {
} // value = jsonObject2Map((JSONObject) value);
list.add(value); // }
} // if (value instanceof String) {
// System.err.println(value);
// }
// list.add(value);
// }
return list; return list;
} }
......
...@@ -23,6 +23,7 @@ import json ...@@ -23,6 +23,7 @@ import json
import logging import logging
import linecache import linecache
import sys import sys
import ast
import re import re
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
...@@ -56,7 +57,10 @@ def docker_check(vm, compose_name): ...@@ -56,7 +57,10 @@ def docker_check(vm, compose_name):
for i in node_ls_resp: for i in node_ls_resp:
line = get_resp_line(i) line = get_resp_line(i)
if line: if line:
cluster_node_info.append(json.loads(line)) node_info = json.loads(line)
if not isinstance(node_info, dict):
node_info = ast.literal_eval(node_info)
cluster_node_info.append(node_info)
json_response ['cluster_node_info'] = cluster_node_info json_response ['cluster_node_info'] = cluster_node_info
services_format = '\'{\"ID\":\"{{.ID}}\",\"name\":\"{{.Name}}\",\"image\":\"{{.Image}}\",\"node\":\"{{.Node}}\",\"desired_state\":\"{{.DesiredState}}\",\"current_state\":\"{{.CurrentState}}\",\"error\":\"{{.Error}}\",\"ports\":\"{{.Ports}}\"}\'' services_format = '\'{\"ID\":\"{{.ID}}\",\"name\":\"{{.Name}}\",\"image\":\"{{.Image}}\",\"node\":\"{{.Node}}\",\"desired_state\":\"{{.DesiredState}}\",\"current_state\":\"{{.CurrentState}}\",\"error\":\"{{.Error}}\",\"ports\":\"{{.Ports}}\"}\''
...@@ -68,6 +72,7 @@ def docker_check(vm, compose_name): ...@@ -68,6 +72,7 @@ def docker_check(vm, compose_name):
for i in stack_ps_resp: for i in stack_ps_resp:
line = get_resp_line(i) line = get_resp_line(i)
if line: if line:
json_dict = {}
json_dict = json.loads(line) json_dict = json.loads(line)
json_dict = json.loads(json.dumps(json_dict)) json_dict = json.loads(json.dumps(json_dict))
if not isinstance(json_dict, dict): if not isinstance(json_dict, dict):
...@@ -89,26 +94,34 @@ def docker_check(vm, compose_name): ...@@ -89,26 +94,34 @@ def docker_check(vm, compose_name):
for i in stack_resp: for i in stack_resp:
line = get_resp_line(i) line = get_resp_line(i)
if line: if line:
json_dict = {}
json_dict = json.loads(line) json_dict = json.loads(line)
if not isinstance(json_dict, dict):
json_dict = json.loads(json_dict)
stack_info.append(json_dict) stack_info.append(json_dict)
json_response ['stack_info'] = stack_info json_response ['stack_info'] = stack_info
cmd = 'sudo docker node inspect ' cmd = 'sudo docker node inspect '
for hostname in nodes_hostname: for hostname in nodes_hostname:
cmd += hostname cmd += ' '+hostname
stdin, stdout, stderr = ssh.exec_command(cmd) stdin, stdout, stderr = ssh.exec_command(cmd)
inspect_resp = stdout.readlines() inspect_resp = stdout.readlines()
response_str = "" response_str = ""
for i in inspect_resp: for i in inspect_resp:
line = get_resp_line(i) line = i.rstrip("\n\r").encode()
if line: if line:
response_str+=line response_str+=line
json_dict = {}
json_dict = json.loads(response_str.rstrip("\n\r").strip().encode('string_escape')) response_str = response_str.rstrip("\n\r").strip(' \t\n\r').strip().encode('string_escape')
print response_str
json_dict = json.loads(response_str)
json_response['nodes_info'] = json_dict json_response['nodes_info'] = json_dict
logger.info("Finished docker info services on: "+vm.ip) logger.info("Finished docker info services on: "+vm.ip)
except Exception as e: except Exception as e:
exc_type, exc_obj, tb = sys.exc_info() exc_type, exc_obj, tb = sys.exc_info()
......
...@@ -170,7 +170,7 @@ def on_request(ch, method, props, body): ...@@ -170,7 +170,7 @@ def on_request(ch, method, props, body):
response["parameters"].append(par) response["parameters"].append(par)
response = json.dumps(response) response = json.dumps(response)
logger.info("Response: " + response) #logger.info("Response: " + response)
ch.basic_publish(exchange='', ch.basic_publish(exchange='',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment