def cleanup_vms(texts, max_hours=24, providers=None, prompt=True): providers = providers or list_all_providers() providers_data = cfme_data.get("management_systems", {}) delta = datetime.timedelta(hours=int(max_hours)) vms_to_delete = defaultdict(set) thread_queue = [] # precompile regexes matchers = [re.compile(text) for text in texts] for provider_key in providers: provider_type = providers_data[provider_key].get('type', None) thread = Thread(target=process_provider_vms, args=(provider_key, provider_type, matchers, delta, vms_to_delete)) # Mark as daemon thread for easy-mode KeyboardInterrupt handling thread.daemon = True thread_queue.append(thread) thread.start() # Join the queued calls for thread in thread_queue: thread.join() for provider_key, vm_set in vms_to_delete.items(): print('{}:'.format(provider_key)) for vm_name, vm_delta in vm_set: days, hours = vm_delta.days, vm_delta.seconds / 3600 print(' {} is {} days, {} hours old'.format(vm_name, days, hours)) if vms_to_delete and prompt: yesno = raw_input('Delete these VMs? [y/N]: ') if str(yesno).lower() != 'y': print('Exiting.') return 0 if not vms_to_delete: print('No VMs to delete.') thread_queue = [] for provider_key, vm_set in vms_to_delete.items(): thread = Thread(target=delete_provider_vms, args=(provider_key, [name for name, t_delta in vm_set])) # Mark as daemon thread for easy-mode KeyboardInterrupt handling thread.daemon = True thread_queue.append(thread) thread.start() for thread in thread_queue: thread.join() print("Deleting finished")
def cleanup_vms(texts, max_hours=24, providers=None, prompt=True): providers = providers or list_all_providers() delta = datetime.timedelta(hours=int(max_hours)) vms_to_delete = defaultdict(set) thread_queue = [] # precompile regexes matchers = [re.compile(text) for text in texts] for provider_key in providers: thread = Thread(target=process_provider_vms, args=(provider_key, matchers, delta, vms_to_delete)) # Mark as daemon thread for easy-mode KeyboardInterrupt handling thread.daemon = True thread_queue.append(thread) thread.start() # Join the queued calls for thread in thread_queue: thread.join() for provider_key, vm_set in vms_to_delete.items(): print '%s:' % provider_key for vm_name, vm_delta in vm_set: days, hours = vm_delta.days, vm_delta.seconds / 3600 print ' %s is %d days, %s hours old' % (vm_name, days, hours) if vms_to_delete and prompt: yesno = raw_input('Delete these VMs? [y/N]: ') if str(yesno).lower() != 'y': print 'Exiting.' return 0 if not vms_to_delete: print 'No VMs to delete.' for provider_key, vm_set in vms_to_delete.items(): provider = provider_factory(provider_key) for vm_name, __ in vm_set: print 'Deleting %s on %s' % (vm_name, provider_key) try: provider.delete_vm(vm_name) except Exception as ex: print 'Failed to delete %s on %s' % (vm_name, provider_key) logger.exception(ex)
def cleanup_vms(texts, max_hours=24, providers=None, prompt=True): providers = providers or list_all_providers() delta = datetime.timedelta(hours=int(max_hours)) vms_to_delete = defaultdict(set) thread_queue = [] # precompile regexes matchers = [re.compile(text) for text in texts] for provider_key in providers: thread = Thread(target=process_provider_vms, args=(provider_key, matchers, delta, vms_to_delete)) # Mark as daemon thread for easy-mode KeyboardInterrupt handling thread.daemon = True thread_queue.append(thread) thread.start() # Join the queued calls for thread in thread_queue: thread.join() for provider_key, vm_set in vms_to_delete.items(): print '%s:' % provider_key for vm_name, vm_delta in vm_set: days, hours = vm_delta.days, vm_delta.seconds / 3600 print ' %s is %d days, %s hours old' % (vm_name, days, hours) if vms_to_delete and prompt: yesno = raw_input('Delete these VMs? [y/N]: ') if str(yesno).lower() != 'y': print 'Exiting.' return 0 if not vms_to_delete: print 'No VMs to delete.' for provider_key, vm_set in vms_to_delete.items(): provider = get_mgmt(provider_key) for vm_name, __ in vm_set: print 'Deleting %s on %s' % (vm_name, provider_key) try: provider.delete_vm(vm_name) except Exception as ex: print 'Failed to delete %s on %s' % (vm_name, provider_key) logger.exception(ex)
def main(trackerbot_url, mark_usable=None): api = trackerbot.api(trackerbot_url) thread_q = [] thread_lock = Lock() template_providers = defaultdict(list) # Queue up list_template calls for provider_key in list_all_providers(): if provider_key.startswith('ec2') or provider_key.startswith('rhevm'): continue thread = Thread(target=get_provider_templates, args=(provider_key, template_providers, thread_lock)) thread_q.append(thread) thread.start() # Join the queued calls for thread in thread_q: thread.join() # Find some templates and update the API for template_name, providers in template_providers.items(): try: stream, datestamp = trackerbot.parse_template(template_name) except ValueError: # No matches continue group = trackerbot.Group(stream) template = trackerbot.Template(template_name, group, datestamp) for provider_key in providers: provider = trackerbot.Provider(provider_key) try: trackerbot.mark_provider_template(api, provider, template, usable=mark_usable, tested=False) print 'template %s updated -- %s %s %r, marked usable: %s' % ( template, stream, datestamp, providers, bool(mark_usable)) except SlumberHttpBaseException as ex: print ex.response.status_code, ex.content
def main(trackerbot_url, mark_usable=None): api = trackerbot.api(trackerbot_url) thread_q = [] thread_lock = Lock() template_providers = defaultdict(list) all_providers = set(list_all_providers()) unresponsive_providers = set() # Queue up list_template calls for provider_key in all_providers: thread = Thread(target=get_provider_templates, args=(provider_key, template_providers, unresponsive_providers, thread_lock)) thread_q.append(thread) thread.start() # Join the queued calls for thread in thread_q: thread.join() seen_templates = set() if mark_usable is None: usable = {} else: usable = {'usable': mark_usable} existing_provider_templates = [ pt['id'] for pt in trackerbot.depaginate(api, api.providertemplate.get())['objects']] # Find some templates and update the API for template_name, providers in template_providers.items(): template_name = str(template_name) group_name, datestamp, stream = trackerbot.parse_template(template_name) # Don't want sprout templates if group_name in ('sprout', 'rhevm-internal'): print 'Ignoring %s from group %s' % (template_name, group_name) continue seen_templates.add(template_name) group = trackerbot.Group(group_name, stream=stream) template = trackerbot.Template(template_name, group, datestamp) for provider_key in providers: provider = trackerbot.Provider(provider_key) if '{}_{}'.format(template_name, provider_key) in existing_provider_templates: print 'Template %s already tracked for provider %s' % (template_name, provider_key) continue try: trackerbot.mark_provider_template(api, provider, template, **usable) print 'Added %s template %s on provider %s (datestamp: %s)' % ( group_name, template_name, provider_key, datestamp) except SlumberHttpBaseException as ex: print ex.response.status_code, ex.content # Remove provider relationships where they no longer exist, skipping unresponsive providers, # and providers not known to this environment for pt in trackerbot.depaginate(api, api.providertemplate.get())['objects']: provider_key, template_name = pt['provider']['key'], pt['template']['name'] if provider_key not in template_providers[template_name] \ and provider_key not in unresponsive_providers: if provider_key in all_providers: print "Cleaning up template %s on %s" % (template_name, provider_key) trackerbot.delete_provider_template(api, provider_key, template_name) else: print "Skipping template cleanup %s on unknown provider %s" % ( template_name, provider_key) # Remove templates that aren't on any providers anymore for template in trackerbot.depaginate(api, api.template.get())['objects']: if not template['providers']: print "Deleting template %s (no providers)" % template['name'] api.template(template['name']).delete()
def main(trackerbot_url, mark_usable=None): api = trackerbot.api(trackerbot_url) thread_q = [] thread_lock = Lock() template_providers = defaultdict(list) all_providers = set(list_all_providers()) unresponsive_providers = set() # Queue up list_template calls for provider_key in all_providers: thread = Thread(target=get_provider_templates, args=(provider_key, template_providers, unresponsive_providers, thread_lock)) thread_q.append(thread) thread.start() # Join the queued calls for thread in thread_q: thread.join() seen_templates = set() if mark_usable is None: usable = {} else: usable = {'usable': mark_usable} existing_provider_templates = [pt['id'] for pt in api.providertemplate.get(limit=0)['objects']] # Find some templates and update the API for template_name, providers in template_providers.items(): template_name = str(template_name) group_name, datestamp, stream = trackerbot.parse_template(template_name) # Don't want sprout templates if group_name in ('sprout', 'rhevm-internal'): print 'Ignoring %s from group %s' % (template_name, group_name) continue seen_templates.add(template_name) group = trackerbot.Group(group_name, stream=stream) template = trackerbot.Template(template_name, group, datestamp) for provider_key in providers: provider = trackerbot.Provider(provider_key) if '{}_{}'.format(template_name, provider_key) in existing_provider_templates: print 'Template %s already tracked for provider %s' % (template_name, provider_key) continue try: trackerbot.mark_provider_template(api, provider, template, **usable) print 'Added %s template %s on provider %s (datestamp: %s)' % ( group_name, template_name, provider_key, datestamp) except SlumberHttpBaseException as ex: print ex.response.status_code, ex.content # Remove provider relationships where they no longer exist, skipping unresponsive providers, # and providers not known to this environment for pt in api.providertemplate.get(limit=0)['objects']: provider_key, template_name = pt['provider']['key'], pt['template']['name'] if provider_key not in template_providers[template_name] \ and provider_key not in unresponsive_providers: if provider_key in all_providers: print "Cleaning up template %s on %s" % (template_name, provider_key) trackerbot.delete_provider_template(api, provider_key, template_name) else: print "Skipping template cleanup %s on unknown provider %s" % ( template_name, provider_key) # Remove templates that aren't on any providers anymore for template in api.template.get(limit=0)['objects']: if not template['providers']: print "Deleting template %s (no providers)" % template['name'] api.template(template['name']).delete()