示例#1
0
    def validate(self, 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"))

        if 'recipe' in body.keys():
            recipe = body['recipe']
        else:
            recipe = "default"
        LOG.info(
            _LI('Processing Request for cookbook {cookbook}, recipe {recipe}, '
                'image {image}').format(cookbook=cookbook, recipe=recipe,
                                        image=image))
        res = self.ve.validate_cookbook(
            cookbook, recipe, image, request
        )
        return res
def main():
    """Launch validator API """
    try:
        config.parse_args()
        logging.setup(CONF, 'chef_validator_api')
        app = config.load_paste_app("chef_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)
 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)
示例#4
0
    def validate_cookbook(self, cookbook, recipe, image, request):
        """
        Cookbook validation
        :param recipe:
        :param image: name of the image to deploy
        :param cookbook: name of the cookbook to validate
        :param request: request context
        :return:
        """
        # process request based on configuration options
        if hasattr(CONF, 'clients_docker') \
                and hasattr(CONF.clients_docker, 'url') \
                and len(CONF.clients_docker.url) > 1:

            # use direct docker connection, fast and simple
            res = self.d.cookbook_deployment_test(cookbook, recipe, image)
        else:
            # nova client connection
            n = NovaClient(request.context)

            # find the image id
            image = n.get_image_by_name(image)
            if not image:
                raise exception.ImageNotFound

            machine = "%s-validate" % cookbook

            # if the machine already exists, destroy it
            if n.get_machine(machine):
                LOG.info(_LI("Server %s already exists, deleting") % machine)
                n.delete_machine(machine)

            # deploy machine
            n.deploy_machine(machine, image=image['name'])
            ip = n.get_ip()

            # generic ssh connection
            c = ChefClientSSH(ip)
            res = c.cookbook_deploy_test(cookbook)
        return res