# Path: shakedown/dcos/zookeeper.py
# def get_zk_node_children(node_name):
#     znode_url = "{}/exhibitor/exhibitor/v1/explorer/node?key={}".format(dcos_url(), node_name)
#     response = http.get(znode_url)
#     return response.json()
# 
# def get_zk_node_data(node_name):
#     znode_url = "{}/exhibitor/exhibitor/v1/explorer/node-data?key={}".format(dcos_url(), node_name)
#     response = http.get(znode_url)
#     return response.json()
# 
# Path: shakedown/dcos/agent.py
# def kill_process_from_pid_file_on_host(hostname, pid_file='app.pid'):
#     """ Retrieves the PID of a process from a pid file on host and kills it.
# 
#     :param hostname: the hostname or ip address of the host on which the process will be killed
#     :param pid_file: pid file to use holding the pid number to kill
#     """
#     status, pid = run_command_on_agent(hostname, 'cat {}'.format(pid_file))
#     status, stdout = run_command_on_agent(hostname, "sudo kill -9 {}".format(pid))
#     if status:
#         print("Killed pid: {}".format(pid))
#         run_command_on_agent(hostname, 'rm {}'.format(pid_file))
#     else:
#         print("Unable to killed pid: {}".format(pid))
# 
# Path: shakedown/dcos/spinner.py
# def time_wait(
#     predicate,
#     timeout_seconds=120,
#     sleep_seconds=1,
#     ignore_exceptions=True,
#     inverse_predicate=False,
#     noisy=True,
#     required_consecutive_success_count=1):
#     """ waits or spins for a predicate and returns the time of the wait.
#         An exception in the function will be returned.
#         A timeout will throw a TimeoutExpired Exception.
# 
#     """
#     start = time_module.time()
#     wait_for(predicate, timeout_seconds, sleep_seconds, ignore_exceptions, inverse_predicate, noisy, required_consecutive_success_count)
#     return elapse_time(start)
# 
# Path: shakedown/dcos/network.py
# def restore_iptables(host):
# def save_iptables(host):
# def run_iptables(host, rule):
# def flush_all_rules(host):
# def allow_all_traffic(host):
# def iptable_rules(host):


import json
from datetime import timedelta
from shakedown import *
from shakedown.cli.helpers import *
from shakedown.dcos.zookeeper import get_zk_node_children, get_zk_node_data
from shakedown.dcos.agent import kill_process_from_pid_file_on_host
from shakedown.dcos.spinner import time_wait
from shakedown.dcos import network
"""Utilities for working with master"""


DISABLE_MASTER_INCOMING = "-I INPUT -p tcp --dport 5050 -j REJECT"
DISABLE_MASTER_OUTGOING = "-I OUTPUT -p tcp --sport 5050 -j REJECT"


def partition_master(incoming=True, outgoing=True):
    """ Partition master's port alone. To keep DC/OS cluster running.

    :param incoming: Partition incoming traffic to master process. Default True.
    :param outgoing: Partition outgoing traffic from master process. Default True.
    """

    echo('Partitioning master. Incoming:{} | Outgoing:{}'.format(incoming, outgoing))

    network.save_iptables(shakedown.master_ip())

# Path: shakedown/dcos/zookeeper.py
# def get_zk_node_children(node_name):
#     znode_url = "{}/exhibitor/exhibitor/v1/explorer/node?key={}".format(dcos_url(), node_name)
#     response = http.get(znode_url)
#     return response.json()
# 
# def get_zk_node_data(node_name):
#     znode_url = "{}/exhibitor/exhibitor/v1/explorer/node-data?key={}".format(dcos_url(), node_name)
#     response = http.get(znode_url)
#     return response.json()
# 
# Path: shakedown/dcos/agent.py
# def kill_process_from_pid_file_on_host(hostname, pid_file='app.pid'):
#     """ Retrieves the PID of a process from a pid file on host and kills it.
# 
#     :param hostname: the hostname or ip address of the host on which the process will be killed
#     :param pid_file: pid file to use holding the pid number to kill
#     """
#     status, pid = run_command_on_agent(hostname, 'cat {}'.format(pid_file))
#     status, stdout = run_command_on_agent(hostname, "sudo kill -9 {}".format(pid))
#     if status:
#         print("Killed pid: {}".format(pid))
#         run_command_on_agent(hostname, 'rm {}'.format(pid_file))
#     else:
#         print("Unable to killed pid: {}".format(pid))
# 
# Path: shakedown/dcos/spinner.py
# def time_wait(
#     predicate,
#     timeout_seconds=120,
#     sleep_seconds=1,
#     ignore_exceptions=True,
#     inverse_predicate=False,
#     noisy=True,
#     required_consecutive_success_count=1):
#     """ waits or spins for a predicate and returns the time of the wait.
#         An exception in the function will be returned.
#         A timeout will throw a TimeoutExpired Exception.
# 
#     """
#     start = time_module.time()
#     wait_for(predicate, timeout_seconds, sleep_seconds, ignore_exceptions, inverse_predicate, noisy, required_consecutive_success_count)
#     return elapse_time(start)
# 
# Path: shakedown/dcos/network.py
# def restore_iptables(host):
# def save_iptables(host):
# def run_iptables(host, rule):
# def flush_all_rules(host):
# def allow_all_traffic(host):
# def iptable_rules(host):


# Path: shakedown/dcos/master.py
import json
from datetime import timedelta
from shakedown import *
from shakedown.cli.helpers import *
from shakedown.dcos.zookeeper import get_zk_node_children, get_zk_node_data
from shakedown.dcos.agent import kill_process_from_pid_file_on_host
from shakedown.dcos.spinner import time_wait
from shakedown.dcos import network

"""Utilities for working with master"""


DISABLE_MASTER_INCOMING = "-I INPUT -p tcp --dport 5050 -j REJECT"
DISABLE_MASTER_OUTGOING = "-I OUTPUT -p tcp --sport 5050 -j REJECT"


def partition_master(incoming=True, outgoing=True):
    """ Partition master's port alone. To keep DC/OS cluster running.

    :param incoming: Partition incoming traffic to master process. Default True.
    :param outgoing: Partition outgoing traffic from master process. Default True.
    """

    echo('Partitioning master. Incoming:{} | Outgoing:{}'.format(incoming, outgoing))

    network.save_iptables(shakedown.master_ip())

    network.flush_all_rules(shakedown.master_ip())