# Path: cfme/utils/dockerbot/dockerbot.py
# def _dgci(d, key):
# def _name(docker_info):
#     def docker_client(self):
#     def __del__(self):
#     def process_bindings(self, bindings):
#     def wait(self):
#     def stop(self):
#     def remove(self):
#     def kill(self):
#     def __init__(self, bindings, image, dry_run=False):
#     def run(self):
#     def __init__(self, name, bindings, env, log_path, links, pytest_con, artifactor_dir,
#                  dry_run=False):
#     def run(self):
#     def __init__(self, **args):
#     def cache_files(self):
#     def get_base_branch(self, pr):
#     def get_dev_branch(self, pr=None):
#     def get_pr_metadata(self, pr=None):
#     def find_files_by_pr(self, pr=None):
#     def check_arg(self, name, default):
#     def validate_args(self):
#     def display_banner(self):
#     def process_appliance(self):
#     def create_pytest_command(self):
#     def enc_key(self):
#     def create_pytest_envvars(self):
#     def handle_pr(self):
#     def create_log_path(self):
#     def create_pytest_bindings(self):
#     def handle_watch(self):
#     def handle_output(self):
# class DockerInstance:
# class SeleniumDocker(DockerInstance):
# class PytestDocker(DockerInstance):
# class DockerBot:


import json
import logging
import traceback
import fauxfactory
import pika
import requests
from datetime import datetime
from slumber.exceptions import HttpClientError
from cfme.utils.appliance import Appliance
from cfme.utils.conf import docker as docker_conf
from cfme.utils.dockerbot import dockerbot
from cfme.utils.log import setup_logger
from cfme.utils.trackerbot import api
def vm_reaper():
    """ Iterates through each task in the db which has not yet been cleaned and runs the reaper

    This function iterates through each task. If the task is either failed or passed, ie, the
    task has completed, then the VM is cleaned up and then the docker container. If both of
    these operations occur, then the cleanup is set to True.
    """
    tasks = tapi.task().get(cleanup=False, limit=0)['objects']
    for task in tasks:
        if task['result'] in ["failed", "passed", "invalid"]:
            vm_cleanup = False
            docker_cleanup = False

            if task['provider'] == "Sprout" and task['vm_name'] == "Sprout":
                vm_cleanup = True
            else:
                if task['provider'] and task['vm_name']:
                    logger.info('Cleaning up {} on {}'.format(task['vm_name'], task['provider']))
                    if task['vm_name'] == "None":
                        vm_cleanup = True
                    else:
                        appliance = Appliance.from_provider(task['provider'], task['vm_name'])
                        try:
                            if appliance.does_vm_exist():
                                logger.info(f"Destroying {appliance.vm_name}")
                                appliance.destroy()
                            vm_cleanup = True
                        except Exception:
                            logger.info('Exception occured cleaning up')


# Path: cfme/utils/dockerbot/dockerbot.py
# def _dgci(d, key):
# def _name(docker_info):
#     def docker_client(self):
#     def __del__(self):
#     def process_bindings(self, bindings):
#     def wait(self):
#     def stop(self):
#     def remove(self):
#     def kill(self):
#     def __init__(self, bindings, image, dry_run=False):
#     def run(self):
#     def __init__(self, name, bindings, env, log_path, links, pytest_con, artifactor_dir,
#                  dry_run=False):
#     def run(self):
#     def __init__(self, **args):
#     def cache_files(self):
#     def get_base_branch(self, pr):
#     def get_dev_branch(self, pr=None):
#     def get_pr_metadata(self, pr=None):
#     def find_files_by_pr(self, pr=None):
#     def check_arg(self, name, default):
#     def validate_args(self):
#     def display_banner(self):
#     def process_appliance(self):
#     def create_pytest_command(self):
#     def enc_key(self):
#     def create_pytest_envvars(self):
#     def handle_pr(self):
#     def create_log_path(self):
#     def create_pytest_bindings(self):
#     def handle_watch(self):
#     def handle_output(self):
# class DockerInstance:
# class SeleniumDocker(DockerInstance):
# class PytestDocker(DockerInstance):
# class DockerBot:


# Path: cfme/utils/dockerbot/check_prs.py
import json
import logging
import traceback
import fauxfactory
import pika
import requests
from datetime import datetime
from slumber.exceptions import HttpClientError
from cfme.utils.appliance import Appliance
from cfme.utils.conf import docker as docker_conf
from cfme.utils.dockerbot import dockerbot
from cfme.utils.log import setup_logger
from cfme.utils.trackerbot import api

def vm_reaper():
    """ Iterates through each task in the db which has not yet been cleaned and runs the reaper

    This function iterates through each task. If the task is either failed or passed, ie, the
    task has completed, then the VM is cleaned up and then the docker container. If both of
    these operations occur, then the cleanup is set to True.
    """
    tasks = tapi.task().get(cleanup=False, limit=0)['objects']
    for task in tasks:
        if task['result'] in ["failed", "passed", "invalid"]:
            vm_cleanup = False
            docker_cleanup = False

            if task['provider'] == "Sprout" and task['vm_name'] == "Sprout":
                vm_cleanup = True
            else:
                if task['provider'] and task['vm_name']:
                    logger.info('Cleaning up {} on {}'.format(task['vm_name'], task['provider']))
                    if task['vm_name'] == "None":
                        vm_cleanup = True
                    else:
                        appliance = Appliance.from_provider(task['provider'], task['vm_name'])
                        try:
                            if appliance.does_vm_exist():
                                logger.info(f"Destroying {appliance.vm_name}")
                                appliance.destroy()
                            vm_cleanup = True
                        except Exception:
                            logger.info('Exception occured cleaning up')


            containers = dockerbot.dc.containers(all=True)