MOON
Server: Apache/2.2.23 (Unix) mod_ssl/2.2.23 OpenSSL/0.9.8e-fips-rhel5 mod_auth_passthrough/2.1 mod_bwlimited/1.4 PHP/5.4.10
System: Linux vps.presagepowered.net 2.6.18-398.el5 #1 SMP Tue Sep 16 20:51:48 EDT 2014 i686
User: mckernan (512)
PHP: 5.4.10
Disabled: NONE
Upload Files
File: //usr/lib/Acronis/PyShell/site-tools/agents.py
# -*- coding: utf-8 -*-

import argparse
import csv
import datetime
import json
import logging
import logging.handlers
import os
import requests
import time


now = datetime.datetime.now()

TITLE = """Install or update the agents in bulk."""
TIMEOUT = 10
LOG_FILENAME = 'agents.{}.log'.format(now.strftime('%y%m%d%H%M%S'))
LOG_FORMAT = "%(asctime)s - %(filename)s - %(levelname)s - %(message)s"
CRED_FIELDNAMES = ['hostname', 'username', 'password']

logger = logging.getLogger(__file__)

parser = None
params = None

_URL = None
_HEADERS = None


def _init_logging():
    logger.setLevel(logging.DEBUG)
    # create file handler which logs even debug messages
    fh = logging.FileHandler(LOG_FILENAME)
    fh.setLevel(logging.DEBUG)
    # create console handler with a higher log level
    ch = logging.StreamHandler()
    ch.setLevel(logging.ERROR)
    # formatter
    fmt = logging.Formatter(LOG_FORMAT)
    fh.setFormatter(fmt)
    ch.setFormatter(fmt)
    # add the handlers to the logger
    logger.addHandler(fh)
    logger.addHandler(ch)


def _logging(method, msg, verbose=False, prefix=None):
    if verbose:
        if prefix:
            msg = prefix + msg
        print(msg)
    method(msg)


def do(method, uri, payload=None):
    """Common function to make HTTP request."""
    url = _URL + uri
    try:
        response = method(url, data=json.dumps(payload), headers=_HEADERS)
    except requests.exceptions.ConnectionError as err:
        _logging(logger.error, err)
        exit(1)

    if payload is None:
        payload = {}
    elif 'password' in payload:
        payload['password'] = '***'

    logger.debug('URL: %s', url)
    logger.debug('PAYLOAD: %s', payload)
    logger.debug('RESPONSE: %s %s', response, response.content)
    try:
        content = response.json()
    except json.decoder.JSONDecodeError:
        content = response.text

    return response.status_code, content


def create(session):
    """Establish the web session."""
    payload = {
        'machine': params.ams,
        'username': params.username,
        'password': params.password,
        'remember': False,
        'type': 'ams',
        'NonRequiredParams': ['username', 'password'],
    }
    msg = 'Establish a web session for {}'.format(params.username)
    _logging(logger.info, msg, params.verbose)
    status_code, response = do(session.post, '/api/ams/session', payload)
    if status_code != 200:
        msg = 'Connection to AMS failed: {}.'.format(params.ams)
        _logging(logger.debug, msg, verbose=True)
        exit(2)
    return response


def lola_register(session, hostname, username, password, action):
    """Returns entry_id as a string."""
    uri = '/api/ams/user_profiles/current/credentials'
    payload = {
        'address': hostname,
        'userName': username,
        'password': password,
        'category': action,
        'temporary': True
    }
    msg = 'Set the credentials for {} into LOLA'.format(username)
    _logging(logger.info, msg, params.verbose)
    return do(session.post, uri, payload)


def make_subscription(session):
    """Creates a subscription."""
    msg = 'Create a subscription'
    _logging(logger.info, msg, params.verbose)
    return do(session.post, '/api/subscriptions')


def get_subscription(session, subscription_id):
    """Gets a content of the subsription."""
    uri = '/api/subscriptions/{}'.format(subscription_id)
    payload = {
        'timeout': TIMEOUT,
        'action': 'pop',
    }
    msg = 'Check the subscription {}.'.format(subscription_id)
    _logging(logger.info, msg, params.verbose)
    return do(session.post, uri, payload)


def bind_activity(session, activity_id, subscription_id):
    """Binds the activity with the subscription."""
    uri = '/api/ams/activities/{}?isRootActivity=true&subscriptionId={}'
    uri = uri.format(activity_id, subscription_id)
    msg = 'Bind the activity {} on the subscription {}.'.format(activity_id, subscription_id)
    _logging(logger.info, msg, params.verbose)
    return do(session.get, uri)


def install_agent_win(session, hostname, subscription_id):
    """Returns activity_id as a string."""
    payload = {
        'subscriptionId': subscription_id,
        'operationId': hostname,
        'machines': [
            {
                'address': hostname,
                'agents': [
                    'agentForWindows',
                ],
                'registrationAddress': params.ams,
                'NonRequiredParams': [],
            }
        ],
        'NonRequiredParams': [],
    }
    msg = 'Install agent on {}'.format(hostname)
    _logging(logger.info, msg, params.verbose)
    return do(session.post, '/api/ams/infrastructure/agents', payload)


def host_list():
    reader = csv.DictReader(params.agents, fieldnames=CRED_FIELDNAMES,
                            delimiter=' ', restkey='_unexpected', skipinitialspace=True)
    for n, line in enumerate(reader):
        if n == 0:
            if all(line.get(col_name) == col_name for col_name in CRED_FIELDNAMES):
                # this is a header line
                continue

        hostname = line['hostname']
        username = params.use_username if params.use_username else line['username']
        password = params.use_password if params.use_password else line['password']

        if n == 0 and "," in hostname:
            raise Exception(
                "Hostname '{}' contains comma character and is invalid. "
                "Make sure the input file is space-delimited.".format(hostname))

        if username is None:
            raise Exception("Username is not specified for host '{}'".format(hostname))

        if password is None:
            raise Exception("Password is not specified for host '{}'".format(hostname))

        if '_unexpected' in line:
            raise Exception(
                "Information for host '{0}' contains unexpected trailing characters:\n{1}\n\n"
                "Make sure values containig spaces are enclosed into double quotes.".format(
                    hostname, line['_unexpected']))

        yield hostname, username, password


def get_agent_ids(session):
    msg = 'Get information about agents'
    _logging(logger.info, msg, params.verbose)
    return do(session.get, '/api/ams/infrastructure/agents')


def update_agent(session, username, password, agent_id):
    """Run update of agents.

    :param session: The web session object.
    :param username: Agent's username.
    :param password: Agent's password.
    :param agent_ids: The agents' ID.
    """

    payload = {
        'credentials': {
            'userName': username,
            'password': password,
        },
        'machinesIds': [agent_id],
    }
    msg = 'Update agent: {}.'.format(agent_id)
    _logging(logger.info, msg, params.verbose)
    return do(session.post, '/api/ams/resource_operations/run_auto_update', payload)


def _get_by_path(o, path):
    if not isinstance(path, list):
        path = path.split('/')
    node, tail = path[0], path[1:]
    value = o.get(node)
    return _get_by_path(value, tail) if isinstance(value, dict) else value


def _check(item, activity_keys, operation_keys):
    data = item['data']

    resource = item.get('resource')
    if resource and resource == 'operations':
        data = item['data']
        error = data.get('error')
        if error:
            logger.error('%s: %s', error['effect'], error['cause'])
            key = data.get('id')
            operation_keys.remove(key)

    # log progress in running state
    if 'progress' in data and 'state' in data:
        msg = 'Activity [{}] for {} is {}'.format(data['id'], data['title'], data['state'])
        _logging(logger.debug, msg)

    # parsing the completed state
    activity_id = item.get('key', _get_by_path(item, 'data/id'))
    if activity_id in activity_keys and 'state' in data and data['state'] == 'completed':
        if data['status'] == 'ok':
            msg = '{} [{}]: {}'.format(data['title'], activity_id, data['state'])
            _logging(logger.info, msg, verbose=True)
        else:
            msg = '{} [{}]: {} with {}'.format(
                data['title'],
                activity_id,
                data['state'],
                data['status']
            )
            logger.error('%s: %s',
                         data['completionResult']['effect'],
                         data['completionResult']['cause'])
        activity_keys.remove(activity_id)


def is_applied(session, subscription_id, activity_keys, operation_keys=None):
    status_code, response = get_subscription(session, subscription_id)
    if not status_code == 200:
        return False

    for item in response:
        if operation_keys:
            if item['resource'] == 'operations':
                hostname = item['key']
                if hostname in operation_keys:
                    activity_id = _get_by_path(item, 'data/result/ExecutionActivity')
                    if activity_id is not None:
                        operation_keys.remove(hostname)
                        activity_keys.add(activity_id)
                        msg = 'Hostname[{}] -> Activity[{}]'.format(hostname, activity_id)
                        _logging(logger.info, msg, verbose=True)

                        status_code, sub_response = bind_activity(session, activity_id, subscription_id)
                        if not status_code == 200:
                            logger.error('Activity binding for %s failed.', hostname)
                            continue
                        _check(sub_response, activity_keys, operation_keys)

        _check(item, activity_keys, operation_keys)

    if operation_keys is None:
        operation_keys = []
    return len(activity_keys) == 0 and len(operation_keys) == 0


def install(session, subscription_id):
    operation_keys = set()
    for hostname, username, password in host_list():
        status_code, response = lola_register(session, hostname, username, password, 'windows_remote_install')
        if not status_code == 200:
            msg = 'Lola register {} failed.'.format(hostname)
            _logging(logger.error, msg, params.verbose)
            continue
        status_code, response = install_agent_win(session, hostname, subscription_id)
        if not status_code == 200:
            msg = 'Install on {} failed.'.format(hostname)
            _logging(logger.error, msg, params.verbose)
            continue

        activity_id = response.get('reminst_result', {}).get('activity_id', None)

        if activity_id:
            # some remote installation really happens
            status_code, response = bind_activity(session, activity_id, subscription_id)
            if not status_code == 200:
                msg = 'Activity binding for {} failed.'.format(hostname)
                _logging(logger.error, msg, params.verbose)
                continue
            msg = 'Installing agent on {}.'.format(hostname)
            _logging(logger.info, msg, verbose=True)
            operation_keys.add(hostname)

    if not params.wait:
        return

    print('Waiting...')
    activity_keys = set()
    while not is_applied(session, subscription_id, activity_keys, operation_keys):
        logger.debug('Activity Keys: %s', activity_keys)
        logger.debug('Operation Keys: %s', operation_keys)
        time.sleep(TIMEOUT)


def update(session, subscription_id):
    status_code, response = get_agent_ids(session)
    if not status_code == 200:
        msg = 'Get agent ids for {} failed.'.format(params.ams)
        logger.error(msg)
        raise RuntimeError(msg)

    agents_map = {}
    activities_map = {}

    creds = {hostname: (username, password)
             for hostname, username, password in host_list()}

    for item in response['data']:
        ip = item['Attributes']['ResidentialAddresses'][0]
        try:
            installed_version = item['Attributes']['Agents'][0]['Version']
        except:
            installed_version = None

        if item['Attributes']['Status'] and installed_version and installed_version >= "12":
            # Skip update of offline agents (Status: 0 - online, 1 - offline)
            # Agents of old version should be upgraded even if they are reported offline.
            msg = 'Agent on {} is offline. Skipping...'.format(ip)
            _logging(logger.info, msg, verbose=True)
            continue
        if not item['Attributes']['UpdateIsAvailable']:
            msg = 'No update will be applied for agent on {}. Skipping...'.format(ip)
            _logging(logger.info, msg, verbose=True)
            continue

        update_version = item['Attributes'].get('UpdateVersion', None)
        if update_version and update_version == installed_version:
            msg = 'Agent on {} is up to date. Skipping...'.format(ip)
            _logging(logger.info, msg, verbose=True)
            continue

        local_id = item['ID']['LocalID']
        if ip not in creds:
            msg = 'No record for agent on {} in the credentials file. Skipping...'.format(ip)
            _logging(logger.info, msg, verbose=True, prefix='\t')
            continue

        username, password = creds[ip]
        agent_info = {
            'machine_id': local_id,
            'name': item['DisplayedName'],
            'ip': ip,
            'username': username,
            'password': password,
        }
        agents_map[local_id] = agent_info
        msg = 'Agent on {} will be updated!'.format(ip)
        _logging(logger.info, msg, verbose=True)

    if len(agents_map) == 0:
        msg = 'Nothing to update!'
        _logging(logger.info, msg, verbose=True)
        return

    for agent_id, agent_info in agents_map.items():
        status_code, response = update_agent(
            session, agent_info['username'], agent_info['password'], agent_id)

        if not params.wait:
            continue

        for info in response['data']:
            agent_id = info['machine_id']
            activity_id = info['activity_id']
            status_code, response = bind_activity(session, activity_id, subscription_id)
            if not status_code == 200:
                msg = 'Activity binding for {} failed.'.format(ip)
                _logging(logger.error, msg, verbose=True)
                continue
            data = response['data']
            status = data['status']
            if status == 'error':
                msg = '{}: {}: {}'.format(
                    data['title'],
                    data['completionResult']['effect'],
                    data['completionResult']['cause'])
                logger.error(msg)
                continue
            agent_info['activity_id'] = activity_id
            activities_map[activity_id] = agent_info

    if params.wait:
        print('Waiting...')
        activity_keys = set(activities_map)
        while not is_applied(session, subscription_id, activity_keys):
            time.sleep(TIMEOUT)


TPL_MACHINE = 'hostname="{}", username="{}", password="{}"'


def main():
    _init_logging()

    global _URL, _HEADERS
    _URL = 'http://{}:{}'.format(params.ams, params.port)
    _HEADERS = {
        'content-type': 'application/json',
        'Origin': _URL,
    }

    with requests.Session() as session:
        data = create(session)

        if params.list:
            status_code, response = get_agent_ids(session)
            if not status_code == 200:
                msg = 'Get agent ids for {} failed.'.format(params.ams)
                logger.error(msg)
                raise RuntimeError(msg)

            if params.dump:
                writer = csv.DictWriter(params.dump, fieldnames=CRED_FIELDNAMES, delimiter=' ')
                writer.writeheader()

            unique_hostnames = set()
            for item in response['data']:
                attributes = item['Attributes']
                for agent in attributes['Agents']:
                    if agent['Name']:
                        ip_address = attributes['ResidentialAddresses'][0]
                        data = {
                            'version': agent['Version'],
                            'ip': ip_address,
                            'name': attributes['Name'],
                            'title': agent['Name'],
                        }
                        print('{name}\t{ip}\t{title}\t{version}'.format(**data))
                        if params.dump:
                            if ip_address in unique_hostnames:
                                continue
                            unique_hostnames.add(ip_address)
                            writer.writerow({
                                'hostname': ip_address,
                                'username': params.dump_username,
                                'password': params.dump_password
                            })
            if params.dump:
                tpl = '-----\nThe agents list was dumped to "{}" file in CSV format.'
                print(tpl.format(params.dump.name))
            return

        status_code, data = make_subscription(session)
        if not isinstance(data, dict):
            raise RuntimeError('See log file.')

        subscription_id = data.get('id')

        if params.install:
            install(session, subscription_id)
        elif params.update:
            update(session, subscription_id)


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description=TITLE)
    parser.add_argument('-v', '--verbose', help='Make this script verbose.',
                        action='store_true', default=False)

    ams_group = parser.add_argument_group('connection')
    ams_group.add_argument('-a', '--ams', required=True,
                           help='AMS\' IP address or hostname.')
    ams_group.add_argument('-p', '--port', help='AMS\' port.', default=9877)
    ams_group.add_argument('--username', help='AMS\' username.', required=True)
    ams_group.add_argument('--password', help='AMS\' password.', required=True)

    main_group = parser.add_argument_group('install or update')
    group = main_group.add_mutually_exclusive_group()
    group.add_argument('-i', '--install', action='store_true',
                       default=False, help='Do install.')
    group.add_argument('-u', '--update', action='store_true',
                       default=False, help='Do update.')

    main_group.add_argument('--agents', type=argparse.FileType('r'),
                            help='Space-delimited CSV file containing list of hostnames and credentials. '
                            'Used for both install and update operations. '
                            'Each line should contain hostname, username and password in this order.')
    main_group.add_argument('--use_username',
                            help='Default username for agents, has a priority over file.')
    main_group.add_argument('--use_password',
                            help='Default password for agents, has a priority over file.')
    main_group.add_argument('--wait', help='Waiting until complete.',
                            action='store_true', default=False)

    listing_group = parser.add_argument_group('agent listing')
    listing_group.add_argument('-l', '--list', help='Listing the agents with versions.',
                               action='store_true', default=False)
    listing_group.add_argument('--dump', nargs='?', type=argparse.FileType('w'),
                               help='Dump agents to file, dump_agent.csv by default.',
                               const='dump_agent.csv')
    listing_group.add_argument('--dump_username', default='Administrator',
                               help='Default username for agents')
    listing_group.add_argument('--dump_password', default='top_secret_password',
                               help='Default password for agents')
    params = parser.parse_args()

    if not any([params.list, params.install, params.update]):
        parser.print_help()
        os.sys.exit(1)

    if (params.install or params.update) and not params.agents:
        print('\nPlease, provide credentials information with --agent option.')
        print('You may generate the appropriate file with -l option. See below...\n')
        parser.print_help()
        os.sys.exit(2)

    main()