def vm_zabbix_sync(sender): """Sync all VMs with internal zabbix""" from api.mon.utils import get_mon_vms from api.mon.vm.tasks import mon_vm_sync for vm in get_mon_vms(): logger.debug('Creating zabbix sync task for VM %s', vm) mon_vm_sync.call(sender, vm=vm) mon_all_groups_sync.call(sender=sender)
def mon_sync_all(task_id, dc_id, clear_cache=True, sync_groups=True, sync_nodes=True, sync_vms=True, **kwargs): """ Clear Zabbix cache and sync everything in Zabbix. Related to a specific DC. Triggered by dc_settings_changed signal. """ dc = Dc.objects.get_by_id(int(dc_id)) if clear_cache: logger.info('Clearing zabbix cache in DC %s', dc) mon_clear_zabbix_cache(dc) get_monitoring(dc) # Cache new Zabbix instance for tasks below if sync_groups: logger.info( 'Running monitoring group synchronization for all user groups in DC %s', dc) mon_all_groups_sync.call(task_id, dc_name=dc.name) if sync_nodes: logger.info( 'Running monitoring host synchronization for all compute nodes') for node in Node.all(): mon_node_sync.call(task_id, node_uuid=node.uuid) if sync_vms: logger.info( 'Running monitoring host synchronization for all VMs in DC %s', dc) for vm_uuid in dc.vm_set.values_list('uuid', flat=True): mon_vm_sync.call(task_id, vm_uuid=vm_uuid)
def init_mgmt(head_node, images=None): """ Initialize the system and create the "admin" datacenter. """ from api.dc.views import dc_node, dc_settings, dc_domain from api.network.base.views import net_manage from api.dns.domain.views import dns_domain from api.node.vm.views import harvest_vm admin = settings.VMS_DC_ADMIN main = settings.VMS_DC_MAIN # Admin DC and default DC should already exist (initial_data) admin_dc = Dc.objects.get_by_name(admin) default_dc = Dc.objects.get_by_name(main) # We need some request with admin DC - important for all subsequent commands request = get_dummy_request(admin_dc, method='POST', system_user=True) # All api calls will use the POST method... api_post = partial(_api_cmd, request, 'POST') # ...except net_manage, dns_record and dc_settings api_put = partial(_api_cmd, request, 'PUT') # Initialize images if images and isinstance(images, list): logger.warn('Initializing %d images', len(images)) _init_images(head_node, images, default_dc, admin_dc) else: logger.error('Could not parse initial images or empty initial images') # Create DNS zone for the domain set during head node installation try: admin_zone = head_node.domain_name if admin_zone: api_post(dns_domain, admin_zone, owner=settings.ADMIN_USERNAME, dc_bound=False) except Exception as e: admin_zone = None logger.exception(e) # Setup miscellaneous stuff depending on admin network info try: mgmt_ifconfig = get_local_netinfo() mgmt_ifconfig['vlan_id'] = head_node.vlan_id mgmt_ip = mgmt_ifconfig['addr'] try: mgmt_net = ipaddress.ip_network(u'%(network)s/%(netmask)s' % mgmt_ifconfig) except Exception as exc: logger.exception(exc) else: try: # Create reverse dns domain ptr_zone = reverse_domain_from_network(mgmt_net) api_post(dns_domain, ptr_zone, owner=settings.ADMIN_USERNAME, dc_bound=False) api_post(dc_domain, ptr_zone, dc=main) api_post(dc_domain, ptr_zone, dc=admin) except Exception as exc: logger.exception(exc) else: # Set PTR zone for admin network mgmt_ifconfig['ptr_domain'] = ptr_zone # Change admin network subnet according to ip/netmask/gw on this machine (mgmt01.local) api_put(net_manage, settings.VMS_NET_ADMIN, dns_domain=admin_zone, **mgmt_ifconfig) # Change SITE_LINK and SITE_SIGNATURE both datacenters (#549, #551) site_link = 'https://%s' % mgmt_ip site_signature = settings.SITE_SIGNATURE.replace( settings.SITE_LINK, site_link) api_put(dc_settings, main, SITE_LINK=site_link, SITE_SIGNATURE=site_signature) api_put(dc_settings, admin, SITE_LINK=site_link, SITE_SIGNATURE=site_signature) _es_api_url(site_link) except Exception as e: logger.exception(e) # Add head node + all its storages into admin DC api_post(dc_node, head_node.hostname, strategy=DcNode.SHARED, add_storage=9, dc=admin) logger.warning('Admin datacenter "%s" was successfully initialized', admin_dc) # Harvest all VMs from head node into admin DC while True: ret = api_post(harvest_vm, head_node.hostname, dc=admin) if status.is_success(ret.status_code): logger.info('POST harvest_vm(%s) has started: %s', head_node.hostname, ret.data) break else: logger.error( 'POST harvest_vm(%s) has failed; retrying in 3 seconds', head_node.hostname) sleep(3) # The harvest is performing some other tasks asynchronously during which the node must stay in online state. # So let's sleep for some time to give the tasks some breathing space. logger.info( 'Sleeping for 60 seconds after admin datacenter initialization') sleep(60) # Let's update the default image server after we've harvested the VMS_VM_IMG01 try: if Vm.objects.filter(uuid=settings.VMS_VM_IMG01).exists(): vm_img01_uuid = settings.VMS_VM_IMG01 else: vm_img01_uuid = None if settings.VMS_IMAGE_VM == vm_img01_uuid: logger.info( 'The current image server (VMS_IMAGE_VM) is already set to %s', vm_img01_uuid) else: api_put(dc_settings, main, VMS_IMAGE_VM=vm_img01_uuid) except Exception as e: logger.exception(e) # We can change the default resolvers after we've harvested the VMS_VM_DNS01 (#chili-831) try: try: vm_dns01_ip = Vm.objects.get(uuid=settings.VMS_VM_DNS01).ips[0] except Vm.DoesNotExist: logger.warning('DNS VM (%s) not found - using default DNS servers', settings.VMS_VM_DNS01) else: api_put(dc_settings, main, VMS_VM_RESOLVERS_DEFAULT=[vm_dns01_ip]) api_put(dc_settings, admin, VMS_VM_RESOLVERS_DEFAULT=[vm_dns01_ip]) except Exception as e: logger.exception(e) # Initial user group zabbix synchronization mon_all_groups_sync.call(sender='init_mgmt') return ret