Ejemplo n.º 1
0
def update_lb(conf, tenant_id, lb_id, lb_body):
    lb_ref = db_api.loadbalancer_get(conf, lb_id, tenant_id=tenant_id)
    old_lb_ref = copy.deepcopy(lb_ref)
    db_api.pack_update(lb_ref, lb_body)
    lb_ref = db_api.loadbalancer_update(conf, lb_id, lb_ref)
    if (lb_ref['algorithm'] == old_lb_ref['algorithm'] and
        lb_ref['protocol'] == old_lb_ref['protocol']):
        LOG.debug("In LB %r algorithm and protocol have not changed, "
                     "nothing to do on the device %r.",
                     lb_ref['id'], lb_ref['device_id'])
        return

    sf_ref = db_api.serverfarm_get_all_by_lb_id(conf, lb_ref['id'])[0]
    if lb_ref['algorithm'] != old_lb_ref['algorithm']:
        predictor_ref = db_api.predictor_get_by_sf_id(conf, sf_ref['id'])
        db_api.predictor_update(conf, predictor_ref['id'],
                                {'type': lb_ref['algorithm']})

    vips = db_api.virtualserver_get_all_by_sf_id(conf, sf_ref['id'])
    if lb_ref['protocol'] != old_lb_ref['protocol']:
        vip_update_values = {'protocol': lb_ref['protocol']}
        for vip in vips:
            db_api.pack_update(vip, vip_update_values)
            db_api.virtualserver_update(conf, vip['id'], vip)

    servers = db_api.server_get_all_by_sf_id(conf, sf_ref['id'])
    probes = db_api.probe_get_all_by_sf_id(conf, sf_ref['id'])
    stickies = db_api.sticky_get_all_by_sf_id(conf, sf_ref['id'])

    device_ref = scheduler.reschedule(conf, lb_ref)
    if device_ref['id'] != lb_ref['device_id']:
        from_driver = drivers.get_device_driver(conf, lb_ref['device_id'])
        to_driver = drivers.get_device_driver(conf, device_ref['id'])
        lb_ref = db_api.loadbalancer_update(conf, lb_ref['id'],
                                            {'device_id': device_ref['id']})
    else:
        from_driver = drivers.get_device_driver(conf, device_ref['id'])
        to_driver = from_driver

    with from_driver.request_context() as ctx:
        try:
            commands.delete_loadbalancer(ctx, sf_ref, vips, servers, probes,
                                         stickies)
        except Exception:
            with utils.save_and_reraise_exception():
                db_api.loadbalancer_update(conf, lb_ref['id'],
                                           {'status': lb_status.ERROR})
    with to_driver.request_context() as ctx:
        try:
            commands.create_loadbalancer(ctx, sf_ref, vips, servers, probes,
                                         stickies)
        except Exception:
            with utils.save_and_reraise_exception():
                db_api.loadbalancer_update(conf, lb_ref['id'],
                                           {'status': lb_status.ERROR})
    db_api.loadbalancer_update(conf, lb_ref['id'],
                               {'status': lb_status.ACTIVE})
 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)
Ejemplo n.º 3
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)
Ejemplo n.º 4
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