Skip to content

Commit d785acb

Browse files
author
Robert Breker
committed
Improve log messages to ease debugging in user environments
Signed-off-by: Robert Breker <[email protected]>
1 parent b1a419d commit d785acb

File tree

7 files changed

+55
-26
lines changed

7 files changed

+55
-26
lines changed

src/xscontainer/api_helper.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def decorated(*args, **kwargs):
2828
try:
2929
return func(*args, **kwargs)
3030
except Exception, exception:
31-
log.debug("Caught exception '%s'. Retrying with new session."
31+
log.error("Caught exception '%s'. Retrying with new session."
3232
% (str(exception)))
3333
reinit_global_xapi_session()
3434
# Return the func undecorated
@@ -332,7 +332,9 @@ def create_vbd(session, vmref, vdiref, vbdmode, bootable):
332332

333333

334334
# ToDo: Ugly - this function may modify the file specified as filename
335-
def import_disk(session, sruuid, filename, fileformat, namelabel, other_config_keys={}):
335+
def import_disk(session, sruuid, filename, fileformat, namelabel,
336+
other_config_keys={}):
337+
log.info("import_disk file %s on sr %s" % (filename, sruuid))
336338
targetsr = session.xenapi.SR.get_by_uuid(sruuid)
337339
sizeinb = None
338340
if fileformat == "vhd":
@@ -374,6 +376,7 @@ def import_disk(session, sruuid, filename, fileformat, namelabel, other_config_k
374376

375377

376378
def export_disk(session, vdiuuid):
379+
log.info("export_disk vdi %s" % (vdiuuid))
377380
filename = tempfile.mkstemp(suffix='.raw')[1]
378381
cmd = ['curl', '-k', '-o', filename,
379382
'https://localhost/export_raw_vdi?session_id=%s&vdi=%s&format=raw'
@@ -431,6 +434,7 @@ def get_idrsa_secret_public_keyonly(session):
431434

432435

433436
def set_idrsa_secret(session):
437+
log.info("set_idrsa_secret is generating a new secret")
434438
(privateidrsa, publicidrsa) = util.create_idrsa()
435439
private_secret_ref = session.xenapi.secret.create(
436440
{'value': '%s' % (privateidrsa)})
@@ -472,6 +476,7 @@ def get_vm_xscontainer_username(session, vmuuid):
472476
username = get_value_from_vm_other_config(session, vmuuid,
473477
XSCONTAINER_USERNAME)
474478
if username == None:
479+
# assume CoreOs's "core" by default
475480
username = 'core'
476481
return username
477482

src/xscontainer/coreos.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ def remove_disks_in_vm_provisioning(session, vm_ref):
2828
def install_vm(session, urlvhdbz2, sruuid,
2929
vmname='CoreOs-%d' % (random.randint(0, 1000)),
3030
templatename='CoreOS'):
31+
# devmode only
32+
log.info("install_vm from url %s to sr %s" %(urlvhdbz2, sruuid))
3133
atempfile = tempfile.mkstemp(suffix='.vhd.bz2')[1]
3234
atempfileunpacked = atempfile.replace('.bz2', '')
3335
# @todo: pipe instead, so the file never actually touches Dom0
@@ -41,6 +43,7 @@ def install_vm(session, urlvhdbz2, sruuid,
4143
templateref = session.xenapi.VM.get_by_name_label(templatename)[0]
4244
vmref = session.xenapi.VM.clone(templateref, vmname)
4345
vmuuid = session.xenapi.VM.get_record(vmref)['uuid']
46+
log.info("install_vm created vm %s" %(vmuuid))
4447
remove_disks_in_vm_provisioning(session, vmref)
4548
session.xenapi.VM.provision(vmref)
4649
api_helper.create_vbd(session, vmref, vdiref, 'rw', True)
@@ -49,6 +52,7 @@ def install_vm(session, urlvhdbz2, sruuid,
4952

5053

5154
def setup_network_on_lowest_pif(session, vmref):
55+
# devmode only
5256
pifs = session.xenapi.PIF.get_all_records()
5357
lowest = None
5458
for pifref in pifs.keys():
@@ -107,6 +111,8 @@ def load_cloud_config_template(template_path=None):
107111
this_dir, _ = os.path.split(__file__)
108112
template_path = os.path.join(this_dir, "data", "cloud-config.template")
109113

114+
log.info("load_cloud_config_template from %s" % (template_path))
115+
110116
fh = open(template_path)
111117
template_data = fh.read()
112118
fh.close()
@@ -126,6 +132,7 @@ def get_config_drive_default(session):
126132

127133

128134
def create_config_drive_iso(session, userdata, vmuuid):
135+
log.info("create_config_drive_iso for vm %s" %(vmuuid))
129136
tempisodir = tempfile.mkdtemp()
130137
tempisofile = tempfile.mkstemp()[1]
131138
openstackfolder = os.path.join(tempisodir, 'openstack')
@@ -182,13 +189,16 @@ def remove_config_drive(session, vmrecord, configdisk_namelabel):
182189
if vbdrecord['VDI'] != api_helper.NULLREF:
183190
vdirecord = session.xenapi.VDI.get_record(vbdrecord['VDI'])
184191
if OTHER_CONFIG_CONFIG_DRIVE_KEY in vdirecord['other_config']:
192+
log.info("remove_config_drive will destroy vdi %s"
193+
%(vdirecord['uuid']))
185194
if vbdrecord['currently_attached']:
186195
session.xenapi.VBD.unplug(vbd)
187196
session.xenapi.VBD.destroy(vbd)
188197
session.xenapi.VDI.destroy(vbdrecord['VDI'])
189198

190199

191200
def create_config_drive(session, vmuuid, sruuid, userdata):
201+
log.info("create_config_drive for vm %s on sr %s" %(vmuuid, sruuid))
192202
vmref = session.xenapi.VM.get_by_uuid(vmuuid)
193203
vmrecord = session.xenapi.VM.get_record(vmref)
194204
prepare_vm_for_config_drive(session, vmref, vmuuid)
@@ -212,6 +222,7 @@ def create_config_drive(session, vmuuid, sruuid, userdata):
212222

213223

214224
def get_config_drive_configuration(session, vdiuuid):
225+
log.info("get_config_drive_configuration from vdi %s" % (vdiuuid))
215226
filename = api_helper.export_disk(session, vdiuuid)
216227
tempdir = tempfile.mkdtemp()
217228
cmd = ['mount', '-o', 'loop', '-t', 'iso9660', filename, tempdir]

src/xscontainer/docker.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,19 @@ def _interact_with_api(session, vmuuid, request_type, request,
3333
#protocol = headersplits[0]
3434
statuscode = headersplits[1]
3535
if statuscode[0] != '2':
36+
# this did not work
3637
status = ' '.join(headersplits[2:])
37-
failure_title = "Container enlightenment error"
38+
failure_title = "Container Management Error"
3839
failure_body = body.strip() + " (" + statuscode + ")"
3940
if ":" in failure_body:
4041
(failure_title, failure_body) = failure_body.split(":", 1)
4142
if message_error:
4243
api_helper.send_message(session, vmuuid, failure_title,
4344
failure_body)
44-
raise util.XSContainerException("Request %s led to failure %s - "
45-
% (request, status)
46-
+ " %s: %s"
47-
% (failure_title, failure_body))
45+
message = ("Request '%s' led to status %s - %s: %s"
46+
% (request, status, failure_title, failure_body))
47+
log.info(message)
48+
raise util.XSContainerException(message)
4849
return body
4950

5051

@@ -143,7 +144,6 @@ def get_inspect_dict(session, vmuuid, container):
143144

144145
def get_inspect_xml(session, vmuuid, container):
145146
result = {'docker_inspect': get_inspect_dict(session, vmuuid, container)}
146-
log.debug(result)
147147
return util.converttoxml(result)
148148

149149

src/xscontainer/docker_monitor/__init__.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ def stop_monitoring(self, force=False):
5454
ssh_client.close()
5555

5656
def _monitoring_loop(self):
57-
# ToDo: not needed and not safe - doesn't survive XAPI restarts
5857
vmuuid = self.get_uuid()
58+
log.info("monitor_loop handles VM %s" % (vmuuid))
5959
start_time = time.time()
6060
error_message = None
6161
docker.wipe_docker_other_config(self)
@@ -73,6 +73,8 @@ def _monitoring_loop(self):
7373
# if we got past the above, it's about time to delete the
7474
# error message, as all appears to be working again
7575
try:
76+
log.info("monitor_loop destroys message for VM %s: %s"
77+
% (vmuuid, error_message))
7678
api_helper.destroy_message(self.get_session(),
7779
error_message)
7880
except XenAPI.Failure:
@@ -87,37 +89,40 @@ def _monitoring_loop(self):
8789
try:
8890
session = self.get_session()
8991
cause = docker.determine_error_cause(session, vmuuid)
92+
log.info("monitor_loop creates message for VM %s: %s"
93+
% (vmuuid, cause))
9094
error_message = api_helper.send_message(
9195
session,
9296
self.get_uuid(),
93-
"Cannot monitor containers on VM",
97+
"Container Management cannot monitor VM",
9498
cause)
9599
except (XenAPI.Failure):
96100
# this can happen when XAPI is not running
97101
pass
98-
log.info("Could not connect to VM %s, retry" % (vmuuid))
102+
log.info("Could not connect to VM %s, will retry" % (vmuuid))
99103
error_in_this_iteration = True
100104
if not error_in_this_iteration:
101105
try:
102106
self.__monitor_vm_events()
103107
except (XenAPI.Failure, util.XSContainerException):
104-
log.exception("monitor_vm_events threw an exception, retry")
108+
log.exception("__monitor_vm_events threw an exception, "
109+
"will retry")
105110
if wipe_other_config_required:
106111
docker.wipe_docker_other_config(self)
107112
if not self._stop_monitoring_request:
108113
time.sleep(MONITORRETRYSLEEPINS)
109-
log.info("monitor_vm returns from handling vm %s" % (vmuuid))
114+
log.info("monitor_loop returns from handling vm %s" % (vmuuid))
110115

111116
def __monitor_vm_events(self):
112117
session = self.get_session()
113118
vmuuid = self.get_uuid()
114119
ssh_client = ssh_helper.prepare_ssh_client(session, vmuuid)
115120
try:
116-
cmd = "ncat -U /var/run/docker.sock"
121+
cmd = "ncat -U %s" % (docker.DOCKER_SOCKET_PATH)
122+
log.info("__monitor_vm_events is running '%s' on VM '%s'"
123+
% (cmd, vmuuid ))
117124
stdin, stdout, _ = ssh_client.exec_command(cmd)
118125
stdin.write("GET /events HTTP/1.0\r\n\r\n")
119-
log.debug("__monitor_vm_events at %r is running: %r"
120-
% (ssh_client.get_transport().getpeername(), cmd))
121126
self._ssh_client = ssh_client
122127
data = ""
123128
# set unblocking io for select.select
@@ -165,7 +170,7 @@ def __monitor_vm_events(self):
165170
except Exception:
166171
util.log.exception("Error when closing ssh_client for %r"
167172
% ssh_client)
168-
log.debug('__monitor_vm_events (%s) exited' % cmd)
173+
log.info('__monitor_vm_events (%s) exited' % cmd)
169174

170175
def handle_docker_event(self, event):
171176
if 'status' in event:
@@ -333,6 +338,7 @@ def monitor_host():
333338
DOCKER_MONITOR = DockerMonitor(host)
334339
else:
335340
DOCKER_MONITOR.set_host(host)
341+
log.info("Monitoring host %s" %(host.get_id()))
336342
try:
337343
# Avoid race conditions - get a current event token
338344
event_from = session.xenapi.event_from(["vm"], '', 0.0)
@@ -359,8 +365,8 @@ def monitor_host():
359365
log.exception("Failed when trying to logout")
360366
except (socket.error, XenAPI.Failure, xmlrpclib.ProtocolError):
361367
if session is not None:
362-
log.info("Could not connect to XAPI - Is XAPI running? " +
363-
"Will retry in %d" % (XAPIRETRYSLEEPINS))
368+
log.error("Could not connect to XAPI - Is XAPI running? " +
369+
"Will retry in %d" % (XAPIRETRYSLEEPINS))
364370
else:
365371
log.exception("Recovering from XAPI failure - Is XAPI " +
366372
"restarting? Will retry in %d."

src/xscontainer/docker_monitor/api.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
from xscontainer import docker_monitor
66
from xscontainer.api_helper import VM
77
from xscontainer.api_helper import XenAPIClient
8-
8+
from xscontainer.util import log
99

1010
def register_vm(vm_uuid, session):
11+
log.info("register_vm %s" %(vm_uuid))
1112
client = XenAPIClient(session)
1213
thevm = VM(client, uuid=vm_uuid)
1314
thevm.update_other_config(docker_monitor.REGISTRATION_KEY,
@@ -16,6 +17,7 @@ def register_vm(vm_uuid, session):
1617

1718

1819
def deregister_vm(vm_uuid, session):
20+
log.info("deregister_vm %s" %(vm_uuid))
1921
client = XenAPIClient(session)
2022
thevm = VM(client, uuid=vm_uuid)
2123
thevm.update_other_config(docker_monitor.REGISTRATION_KEY,
@@ -27,7 +29,7 @@ def deregister_vm(vm_uuid, session):
2729
def mark_monitorable_vm(vm_uuid, session):
2830
""" Ensure the VM has a REGISTRATION_KEY in vm:other_config. This key is
2931
used by XC to know whether monitoring is an option for this VM """
30-
32+
log.info("mark_monitorable_vm %s" %(vm_uuid))
3133
client = XenAPIClient(session)
3234
thevm = VM(client, uuid=vm_uuid)
3335
other_config = thevm.get_other_config()

src/xscontainer/ssh_helper.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ def missing_host_key(self, client, hostname, key):
7575
def prepare_ssh_client(session, vmuuid):
7676
username = api_helper.get_vm_xscontainer_username(session, vmuuid)
7777
host = api_helper.get_suitable_vm_ip(session, vmuuid)
78+
log.info("prepare_ssh_client for vm %s via %s" %(vmuuid, host))
7879
ensure_idrsa(session)
7980
client = paramiko.SSHClient()
8081
pkey = paramiko.rsakey.RSAKey.from_private_key(
@@ -88,9 +89,10 @@ def prepare_ssh_client(session, vmuuid):
8889
# This exception is already improved - leave it as it is
8990
raise
9091
except paramiko.AuthenticationException, exception:
91-
log.exception(exception)
92-
raise AuthenticationException("Failed to authenticate with private key"
93-
" on VM %s." % (vmuuid))
92+
message = ("prepare_ssh_client failed to authenticate with private key"
93+
" on VM %s" % (vmuuid))
94+
log.info(message)
95+
raise AuthenticationException(message)
9496
except (paramiko.SSHException, socket.error), exception:
9597
# reraise as SshException
9698
raise SshException, "prepare_ssh_client: %s" % exception, (
@@ -99,7 +101,6 @@ def prepare_ssh_client(session, vmuuid):
99101

100102

101103
def execute_ssh(session, vmuuid, cmd):
102-
log.info(" ".join(cmd))
103104
# The heavy weight is docker ps with plenty of containers.
104105
# Assume 283 bytes per container.
105106
# 300KB should be enough for 1085 containers.
@@ -110,13 +111,17 @@ def execute_ssh(session, vmuuid, cmd):
110111
client = prepare_ssh_client(session, vmuuid)
111112
if isinstance(cmd, list):
112113
cmd = ' '.join(cmd)
114+
log.info("execute_ssh will run '%s' on vm %s"
115+
% (cmd, vmuuid))
113116
_, stdout, _ = client.exec_command(cmd)
114117
output = stdout.read(max_read_size)
115118
if stdout.read(1) != "":
116119
raise SshException("too much data was returned when executing"
117120
"'%s'" % (cmd))
118121
returncode = stdout.channel.recv_exit_status()
119122
if returncode != 0:
123+
log.info("execute_ssh '%s' on vm %s exited with rc %d: Stdout:"
124+
" %s" % (cmd, vmuuid, returncode, stdout))
120125
raise SshException("Returncode for '%s' is not 0" % cmd)
121126
return output
122127
except SshException:

src/xscontainer/util/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def runlocal(cmd, shell=False, canfail=False):
2424
shell=shell)
2525
stdout, stderr = process.communicate('')
2626
returncode = process.returncode
27-
log.debug('Command %s exited with rc %d: Stdout: %s Stderr: %s' %
27+
log.info('Command %s exited with rc %d: Stdout: %s Stderr: %s' %
2828
(cmd, returncode, stdout, stderr))
2929
if returncode != 0 and not canfail:
3030
raise(XSContainerException('Command failed'))

0 commit comments

Comments
 (0)