#!/usr/bin/env python
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at http://mozilla.org/MPL/2.0/.

import logging
import os
import subprocess
import uuid

import stoneridge


class StoneRidgeMaster(stoneridge.QueueListener):
    def setup(self, config):
        self.queues = {
            'broadband': stoneridge.QueueWriter(stoneridge.NETCONFIG_QUEUES['broadband']),
            'umts': stoneridge.QueueWriter(stoneridge.NETCONFIG_QUEUES['umts']),
            'gsm': stoneridge.QueueWriter(stoneridge.NETCONFIG_QUEUES['gsm'])
        }
        self.logdir = stoneridge.get_config('stoneridge', 'logs')
        self.config = config

    def handle(self, nightly, ldap, sha, netconfigs, operating_systems,
            srid=None, attempt=1):
        if srid is None:
            srid = str(uuid.uuid4())
        logfile = 'cloner_%s.log' % (srid,)
        cloner_log = os.path.join(self.logdir, logfile)
        args = ['srcloner.py', '--config', self.config, '--srid', srid,
                '--log', cloner_log, '--attempt', attempt]
        if nightly:
            path = 'nightly/latest-mozilla-central'
            args.append('--nightly')
        else:
            path = 'try-builds/%s-%s' % (ldap, sha)
        logging.debug('Path to builds: %s' % (path,))

        args.extend(['--path', path])

        if ldap:
            args.extend(['--ldap', ldap])
        if sha:
            args.extend(['--sha', sha])
        for ops in operating_systems:
            args.append('--%s' % (ops,))
        for nc in netconfigs:
            args.append('--%s' % (nc,))

        try:
            stoneridge.run_process(*args)
        except subprocess.CalledProcessError as e:
            # Either we will retry this later, at the cloner's request, or the
            # error has already been logged by run_process and there's no
            # recovery we can do.
            return

        for nc in netconfigs:
            queue = self.queues.get(nc, None)
            if queue is None:
                logging.warning('Got request for invalid netconfig %s' % (nc,))
                continue

            queue.enqueue(operating_systems=operating_systems, srid=srid)


def daemon(config):
    master = StoneRidgeMaster(stoneridge.INCOMING_QUEUE,
            config=config)
    master.run()


@stoneridge.main
def main():
    parser = stoneridge.DaemonArgumentParser()
    args = parser.parse_args()

    parser.start_daemon(daemon, config=args._sr_config_)

@@ -919,16 +919,431 @@
 uid4())%0A
+%0A        logging.debug('Got request')%0A        logging.debug('Nightly: %25s' %25 (nightly,))%0A        logging.debug('LDAP: %25s' %25 (ldap,))%0A        logging.debug('SHA: %25s' %25 (sha,))%0A        logging.debug('Netconfigs: %25s' %25 (' '.join(netconfigs),))%0A        logging.debug('Operating systems: %25s' %25 (' '.join(operating_systems),))%0A        logging.debug('Attempt: %25s' %25 (attempt,))%0A        logging.debug('SRID: %25s' %25 (srid,))%0A%0A
         

Add logging to master for each time a request comes in