saltstack-formulas / nginx-formula

Nginx Salt Formula
http://docs.saltstack.com/en/latest/topics/development/conventions/formulas.html
Other
163 stars 421 forks source link

[BUG] TypeError: object of type 'NoneType' has no len() #259

Open arthurzenika opened 4 years ago

arthurzenika commented 4 years ago

Your setup

Formula commit hash / release tag

e8ed39a62cd40fe43af2aae67a3e2347d02b6b6a

Versions reports (master & minion)

Salt Version:
           Salt: 2019.2.2

Dependency Versions:
           cffi: Not Installed
       cherrypy: Not Installed
       dateutil: 2.5.3
      docker-py: Not Installed
          gitdb: 2.0.0
      gitpython: 2.1.1
          ioflo: Not Installed
         Jinja2: 2.8
        libgit2: Not Installed
        libnacl: Not Installed
       M2Crypto: Not Installed
           Mako: Not Installed
   msgpack-pure: Not Installed
 msgpack-python: 0.4.8
   mysql-python: Not Installed
      pycparser: Not Installed
       pycrypto: 2.6.1
   pycryptodome: Not Installed
         pygit2: Not Installed
         Python: 2.7.13 (default, Nov 24 2017, 17:33:09)
   python-gnupg: Not Installed
         PyYAML: 3.12
          PyZMQ: 16.0.2
           RAET: Not Installed
          smmap: 2.0.1
        timelib: Not Installed
        Tornado: 4.4.3
            ZMQ: 4.2.1

System Versions:
           dist: debian 9.5 
         locale: UTF-8
        machine: x86_64
        release: 4.9.0-7-amd64
         system: Linux
        version: debian 9.5 

Bug details

Describe the bug

    Function: file.managed
        Name: /etc/nginx/sites-available/francearchives
      Result: False
     Comment: Unable to manage file: Jinja error: object of type 'NoneType' has no len()
              Traceback (most recent call last):
                File "/usr/lib/python2.7/dist-packages/salt/utils/templates.py", line 394, in render_jinja_tmpl
                  output = template.render(**decoded_context)
                File "/usr/lib/python2.7/dist-packages/jinja2/environment.py", line 989, in render
                  return self.environment.handle_exception(exc_info, True)
                File "/usr/lib/python2.7/dist-packages/jinja2/environment.py", line 754, in handle_exception
                  reraise(exc_type, exc_value, tb)
                File "<template>", line 29, in top-level template code
                File "<template>", line 10, in template
              TypeError: object of type 'NoneType' has no len()

              ; line 10

              ---
              [...]
              {{ key|indent(ind, True) }}{{ operator }}{{ value }}{{ delim }}{{ lb }}
                      {%- elif value is mapping -%}
                          {%- for k, v in value.items() -%}
                              {%- if v is number or v is string -%}
              {{ server_config([v], k, ind) }}
                              {%- elif v|length() > 0 and (v[0] is number or v[0] is string) -%}    <======================
              {{ server_config(v, k, ind) }}
                              {%- else -%}
              {{ lb }}{{ k|indent(ind, True) }} {{ '{' }}
              {{ server_config(v, '', ind + ind_increment) -}}
              {{ '}'|indent(ind, True) }}{{ lb }}
              [...]
              ---
arthurzenika commented 4 years ago

I think I found the culprint in my pillars :

salt-call pillar.get nginx --output yaml 
[snip] 
        - map $host$uri $uuidredirect:
          - include: /srv/nfs/folder/uuid-redirect-map.conf
        - map $host$uri $newfauri: null
        - server:
          - listen:
            - 80
            - default_server
[snip]

the null is produced by a for loop that ends up empty in certain conditions. I've fixed my pillar mine code, but it might be interresting to render the jinja in nginx/files/default/server.conf more robust ?

myii commented 4 years ago

@arthurlogilab Thanks for the report.

the null is produced by a for loop that ends up empty in certain conditions. I've fixed my pillar mine code, but it might be interresting to render the jinja in nginx/files/default/server.conf more robust ?

Sure, would you be willing to take this on?