def _cleanup(tm_env, container_dir, app): """Cleanup a container that actually ran. """ # Generate a unique name for the app unique_name = appcfg.app_unique_name(app) # Create service clients cgroup_client = tm_env.svc_cgroup.make_client( os.path.join(container_dir, 'resources', 'cgroups')) localdisk_client = tm_env.svc_localdisk.make_client( os.path.join(container_dir, 'resources', 'localdisk')) network_client = tm_env.svc_network.make_client( os.path.join(container_dir, 'resources', 'network')) presence_client = tm_env.svc_presence.make_client( os.path.join(container_dir, 'resources', 'presence')) # Unregister presence presence_client.delete(app.name) # Destroy the volume try: localdisk_client.delete(unique_name) except (IOError, OSError) as err: if err.errno == errno.ENOENT: pass else: raise if not app.shared_network: _cleanup_network(tm_env, container_dir, app, network_client) # Add metrics to archive rrd_file = os.path.join( tm_env.metrics_dir, 'apps', '{name}-{instanceid}-{uniqueid}.rrd'.format( name=app.app, instanceid=app.task, uniqueid=app.uniqueid, )) rrdutils.flush_noexc(rrd_file) _copy_metrics(rrd_file, container_dir) # Cleanup our cgroup resources try: cgroup_client.delete(unique_name) except (IOError, OSError) as err: if err.errno == errno.ENOENT: pass else: raise try: runtime.archive_logs(tm_env, appcfg.app_unique_name(app), container_dir) except Exception: # pylint: disable=W0703 _LOGGER.exception('Unexpected exception storing local logs.')
def _cleanup(tm_env, zkclient, container_dir, app): """Cleanup a container that actually ran. """ # Too many branches. # # pylint: disable=R0912 rootdir = os.path.join(container_dir, 'root') # Generate a unique name for the app unique_name = appmgr.app_unique_name(app) # Create service clients cgroup_client = tm_env.svc_cgroup.make_client( os.path.join(container_dir, 'cgroups')) localdisk_client = tm_env.svc_localdisk.make_client( os.path.join(container_dir, 'localdisk')) network_client = tm_env.svc_network.make_client( os.path.join(container_dir, 'network')) # Make sure all processes are killed # FIXME(boysson): Should we use `kill_apps_in_cgroup` instead? _kill_apps_by_root(rootdir) # Setup the archive filename that will hold this container's data filetime = utils.datetime_utcnow().strftime('%Y%m%d_%H%M%S%f') archive_filename = os.path.join( container_dir, '{instance_name}_{hostname}_{timestamp}.tar'.format( instance_name=appmgr.appname_task_id(app.name), hostname=sysinfo.hostname(), timestamp=filetime)) # Tar up container root filesystem if archive list is in manifest try: localdisk = localdisk_client.get(unique_name) fs.archive_filesystem(localdisk['block_dev'], rootdir, archive_filename, app.archive) except services.ResourceServiceError: _LOGGER.warning('localdisk never allocated') except subprocess.CalledProcessError: _LOGGER.exception('Unable to archive root device of %r', unique_name) except: # pylint: disable=W0702 _LOGGER.exception('Unknown exception while archiving %r', unique_name) # Destroy the volume try: localdisk = localdisk_client.delete(unique_name) except (IOError, OSError) as err: if err.errno == errno.ENOENT: pass else: raise if not app.shared_network: _cleanup_network(tm_env, app, network_client) # Add metrics to archive rrd_file = os.path.join( tm_env.metrics_dir, 'apps', '{name}-{instanceid}-{uniqueid}.rrd'.format( name=app.app, instanceid=app.task, uniqueid=app.uniqueid, )) rrdutils.flush_noexc(rrd_file) _copy_metrics(rrd_file, container_dir) # Cleanup our cgroup resources try: cgroup_client.delete(unique_name) except (IOError, OSError) as err: if err.errno == errno.ENOENT: pass else: raise try: _archive_logs(tm_env, container_dir) except Exception: # pylint: disable=W0703 _LOGGER.exception('Unexpected exception storing local logs.') # Append or create the tarball with folders outside of container # Compress and send the tarball to HCP try: archive_filename = fs.tar(sources=container_dir, target=archive_filename, compression='gzip').name _send_container_archive(zkclient, app, archive_filename) except: # pylint: disable=W0702 _LOGGER.exception("Failed to update archive")