<commit_before>#!/usr/bin/env python
"""Usage: ./marathon_servceinit.py [-v] <servicename> <stop|start|restart|status>

Interacts with the Marathon API to start/stop/restart/status a service.
Assumes that the credentials are available, so must run as root.

"""
import argparse
import sys
import logging

from paasta_tools import marathon_tools
from paasta_tools.paasta_cli.utils import PaastaColors
from paasta_tools.monitoring.replication_utils import get_replication_for_services
from paasta_tools.mesos_tools import get_running_mesos_tasks_for_service

log = logging.getLogger('__main__')
log.addHandler(logging.StreamHandler(sys.stdout))

SYNAPSE_HOST_PORT = "localhost:3212"


def parse_args():
    parser = argparse.ArgumentParser(description='Runs start/stop/restart/status an existing Marathon service.')
    parser.add_argument('-v', '--verbose', action='store_true',
                        dest="verbose", default=False)
    parser.add_argument('service_instance', help='Instance to operate on. Eg: example_service.main')
    command_choices = ['start', 'stop', 'restart', 'status']
    parser.add_argument('command', choices=command_choices, help='Command to run. Eg: status')
    args = parser.parse_args()
    return args


def validate_service_instance(service, instance, cluster):
    log.info("Operating on cluster: %s" % cluster)
    all_services = marathon_tools.get_marathon_services_for_cluster(cluster)
    if (service, instance) not in all_services:
        log.info(all_services)
        print "Error: %s.%s doesn't look like it has been deployed to this cluster! (%s)" % (service, instance, cluster)
        sys.exit(3)
    return True


def start_marathon_job(service, instance, app_id, normal_instance_count, client):
    name = PaastaColors.cyan("%s.%s" % (service, instance))
    print "Scaling %s up to %d instances" % (name, normal_instance_count)
    client.scale_app(app_id, instances=normal_instance_count, force=True)


def stop_marathon_job(service, instance, app_id, client):
    name = PaastaColors.cyan("%s.%s" % (service, instance))
    print "Scaling %s down to 0 instances" % (name)
    client.scale_app(app_id, instances=0, force=True)


def restart_marathon_job(service, instance, app_id, normal_instance_count, client):
    stop_marathon_job(service, instance, app_id, client)
    start_marathon_job(service, instance, app_id, normal_instance_count, client)


def status_marathon_job(service, instance, app_id, normal_instance_count, client):
    name = PaastaColors.cyan("%s.%s" % (service, instance))
    if marathon_tools.is_app_id_running(app_id, client):
        app = client.get_app(app_id)
        running_instances = app.tasks_running
        if len(app.deployments) == 0:
            deploy_status = PaastaColors.bold("Running")
        else:
            deploy_status = PaastaColors.yellow("Deploying")
        if running_instances >= normal_instance_count:
            status = PaastaColors.green("Healthy")
            instance_count = PaastaColors.green("(%d/%d)" % (running_instances, normal_instance_count))
        else:
            status = PaastaColors.yellow("Warning")
            instance_count = PaastaColors.red("(%d/%d)" % (running_instances, normal_instance_count))
        return "Marathon:   %s - up with %s instances. Status: %s." % (status, instance_count, deploy_status)
    else:
        red_not = PaastaColors.red("NOT")
        status = PaastaColors.red("Critical")
        return "Marathon:   %s: - %s (app %s) is %s running in Marathon." % (status, name, app_id, red_not)


def haproxy_backend_report(normal_instance_count, up_backends):
    """ Given that a service is in smartstack, this returns a human readable
    report of the up backends """
    if up_backends >= normal_instance_count:
        status = PaastaColors.green("Healthy")
        count = PaastaColors.green("(%d/%d)" % (up_backends, normal_instance_count))
    elif up_backends == 0:
        status = PaastaColors.red("Critical")
        count = PaastaColors.red("(%d/%d)" % (up_backends, normal_instance_count))
    else:
        status = PaastaColors.yellow("Warning")
        count = PaastaColors.yellow("(%d/%d)" % (up_backends, normal_instance_count))<commit_msg>Fix paasta_serviceinit output to show the correct color for marathon status<commit_after>#!/usr/bin/env python
"""Usage: ./marathon_servceinit.py [-v] <servicename> <stop|start|restart|status>

Interacts with the Marathon API to start/stop/restart/status a service.
Assumes that the credentials are available, so must run as root.

"""
import argparse
import sys
import logging

from paasta_tools import marathon_tools
from paasta_tools.paasta_cli.utils import PaastaColors
from paasta_tools.monitoring.replication_utils import get_replication_for_services
from paasta_tools.mesos_tools import get_running_mesos_tasks_for_service

log = logging.getLogger('__main__')
log.addHandler(logging.StreamHandler(sys.stdout))

SYNAPSE_HOST_PORT = "localhost:3212"


def parse_args():
    parser = argparse.ArgumentParser(description='Runs start/stop/restart/status an existing Marathon service.')
    parser.add_argument('-v', '--verbose', action='store_true',
                        dest="verbose", default=False)
    parser.add_argument('service_instance', help='Instance to operate on. Eg: example_service.main')
    command_choices = ['start', 'stop', 'restart', 'status']
    parser.add_argument('command', choices=command_choices, help='Command to run. Eg: status')
    args = parser.parse_args()
    return args


def validate_service_instance(service, instance, cluster):
    log.info("Operating on cluster: %s" % cluster)
    all_services = marathon_tools.get_marathon_services_for_cluster(cluster)
    if (service, instance) not in all_services:
        log.info(all_services)
        print "Error: %s.%s doesn't look like it has been deployed to this cluster! (%s)" % (service, instance, cluster)
        sys.exit(3)
    return True


def start_marathon_job(service, instance, app_id, normal_instance_count, client):
    name = PaastaColors.cyan("%s.%s" % (service, instance))
    print "Scaling %s up to %d instances" % (name, normal_instance_count)
    client.scale_app(app_id, instances=normal_instance_count, force=True)


def stop_marathon_job(service, instance, app_id, client):
    name = PaastaColors.cyan("%s.%s" % (service, instance))
    print "Scaling %s down to 0 instances" % (name)
    client.scale_app(app_id, instances=0, force=True)


def restart_marathon_job(service, instance, app_id, normal_instance_count, client):
    stop_marathon_job(service, instance, app_id, client)
    start_marathon_job(service, instance, app_id, normal_instance_count, client)


def status_marathon_job(service, instance, app_id, normal_instance_count, client):
    name = PaastaColors.cyan("%s.%s" % (service, instance))
    if marathon_tools.is_app_id_running(app_id, client):
        app = client.get_app(app_id)
        running_instances = app.tasks_running
        if len(app.deployments) == 0:
            deploy_status = PaastaColors.bold("Running")
        else:
            deploy_status = PaastaColors.yellow("Deploying")
        if running_instances >= normal_instance_count:
            status = PaastaColors.green("Healthy")
            instance_count = PaastaColors.green("(%d/%d)" % (running_instances, normal_instance_count))
        elif running_instances == 0:
            status = PaastaColors.yellow("Critical")
            instance_count = PaastaColors.red("(%d/%d)" % (running_instances, normal_instance_count))
        else:
            status = PaastaColors.yellow("Warning")
            instance_count = PaastaColors.yellow("(%d/%d)" % (running_instances, normal_instance_count))
        return "Marathon:   %s - up with %s instances. Status: %s." % (status, instance_count, deploy_status)
    else:
        red_not = PaastaColors.red("NOT")
        status = PaastaColors.red("Critical")
        return "Marathon:   %s: - %s (app %s) is %s running in Marathon." % (status, name, app_id, red_not)


def haproxy_backend_report(normal_instance_count, up_backends):
    """ Given that a service is in smartstack, this returns a human readable
    report of the up backends """
    if up_backends >= normal_instance_count:
        status = PaastaColors.green("Healthy")
        count = PaastaColors.green("(%d/%d)" % (up_backends, normal_instance_count))
    elif up_backends == 0:
        status = PaastaColors.red("Critical")
        count = PaastaColors.red("(%d/%d)" % (up_backends, normal_instance_count))
    else:
        status = PaastaColors.yellow("Warning")
        count = PaastaColors.yellow("(%d/%d)" % (up_backends, normal_instance_count))
{'fraction_non_alphanumeric': 0.11434487518693201, 'fraction_numerical': 0.002185666628321638, 'mean_word_length': 4.196652719665272, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5511377', 'n_tokens_mistral': 2707, 'n_tokens_neox': 2662, 'n_words': 626}