Exemplo n.º 1
0
def create_loadbalancer(ctx, balancer, nodes, probes, vips):
    lb = db_api.unpack_extra(balancer)
    sf = db_api.serverfarm_create(ctx.conf, {'lb_id': lb['id']})
    if 'algorithm' in lb:
        predictor_params = {'sf_id': sf['id'], 'type': lb['algorithm']}
    else:
        predictor_params = {'sf_id': sf['id']}
    db_api.predictor_create(ctx.conf, predictor_params)
    create_server_farm(ctx, sf)
    for node in nodes:
        node_values = db_api.server_pack_extra(node)
        node_values['sf_id'] = sf['id']
        rs_ref = db_api.server_create(ctx.conf, node_values)
        create_rserver(ctx, rs_ref)
        add_rserver_to_server_farm(ctx, sf, rs_ref)

    for probe in probes:
        probe_values = db_api.probe_pack_extra(probe)
        probe_values['lb_id'] = lb['id']
        probe_values['sf_id'] = sf['id']
        probe_ref = db_api.probe_create(ctx.conf, probe_values)
        create_probe(ctx,  probe_ref)
        add_probe_to_server_farm(ctx, sf, probe_ref)

    for vip in vips:
        vip_values = db_api.virtualserver_pack_extra(vip)
        vip_values['lb_id'] = lb['id']
        vip_values['sf_id'] = sf['id']
        vip_ref = db_api.virtualserver_create(ctx.conf, vip_values)
        create_vip(ctx, vip_ref, sf)
 def test_predictor_get_all_by_sf_id(self):
     values = get_fake_predictor("1")
     predictor1 = db_api.predictor_create(self.conf, values)
     values = get_fake_predictor("2")
     predictor2 = db_api.predictor_create(self.conf, values)
     predictors1 = db_api.predictor_get_all_by_sf_id(self.conf, "1")
     predictors2 = db_api.predictor_get_all_by_sf_id(self.conf, "2")
     self.assertEqual([dict(predictor1.iteritems())], [dict(pr.iteritems()) for pr in predictors1])
     self.assertEqual([dict(predictor2.iteritems())], [dict(pr.iteritems()) for pr in predictors2])
Exemplo n.º 3
0
 def test_predictor_get_all_by_sf_id(self):
     values = get_fake_predictor('1')
     predictor1 = db_api.predictor_create(self.conf, values)
     values = get_fake_predictor('2')
     predictor2 = db_api.predictor_create(self.conf, values)
     predictors1 = db_api.predictor_get_all_by_sf_id(self.conf, '1')
     predictors2 = db_api.predictor_get_all_by_sf_id(self.conf, '2')
     self.assertEqual(len(predictors1), 1)
     self.assertEqual(predictors1[0]['id'], predictor1['id'])
     self.assertEqual(len(predictors2), 1)
     self.assertEqual(predictors2[0]['id'], predictor2['id'])
Exemplo n.º 4
0
 def test_predictor_get_by_sf_id(self):
     values1 = get_fake_predictor('1')
     values2 = get_fake_predictor('2')
     predictor1 = db_api.predictor_create(self.conf, values1)
     predictor2 = db_api.predictor_create(self.conf, values2)
     predictor_ref1 = db_api.predictor_get_by_sf_id(self.conf, '1')
     predictor_ref2 = db_api.predictor_get_by_sf_id(self.conf, '2')
     self.assertEqual(dict(predictor_ref1.iteritems()),
                      dict(predictor1.iteritems()))
     self.assertEqual(dict(predictor_ref2.iteritems()),
                      dict(predictor2.iteritems()))
Exemplo n.º 5
0
 def test_predictor_destroy_by_sf_id(self):
     values = get_fake_predictor('1')
     predictor_ref1 = db_api.predictor_create(self.conf, values)
     values = get_fake_predictor('2')
     predictor_ref2 = db_api.predictor_create(self.conf, values)
     db_api.predictor_destroy_by_sf_id(self.conf, '1')
     with self.assertRaises(exception.PredictorNotFound) as cm:
         db_api.predictor_get(self.conf, predictor_ref1['id'])
     err = cm.exception
     self.assertEqual(err.kwargs, {'predictor_id': predictor_ref1['id']})
     predictor_ref3 = db_api.predictor_get(self.conf, predictor_ref2['id'])
     self.assertEqual(predictor_ref3['id'], predictor_ref2['id'])
 def test_predictor_destroy_by_sf_id(self):
     values = get_fake_predictor("1")
     predictor_ref1 = db_api.predictor_create(self.conf, values)
     values = get_fake_predictor("2")
     predictor_ref2 = db_api.predictor_create(self.conf, values)
     db_api.predictor_destroy_by_sf_id(self.conf, "1")
     with self.assertRaises(exception.PredictorNotFound) as cm:
         db_api.predictor_get(self.conf, predictor_ref1["id"])
     err = cm.exception
     self.assertEqual(err.kwargs, {"predictor_id": predictor_ref1["id"]})
     predictor_ref3 = db_api.predictor_get(self.conf, predictor_ref2["id"])
     self.assertEqual(dict(predictor_ref2.iteritems()), dict(predictor_ref3.iteritems()))
Exemplo n.º 7
0
def create_lb(conf, params):
    node_values = params.pop('nodes', [])
    probe_values = params.pop('healthMonitor', [])
    vip_values = params.pop('virtualIps', [])
    lb_values = db_api.loadbalancer_pack_extra(params)

    lb_ref = db_api.loadbalancer_create(conf, lb_values)
    sf_ref = db_api.serverfarm_create(conf, {'lb_id': lb_ref['id']})
    db_api.predictor_create(conf, {'sf_id': sf_ref['id'],
                                   'type': lb_ref['algorithm']})
    vip_update_values = {'protocol': lb_ref['protocol']}

    vips = []
    for vip in vip_values:
        vip = db_api.virtualserver_pack_extra(vip)
        db_api.pack_update(vip, vip_update_values)
        vip['lb_id'] = lb_ref['id']
        vip['sf_id'] = sf_ref['id']
        vips.append(db_api.virtualserver_create(conf, vip))

    servers = []
    for server in node_values:
        server = db_api.server_pack_extra(server)
        server['sf_id'] = sf_ref['id']
        servers.append(db_api.server_create(conf, server))

    probes = []
    for probe in probe_values:
        probe = db_api.probe_pack_extra(probe)
        probe['lb_id'] = lb_ref['id']
        probe['sf_id'] = sf_ref['id']
        probes.append(db_api.probe_create(conf, probe))

    device_ref = scheduler.schedule(conf, lb_ref)
    db_api.loadbalancer_update(conf, lb_ref['id'],
                               {'device_id': device_ref['id']})
    device_driver = drivers.get_device_driver(conf, device_ref['id'])
    with device_driver.request_context() as ctx:
        try:
            commands.create_loadbalancer(ctx, sf_ref, vips, servers, probes,
                                         [])
        except Exception:
            with utils.save_and_reraise_exception():
                db_api.loadbalancer_update(conf, lb_ref['id'],
                                           {'status': lb_status.ERROR,
                                            'deployed': False})
    db_api.loadbalancer_update(conf, lb_ref['id'],
                               {'status': lb_status.ACTIVE,
                                'deployed': True})
    return lb_ref['id']
Exemplo n.º 8
0
 def test_predictor_create(self):
     values = get_fake_predictor('1')
     predictor_ref = db_api.predictor_create(self.conf, values)
     predictor = dict(predictor_ref.iteritems())
     self.assertIsNotNone(predictor['id'])
     values['id'] = predictor['id']
     self.assertEqual(predictor, values)
Exemplo n.º 9
0
 def test_predictor_destroy(self):
     values = get_fake_predictor('1')
     predictor = db_api.predictor_create(self.conf, values)
     db_api.predictor_destroy(self.conf, predictor['id'])
     with self.assertRaises(exception.PredictorNotFound) as cm:
         db_api.predictor_get(self.conf, predictor['id'])
     err = cm.exception
     self.assertEqual(err.kwargs, {'predictor_id': predictor['id']})
Exemplo n.º 10
0
 def test_predictor_update(self):
     values = get_fake_predictor("1")
     predictor_ref = db_api.predictor_create(self.conf, values)
     update = {"type": "LEASTCONNECTIONS", "extra": {"extrafield": "extravalue"}}
     predictor_ref = db_api.predictor_update(self.conf, predictor_ref["id"], update)
     predictor = dict(predictor_ref.iteritems())
     values.update(update)
     self.assertIsNotNone(predictor["id"])
     values["id"] = predictor["id"]
     self.assertEqual(predictor, values)
Exemplo n.º 11
0
 def test_predictor_update(self):
     values = get_fake_predictor('1')
     predictor_ref = db_api.predictor_create(self.conf, values)
     update = {'type': 'LEASTCONNECTIONS',
               'extra': {'extrafield': 'extravalue'}}
     predictor_ref = db_api.predictor_update(self.conf, predictor_ref['id'],
                                             update)
     predictor = dict(predictor_ref.iteritems())
     values.update(update)
     self.assertIsNotNone(predictor['id'])
     values['id'] = predictor['id']
     self.assertEqual(predictor, values)
Exemplo n.º 12
0
 def test_predictor_get(self):
     values = get_fake_predictor('1')
     predictor_ref1 = db_api.predictor_create(self.conf, values)
     predictor_ref2 = db_api.predictor_get(self.conf, predictor_ref1['id'])
     self.assertEqual(dict(predictor_ref1.iteritems()),
                      dict(predictor_ref2.iteritems()))
Exemplo n.º 13
0
    def savetoDB(self):
        try:
            lb_ref = db_api.loadbalancer_update(self.conf, self.lb['id'],
                                                self.lb)
        except exception.LoadBalancerNotFound:
            lb_ref = db_api.loadbalancer_create(self.conf, self.lb)

        self.sf['lb_id'] = lb_ref['id']
        try:
            sf_ref = db_api.serverfarm_update(self.conf, self.sf['id'],
                                              self.sf)
        except exception.ServerFarmNotFound:
            sf_ref = db_api.serverfarm_create(self.conf, self.sf)

        self.sf._predictor['sf_id'] = sf_ref['id']
        try:
            db_api.predictor_update(self.conf, self.sf._predictor['id'],
                                    self.sf._predictor)
        except exception.PredictorNotFound:
            db_api.predictor_create(self.conf, self.sf._predictor)

        stickies = self.sf._sticky
        vips = []

        self.sf = sf_ref
        self.sf._rservers = []
        self.sf._probes = []
        self.sf._sticky = []

        for rs in self.rs:
            rs['sf_id'] = sf_ref['id']
            try:
                rs_ref = db_api.server_update(self.conf, rs['id'], rs)
            except exception.ServerNotFound:
                rs_ref = db_api.server_create(self.conf, rs)
            self.sf._rservers.append(rs_ref)

        for pr in self.probes:
            pr['sf_id'] = sf_ref['id']
            try:
                pr_ref = db_api.probe_update(self.conf, pr['id'], pr)
            except exception.ProbeNotFound:
                pr_ref = db_api.probe_create(self.conf, pr)
            self.sf._probes.append(pr_ref)

        for vip in self.vips:
            vip['sf_id'] = sf_ref['id']
            vip['lb_id'] = lb_ref['id']
            try:
                vip_ref = db_api.virtualserver_update(self.conf, vip['id'],
                                                      vip)
            except exception.VirtualServerNotFound:
                vip_ref = db_api.virtualserver_create(self.conf, vip)
            vips.append(vip_ref)

        for st in stickies:
            st['sf_id'] = sf_ref['id']
            try:
                st_ref = db_api.sticky_update(self.conf, st['id'], st)
            except exception.StickyNotFound:
                st_ref = db_api.sticky_create(self.conf, st)
            self.sf._sticky.append(st_ref)

        self.rs = self.sf._rservers
        self.probes = self.sf._probes
        self.vips = vips