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 _finish(self): app = runtime.load_app(self._service.data_dir, runtime.STATE_JSON) if app: client = self._get_client() container = state = None name = appcfg.app_unique_name(app) try: container = client.containers.get(name) state = container.attrs.get('State') except docker.errors.NotFound: pass if container is not None: try: container.remove(force=True) except docker.errors.APIError: _LOGGER.error('Failed to remove %s', container.id) aborted = _check_aborted(self._service.data_dir) if aborted is not None: app_abort.report_aborted(self._tm_env, app.name, why=aborted.get('why'), payload=aborted.get('payload')) elif state is not None: if state.get('OOMKilled', False): event = events.KilledTraceEvent( instanceid=app.name, is_oom=True, ) else: event = events.FinishedTraceEvent(instanceid=app.name, rc=state.get( 'ExitCode', 256), signal=0, payload=state) trace.post(self._tm_env.app_events_dir, event) if os.name == 'nt': credential_spec.cleanup(name, client) try: runtime.archive_logs(self._tm_env, name, self._service.data_dir) except Exception: # pylint: disable=W0703 _LOGGER.exception('Unexpected exception storing local logs.')