def task_user_callback_cb(task_id, parent_task_id, cb, **kwargs): """ Task for calling remote url in user defined callback """ try: obj = get_task_object(kwargs) except ObjectDoesNotExist: obj = None user = User.objects.get(id=user_id_from_task_id(parent_task_id)) payload, status = get_task_status(parent_task_id) try: response = UserCallback(parent_task_id).request( cb, user.callback_key, payload) except RequestException as ex: status = states.FAILURE details = ex else: status = states.SUCCESS details = str(response.status_code) + ': ' + response.reason if cb.get('cb_log'): task_log(parent_task_id, LOG_REMOTE_CALLBACK, obj=obj, task_status=status, detail=details)
def task_status_loop(request, response, task_id, stream=None): """ Loop and check for task result. Send keep-alive whitespace for every loop. TODO: Trailing headers are not supported, so status code cannot be changed. """ logger.debug('Starting APISyncMiddleware loop (%s)', request.path) logger.info('Waiting for pending task %s status in "%s" streaming loop', task_id, stream) elapsed = 0 task_status_request = set_request_method(request, 'GET') # Task check loop while elapsed < settings.API_SYNC_TIMEOUT: if elapsed > 1200: nap = 30 elif elapsed > 60: nap = 12 elif elapsed > 30: nap = 6 elif elapsed > 5: nap = 3 else: nap = 1 sleep(nap) elapsed += nap result, status = get_task_status(task_id, get_task_result) logger.debug('APISyncMiddleware loop (%s) status: %s', request.path, status) if status != HTTP_201_CREATED: logger.debug('APISyncMiddleware loop finished (%s) in %d seconds', request.path, elapsed) logger.info('Task %s finished', task_id) res = task_status(task_status_request, task_id=task_id) if stream == ES_STREAM_CLIENT: yield '%d\n' % res.status_code # new status code (es knows how to interpret this) yield res.rendered_content # will call render() break else: yield ' ' # keep-alive else: # Timeout logger.debug('APISyncMiddleware loop finished with timeout (%s)', request.path) logger.warning('Task %s is running too long; no point to wait', task_id) yield response # is already rendered
def __init__(self, request, task_id, data=None, **kwargs): result, kwargs['status'] = get_task_status(task_id, get_task_result) self.task_status = False # APISyncMiddleware should wait for the task if data is not None and 'cb_url' in data: cb = data else: cb = None super(TaskResponse, self).__init__(request, result, task_id, callback=cb, **kwargs) self['es_task_response'] = 'true'
def __init__(self, request, task_id, **kwargs): result, status = get_task_status(task_id, get_task_successful) super(TaskDoneResponse, self).__init__(request, result, status, **kwargs)
def __init__(self, request, task_id, **kwargs): result, status = get_task_status(task_id, get_task_result) super(TaskStatusResponse, self).__init__(request, result, status, **kwargs) self['es_task_response'] = 'true'
def init_mgmt(head_node, images=None): # noqa: R701 """ 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.dns.record.views import dns_record 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, but: # we don't support reverse domains that are not rounded to classful boundary # (with the exception of networks smaller that C) if mgmt_net.prefixlen % 8 == 0 or mgmt_net.prefixlen > 24: 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) # Set PTR zone for admin network mgmt_ifconfig['ptr_domain'] = ptr_zone except Exception as exc: logger.exception(exc) # 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): harvest_vm_task_id = ret.data.get('task_id') logger.info('POST harvest_vm(%s) has started with task ID: %s', head_node.hostname, harvest_vm_task_id) break else: logger.error( 'POST harvest_vm(%s) has failed; retrying in 3 seconds', head_node.hostname) sleep(3) continue # The harvest is performing some other tasks asynchronously during which the node must stay in online state. while True: sleep(3) result, status_code = get_task_status(harvest_vm_task_id) if status_code == status.HTTP_201_CREATED: logger.info( 'POST harvest_vm(%s) has not finished yet; sleeping for 3 seconds', head_node.hostname) continue else: logger.info('POST harvest_vm(%s) has finished with: %s', head_node.hostname, result) break logger.warning( 'Admin datacenter "%s" service VMs were successfully harvested', admin_dc) # 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]) api_put(net_manage, settings.VMS_NET_ADMIN, resolvers=[vm_dns01_ip]) api_post(dns_record, settings.DNS_MGMT_DOMAIN, 0, name=settings.DNS_NAMESERVERS[0], type='A', content=vm_dns01_ip) except Exception as e: logger.exception(e) # Initial user group zabbix synchronization try: mon_all_groups_sync.call('init_mgmt') except Exception as e: logger.exception(e) return ret