Commit b38bd2ce authored by Spiros Koulouzis's avatar Spiros Koulouzis

fixed encoding error

parent 4826746e
...@@ -601,8 +601,9 @@ public class DeployService { ...@@ -601,8 +601,9 @@ public class DeployService {
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("^\"|\"$", "");
System.err.println(jsonResp);
Map<String, Object> kv = Converter.jsonString2Map(jsonResp); Map<String, Object> kv = Converter.jsonString2Map(jsonResp);
info.putAll(kv); info.putAll(kv);
} }
return info; return info;
......
...@@ -26,6 +26,8 @@ import java.util.Iterator; ...@@ -26,6 +26,8 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import nl.uva.sne.drip.drip.commons.data.v1.external.CloudCredentials; import nl.uva.sne.drip.drip.commons.data.v1.external.CloudCredentials;
import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.FilenameUtils;
...@@ -102,6 +104,9 @@ public class Converter { ...@@ -102,6 +104,9 @@ public class Converter {
value = jsonArray2List((JSONArray) value); value = jsonArray2List((JSONArray) value);
} else if (value instanceof JSONObject) { } else if (value instanceof JSONObject) {
value = jsonObject2Map((JSONObject) value); value = jsonObject2Map((JSONObject) value);
} else if (value.equals("[]")) {
Logger.getLogger(Converter.class.getName()).log(Level.WARNING, "value: {0} is not JSONArray or an JSONObject", value);
value = null;
} }
map.put(key, value); map.put(key, value);
} }
......
...@@ -23,10 +23,11 @@ import json ...@@ -23,10 +23,11 @@ import json
import logging import logging
import linecache import linecache
import sys import sys
import re
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
if not getattr(logger, 'handler_set', None): if not getattr(logger, 'handler_set', None):
logger.setLevel(logging.INFO) #logger.setLevel(logging.INFO)
h = logging.StreamHandler() h = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
h.setFormatter(formatter) h.setFormatter(formatter)
...@@ -34,6 +35,10 @@ if not getattr(logger, 'handler_set', None): ...@@ -34,6 +35,10 @@ if not getattr(logger, 'handler_set', None):
logger.handler_set = True logger.handler_set = True
def get_resp_line(line):
line = line.encode('utf-8').strip('\n').encode('string_escape')
return json.dumps(line)
def docker_check(vm, compose_name): def docker_check(vm, compose_name):
try: try:
...@@ -48,10 +53,10 @@ def docker_check(vm, compose_name): ...@@ -48,10 +53,10 @@ def docker_check(vm, compose_name):
cluster_node_info = [] cluster_node_info = []
stdin, stdout, stderr = ssh.exec_command(cmd) stdin, stdout, stderr = ssh.exec_command(cmd)
node_ls_resp = stdout.readlines() node_ls_resp = stdout.readlines()
for i in node_ls_resp: for i in node_ls_resp:
if i.encode(): line = get_resp_line(i)
json_str = json.loads(i.encode().strip('\n')) if line:
cluster_node_info.append(json_str) cluster_node_info.append(json.loads(line))
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}}\"}\''
...@@ -60,22 +65,32 @@ def docker_check(vm, compose_name): ...@@ -60,22 +65,32 @@ def docker_check(vm, compose_name):
stack_ps_resp = stdout.readlines() stack_ps_resp = stdout.readlines()
services_info = [] services_info = []
nodes_hostname = set() nodes_hostname = set()
for i in stack_ps_resp: for i in stack_ps_resp:
if i.encode(): line = get_resp_line(i)
json_str = json.loads(i.encode().strip('\n')) if line:
if json_str['node'] not in nodes_hostname: json_dict = json.loads(line)
nodes_hostname.add(json_str['node']) json_dict = json.loads(json.dumps(json_dict))
services_info.append(json_str) if not isinstance(json_dict, dict):
try:
json_dict = json.loads(json_dict)
except Exception as e:
json_dict = json_dict.replace('\"ports\":\"\"', '\"ports\":null').replace('\"\"', '\"')
json_dict = json_dict.replace("\\", "").replace("Noxe2x80xa6", "No...")
json_dict = json.loads(json_dict)
nodes_hostname.add(json_dict['node'])
services_info.append(json_dict)
json_response ['services_info'] = services_info json_response ['services_info'] = services_info
stack_format = '\'{"ID":"{{.ID}}","name":"{{.Name}}","mode":"{{.Mode}}","replicas":"{{.Replicas}}","image":"{{.Image}}"}\'' stack_format = '\'{"ID":"{{.ID}}","name":"{{.Name}}","mode":"{{.Mode}}","replicas":"{{.Replicas}}","image":"{{.Image}}"}\''
cmd = 'sudo docker stack services '+ compose_name +' --format ' + (stack_format) cmd = 'sudo docker stack services '+ compose_name +' --format ' + (stack_format)
stdin, stdout, stderr = ssh.exec_command(cmd) stdin, stdout, stderr = ssh.exec_command(cmd)
stack_resp = stdout.readlines() stack_resp = stdout.readlines()
stack_info = [] stack_info = []
for i in stack_resp: for i in stack_resp:
if i.encode(): line = get_resp_line(i)
json_str = json.loads(i.encode().strip('\n')) if line:
stack_info.append(json_str) json_dict = json.loads(line)
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 '
...@@ -87,11 +102,12 @@ def docker_check(vm, compose_name): ...@@ -87,11 +102,12 @@ def docker_check(vm, compose_name):
response_str = "" response_str = ""
for i in inspect_resp: for i in inspect_resp:
if i.encode(): line = get_resp_line(i)
response_str+=i.encode().strip('\n') if line:
response_str+=line
json_str = json.loads(response_str.rstrip("\n\r").strip().encode('string_escape')) json_dict = json.loads(response_str.rstrip("\n\r").strip().encode('string_escape'))
json_response['nodes_info'] = json_str 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:
...@@ -104,7 +120,7 @@ def docker_check(vm, compose_name): ...@@ -104,7 +120,7 @@ def docker_check(vm, compose_name):
print 'EXCEPTION IN ({}, LINE {} "{}"): {}'.format(filename, lineno, line.strip(), exc_obj) print 'EXCEPTION IN ({}, LINE {} "{}"): {}'.format(filename, lineno, line.strip(), exc_obj)
logger.error(vm.ip + " " + str(e)+ " line:" +lineno) #logger.error(vm.ip + " " + str(e)+ " line:" +lineno)
return "ERROR:" + vm.ip + " " + str(e) return "ERROR:" + vm.ip + " " + str(e)
ssh.close() ssh.close()
return json_response return json_response
......
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