Revision 45ead074

b/snf-cyclades-app/synnefo/logic/reconciliation.py
158 158
        # Fix them
159 159
        if stale and self.options["fix_stale"]:
160 160
            for server_id in stale:
161
                db_server = self.db_servers[server_id]
161
                vm = get_locked_server(server_id)
162 162
                backend_mod.process_op_status(
163
                    vm=db_server,
163
                    vm=vm,
164 164
                    etime=self.event_time,
165 165
                    jobid=-0,
166 166
                    opcode='OP_INSTANCE_REMOVE', status='success',
......
210 210
                      db_server.id)
211 211
        if self.options["fix_unsynced"]:
212 212
            fix_opcode = "OP_INSTANCE_CREATE"
213
            vm = get_locked_server(db_server.id)
213 214
            backend_mod.process_op_status(
214
                vm=db_server,
215
                vm=vm,
215 216
                etime=self.event_time,
216 217
                jobid=-0,
217 218
                opcode=fix_opcode, status='error',
......
224 225
            self.log.info("Server '%s' is '%s' in DB and '%s' in Ganeti.",
225 226
                          server_id, db_server.operstate, gnt_server["state"])
226 227
            if self.options["fix_unsynced"]:
228
                vm = get_locked_server(server_id)
227 229
                # If server is in building state, you will have first to
228 230
                # reconcile it's creation, to avoid wrong quotas
229 231
                if db_server.operstate == "BUILD":
230 232
                    backend_mod.process_op_status(
231
                        vm=db_server, etime=self.event_time, jobid=-0,
233
                        vm=vm, etime=self.event_time, jobid=-0,
232 234
                        opcode="OP_INSTANCE_CREATE", status='success',
233 235
                        logmsg='Reconciliation: simulated Ganeti event')
234 236
                fix_opcode = "OP_INSTANCE_STARTUP"\
235 237
                    if gnt_server["state"] == "STARTED"\
236 238
                    else "OP_INSTANCE_SHUTDOWN"
237 239
                backend_mod.process_op_status(
238
                    vm=db_server, etime=self.event_time, jobid=-0,
240
                    vm=vm, etime=self.event_time, jobid=-0,
239 241
                    opcode=fix_opcode, status='success',
240 242
                    logmsg='Reconciliation: simulated Ganeti event')
241 243
                self.log.debug("Simulated Ganeti state event for server '%s'",
......
259 261
            self.log.info("Server '%s' has flavor '%s' in DB and '%s' in"
260 262
                          " Ganeti", server_id, db_flavor, gnt_flavor)
261 263
            if self.options["fix_unsynced_flavors"]:
262
                old_state = db_server.operstate
264
                vm = get_locked_server(server_id)
265
                old_state = vm.operstate
263 266
                opcode = "OP_INSTANCE_SET_PARAMS"
264 267
                beparams = {"vcpus": gnt_flavor.cpu,
265 268
                            "minmem": gnt_flavor.ram,
266 269
                            "maxmem": gnt_flavor.ram}
267 270
                backend_mod.process_op_status(
268
                    vm=db_server, etime=self.event_time, jobid=-0,
271
                    vm=vm, etime=self.event_time, jobid=-0,
269 272
                    opcode=opcode, status='success',
270 273
                    job_fields={"beparams": beparams},
271 274
                    logmsg='Reconciliation: simulated Ganeti event')
......
301 304
                                         sorted(gnt_nics_parsed.items())))
302 305
            self.log.info(msg, server_id, db_nics_str, gnt_nics_str)
303 306
            if self.options["fix_unsynced_nics"]:
304
                backend_mod.process_net_status(vm=db_server,
307
                vm = get_locked_server(server_id)
308
                backend_mod.process_net_status(vm=vm,
305 309
                                               etime=self.event_time,
306 310
                                               nics=gnt_nics)
307 311

  
......
319 323
                pending_task = True
320 324

  
321 325
        if pending_task:
326
            db_server = get_locked_server(server_id)
327
            if db_server.task_job_id != job_id:
328
                # task has changed!
329
                return
322 330
            self.log.info("Found server '%s' with pending task: '%s'",
323 331
                          server_id, db_server.task)
324 332
            if self.options["fix_pending_tasks"]:
......
577 585
        self.log.info("D: Stale DB entry for network %s in backend %s",
578 586
                      backend_network.network, backend_network.backend)
579 587
        if self.fix:
588
            backend_network = BackendNetwork.objects.select_for_update()\
589
                                                    .get(id=backend_network.id)
580 590
            backend_mod.process_network_status(
581 591
                backend_network, self.event_time, 0,
582 592
                "OP_NETWORK_REMOVE",
......
606 616
        self.log.info("D: Unsynced network %s in backend %s", network, backend)
607 617
        if self.fix:
608 618
            self.log.info("F: Issuing OP_NETWORK_CONNECT")
619
            backend_network = BackendNetwork.objects.select_for_update()\
620
                                                    .get(id=backend_network.id)
609 621
            backend_mod.process_network_status(
610 622
                backend_network, self.event_time, 0,
611 623
                "OP_NETWORK_CONNECT",
......
752 764
    pool_row.available_map = ""
753 765
    pool_row.reserved_map = ""
754 766
    return pool_class(pool_row)
767

  
768

  
769
def get_locked_server(server_id):
770
    return VirtualMachine.objects.select_for_update().get(id=server_id)

Also available in: Unified diff