Beispiel #1
0
def main():
    """Launch validator API """
    try:
        config.parse_args()
        logging.setup(CONF, 'validator_api')
        app = config.load_paste_app("validator_api")
        port, host = (CONF.bind_port, CONF.bind_host)
        LOG.info(_LI('Starting Validator ReST API on %(host)s:%(port)s'),
                 {'host': host, 'port': port})
        server = wsgi.Service(app, port, host)
        server.start()
        server.wait()
    except RuntimeError as e:
        msg = six.text_type(e)
        sys.exit("ERROR: %s" % msg)
Beispiel #2
0
    def validate(request, body):
        """ Validate the given cookbook
        :param request: request context
        :param body: a json with deployment parameters
        :return : a json file with process results
        """
        body = body or {}
        if len(body) < 1:
            raise exc.HTTPBadRequest(_("No action specified"))
        try:
            cookbook = body['cookbook']
            image = body['image']
        except KeyError:
            raise exc.HTTPBadRequest(_("Insufficient payload"))

        LOG.info(_LI('Processing Request for cookbook %s, image %s' % (cookbook, image)))
        res = ValidateEngine().validate_chef_cookbook(cookbook, image, request)
        return res
Beispiel #3
0
 def run_container(self, image):
     """Run and start a container based on the given image
     :param image: image to run
     :return:
     """
     contname = "{}-validate".format(image).replace("/", "_")
     try:
         try:
             self.dc.remove_container(contname, force=True)
             LOG.info(_LI('Removing old %s container' % contname))
         except NotFound:
             pass
         self.container = self.dc.create_container(
             image,
             tty=True,
             name=contname
         ).get('Id')
         self.dc.start(container=self.container)
     except AttributeError as e:
         LOG.error(_LW("Error creating container: %s" % e))
         raise DockerContainerException(image=image)

# If ../validator/__init__.py exists, add ../ to Python search path,
# so that it will override what happens to be installed in
# /usr/(local/)lib/python...
root = os.path.join(os.path.abspath(__file__), os.pardir, os.pardir, os.pardir)
if os.path.exists(os.path.join(root, 'validator', '__init__.py')):
    sys.path.insert(0, root)

i18n.enable_lazy()

LOG = logging.getLogger()
CONF = config.CONF

if __name__ == '__main__':
    try:
        logging.register_options(CONF)
        config.parse_args()
        logging.setup(CONF, 'validator_api')

        app = config.load_paste_app("validator_api")
        port, host = (CONF.bind_port, CONF.bind_host)
        LOG.info(_LI('Starting Chef Validator ReST API on %(host)s:%(port)s'),
                 {'host': host, 'port': port})
        server = wsgi.Service(app, port, host)
        server.start()
        server.wait()
    except RuntimeError as e:
        msg = six.text_type(e)
        sys.exit("ERROR: %s" % msg)
Beispiel #5
0
def _setup_logging_from_conf(project, version):
    log_root = getLogger(None).logger
    for handler in log_root.handlers:
        log_root.removeHandler(handler)

    logpath = _get_log_file_path()
    if logpath:
        filelog = logging.handlers.WatchedFileHandler(logpath)
        log_root.addHandler(filelog)

    if CONF.use_stderr:
        streamlog = ColorHandler()
        log_root.addHandler(streamlog)

    elif not logpath:
        # pass sys.stdout as a positional argument
        # python2.6 calls the argument strm, in 2.7 it's stream
        streamlog = logging.StreamHandler(sys.stdout)
        log_root.addHandler(streamlog)

    if CONF.publish_errors:
        handler = importutils.import_object(
            "oslo.messaging.notify.log_handler.PublishErrorsHandler",
            logging.ERROR)
        log_root.addHandler(handler)

    datefmt = CONF.log_date_format
    for handler in log_root.handlers:
        # NOTE(alaski): CONF.log_format overrides everything currently.  This
        # should be deprecated in favor of context aware formatting.
        if CONF.log_format:
            handler.setFormatter(logging.Formatter(fmt=CONF.log_format,
                                                   datefmt=datefmt))
            log_root.info(_LI(
                'Deprecated: log_format is now deprecated and will '
                'be removed in the next release'))
        else:
            handler.setFormatter(ContextFormatter(project=project,
                                                  version=version,
                                                  datefmt=datefmt))

    if CONF.debug:
        log_root.setLevel(logging.DEBUG)
    elif CONF.verbose:
        log_root.setLevel(logging.INFO)
    else:
        log_root.setLevel(logging.WARNING)

    for pair in CONF.default_log_levels:
        mod, _sep, level_name = pair.partition('=')
        logger = logging.getLogger(mod)
        # NOTE(AAzza) in python2.6 Logger.setLevel doesn't convert string name
        # to integer code.
        if sys.version_info < (2, 7):
            level = logging.getLevelName(level_name)
            logger.setLevel(level)
        else:
            logger.setLevel(level_name)

    if CONF.use_syslog:
        try:
            facility = _find_facility_from_conf()
            # TODO(bogdando) use the format provided by RFCSysLogHandler
            #   after existing syslog format deprecation in J
            if CONF.use_syslog_rfc_format:
                syslog = RFCSysLogHandler(address='/dev/log',
                                          facility=facility)
            else:
                syslog = logging.handlers.SysLogHandler(address='/dev/log',
                                                        facility=facility)
            log_root.addHandler(syslog)
        except socket.error:
            log_root.error('Unable to add syslog handler. Verify that syslog '
                           'is running.')