#!/usr/bin/env python
# Copyright 2016 Yelp Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Usage: ./cleanup_maintenance.py

Clean up boxes that should no longer be marked as 'draining' or 'down' for
maintenance. Also cleanup the associated dynamic reservations.
"""
import argparse
import logging
import sys

from paasta_tools.mesos_maintenance import get_draining_hosts
from paasta_tools.mesos_maintenance import get_hosts_forgotten_down
from paasta_tools.mesos_maintenance import get_hosts_forgotten_draining
from paasta_tools.mesos_maintenance import reserve_all_resources
from paasta_tools.mesos_maintenance import seconds_to_nanoseconds
from paasta_tools.mesos_maintenance import undrain
from paasta_tools.mesos_maintenance import unreserve_all_resources
from paasta_tools.mesos_maintenance import up
from paasta_tools.mesos_tools import get_slaves


log = logging.getLogger(__name__)


def parse_args():
    parser = argparse.ArgumentParser(description='Cleans up forgotten maintenance cruft.')
    args = parser.parse_args()
    return args


def cleanup_forgotten_draining():
    """Clean up hosts forgotten draining"""
    log.debug("Cleaning up hosts forgotten draining")
    hosts_forgotten_draining = get_hosts_forgotten_draining(grace=seconds_to_nanoseconds(10 * 60))
    undrain(hostnames=hosts_forgotten_draining)


def cleanup_forgotten_down():
    """Clean up hosts forgotten down"""
    log.debug("Cleaning up hosts forgotten down")
    hosts_forgotten_down = get_hosts_forgotten_down(grace=seconds_to_nanoseconds(10 * 60))
    up(hostnames=hosts_forgotten_down)


def unreserve_all_resources_on_non_draining_hosts():
    """Unreserve all resources on non-draining hosts"""
    log.debug("Unreserving all resources on non-draining hosts")
    slaves = get_slaves()
    hostnames = [slave['hostname'] for slave in slaves]
    draining_hosts = get_draining_hosts()
    non_draining_hosts = list(set(hostnames) - set(draining_hosts))
    unreserve_all_resources(hostnames=non_draining_hosts)


def reserve_all_resources_on_draining_hosts():
    """Reserve all resources on draining hosts"""
    log.debug("Reserving all resources on draining hosts")
    reserve_all_resources(hostnames=get_draining_hosts())


def main():
    log.debug("Cleaning up maintenance cruft")
    parse_args()

    cleanup_forgotten_draining()
    cleanup_forgotten_down()
    unreserve_all_resources_on_non_draining_hosts()
    reserve_all_resources_on_draining_hosts()


if __name__ == "__main__":
    if main():
        sys.exit(0)
    sys.exit(1)

@@ -1502,16 +1502,135 @@
 ruft.')%0A
+    parser.add_argument('-v', '--verbose', action='store_true',%0A                        dest=%22verbose%22, default=False)%0A
     args
@@ -1897,24 +1897,61 @@
 s(10 * 60))%0A
+    if hosts_forgotten_draining:%0A    
     undrain(
@@ -1986,16 +1986,75 @@
 aining)%0A
+    else:%0A        log.debug(%22No hosts forgotten draining%22)%0A
 %0A%0Adef cl
@@ -2266,41 +2266,129 @@
     
-up(hostnames=hosts_forgotten_down
+if hosts_forgotten_down:%0A        up(hostnames=hosts_forgotten_down)%0A    else:%0A        log.debug(%22No hosts forgotten down%22
 )%0A%0A%0A
@@ -2753,16 +2753,47 @@
 hosts))%0A
+    if non_draining_hosts:%0A    
     unre
@@ -2842,16 +2842,69 @@
 _hosts)%0A
+    else:%0A        log.debug(%22No non-draining hosts%22)%0A
 %0A%0Adef re
@@ -3061,52 +3061,165 @@
     
-reserve_all_resources(hostnames=get_
+draining_hosts = get_draining_hosts()%0A    if draining_hosts:%0A        reserve_all_resources(hostnames=draining_hosts)%0A    else:%0A        log.debug(%22No 
 draining
 _hos
@@ -3214,24 +3214,23 @@
 draining
-_
+ 
 hosts
-()
+%22
 )%0A%0A%0Adef 
@@ -3283,24 +3283,31 @@
  cruft%22)%0A   
+ args =
  parse_args(
@@ -3309,16 +3309,148 @@
 args()%0A%0A
+    if args.verbose:%0A        logging.basicConfig(level=logging.DEBUG)%0A    else:%0A        logging.basicConfig(level=logging.WARNING)%0A%0A
     clea

Make cleanup-maintenance check for empty lists of hosts and support -v