'Attempt to shutdown the instance gracefully.
:param instance: The instance to be shutdown
:param timeout: How long to wait in seconds for the instance to
shutdown
:param retry_interval: How often in seconds to signal the instance
to shutdown while waiting
:returns: True if the shutdown succeeded'

def _clean_shutdown(self, instance, timeout, retry_interval):

    SHUTDOWN_STATES = [power_state.SHUTDOWN, power_state.CRASHED]
    try:
        guest = self._host.get_guest(instance)
    except exception.InstanceNotFound:
        return True
    state = guest.get_power_state(self._host)
    if (state in SHUTDOWN_STATES):
        LOG.info('Instance    already    shutdown.', instance=instance)
        return True
    LOG.debug('Shutting    down    instance    from    state    %s', state, instance=instance)
    guest.shutdown()
    retry_countdown = retry_interval
    for sec in range(timeout):
        guest = self._host.get_guest(instance)
        state = guest.get_power_state(self._host)
        if (state in SHUTDOWN_STATES):
            LOG.info('Instance    shutdown    successfully    after    %d    seconds.', sec, instance=instance)
            return True
        if (retry_countdown == 0):
            retry_countdown = retry_interval
            try:
                LOG.debug('Instance    in    state    %s    after    %d    seconds    -    resending    shutdown', state, sec, instance=instance)
                guest.shutdown()
            except libvirt.libvirtError:
                LOG.debug('Ignoring    libvirt    exception    from    shutdown    request.', instance=instance)
                continue
        else:
            retry_countdown -= 1
        time.sleep(1)
    LOG.info('Instance    failed    to    shutdown    in    %d    seconds.', timeout, instance=instance)
    return False
