<commit_before>#!/usr/bin/python

import argparse
import os
import sys
from azure.servicemanagement import ServiceManagementService
from azuremonitor.publishsettings import PublishSettings


def handle_args():
    parser = argparse.ArgumentParser(description='Check Azure.',
                                     epilog='(c) MS Open Tech')
    parser.add_argument(
        '-p', '--publish-settings',
        required=True,
        help='.publishsettings file to authenticate with azure',
        dest='psfile')
    parser.add_argument('-v', '--verbose', action='count', default=0)
    parser.add_argument('--version', action='version', version='%(prog)s 0.1')
    return parser.parse_args()


def check_for_errors(service):
    hosted_services = service.list_hosted_services()
    errors = []
    for service in hosted_services:
        hosted_service = service.get_hosted_service_properties(
            service.service_name, embed_detail=True)
        for deployment in hosted_service.deployments:
            if deployment.status != 'Running':
                errors.append(
                    'Hosted Service {}, Deployment {}, Status: {}'.format(
                        hosted_service.service_name, deployment.name,
                        deployment.status))
            for role_inst in deployment.role_instance_list:
                if role_inst.power_state != 'Started':
                    errors.append(
                        'Hosted Service {}, Deployment {}, Role {}, Power: {}'.format(
                            hosted_service.service_name, deployment.name,
                            role_inst.role_name, role_inst.power_state))
                if role_inst.instance_status != 'ReadyRole':
                    errors.append(
                        'Hosted Service {}, Deployment {}, Role {}, Status: {}'.format(
                            hosted_service.service_name, deployment.name,
                            role_inst.role_name, role_inst.instance_status))
    return errors


def print_errors(errors, verbosity):
    if errors:
        if verbosity == 0:
            print 'Errors!'
        else:
            if len(errors) == 1:
                print errors[0]
            else:
                print 'Multiple errors'
                if verbosity >= 2:
                    for error in errors:
                        print error
        return 2
    else:
        print 'All cool'
        return 0


def main():
    args = handle_args()

    if args.verbose >= 3:
        print 'Converting publishsettings.'
    try:
        publishsettings = PublishSettings(args.psfile)
    except Exception, error:
        print 'Publishsettings file is not good'
        print error
        sys.exit(1)
    pem_path = publishsettings.write_pem()
    if args.verbose >= 3:
        print 'Pem file saved to temp file {}'.format(pem_path)
        print 'Azure sub id {}'.format(publishsettings.sub_id)

    service = ServiceManagementService(
        subscription_id=publishsettings.sub_id,
        cert_file=pem_path)
    errors = check_for_errors(service)
    if args.verbose >= 3:
        print 'Azure status retreived.'

    os.unlink(pem_path)
    if args.verbose >= 3:
        print 'Deleted pem.'

    ret_val = print_errors(errors, args.verbose)
    sys.exit(ret_val)

if __name__ == '__main__':
    main()<commit_msg>Update to take hostname to check, improve output.<commit_after>#!/usr/bin/python

import argparse
import os
import sys
import azure
from azure.servicemanagement import ServiceManagementService
from azuremonitor.publishsettings import PublishSettings


def handle_args():
    parser = argparse.ArgumentParser(description='Check Azure Compute',
                                     epilog='(c) MS Open Tech')
    parser.add_argument('hostname', help='hosted service to check')
    parser.add_argument(
        '-p', '--publish-settings',
        required=True,
        help='.publishsettings file to authenticate with azure',
        dest='psfile')
    parser.add_argument('-v', '--verbose', action='count', 
                        default=0, help='verbosity')
    parser.add_argument('--version', action='version', version='%(prog)s 0.1')
    return parser.parse_args()


def check_for_errors(service, hostname):
    #hosted_services = service.list_hosted_services()
    errors = []
    try:
        service = service.get_hosted_service_properties(
            hostname,
            embed_detail=True)
    except azure.WindowsAzureMissingResourceError, error:
        errors.append('Hosted service {0} not found'.format(hostname))
        return errors
    if service.hosted_service_properties.status != 'Created':
        errors.append('Service status: {0}'.format(service.hosted_service_properties.status))
    if not service.deployments:
        errors.append('No deployments found')
    for deployment in service.deployments:
        if deployment.status != 'Running':
            errors.append('Deployment status: {0}'.format(deployment.status))
        if not deployment.role_instance_list:
            errors.append('No role instances found')
        for role_inst in deployment.role_instance_list:
            if role_inst.power_state != 'Started':
                errors.append('Power state: {0}'.format(role_inst.power_state))
            if role_inst.instance_status != 'ReadyRole':
                errors.append('Role status: {0}'.format(role_inst.instance_status))
    return errors


def print_errors(errors, verbosity):
    if errors:
        print ', '.join(errors)
        return 2
    else:
        print 'All cool'
        return 0


def main():
    args = handle_args()

    if args.verbose >= 3:
        print 'Converting publishsettings.'
    try:
        publishsettings = PublishSettings(args.psfile)
    except Exception, error:
        print 'Publishsettings file is not good'
        print error
        sys.exit(1)
    pem_path = publishsettings.write_pem()
    if args.verbose >= 3:
        print 'Pem file saved to temp file {}'.format(pem_path)
        print 'Azure sub id {}'.format(publishsettings.sub_id)

    service = ServiceManagementService(
        subscription_id=publishsettings.sub_id,
        cert_file=pem_path)
    errors = check_for_errors(service, args.hostname)
    if args.verbose >= 3:
        print 'Azure status retreived.'

    os.unlink(pem_path)
    if args.verbose >= 3:
        print 'Deleted pem.'

    ret_val = print_errors(errors, args.verbose)
    sys.exit(ret_val)

if __name__ == '__main__':
    main()
{'fraction_non_alphanumeric': 0.1009371639268705, 'fraction_numerical': 0.004455369488400676, 'mean_word_length': 2.3556701030927836, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6473774', 'n_tokens_mistral': 1815, 'n_tokens_neox': 1763, 'n_words': 431}