# Path: mongoctl/repository.py
# DEFAULT_SERVERS_FILE = "servers.config"
# DEFAULT_CLUSTERS_FILE = "clusters.config"
# DEFAULT_SERVERS_COLLECTION = "servers"
# DEFAULT_CLUSTERS_COLLECTION = "clusters"
# DEFAULT_ACTIVITY_COLLECTION = "logs.server-activity"
# LOOKUP_TYPE_MEMBER = "members"
# LOOKUP_TYPE_CONFIG_SVR = "configServers"
# LOOKUP_TYPE_SHARDS = "shards"
# LOOKUP_TYPE_ANY = [LOOKUP_TYPE_CONFIG_SVR, LOOKUP_TYPE_MEMBER,
#                    LOOKUP_TYPE_SHARDS]
# MONGOD_SERVER_CLASS_NAME = "mongoctl.objects.mongod.MongodServer"
# MONGOS_ROUTER_CLASS_NAME = "mongoctl.objects.mongos.MongosServer"
# SERVER_TYPE_MAP = {
#     "Mongod": MONGOD_SERVER_CLASS_NAME,
#     "ConfigMongod": MONGOD_SERVER_CLASS_NAME,
#     "Mongos": MONGOS_ROUTER_CLASS_NAME,
#     # === XXX deprecated XXX ===
#     "mongod": MONGOD_SERVER_CLASS_NAME,  # XXX deprecated
#     "mongos": MONGOS_ROUTER_CLASS_NAME,  # XXX deprecated
# }
# def get_mongoctl_database():
# def has_db_repository():
# def has_file_repository():
# def has_commandline_servers_or_clusters():
# def consulting_db_repository():
# def is_db_repository_online():
# def _db_repo_connect():
# def validate_repositories():
# def set_commandline_servers_and_clusters(servers_json_str, clusters_json_str):
# def clear_repository_cache():
# def lookup_server(server_id):
# def lookup_and_validate_server(server_id):
# def db_lookup_server(server_id):
# def config_lookup_server(server_id):
# def lookup_all_servers():
# def db_lookup_all_servers():
# def lookup_and_validate_cluster(cluster_id):
# def lookup_cluster(cluster_id):
# def config_lookup_cluster(cluster_id):
# def db_lookup_cluster(cluster_id):
# def lookup_all_clusters():
# def db_lookup_all_clusters():
# def db_lookup_cluster_by_server(server, lookup_type=LOOKUP_TYPE_ANY):
# def db_lookup_cluster_by_shard(shard):
# def db_lookup_sharded_cluster_by_config_replica(cluster):
# def config_lookup_cluster_by_server(server, lookup_type=LOOKUP_TYPE_ANY):
# def config_lookup_cluster_by_shard(shard):
# def config_lookup_sharded_cluster_by_config_replica(cluster):
# def cluster_has_config_server(cluster, server):
# def cluster_has_shard(cluster, shard):
# def cluster_has_config_replica(cluster, conf_replica):
# def get_configured_servers():
# def get_configured_clusters():
# def validate_cluster(cluster):
# def validate_replicaset_cluster(cluster):
# def validate_sharded_cluster(cluster):
# def lookup_validate_cluster_by_server(server):
# def lookup_cluster_by_server(server, lookup_type=LOOKUP_TYPE_ANY):
# def lookup_cluster_by_shard(shard):
# def lookup_sharded_cluster_by_config_replica(cluster):
# def validate_server(server):
# def get_mongoctl_server_db_collection():
# def get_mongoctl_cluster_db_collection():
# def get_activity_collection():
# def new_server(server_doc):
# def build_server_from_address(address):
# def build_server_from_uri(uri):
# def build_cluster_from_uri(uri):
# def build_server_or_cluster_from_uri(uri):
# def new_servers_dict(docs):
# def new_cluster(cluster_doc):
# def new_replicaset_clusters_dict(docs):
# def new_replicaset_cluster_member(cluster_mem_doc):
# def new_replicaset_cluster_member_list(docs_iteratable):
# def replicaset_cluster_type():
# def sharded_cluster_type():
# 
# Path: mongoctl/utils.py
# def execute_command(command, **kwargs):
# 
#     # Python 2.7+ : Use the new method because i think its better
#     if  hasattr(subprocess, 'check_output'):
#         return subprocess.check_output(command,stderr=subprocess.STDOUT, **kwargs)
#     else: # Python 2.6 compatible, check_output is not available in 2.6
#         return subprocess.Popen(command,
#                                 stdout=subprocess.PIPE,
#                                 stderr=subprocess.STDOUT,
#                                 **kwargs).communicate()[0]


import os
from mongoctl.processes import create_subprocess
from mongoctl.mongoctl_logging import *
from mongoctl import repository
from mongoctl.utils import execute_command
__author__ = 'abdul'



###############################################################################
# CONSTS
###############################################################################
DEFAULT_TAIL_LINES = 15

###############################################################################
# tail log command
###############################################################################
def tail_log_command(parsed_options):
    server = repository.lookup_server(parsed_options.server)
    server.validate_local_op("tail-log")
    log_path = server.get_log_file_path()
    # check if log file exists
    if os.path.exists(log_path):
        log_tailer = tail_server_log(server)
        log_tailer.communicate()
    else:
        log_info("Log file '%s' does not exist." % log_path)


###############################################################################
def tail_server_log(server):

# Path: mongoctl/repository.py
# DEFAULT_SERVERS_FILE = "servers.config"
# DEFAULT_CLUSTERS_FILE = "clusters.config"
# DEFAULT_SERVERS_COLLECTION = "servers"
# DEFAULT_CLUSTERS_COLLECTION = "clusters"
# DEFAULT_ACTIVITY_COLLECTION = "logs.server-activity"
# LOOKUP_TYPE_MEMBER = "members"
# LOOKUP_TYPE_CONFIG_SVR = "configServers"
# LOOKUP_TYPE_SHARDS = "shards"
# LOOKUP_TYPE_ANY = [LOOKUP_TYPE_CONFIG_SVR, LOOKUP_TYPE_MEMBER,
#                    LOOKUP_TYPE_SHARDS]
# MONGOD_SERVER_CLASS_NAME = "mongoctl.objects.mongod.MongodServer"
# MONGOS_ROUTER_CLASS_NAME = "mongoctl.objects.mongos.MongosServer"
# SERVER_TYPE_MAP = {
#     "Mongod": MONGOD_SERVER_CLASS_NAME,
#     "ConfigMongod": MONGOD_SERVER_CLASS_NAME,
#     "Mongos": MONGOS_ROUTER_CLASS_NAME,
#     # === XXX deprecated XXX ===
#     "mongod": MONGOD_SERVER_CLASS_NAME,  # XXX deprecated
#     "mongos": MONGOS_ROUTER_CLASS_NAME,  # XXX deprecated
# }
# def get_mongoctl_database():
# def has_db_repository():
# def has_file_repository():
# def has_commandline_servers_or_clusters():
# def consulting_db_repository():
# def is_db_repository_online():
# def _db_repo_connect():
# def validate_repositories():
# def set_commandline_servers_and_clusters(servers_json_str, clusters_json_str):
# def clear_repository_cache():
# def lookup_server(server_id):
# def lookup_and_validate_server(server_id):
# def db_lookup_server(server_id):
# def config_lookup_server(server_id):
# def lookup_all_servers():
# def db_lookup_all_servers():
# def lookup_and_validate_cluster(cluster_id):
# def lookup_cluster(cluster_id):
# def config_lookup_cluster(cluster_id):
# def db_lookup_cluster(cluster_id):
# def lookup_all_clusters():
# def db_lookup_all_clusters():
# def db_lookup_cluster_by_server(server, lookup_type=LOOKUP_TYPE_ANY):
# def db_lookup_cluster_by_shard(shard):
# def db_lookup_sharded_cluster_by_config_replica(cluster):
# def config_lookup_cluster_by_server(server, lookup_type=LOOKUP_TYPE_ANY):
# def config_lookup_cluster_by_shard(shard):
# def config_lookup_sharded_cluster_by_config_replica(cluster):
# def cluster_has_config_server(cluster, server):
# def cluster_has_shard(cluster, shard):
# def cluster_has_config_replica(cluster, conf_replica):
# def get_configured_servers():
# def get_configured_clusters():
# def validate_cluster(cluster):
# def validate_replicaset_cluster(cluster):
# def validate_sharded_cluster(cluster):
# def lookup_validate_cluster_by_server(server):
# def lookup_cluster_by_server(server, lookup_type=LOOKUP_TYPE_ANY):
# def lookup_cluster_by_shard(shard):
# def lookup_sharded_cluster_by_config_replica(cluster):
# def validate_server(server):
# def get_mongoctl_server_db_collection():
# def get_mongoctl_cluster_db_collection():
# def get_activity_collection():
# def new_server(server_doc):
# def build_server_from_address(address):
# def build_server_from_uri(uri):
# def build_cluster_from_uri(uri):
# def build_server_or_cluster_from_uri(uri):
# def new_servers_dict(docs):
# def new_cluster(cluster_doc):
# def new_replicaset_clusters_dict(docs):
# def new_replicaset_cluster_member(cluster_mem_doc):
# def new_replicaset_cluster_member_list(docs_iteratable):
# def replicaset_cluster_type():
# def sharded_cluster_type():
# 
# Path: mongoctl/utils.py
# def execute_command(command, **kwargs):
# 
#     # Python 2.7+ : Use the new method because i think its better
#     if  hasattr(subprocess, 'check_output'):
#         return subprocess.check_output(command,stderr=subprocess.STDOUT, **kwargs)
#     else: # Python 2.6 compatible, check_output is not available in 2.6
#         return subprocess.Popen(command,
#                                 stdout=subprocess.PIPE,
#                                 stderr=subprocess.STDOUT,
#                                 **kwargs).communicate()[0]


# Path: mongoctl/commands/server/tail_log.py
import os
from mongoctl.processes import create_subprocess
from mongoctl.mongoctl_logging import *
from mongoctl import repository
from mongoctl.utils import execute_command

__author__ = 'abdul'



###############################################################################
# CONSTS
###############################################################################
DEFAULT_TAIL_LINES = 15

###############################################################################
# tail log command
###############################################################################
def tail_log_command(parsed_options):
    server = repository.lookup_server(parsed_options.server)
    server.validate_local_op("tail-log")
    log_path = server.get_log_file_path()
    # check if log file exists
    if os.path.exists(log_path):
        log_tailer = tail_server_log(server)
        log_tailer.communicate()
    else:
        log_info("Log file '%s' does not exist." % log_path)


###############################################################################
def tail_server_log(server):

    try: