def add_hosts(self, ip): host_list = nova_hosts.host_list() for host in host_list: if not asset_models.Host.objects.filter(hostname=host): # 监测该主机是否存在数据库中 asset_models.Host.objects.create(hostname=host) # 添加INFO事件 event_hw_node.node_info( host, 'add host: %s ' % host ) log_info = 'Create Host: %s to DB' % ' '.join(host_list) log.info(log_info) self.add_hosts_ip(ip)
def add_hosts_ip(self, ip): host_db_all = nova_hosts.host_db_list() for host in host_db_all: if host.ip_manager and host.ip_pxe and host.ip_storage: # 如果IP地址存在就表明该主机的IP地址存在了 continue # 通过hosts文件获取其他主机的管理IP地址 discover_host_name_comm = "ssh %s cat /etc/hosts |grep %s |awk '{print $1}'" % (ip, host.hostname) ac = opentack_ansible.CmmAndRun(host=ip, cmd=discover_host_name_comm) data = ac.start() # pdb.set_trace() # status, manage_ip_out = commands.getstatusoutput(discover_host_name_comm) if not data: # 如果执行错误就return 结束 return 'comm error: %s' % discover_host_name_comm # import pdb # pdb.set_trace() host_ip_info = self.get_host_ip(data) # import pdb # pdb.set_trace() host.ip_manager = host_ip_info['br-mgmt'] host.ip_pxe = host_ip_info['br-fw-admin'] host.ip_storage = host_ip_info['br-storage'] host.ip_public = host_ip_info['br-ex'] host.save() event_hw_node.node_info( host.hostname, 'add host ip: %s %s %s %s ' % ( host_ip_info['br-mgmt'], host_ip_info['br-fw-admin'], host_ip_info['br-storage'], host_ip_info['br-ex'], ) ) log_info = ('Save Host:%s to DB' % host.hostname) log.info(log_info)