def test_create_apps(self): """Tests app api.""" zkclient = kazoo.client.KazooClient() kazoo.client.KazooClient.create.return_value = '/scheduled/foo.bar#12' masterapi.create_apps(zkclient, 'foo.bar', {}, 2) kazoo.client.KazooClient.create.assert_has_calls([ mock.call( '/scheduled/foo.bar#', b'{}\n', makepath=True, sequence=True, ephemeral=False, acl=mock.ANY ), mock.call( '/trace/000C/foo.bar#12,123.34,xxx,pending,created', b'', ephemeral=False, makepath=True, sequence=False, acl=mock.ANY ), # Mock call returns same instance (#12), so same task is created # twice. mock.call('/scheduled/foo.bar#', b'{}\n', makepath=True, sequence=True, ephemeral=False, acl=mock.ANY), mock.call('/trace/000C/foo.bar#12,123.34,xxx,pending,created', b'', ephemeral=False, makepath=True, sequence=False, acl=mock.ANY) ]) kazoo.client.KazooClient.create.reset_mock() masterapi.create_apps(zkclient, 'foo.bar', {}, 1, 'monitor') kazoo.client.KazooClient.create.assert_has_calls([ mock.call('/scheduled/foo.bar#', b'{}\n', makepath=True, sequence=True, ephemeral=False, acl=mock.ANY), mock.call( '/trace/000C/foo.bar#12,123.34,xxx,pending,monitor:created', b'', ephemeral=False, makepath=True, sequence=False, acl=mock.ANY ) ])
def create(rsrc_id, rsrc, count=1, created_by=None): """Create (configure) instance.""" _LOGGER.info('create: count = %s, %s %r, created_by = %s', count, rsrc_id, rsrc, created_by) admin_app = admin.Application(context.GLOBAL.ldap.conn) if not rsrc: configured = admin_app.get(rsrc_id) else: # Make sure defaults are present configured = admin_app.from_entry(admin_app.to_entry(rsrc)) app.verify_feature(rsrc.get('features', [])) if 'services' in configured and not configured['services']: del configured['services'] if '_id' in configured: del configured['_id'] _LOGGER.info('Configured: %s %r', rsrc_id, configured) _validate(configured) self.plugins = _api_plugins(self.plugins) for plugin in self.plugins: configured = plugin.add_attributes(rsrc_id, configured) _check_required_attributes(configured) _set_defaults(configured, rsrc_id) scheduled = masterapi.create_apps(context.GLOBAL.zk.conn, rsrc_id, configured, count, created_by) return scheduled
def create(rsrc_id, rsrc, count=1, created_by=None): """Create (configure) instance.""" _LOGGER.info('create: count = %s, %s %r, created_by = %s', count, rsrc_id, rsrc, created_by) # Check scheduled quota. zkclient = context.GLOBAL.zk.conn scheduled_stats = masterapi.get_scheduled_stats(zkclient) total_apps = sum(scheduled_stats.values()) if total_apps + count > _TOTAL_SCHEDULED_QUOTA: raise exc.QuotaExceededError( 'Total scheduled apps quota exceeded.') proid_apps = scheduled_stats.get(rsrc_id[:rsrc_id.find('.')], 0) if proid_apps + count > _PROID_SCHEDULED_QUOTA: raise exc.QuotaExceededError( 'Proid scheduled apps quota exceeded.') admin_app = admin.Application(context.GLOBAL.ldap.conn) if not rsrc: configured = admin_app.get(rsrc_id) else: # Make sure defaults are present configured = admin_app.from_entry(admin_app.to_entry(rsrc)) app.verify_feature(rsrc.get('features', [])) if 'services' in configured and not configured['services']: del configured['services'] if '_id' in configured: del configured['_id'] _LOGGER.info('Configured: %s %r', rsrc_id, configured) _validate(configured) self.plugins = _api_plugins(self.plugins) for plugin in self.plugins: configured = plugin.add_attributes(rsrc_id, configured) _check_required_attributes(configured) _set_defaults(configured, rsrc_id) scheduled = masterapi.create_apps(zkclient, rsrc_id, configured, count, created_by) return scheduled
def schedule(app, manifest, count, env, proid): """Schedule app(s) on the cell master""" with io.open(manifest, 'rb') as fd: data = yaml.load(stream=fd) # TODO: should we delete all potential attributes starting # with _ ? if '_id' in data: del data['_id'] data['environment'] = env if 'affinity' not in data: # TODO: allow custom affinity formats. data['affinity'] = '{0}.{1}'.format(*app.split('.')) data['proid'] = proid scheduled = masterapi.create_apps(context.GLOBAL.zk.conn, app, data, count, 'admin') for app_id in scheduled: print(app_id)
def create(rsrc_id, rsrc, count=1, created_by=None, debug=False, debug_services=None): """Create (configure) instance.""" _LOGGER.info('create: count = %s, %s %r, created_by = %s', count, rsrc_id, rsrc, created_by) # Check scheduled quota. zkclient = context.GLOBAL.zk.conn scheduled_stats = masterapi.get_scheduled_stats(zkclient) if not scheduled_stats: scheduled_stats = {} total_apps = sum(scheduled_stats.values()) if total_apps + count > _TOTAL_SCHEDULED_QUOTA: raise exc.QuotaExceededError( 'Total scheduled apps quota exceeded.') proid_apps = scheduled_stats.get(rsrc_id[:rsrc_id.find('.')], 0) if proid_apps + count > _PROID_SCHEDULED_QUOTA: raise exc.QuotaExceededError( 'Proid scheduled apps quota exceeded.') admin_app = context.GLOBAL.admin.application() if not rsrc: configured = admin_app.get(rsrc_id) else: # Make sure defaults are present configured = admin_app.from_entry(admin_app.to_entry(rsrc)) app.verify_feature(rsrc.get('features', [])) if 'services' in configured and not configured['services']: del configured['services'] if '_id' in configured: del configured['_id'] _LOGGER.info('Configured: %s %r', rsrc_id, configured) _validate(configured) for plugin in self._plugins: configured = plugin.add_attributes(rsrc_id, configured) _check_required_attributes(configured) _set_defaults(configured, rsrc_id) services = { service['name']: service for service in configured.get('services', []) } if not debug_services: debug_services = list(services) if debug else [] for service in debug_services: if service in services: services[service]['downed'] = True _LOGGER.info('Configuring service %s as down', service) else: raise exc.InvalidInputError( __name__, 'Invalid service %s' % service ) scheduled = masterapi.create_apps( zkclient, rsrc_id, configured, count, created_by ) return scheduled