def register_backend(self): name = gethostname() local_ip = None backend = BackendServer.objects(name=name).first() if not backend: for local_ip in local_ipv4_addresses(): backend = BackendServer.objects(ip=local_ip).first() if backend: break if not backend and not local_ip: log.error(_("No IP address found for local backend!")) return if backend: if backend.autodetect: self.update_backend(backend) else: log.info(_("Local backend not found, registering as '{name}' " "with IP {ip}").format(name=name, ip=local_ip)) backend = BackendServer(name=name, ip=local_ip, is_enabled=False, cpu_cores=cpu_count(), memory_mb=backend_total_memory()) backend.save() return backend
def update_backend(self, backend): if backend.ip == IP('127.0.0.1'): # allow using localhost as backend IP, don't auto update # it in such case return updates = {} local_ips = local_ipv4_addresses() if backend.ip not in [IP(ip) for ip in local_ips]: local_ip = local_ips[0] log.info(_("Updating IP for {name} from {oldip} to " "{newip}").format(name=backend.name, oldip=backend.ip, newip=local_ip)) updates['set__ip'] = local_ip cpus = cpu_count() if backend.cpu_cores != cpus: log.info(_("Updating cpu cores for {name} from {oldcpus} to " "{cpus}").format(name=backend.name, oldcpus=backend.cpu_cores or 0, cpus=cpus)) updates['set__cpu_cores'] = cpus mem = backend_total_memory() if mem: # convert to MB mem = mem / 1024 / 1024 if backend.memory_mb != mem: log.info(_( "Updating memory size for {name} from {oldmem}MB to " "{mem}MB").format(name=backend.name, oldmem=backend.memory_mb or 0, mem=mem)) updates['set__memory_mb'] = mem else: log.error(_("Can't find local backend physical memory size, " "is /proc mounted ?")) if updates: backend.update(**updates) backend.reload()