# Path: mongoctl/utils.py
# def document_pretty_string(document):
#     return json.dumps(document, indent=4, default=json_util.default)
# 
# def wait_for(predicate, timeout=None, sleep_duration=2, grace=True):
#     start_time = now()
#     must_retry = may_retry = not predicate()
# 
#     if must_retry and grace:
#         # optimizing for predicates whose first invocations may be slooooooow
#         log_verbose("GRACE: First eval finished in %d secs - resetting timer." %
#                     (now() - start_time))
#         start_time = now()
# 
#     while must_retry and may_retry:
# 
#         must_retry = not predicate()
#         if must_retry:
#             net_time = now() - start_time
#             if timeout and net_time + sleep_duration > timeout:
#                 may_retry = False
#             else:
#                 left = "[-%d sec] " % (timeout - net_time) if timeout else ""
#                 log_info("-- waiting %s--" % left)
#                 time.sleep(sleep_duration)
# 
#     return not must_retry
# 
# def kill_process(pid, force=False):
#     sig = signal.SIGKILL if force else signal.SIGTERM
#     try:
#         os.kill(pid, sig)
#         return True
#     except OSError:
#         return False
# 
# def is_pid_alive(pid):
# 
#     try:
#         os.kill(pid,0)
#         return True
#     except OSError:
#         return False
# 
# Path: mongoctl/errors.py
# class MongoctlException(Exception):
#     def __init__(self, message, cause=None):
#         super(MongoctlException, self).__init__(message)
#         self._cause = cause


from bson.son import SON
from mongoctl.utils import (
    document_pretty_string, wait_for, kill_process, is_pid_alive
)
from mongoctl.mongoctl_logging import *
from mongoctl.errors import MongoctlException
from start import server_stopped_predicate
from mongoctl.prompt import prompt_execute_task
import mongoctl.repository
import mongoctl.objects.server
        shutdown_success = mongo_stop_server(server, pid, force=False)

    if not can_stop_mongoly or not shutdown_success:
        log_verbose("  ... taking more forceful measures ... ")
        shutdown_success = \
            prompt_or_force_stop_server(server, pid, force,
                                        try_mongo_force=can_stop_mongoly)

    if shutdown_success:
        log_info("Server '%s' has stopped." % server.id)
    else:
        raise MongoctlException("Unable to stop server '%s'." %
                                server.id)

###############################################################################
def step_down_if_needed(server, force):
    ## if server is a primary replica member then step down
    if server.is_primary():
        if force:
            step_server_down(server, force)
        else:
            prompt_step_server_down(server, force)

###############################################################################
def mongo_stop_server(server, pid, force=False):

    try:
        shutdown_cmd = SON( [('shutdown', 1),('force', force)])
        log_info("\nSending the following command to %s:\n%s\n" %
                 (server.get_connection_address(),

# Path: mongoctl/utils.py
# def document_pretty_string(document):
#     return json.dumps(document, indent=4, default=json_util.default)
# 
# def wait_for(predicate, timeout=None, sleep_duration=2, grace=True):
#     start_time = now()
#     must_retry = may_retry = not predicate()
# 
#     if must_retry and grace:
#         # optimizing for predicates whose first invocations may be slooooooow
#         log_verbose("GRACE: First eval finished in %d secs - resetting timer." %
#                     (now() - start_time))
#         start_time = now()
# 
#     while must_retry and may_retry:
# 
#         must_retry = not predicate()
#         if must_retry:
#             net_time = now() - start_time
#             if timeout and net_time + sleep_duration > timeout:
#                 may_retry = False
#             else:
#                 left = "[-%d sec] " % (timeout - net_time) if timeout else ""
#                 log_info("-- waiting %s--" % left)
#                 time.sleep(sleep_duration)
# 
#     return not must_retry
# 
# def kill_process(pid, force=False):
#     sig = signal.SIGKILL if force else signal.SIGTERM
#     try:
#         os.kill(pid, sig)
#         return True
#     except OSError:
#         return False
# 
# def is_pid_alive(pid):
# 
#     try:
#         os.kill(pid,0)
#         return True
#     except OSError:
#         return False
# 
# Path: mongoctl/errors.py
# class MongoctlException(Exception):
#     def __init__(self, message, cause=None):
#         super(MongoctlException, self).__init__(message)
#         self._cause = cause


# Path: mongoctl/commands/server/stop.py
from bson.son import SON
from mongoctl.utils import (
    document_pretty_string, wait_for, kill_process, is_pid_alive
)
from mongoctl.mongoctl_logging import *
from mongoctl.errors import MongoctlException
from start import server_stopped_predicate
from mongoctl.prompt import prompt_execute_task
import mongoctl.repository
import mongoctl.objects.server

        shutdown_success = mongo_stop_server(server, pid, force=False)

    if not can_stop_mongoly or not shutdown_success:
        log_verbose("  ... taking more forceful measures ... ")
        shutdown_success = \
            prompt_or_force_stop_server(server, pid, force,
                                        try_mongo_force=can_stop_mongoly)

    if shutdown_success:
        log_info("Server '%s' has stopped." % server.id)
    else:
        raise MongoctlException("Unable to stop server '%s'." %
                                server.id)

###############################################################################
def step_down_if_needed(server, force):
    ## if server is a primary replica member then step down
    if server.is_primary():
        if force:
            step_server_down(server, force)
        else:
            prompt_step_server_down(server, force)

###############################################################################
def mongo_stop_server(server, pid, force=False):

    try:
        shutdown_cmd = SON( [('shutdown', 1),('force', force)])
        log_info("\nSending the following command to %s:\n%s\n" %
                 (server.get_connection_address(),

                  document_pretty_string(shutdown_cmd)))