Merge pull request #37 from rackerlabs/jay/AgentLogging

add(logging): Log and time various events
This commit is contained in:
Jason Faulkner
2014-01-24 15:33:38 -08:00

View File

@@ -16,15 +16,18 @@ limitations under the License.
import hashlib import hashlib
import os import os
import subprocess
import requests import requests
import structlog
import subprocess
import time
from teeth_agent import base from teeth_agent import base
from teeth_agent import configdrive from teeth_agent import configdrive
from teeth_agent import errors from teeth_agent import errors
from teeth_agent import hardware from teeth_agent import hardware
log = structlog.get_logger()
def _configdrive_location(): def _configdrive_location():
return '/tmp/configdrive' return '/tmp/configdrive'
@@ -40,14 +43,17 @@ def _path_to_script(script):
def _write_image(image_info, configdrive_dir, device): def _write_image(image_info, configdrive_dir, device):
starttime = time.time()
image = _image_location(image_info) image = _image_location(image_info)
script = _path_to_script('shell/makefs.sh') script = _path_to_script('shell/makefs.sh')
command = ['/bin/bash', script, configdrive_dir, image, device] command = ['/bin/bash', script, configdrive_dir, image, device]
log.info('Writing image', command=' '.join(command))
exit_code = subprocess.call(command) exit_code = subprocess.call(command)
if exit_code != 0: if exit_code != 0:
raise errors.ImageWriteError(exit_code, device) raise errors.ImageWriteError(exit_code, device)
totaltime = time.time() - starttime
log.info('Image written', device=device, seconds=totaltime, image=image)
def _request_url(image_info, url): def _request_url(image_info, url):
@@ -58,11 +64,15 @@ def _request_url(image_info, url):
def _download_image(image_info): def _download_image(image_info):
starttime = time.time()
resp = None resp = None
for url in image_info['urls']: for url in image_info['urls']:
try: try:
log.info("Attempting to download image", url=url)
resp = _request_url(image_info, url) resp = _request_url(image_info, url)
except errors.ImageDownloadError: except errors.ImageDownloadError:
failtime = time.time() - starttime
log.warning("Image download failed", url=url, seconds=failtime)
continue continue
else: else:
break break
@@ -77,6 +87,9 @@ def _download_image(image_info):
except Exception: except Exception:
raise errors.ImageDownloadError(image_info['id']) raise errors.ImageDownloadError(image_info['id'])
totaltime = time.time() - starttime
log.info("Image downloaded", image=image_location, seconds=totaltime)
if not _verify_image(image_info, image_location): if not _verify_image(image_info, image_location):
raise errors.ImageChecksumError(image_info['id']) raise errors.ImageChecksumError(image_info['id'])
@@ -87,14 +100,25 @@ def _verify_image(image_info, image_location):
algo = getattr(hashlib, k, None) algo = getattr(hashlib, k, None)
if algo is None: if algo is None:
continue continue
log.debug('Verifying image',
image=image_location,
algo=k,
passed_hash=v)
hash_ = algo(open(image_location).read()).hexdigest() hash_ = algo(open(image_location).read()).hexdigest()
if hash_ == v: if hash_ == v:
return True return True
else:
log.warning('Image verification failed',
image=image_location,
algo=k,
imagehash=hash_,
passed_hash=v)
return False return False
def _run_image(): def _run_image():
script = _path_to_script('shell/reboot.sh') script = _path_to_script('shell/reboot.sh')
log.info("Rebooting system")
command = ['/bin/bash', script] command = ['/bin/bash', script]
# this should never return if successful # this should never return if successful
exit_code = subprocess.call(command) exit_code = subprocess.call(command)
@@ -118,6 +142,7 @@ class PrepareImageCommand(base.AsyncCommandResult):
device = hardware.get_manager().get_os_install_device() device = hardware.get_manager().get_os_install_device()
_download_image(image_info) _download_image(image_info)
log.debug('Writing configdrive', location=location)
configdrive.write_configdrive(location, metadata, files) configdrive.write_configdrive(location, metadata, files)
_write_image(image_info, location, device) _write_image(image_info, location, device)