예제 #1
0
def mainrun(argv=None):
    """Consume inputs, configure logging, and launch the action.

    Keyword arguments:

    * argv -- Executable's arguments (default None)

    Return exit code:

    * 0 -- Success

    * 1 -- Input problem

    * 2 -- Configuration problem

    * 3 -- Problem with the local or remote system environment.

    * 4 -- Failure to carry out action.

    * 42 -- Programming error, please report if this is a non-modified release

    """

    if os.name != 'posix':
        print >> sys.stderr, "Only runs on POSIX systems."
        return 3

    starttimer()

    parser = parsersetup()

    if argv:
        (opts, args) = parser.parse_args(argv[1:])
    else:
        (opts, args) = parser.parse_args()

    loglevel = None
    if opts.verbose or opts.trace:
        loglevel = logging.DEBUG
    elif opts.quiet:
        loglevel = logging.ERROR
    else:
        loglevel = logging.INFO
    log = configureLogging(loglevel, trace=opts.trace)

    try:
        validateargs(opts)

        #if opts.evaluate:
        #    log.info("EVALUATE MODE ENABLED")

        if opts.configpath:
            config = getconfig(filepath=opts.configpath)
        else:
            config = getconfig(string=DEFAULTCONFIG)

        commonconf = getCommonConf(opts, config)

        if commonconf.logfilepath:
            addFileLogging(log,
                           commonconf.logfilepath,
                           None,
                           loglevel,
                           trace=opts.trace)
            setlogfilepath(commonconf.logfilepath)
            log.debug("[file logging enabled @ '%s'] " %
                      commonconf.logfilepath)

        if opts.tryall:
            log.debug("action %s" % ARGS.TRYALL)
        elif opts.amazon:
            log.debug("action %s" % ARGS.AMAZON)
        elif opts.regular:
            log.debug("action %s" % ARGS.REGULAR)

        if opts.poweroff:
            log.info("OK to poweroff")
            try:
                problemscript = config.get("taskpaths", "problemscript")
            except:
                exception_type = sys.exc_type
                try:
                    exceptname = exception_type.__name__
                except AttributeError:
                    exceptname = exception_type
                msg = "%s: %s" % (str(exceptname), str(sys.exc_value))
                raise InvalidConfig(msg)

            setterminateok(problemscript)

        #######################################
        ##  I. Run one Instantiation action  ##
        #######################################

        # try-all could be replaced by supporting multiple action flags and
        # having an order (the order itself could be set via conf)
        iactionresult = None
        if not opts.tryall:

            if opts.regular:
                regconf = getReginstConf(opts, config)
                iaction = RegularInstantiation(commonconf, regconf)
            elif opts.amazon:
                ec2conf = getAmazonConf(opts, config)
                iaction = AmazonInstantiation(commonconf, ec2conf)

            log.info("Running instantiation action")
            iaction.run()
            iactionresult = iaction.result

        else:

            # embedded run order right now
            try:
                log.info("First running regular instantiation action")
                regconf = getReginstConf(opts, config)
                reg_iaction = RegularInstantiation(commonconf, regconf)
                reg_iaction.run()
                iactionresult = reg_iaction.result
            except:
                msg = "Problem with regular instantiation action: "
                exception_type = sys.exc_type
                try:
                    exceptname = exception_type.__name__
                except AttributeError:
                    exceptname = exception_type
                msg += "%s: %s" % (str(exceptname), str(sys.exc_value))
                log.error(msg)

                log.info("Second, running Amazon instantiation action")

                ec2conf = getAmazonConf(opts, config)
                ec2_iaction = AmazonInstantiation(commonconf, ec2conf)
                ec2_iaction.run()
                iactionresult = ec2_iaction.result

        # If there was an issue, exception should have been thrown:
        if iactionresult == None:
            raise ProgrammingError(
                "Instantiation action(s) ran to completion but no result?")

        #############################################################
        ## II. Run one Retrieval action                            ##
        ##     The Instantiation action throws an exception or     ##
        ##     places InstantiationResult in its "result" field.   ##
        #############################################################

        ractionresult = None

        # only one impl right now:
        raction = DefaultRetrieveAction(commonconf, iactionresult)
        log.info("Running retrieval action")
        raction.run()
        ractionresult = raction.result

        if ractionresult == None:
            raise ProgrammingError(
                "Retrieve Action ran to completion but no result?")

        ###############################################################
        ## III. Run one Consumption action                           ##
        ##      The Retrieval action either throws an exception or   ##
        ##      places RetrieveResult object in its "result" field.  ##
        ###############################################################

        # only one impl right now:
        caction = DefaultConsumeRetrieveResult(commonconf, ractionresult,
                                               iactionresult)
        log.info("Running consume action")
        caction.run()

        return 0

    except InvalidInput, e:
        msg = "Problem with input: %s" % e.msg
        if log:
            log.critical(msg)
        else:
            print >> sys.stderr, msg
        return 1
def mainrun(argv=None):
    """Consume inputs, configure logging, and launch the action.

    Keyword arguments:

    * argv -- Executable's arguments (default None)

    Return exit code:

    * 0 -- Success

    * 1 -- Input problem

    * 2 -- Configuration problem

    * 3 -- Problem with the local or remote system environment.

    * 4 -- Failure to carry out action.

    * 42 -- Programming error, please report if this is a non-modified release

    """

    if os.name != "posix":
        print >> sys.stderr, "Only runs on POSIX systems."
        return 3

    starttimer()

    parser = parsersetup()

    if argv:
        (opts, args) = parser.parse_args(argv[1:])
    else:
        (opts, args) = parser.parse_args()

    loglevel = None
    if opts.verbose or opts.trace:
        loglevel = logging.DEBUG
    elif opts.quiet:
        loglevel = logging.ERROR
    else:
        loglevel = logging.INFO
    log = configureLogging(loglevel, trace=opts.trace)

    try:
        validateargs(opts)

        # if opts.evaluate:
        #    log.info("EVALUATE MODE ENABLED")

        if opts.configpath:
            config = getconfig(filepath=opts.configpath)
        else:
            config = getconfig(string=DEFAULTCONFIG)

        commonconf = getCommonConf(opts, config)

        if commonconf.logfilepath:
            addFileLogging(log, commonconf.logfilepath, None, loglevel, trace=opts.trace)
            setlogfilepath(commonconf.logfilepath)
            log.debug("[file logging enabled @ '%s'] " % commonconf.logfilepath)

        if opts.tryall:
            log.debug("action %s" % ARGS.TRYALL)
        elif opts.amazon:
            log.debug("action %s" % ARGS.AMAZON)
        elif opts.regular:
            log.debug("action %s" % ARGS.REGULAR)

        if opts.poweroff:
            log.info("OK to poweroff")
            try:
                problemscript = config.get("taskpaths", "problemscript")
            except:
                exception_type = sys.exc_type
                try:
                    exceptname = exception_type.__name__
                except AttributeError:
                    exceptname = exception_type
                msg = "%s: %s" % (str(exceptname), str(sys.exc_value))
                raise InvalidConfig(msg)

            setterminateok(problemscript)

        #######################################
        ##  I. Run one Instantiation action  ##
        #######################################

        # try-all could be replaced by supporting multiple action flags and
        # having an order (the order itself could be set via conf)
        iactionresult = None
        if not opts.tryall:

            if opts.regular:
                regconf = getReginstConf(opts, config)
                iaction = RegularInstantiation(commonconf, regconf)
            elif opts.amazon:
                ec2conf = getAmazonConf(opts, config)
                iaction = AmazonInstantiation(commonconf, ec2conf)

            log.info("Running instantiation action")
            iaction.run()
            iactionresult = iaction.result

        else:

            # embedded run order right now
            try:
                log.info("First running regular instantiation action")
                regconf = getReginstConf(opts, config)
                reg_iaction = RegularInstantiation(commonconf, regconf)
                reg_iaction.run()
                iactionresult = reg_iaction.result
            except:
                msg = "Problem with regular instantiation action: "
                exception_type = sys.exc_type
                try:
                    exceptname = exception_type.__name__
                except AttributeError:
                    exceptname = exception_type
                msg += "%s: %s" % (str(exceptname), str(sys.exc_value))
                log.error(msg)

                log.info("Second, running Amazon instantiation action")

                ec2conf = getAmazonConf(opts, config)
                ec2_iaction = AmazonInstantiation(commonconf, ec2conf)
                ec2_iaction.run()
                iactionresult = ec2_iaction.result

        # If there was an issue, exception should have been thrown:
        if iactionresult == None:
            raise ProgrammingError("Instantiation action(s) ran to completion but no result?")

        #############################################################
        ## II. Run one Retrieval action                            ##
        ##     The Instantiation action throws an exception or     ##
        ##     places InstantiationResult in its "result" field.   ##
        #############################################################

        ractionresult = None

        # only one impl right now:
        raction = DefaultRetrieveAction(commonconf, iactionresult)
        log.info("Running retrieval action")
        raction.run()
        ractionresult = raction.result

        if ractionresult == None:
            raise ProgrammingError("Retrieve Action ran to completion but no result?")

        ###############################################################
        ## III. Run one Consumption action                           ##
        ##      The Retrieval action either throws an exception or   ##
        ##      places RetrieveResult object in its "result" field.  ##
        ###############################################################

        # only one impl right now:
        caction = DefaultConsumeRetrieveResult(commonconf, ractionresult, iactionresult)
        log.info("Running consume action")
        caction.run()

        return 0

    except InvalidInput, e:
        msg = "Problem with input: %s" % e.msg
        if log:
            log.critical(msg)
        else:
            print >> sys.stderr, msg
        return 1
def config_logging(commonconf, opts, loglevel=logging.DEBUG):
    print "commonconf.logfilepath => ", commonconf.logfilepath
    log = configureLogging(loglevel, trace=opts.trace)
    addFileLogging(log, commonconf.logfilepath, None, loglevel, trace=opts.trace)
    setlogfilepath(commonconf.logfilepath)