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
This commit is contained in:
James E. Blair 2013-08-29 16:05:35 -07:00
parent 1e190f5d57
commit b1b8a569ef
3 changed files with 25 additions and 18 deletions

View File

@ -401,7 +401,10 @@ class ImageUpdater(threading.Thread):
(server_id, self.snap_image.id)) (server_id, self.snap_image.id))
def bootstrapServer(self, server, key): 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: if key:
ssh_kwargs['pkey'] = key ssh_kwargs['pkey'] = key
else: else:
@ -429,11 +432,9 @@ class ImageUpdater(threading.Thread):
for k, v in os.environ.items(): for k, v in os.environ.items():
if k.startswith('NODEPOOL_'): if k.startswith('NODEPOOL_'):
env_vars += ' %s="%s"' % (k, v) env_vars += ' %s="%s"' % (k, v)
r = host.ssh("run setup script", host.ssh("run setup script",
"cd /opt/nodepool-scripts && %s ./%s" % "cd /opt/nodepool-scripts && %s ./%s" %
(env_vars, self.image.setup)) (env_vars, self.image.setup))
if not r:
raise Exception("Unable to run setup scripts")
class ConfigValue(object): class ConfigValue(object):

View File

@ -50,7 +50,7 @@ def ssh_connect(ip, username, connect_kwargs={}, timeout=60):
if e[0] not in [errno.ECONNREFUSED, errno.EHOSTUNREACH]: if e[0] not in [errno.ECONNREFUSED, errno.EHOSTUNREACH]:
log.exception('Exception while testing ssh access:') 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: if "access okay" in out:
return client return client
return None return None

View File

@ -19,34 +19,40 @@
# limitations under the License. # limitations under the License.
import paramiko import paramiko
import sys
class SSHClient(object): class SSHClient(object):
def __init__(self, ip, username, password=None, pkey=None, def __init__(self, ip, username, password=None, pkey=None,
key_filename=None): key_filename=None, log=None):
client = paramiko.SSHClient() client = paramiko.SSHClient()
client.load_system_host_keys() client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.WarningPolicy()) client.set_missing_host_key_policy(paramiko.WarningPolicy())
client.connect(ip, username=username, password=password, pkey=pkey, client.connect(ip, username=username, password=password, pkey=pkey,
key_filename=key_filename) key_filename=key_filename)
self.client = client 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) stdin, stdout, stderr = self.client.exec_command(command)
print command out = ''
output = '' for line in stdout:
for x in stdout: if output:
output += x out += line
sys.stdout.write(x) 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() ret = stdout.channel.recv_exit_status()
print stderr.read()
if ret: if ret:
raise Exception("Unable to %s" % action) raise Exception("Unable to %s" % action)
return output return out
def scp(self, source, dest): 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 = self.client.open_sftp()
ftp.put(source, dest) ftp.put(source, dest)
ftp.close() ftp.close()