def setUp(self, mock_resource_service): # W0221 Arguments number differs from overridden method # pylint: disable=W0221 def _fake_service_factory(impl, *_args, **_kw_args): """Generate a unique mock object for each service implementation. """ return mock.Mock(name=impl) mock_resource_service.side_effect = _fake_service_factory self.root = tempfile.mkdtemp() self.app_env = appmgr.AppEnvironment(root=self.root)
def register(approot, refresh_interval, manifest, container_dir, appevents_dir): """Register container presence.""" del appevents_dir tm_env = appmgr.AppEnvironment(approot) app = yaml.load(manifest.read()) appname = app['name'] app_presence = presence.EndpointPresence( context.GLOBAL.zk.conn, app ) try: app_presence.register() _get_tickets(appname, app, container_dir) _start_service_sup(container_dir) except exc.ContainerSetupError as err: app_abort.abort( tm_env, appname, reason=str(err) ) # If tickets are not ok, app will be aborted. Waiting for tickets # in the loop is harmless way to wait for that. # # If tickets acquired successfully, services will start, and # tickets will be refreshed after each interval. tkts_spool_dir = os.path.join( container_dir, 'root', 'var', 'spool', 'tickets') while True: time.sleep(refresh_interval) reply = tickets.request_tickets(context.GLOBAL.zk.conn, appname) if reply: tickets.store_tickets(reply, tkts_spool_dir) else: _LOGGER.error('Error requesting tickets.')
def test_finish_no_manifest(self): """Test app finish on directory with no app.yml. """ app_env = appmgr.AppEnvironment(root=self.root) app_finish.finish(app_env, None, self.root)
def metrics(step, approot): """Collect node and container metrics.""" app_env = appmgr.AppEnvironment(root=approot) app_metrics_dir = os.path.join(app_env.metrics_dir, 'apps') core_metrics_dir = os.path.join(app_env.metrics_dir, 'core') fs.mkdir_safe(app_metrics_dir) fs.mkdir_safe(core_metrics_dir) interval = int(step) * 2 rrdclient = rrdutils.RRDClient('/tmp/treadmill.rrd') # Initiate the list for monitored applications monitored_apps = set( os.path.basename(metric_name)[:-len('.rrd')] for metric_name in glob.glob('%s/*' % app_metrics_dir) if metric_name.endswith('.rrd')) sys_svcs = _core_svcs(approot) sys_svcs_no_metrics = set() sys_maj_min = '%s:0' % os.major(os.stat(approot).st_dev) logging.info('Device maj:min = %s for approot: %s', sys_maj_min, approot) core_rrds = [ 'treadmill.apps.rrd', 'treadmill.core.rrd', 'treadmill.system.rrd' ] for core_rrd in core_rrds: rrdfile = os.path.join(core_metrics_dir, core_rrd) if not os.path.exists(rrdfile): rrdclient.create(rrdfile, step, interval) while True: rrdclient.update( os.path.join(core_metrics_dir, 'treadmill.apps.rrd'), rrdutils.app_metrics('treadmill/apps', sys_maj_min)) rrdclient.update( os.path.join(core_metrics_dir, 'treadmill.core.rrd'), rrdutils.app_metrics('treadmill/core', sys_maj_min)) rrdclient.update( os.path.join(core_metrics_dir, 'treadmill.system.rrd'), rrdutils.app_metrics('treadmill/system', sys_maj_min)) for svc in sys_svcs: if svc in sys_svcs_no_metrics: continue rrdfile = os.path.join(core_metrics_dir, '{svc}.rrd'.format(svc=svc)) if not os.path.exists(rrdfile): rrdclient.create(rrdfile, step, interval) svc_cgrp = os.path.join('treadmill', 'core', svc) svc_metrics = rrdutils.app_metrics(svc_cgrp, sys_maj_min) rrdclient.update(rrdfile, svc_metrics) seen_apps = set() for app_dir in glob.glob('%s/*' % app_env.apps_dir): if not os.path.isdir(app_dir): continue app_unique_name = os.path.basename(app_dir) seen_apps.add(app_unique_name) try: localdisk = app_env.svc_localdisk.get(app_unique_name) blkio_major_minor = '{major}:{minor}'.format( major=localdisk['dev_major'], minor=localdisk['dev_minor'], ) except (exc.TreadmillError, IOError, OSError): blkio_major_minor = None rrd_file = os.path.join( app_metrics_dir, '{app}.rrd'.format(app=app_unique_name)) if not os.path.exists(rrd_file): rrdclient.create(rrd_file, step, interval) app_cgrp = os.path.join('treadmill', 'apps', app_unique_name) app_metrics = rrdutils.app_metrics(app_cgrp, blkio_major_minor) rrdclient.update(rrd_file, app_metrics) for app_unique_name in monitored_apps - seen_apps: # Removed metrics for apps that are not present anymore rrd_file = os.path.join( app_metrics_dir, '{app}.rrd'.format(app=app_unique_name)) logging.info('removing %r', rrd_file) rrdclient.forget(rrd_file) os.unlink(rrd_file) monitored_apps = seen_apps time.sleep(step) # Gracefull shutdown. logging.info('service shutdown.')
def configure(approot, eventfile): """Configure local manifest and schedule app to run.""" app_env = appmgr.AppEnvironment(root=approot) container_dir = app_cfg.configure(app_env, eventfile) _LOGGER.info('Configured %r', container_dir)