Пример #1
0
 def get_nginx_config(self):
     """
     Generate nginx config from service attributes
     """
     if self.domains[u'http2']:
         self.check_certificate()
     return template_loader.load('service.html').generate(service=self, config=config)
Пример #2
0
    def validate(self):
        """
        Deploy temporary service & nginx config and validate it with nginx
        :return: bool
        """

        temp_dir = tempfile.mkdtemp()

        files = {}
        for config_type in self.get_config_types():
            path = os.path.join(temp_dir, config_type)
            config_file = open(path, 'w+')
            config_file.write(self.get_nginx_config(config_type))
            config_file.close()
            files['service_%s' % config_type] = path

        files['pid_file'] = os.path.join(temp_dir, 'pid')

        nginx_config_file = open(os.path.join(temp_dir, 'service'), 'w+')
        nginx_config_file.write(
            template_loader.load('service_validate.html').generate(**files))
        nginx_config_file.close()

        try:
            return_code = subprocess.check_call(
                [config.NGINX_BINARY, '-t', '-c', nginx_config_file.name])
        except subprocess.CalledProcessError:
            return_code = 1
        finally:
            rmtree(temp_dir, ignore_errors=True)

        return return_code == 0
Пример #3
0
    def validate(self):
        """
        Deploy temporary service & nginx config and validate it with nginx
        :return: bool
        """
        service_config_file = tempfile.NamedTemporaryFile(delete=False)
        service_config_file.write(self.get_nginx_config())
        service_config_file.close()

        nginx_config_file = tempfile.NamedTemporaryFile(delete=False)
        nginx_config_file.write(template_loader.load('service_validate.html')
                                .generate(service_config=service_config_file.name,
                                          pid_file='%s.pid' % service_config_file.name)
                                )
        nginx_config_file.close()

        try:
            return_code = subprocess.check_call([config.NGINX_BINARY, '-t', '-c', nginx_config_file.name])
        except subprocess.CalledProcessError:
            return_code = 1
        finally:
            os.unlink(service_config_file.name)
            os.unlink('%s.pid' % service_config_file.name)
            os.unlink(nginx_config_file.name)

        return return_code == 0
Пример #4
0
    def get_nginx_config(self, config_type):
        """
        Generate nginx config from service attributes
        :param config_type: string
        """
        if config_type == 'http2' and len(self.binds['http2']):
            self.check_certificate()

        if config_type in ['tcp', 'udp']:
            self.check_port()

        return template_loader.load('service_%s.html' % config_type).generate(
            service=self, config=config)