From b1b8a569ef5207f9069d50cda830c1422dfc0ead Mon Sep 17 00:00:00 2001 From: "James E. Blair" Date: Thu, 29 Aug 2013 16:05:35 -0700 Subject: [PATCH] Add image logging Log stdout/stderr from the image build process. Use the provider and image name in the log selector so that admins can route appropriately (or at least grep). Change-Id: I7bc74ebfca3184340b51b083695b3441f0924e83 --- nodepool/nodepool.py | 13 +++++++------ nodepool/nodeutils.py | 2 +- nodepool/sshclient.py | 28 +++++++++++++++++----------- 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/nodepool/nodepool.py b/nodepool/nodepool.py index e680b8b98..30bbeb8e7 100644 --- a/nodepool/nodepool.py +++ b/nodepool/nodepool.py @@ -401,7 +401,10 @@ class ImageUpdater(threading.Thread): (server_id, self.snap_image.id)) def bootstrapServer(self, server, key): - ssh_kwargs = {} + log = logging.getLogger("nodepool.image.build.%s.%s" % + (self.provider.name, self.image.name)) + + ssh_kwargs = dict(log=log) if key: ssh_kwargs['pkey'] = key else: @@ -429,11 +432,9 @@ class ImageUpdater(threading.Thread): for k, v in os.environ.items(): if k.startswith('NODEPOOL_'): env_vars += ' %s="%s"' % (k, v) - r = host.ssh("run setup script", - "cd /opt/nodepool-scripts && %s ./%s" % - (env_vars, self.image.setup)) - if not r: - raise Exception("Unable to run setup scripts") + host.ssh("run setup script", + "cd /opt/nodepool-scripts && %s ./%s" % + (env_vars, self.image.setup)) class ConfigValue(object): diff --git a/nodepool/nodeutils.py b/nodepool/nodeutils.py index 233b7bc9b..05d32c55e 100644 --- a/nodepool/nodeutils.py +++ b/nodepool/nodeutils.py @@ -50,7 +50,7 @@ def ssh_connect(ip, username, connect_kwargs={}, timeout=60): if e[0] not in [errno.ECONNREFUSED, errno.EHOSTUNREACH]: log.exception('Exception while testing ssh access:') - out = client.ssh("test ssh access", "echo access okay") + out = client.ssh("test ssh access", "echo access okay", output=True) if "access okay" in out: return client return None diff --git a/nodepool/sshclient.py b/nodepool/sshclient.py index 75e7cb15c..4b056e1a4 100644 --- a/nodepool/sshclient.py +++ b/nodepool/sshclient.py @@ -19,34 +19,40 @@ # limitations under the License. import paramiko -import sys class SSHClient(object): def __init__(self, ip, username, password=None, pkey=None, - key_filename=None): + key_filename=None, log=None): client = paramiko.SSHClient() client.load_system_host_keys() client.set_missing_host_key_policy(paramiko.WarningPolicy()) client.connect(ip, username=username, password=password, pkey=pkey, key_filename=key_filename) self.client = client + self.log = log - def ssh(self, action, command): + def ssh(self, action, command, output=False): + if self.log: + self.log.info(command) stdin, stdout, stderr = self.client.exec_command(command) - print command - output = '' - for x in stdout: - output += x - sys.stdout.write(x) + out = '' + for line in stdout: + if output: + out += line + if self.log: + self.log.info(line.rstrip()) + for line in stderr: + if self.log: + self.log.error(line.rstrip()) ret = stdout.channel.recv_exit_status() - print stderr.read() if ret: raise Exception("Unable to %s" % action) - return output + return out def scp(self, source, dest): - print 'copy', source, dest + if self.log: + self.log.info("Copy %s -> %s" % (source, dest)) ftp = self.client.open_sftp() ftp.put(source, dest) ftp.close()