#!/bin/env python
from sys import exit
import argparse
from jenkins_control import JenkinsControl


if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        description=(
            'Control jenkins nodes'
        )
    )
    parser.add_argument(
        '-u',
        '--url',
        type=str,
        help='base url of the jenkins instance'
    )

    nodes = parser.add_mutually_exclusive_group(required=True)
    nodes.add_argument(
        '-c',
        '--computer',
        action='append',
        type=str,
        help='the node to check for'
    )
    nodes.add_argument(
        '-l',
        '--label',
        type=str,
        help='the label to check for'
    )

    group = parser.add_mutually_exclusive_group()
    group.add_argument(
        '-j',
        '--job',
        type=str,
        help='returns "True" if job is running on the node, otherwise False'
    )
    group.add_argument(
        '-s',
        '--status',
        action='store_true',
        help='print the status of the node'
    )
    group.add_argument(
        '-o',
        '--offline',
        action='store_true',
        help='mark the node temporarily offline'
    )
    group.add_argument(
        '-a',
        '--agent',
        action='store_true',
        help='launch slave agent on node'
    )

    parser.add_argument(
        '-r',
        '--reason',
        type=str,
        default='',
        help='offline reason'
    )
    args = parser.parse_args()

    jenkins = JenkinsControl(args.url)

    if args.label is not None:
        computers = jenkins.get_computers_in_label(args.label)
    else:
        computers = args.computer

    for computer in computers:
        if args.job:
            print(jenkins.job_running_on_computer(args.job, computer))
        elif args.status:
            jenkins.status(computer)
        elif args.offline:
            jenkins.toggle_offline(computer, args.reason)
        elif args.reason != '':
            jenkins.change_offline_cause(computer, args.reason)
        elif args.agent:
            jenkins.launch_slave_agent(computer)
        else:
            print("You need to specify a command")
            exit(1)
