def import_links(self): self.verbose('retrieving links from old mysql DB...') self.old_links = list(OldLink.objects.all()) self.message('retrieved %d links' % len(self.old_links)) saved_links = [] for old_link in self.old_links: skip = False try: interface_a = Interface.objects.get(pk=old_link.from_interface_id) if interface_a.type != INTERFACE_TYPES.get('wireless'): interface_a.type = INTERFACE_TYPES.get('wireless') interface_a.save() except Interface.DoesNotExist: self.message('Interface #%s does not exist, probably link #%s is orphan!' % (old_link.from_interface_id, old_link.id)) skip = True try: interface_b = Interface.objects.get(pk=old_link.to_interface_id) if interface_b.type != INTERFACE_TYPES.get('wireless'): interface_b.type = INTERFACE_TYPES.get('wireless') interface_b.save() except Interface.DoesNotExist: self.message('Interface #%s does not exist, probably link #%s is orphan!' % (old_link.to_interface_id, old_link.id)) skip = True if skip: self.verbose('Skipping to next cycle') continue old_bandwidth = [old_link.sync_tx, old_link.sync_rx] link = Link(**{ "id": old_link.id, "interface_a": interface_a, "interface_b": interface_b, "status": LINK_STATUS.get('active'), "type": LINK_TYPES.get('radio'), "metric_type": 'etx', "metric_value": old_link.etx, "dbm": old_link.dbm, "min_rate": min(old_bandwidth), "max_rate": max(old_bandwidth), }) if old_link.hide: link.access_level = 3 try: link.full_clean() link.save() saved_links.append(link) self.verbose('Saved link %s' % link) except Exception as e: self.message('Could not save link %s, got exception: %s' % (old_link.id, e)) self.message('saved %d links into local DB' % len(saved_links)) self.saved_links = saved_links
def import_links(self): if not IMPORT_LINKS: self.message('skpped import_links because NODESHOT_OLDIMPORTER_IMPORT_LINKS is set to False') return self.verbose('retrieving links from old mysql DB...') self.old_links = list(OldLink.objects.all()) self.message('retrieved %d links' % len(self.old_links)) saved_links = [] for old_link in self.old_links: skip = False try: interface_a = Interface.objects.get(pk=old_link.from_interface_id) if interface_a.type != INTERFACE_TYPES.get('wireless'): interface_a.type = INTERFACE_TYPES.get('wireless') interface_a.save() except Interface.DoesNotExist: self.message('Interface #%s does not exist, probably link #%s is orphan!' % (old_link.from_interface_id, old_link.id)) skip = True try: interface_b = Interface.objects.get(pk=old_link.to_interface_id) if interface_b.type != INTERFACE_TYPES.get('wireless'): interface_b.type = INTERFACE_TYPES.get('wireless') interface_b.save() except Interface.DoesNotExist: self.message('Interface #%s does not exist, probably link #%s is orphan!' % (old_link.to_interface_id, old_link.id)) skip = True if skip: self.verbose('Skipping to next cycle') continue old_bandwidth = [old_link.sync_tx, old_link.sync_rx] link = Link(**{ "id": old_link.id, "interface_a": interface_a, "interface_b": interface_b, "status": LINK_STATUS.get('active'), "type": LINK_TYPES.get('radio'), "metric_type": 'etx', "metric_value": old_link.etx, "dbm": old_link.dbm, "min_rate": min(old_bandwidth), "max_rate": max(old_bandwidth), "data": { "imported": "true" } }) # if link already exists flag it for UPDATE instead of INSERT try: Link.objects.get(pk=old_link.id) link._state.adding = False except Link.DoesNotExist: pass if old_link.hide: link.access_level = 3 try: link.full_clean() link.save() saved_links.append(link) self.verbose('Saved link %s' % link) except Exception: tb = traceback.format_exc() self.message('Could not save link %s, got exception:\n\n%s' % (old_link.id, tb)) self.message('saved %d links into local DB' % len(saved_links)) self.saved_links = saved_links