def action_destroy(exts, rsrc): """ Reclaim any resources released. """ try: rsrc_name = rsrc.name LOGGER.info("%s: action_destroy started %s %s", rsrc.pool.name, rsrc.pool.host.product, rsrc.name) ext1 = exts[rsrc.pool.host.product] pool = ext1.pool_get(rsrc.pool) pool1 = rsrc.pool algo.resource_destroy(pool, rsrc) ## # If all of the resources have been removed and the max is zero then # remove the resource. if pool1.deleteable(): pool1.delete() LOGGER.info("%s: action_destroy pool deleted", pool1.name) else: algo.adapt(pool, pool1) ## LOGGER.info("%s: action_destroy %s done", pool1.name, rsrc_name) except Exception, arg: LOGGER.debug("action_destroy %s interrupted", rsrc_name) LOGGER.exception(arg) delta = pool.timing_get(api.Pool.TIMING_REQUEST_DESTROY) rsrc.transition(rsrc.status, rsrc.action, delta)
def adapt(exts): """ Check to see if the pools should change. """ LOGGER.info("adapt started") for pool1 in models.Pool.objects.all(): if POOL_LOGGER: POOL_LOGGER.info(pool=pool1.name, resource_count=pool1.resource_available(), resource_max=pool1.resource_max) ext1 = exts[pool1.host.product] pool = ext1.pool_get(pool1) algo.adapt(pool, pool1) LOGGER.info("adapt ended")
def test_push_too_many(self): """ test_push_too_many""" pool_name = "fake.pool" (host1, _) = models.Host.objects.get_or_create(connection="localhost", product="fake") (pool1, _) = models.Pool.objects.get_or_create( name="fake.pool", host=host1, template_name="test.template", resource_max=10) pool = api.pool_get(pool1) self.assertTrue(pool) algo.destroy(pool, pool1) rtc = algo.adapt(pool, pool1) self.assertEqual(rtc, 10) rsrc = algo.pop(pool_name, 1) self.assertTrue(rsrc) algo.push(rsrc.id) with self.assertRaises(algo.ResourceReleased): algo.push(rsrc.id) api_exts = ext.api_ext_list() server.adapt(api_exts)
def action_clone(exts, rsrc): """ Clone a new resource. """ rsrc_name = rsrc.name LOGGER.info("%s: action_clone started %s %s", rsrc.pool.name, rsrc.pool.host.product, rsrc.name) try: ext1 = exts[rsrc.pool.host.product] pool = ext1.pool_get(rsrc.pool) pool1 = rsrc.pool algo.resource_clone(pool, rsrc) algo.adapt(pool, rsrc.pool) LOGGER.info("%s: action_clone %s done", pool1.name, rsrc_name) except Exception: LOGGER.exception("action_clone %s interrupted", rsrc.name) delta = pool.timing_get(api.Pool.TIMING_REQUEST_DESTROY) rsrc.transition(rsrc.status, rsrc.action, delta) LOGGER.info("%s: action_clone done", rsrc.pool.name)
def test_setup(self): """ test clone """ (host1, _) = models.Host.objects.get_or_create(connection="localhost", product="fake") (pool1, _) = models.Pool.objects.get_or_create( name="fake.pool", host=host1, template_name="test.template", resource_max=10) pool = api.Pool("fake.pool") algo.destroy(pool, pool1) rtc = algo.adapt(pool, pool1) self.assertEqual(rtc, 10)
def action_attr(exts, rsrc): """ Retrieve attributes. """ LOGGER.info("%s: action_attr started %s %s", rsrc.pool.name, rsrc.pool.host.product, rsrc.name) ## # If resource expires reclaim it. ext1 = exts[rsrc.pool.host.product] pool = ext1.pool_get(rsrc.pool) rsrc.ip_addr = pool.ip_get(rsrc.name) if rsrc.ip_addr: LOGGER.info("%s: resource %s ip %s", rsrc.pool.name, rsrc.name, rsrc.ip_addr) delta = pool.timing_get(api.Pool.TIMING_REQUEST_NONE) rsrc.transition(models.Resource.READY, algo.ACTION_NONE, delta) algo.adapt(pool, rsrc.pool) else: LOGGER.info("%s: resource %s waiting for ip addr", rsrc.pool.name, rsrc.name) rsrc.transition(rsrc.status, rsrc.action, 60) ## LOGGER.info("%s: action_attr ended", rsrc.pool.name)
def test_pop(self): """ test_pop Popping resources. """ (host1, _) = models.Host.objects.get_or_create(connection="localhost", product="fake") (pool1, _) = models.Pool.objects.get_or_create( name="fake.pool", host=host1, template_name="test.template", resource_max=10) pool = api.Pool("fake.pool") algo.destroy(pool, pool1) rtc = algo.adapt(pool, pool1) self.assertEqual(rtc, 10) for count in range(10): logging.debug("pop count %d", count) rsrc = algo.pop("fake.pool", 1) self.assertTrue(rsrc) with self.assertRaises(algo.NoResources): algo.pop("fake.pool", 1)