Skip to content

Commit

Permalink
python3: move to unicode strings
Browse files Browse the repository at this point in the history
Handle unicode strings, since we've supported
unicode VM names since 4.0

Bug-Url:https://bugzilla.redhat.com/show_bug.cgi?id=1713288
Change-Id: I77a1ce082a047bdf815faa79b55d664a32ad4e5d
Signed-off-by: Ryan Barry <[email protected]>
Signed-off-by: Andrej Krejcir <[email protected]>
Reviewed-on: https://gerrit.ovirt.org/100281
Reviewed-by: Sandro Bonazzola <[email protected]>
Continuous-Integration: Jenkins CI <[email protected]>
  • Loading branch information
Ryan Barry authored and akrejcir committed Jun 18, 2019
1 parent d2df944 commit e592003
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 28 deletions.
4 changes: 2 additions & 2 deletions mom-guestd.in
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ def configure_logger(config):
handler = logging.StreamHandler()
else:
print("logging to file %s" % log)
bytes = config.getint('logging', 'max-bytes')
max_bytes = config.getint('logging', 'max-bytes')
backups = config.getint('logging', 'backup-count')
handler = logging.handlers.RotatingFileHandler(log, 'a', bytes, backups)
handler = logging.handlers.RotatingFileHandler(log, 'a', max_bytes, backups)
handler.setLevel(level)
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
handler.setFormatter(formatter)
Expand Down
24 changes: 12 additions & 12 deletions mom/Collectors/GuestNetworkDaemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def sock_send(conn, msg):
"""
Send a message via a socket connection. '\n' marks the end of the message.
"""
msg = msg + "\n"
msg = msg + b"\n"
sent = 0
while sent < len(msg):
ret = conn.send(msg[sent:])
Expand All @@ -38,23 +38,23 @@ def sock_receive(conn, logger=None):
"""
Receive a '\n' terminated message via a socket connection.
"""
msg = ""
msg = b""
done = False
if logger:
logger.debug('sock_receive(%s)' % conn)
while not done:
chunk = conn.recv(4096)
if logger:
logger.debug("sock_receive: received next chunk: %s" % repr(chunk))
if chunk == '':
if chunk == b'':
done = True
msg = msg + chunk
if msg[-1:] == '\n':
if msg[-1:] == b'\n':
done = True
if len(msg) == 0:
raise socket.error("Unable to receive on socket")
else:
return msg.rstrip("\n")
return msg.rstrip(b"\n")

def sock_close(sock):
try:
Expand Down Expand Up @@ -100,7 +100,7 @@ def get_guest_ip(self, properties):
except OSError as e:
self.logger.warn("Cannot call name-to-ip-helper: %s", e.strerror)
return None
matches = re.findall("^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})",
matches = re.findall(r"^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})",
output, re.M)
if len(matches) is not 1:
self.logger.warn("Output from name-to-ip-helper %s is not an IP " \
Expand Down Expand Up @@ -133,8 +133,8 @@ def collect(self):
if self.socket is None:
self.connect()
try:
sock_send(self.socket, "stats")
data = sock_receive(self.socket, self.logger)
sock_send(self.socket, b"stats")
data = sock_receive(self.socket, self.logger).decode('utf-8')
except socket.error as msg:
sock_close(self.socket)
self.socket = None
Expand Down Expand Up @@ -192,7 +192,7 @@ def __del__(self):

def send_props(self, conn):
response = "min_free:" + self.min_free + ",max_free:" + self.max_free
sock_send(conn, response)
sock_send(conn, response.encode('utf-8'))

def send_stats(self, conn):
data = self.collector.collect()
Expand All @@ -205,17 +205,17 @@ def send_stats(self, conn):
"major_fault:%i,minor_fault:%i" % \
(data['mem_available'], data['mem_free'], data['swap_in'], \
data['swap_out'], majflt, minflt)
sock_send(conn, response)
sock_send(conn, response.encode('utf-8'))

def session(self, conn, addr):
self.logger.debug("Connection received from %s", addr)
conn.settimeout(10)
while self.running:
try:
cmd = sock_receive(conn)
if cmd == "props":
if cmd == b"props":
self.send_props(conn)
elif cmd == "stats":
elif cmd == b"stats":
self.send_stats(conn)
else:
break
Expand Down
4 changes: 2 additions & 2 deletions mom/Collectors/GuestQemuAgent.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def getfile(self, path, maxSize=1048576):
Convenience function to fetch a whole file using open/read/close APIs
"""
fh = self.agent_cmd('file_open', path, "r")
data = ""
data = b""
while True:
ret = self.agent_cmd('file_read', fh, 1024)
data += ret['buf']
Expand All @@ -138,7 +138,7 @@ def getfile(self, path, maxSize=1048576):
raise CollectionError("Remote file '%s' is too large" % \
path)
self.agent_cmd('file_close', fh)
return data
return data.decode('utf-8')

def collect(self):
if not self.connect():
Expand Down
2 changes: 1 addition & 1 deletion mom/Collectors/HostKSM.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def get_ksmd_jiffies(self):
if self.pid is None:
return 0
else:
return sum(map(int, file('/proc/%s/stat' % self.pid) \
return sum(map(int, open('/proc/%s/stat' % self.pid)
.read().split()[13:15]))

def get_ksmd_cpu_usage(self):
Expand Down
12 changes: 6 additions & 6 deletions mom/Collectors/QemuGuestAgentClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,11 @@ def _reset_conn(self, sock):
seq = int(time.time() % 2147483647) # Long_max
request = { 'execute': 'guest-sync', 'arguments': { 'id': seq } }
req_str = json.dumps(request)
self._sock_send(sock, req_str)
self._sock_send(sock, req_str.encode('utf-8'))

# Read data from the channel until we get a matching response
while True:
response = self._sock_recv_until(sock, "\n")
response = self._sock_recv_until(sock, b"\n").decode('utf-8')
resp_obj = json.loads(response)
if 'return' in resp_obj:
try:
Expand Down Expand Up @@ -226,7 +226,7 @@ def _sock_recv_until(self, sock, token):
"""
Receive data from the socket one byte at a time until the token is read
"""
data = ""
data = b""
while True:
if len(data) > 4096:
return None
Expand All @@ -240,7 +240,7 @@ def _sock_recv_until(self, sock, token):
self._sock_close(self.sock)
self.sock = None
raise ProtocolError(e.errno, e.strerror)
if ch == '':
if ch == b'':
print("Connection closed")
return None
data += ch
Expand All @@ -267,8 +267,8 @@ def _call(self, command, args={}):
json_str = json.dumps(request)

sock = self._make_connection()
self._sock_send(sock, json_str)
response = self._sock_recv_until(sock, "\n")
self._sock_send(sock, json_str.encode('utf-8'))
response = self._sock_recv_until(sock, b"\n").decode('utf-8')
return QemuAgentRet(response)

class _QemuGuestAgentAPI():
Expand Down
12 changes: 10 additions & 2 deletions mom/GuestMonitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

import six
import threading
import time
import re
Expand Down Expand Up @@ -53,9 +54,16 @@ def getGuestName(self):

class GuestMonitorThread(threading.Thread):
def __init__(self, info, monitor):
threading.Thread.__init__(self, name="guest:%s" % id)
threading.Thread.__init__(self)

self.setName("GuestMonitor-%s" % info['name'])
name = "GuestMonitor-%s" % info['name']
if six.PY2:
# In python 2 the name should not have type 'unicode'.
# The name is only used for logging, so it should
# be safe to represent it in utf-8 encoding.
name = name.encode('utf-8')

self.setName(name)
self.setDaemon(True)
self.logger = logging.getLogger('mom.GuestMonitor.Thread')

Expand Down
2 changes: 1 addition & 1 deletion mom/HypervisorInterfaces/libvirtInterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def _domainGetPid(self, uuid):
Something is probably wrong if more or less than 1 match is returned.
"""
p1 = Popen(["ps", "axww"], stdout=PIPE).communicate()[0]
matches = re.findall("^\s*(\d+)\s+.*" + uuid, p1, re.M)
matches = re.findall(r"^\s*(\d+)\s+.*" + uuid, p1, re.M)
if len(matches) < 1:
self.logger.warn("No matching process for domain with uuid %s", \
uuid)
Expand Down
4 changes: 2 additions & 2 deletions mom/unixrpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class UnixXmlRpcClient(ServerProxy):
def __init__(self, sock_path):
# We can't pass funny characters in the host part of a URL, so we
# encode the socket path in base16.
ServerProxy.__init__(self, 'http://' + base64.b16encode(sock_path),
ServerProxy.__init__(self, 'http://' + base64.b16encode(sock_path.encode('utf-8')),
transport=UnixXmlRpcTransport(),
allow_none=1)

Expand All @@ -42,6 +42,6 @@ def make_connection(self, host):
class UnixXmlRpcHttpConnection(http_client.HTTPConnection):
def connect(self):
self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self.sock.connect(base64.b16decode(self.host))
self.sock.connect(base64.b16decode(self.host).decode('utf-8'))


0 comments on commit e592003

Please sign in to comment.