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

fixed all parsing errors

parent 3882fb2b
......@@ -597,7 +597,7 @@ public class DeployService {
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();
for (MessageParameter param : params) {
String jsonResp = param.getValue().replaceAll("^\"|\"$", "");
......
......@@ -82,52 +82,61 @@ public class Converter {
return jsonObject.toString();
}
public static Map<String, Object> jsonString2Map(String jsonString) throws JSONException {
JSONObject jsonObject = new JSONObject(jsonString);
return jsonObject2Map(jsonObject);
public static Map<String, Object> jsonString2Map(String jsonString) throws JSONException, IOException {
// JSONObject jsonObject = new JSONObject(jsonString);
// 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);
return jsonArray2List(jSONArray);
}
public static Map<String, Object> jsonObject2Map(JSONObject object) throws JSONException {
Map<String, Object> map = new HashMap();
Iterator<String> keysItr = object.keys();
while (keysItr.hasNext()) {
String key = keysItr.next();
Object value = object.get(key);
if (value instanceof JSONArray) {
value = jsonArray2List((JSONArray) value);
} else if (value instanceof JSONObject) {
value = jsonObject2Map((JSONObject) value);
} else if (value.equals("[]")) {
value = new ArrayList();
} else if (value.getClass().getName().equals("org.json.JSONObject$Null")) {
value = new String();
}
map.put(key, value);
}
return map;
public static Map<String, Object> jsonObject2Map(JSONObject object) throws JSONException, IOException {
return new ObjectMapper().readValue(object.toString(), HashMap.class);
// Map<String, Object> map = new HashMap();
//
// Iterator<String> keysItr = object.keys();
// while (keysItr.hasNext()) {
// String key = keysItr.next();
// Object value = object.get(key);
//
// if (value instanceof JSONArray) {
// value = jsonArray2List((JSONArray) value);
// } else if (value instanceof JSONObject) {
// value = new ObjectMapper().readValue(((JSONObject) value).toString(), HashMap.class);
// } else if (value.equals("[]")) {
// value = new ArrayList();
// } else if (value.getClass().getName().equals("org.json.JSONObject$Null")) {
// value = new String();
// }
// map.put(key, value);
// }
// 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();
for (int i = 0; i < array.length(); i++) {
Object value = array.get(i);
if (value instanceof JSONArray) {
value = jsonArray2List((JSONArray) value);
} else if (value instanceof JSONObject) {
value = jsonObject2Map((JSONObject) value);
}
if (value instanceof String) {
System.err.println(value);
if (array != null) {
for (int i = 0; i < array.length(); i++) {
list.add(array.getString(i));
}
list.add(value);
}
// for (int i = 0; i < array.length(); i++) {
// Object value = array.get(i);
// if (value instanceof JSONArray) {
// value = jsonArray2List((JSONArray) value);
// } else if (value instanceof JSONObject) {
// value = jsonObject2Map((JSONObject) value);
// }
// if (value instanceof String) {
// System.err.println(value);
// }
// list.add(value);
// }
return list;
}
......
......@@ -23,6 +23,7 @@ import json
import logging
import linecache
import sys
import ast
import re
logger = logging.getLogger(__name__)
......@@ -56,7 +57,10 @@ def docker_check(vm, compose_name):
for i in node_ls_resp:
line = get_resp_line(i)
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
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):
for i in stack_ps_resp:
line = get_resp_line(i)
if line:
json_dict = {}
json_dict = json.loads(line)
json_dict = json.loads(json.dumps(json_dict))
if not isinstance(json_dict, dict):
......@@ -89,26 +94,34 @@ def docker_check(vm, compose_name):
for i in stack_resp:
line = get_resp_line(i)
if line:
json_dict = {}
json_dict = json.loads(line)
if not isinstance(json_dict, dict):
json_dict = json.loads(json_dict)
stack_info.append(json_dict)
json_response ['stack_info'] = stack_info
cmd = 'sudo docker node inspect '
for hostname in nodes_hostname:
cmd += hostname
cmd += ' '+hostname
stdin, stdout, stderr = ssh.exec_command(cmd)
inspect_resp = stdout.readlines()
response_str = ""
for i in inspect_resp:
line = get_resp_line(i)
line = i.rstrip("\n\r").encode()
if line:
response_str+=line
json_dict = json.loads(response_str.rstrip("\n\r").strip().encode('string_escape'))
json_dict = {}
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
logger.info("Finished docker info services on: "+vm.ip)
except Exception as e:
exc_type, exc_obj, tb = sys.exc_info()
......
......@@ -168,9 +168,9 @@ def on_request(ch, method, props, body):
par["value"] = ret
par["attributes"] = "null"
response["parameters"].append(par)
response = json.dumps(response)
logger.info("Response: " + response)
#logger.info("Response: " + response)
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