Saltstack Official Nginx Formula
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 line
3.8KB

  1. # nginx.ng.vhosts_config
  2. #
  3. # Manages the configuration of virtual host files.
  4. {% from 'nginx/ng/map.jinja' import nginx, sls_block with context %}
  5. {% set vhost_states = [] %}
  6. # Simple path concatenation.
  7. # Needs work to make this function on windows.
  8. {% macro path_join(file, root) -%}
  9. {{ root ~ '/' ~ file }}
  10. {%- endmacro %}
  11. # Retrieves the disabled name of a particular vhost
  12. {% macro disabled_name(vhost) -%}
  13. {%- if nginx.lookup.vhost_use_symlink -%}
  14. {{ nginx.vhosts.managed.get(vhost).get('disabled_name', vhost) }}
  15. {%- else -%}
  16. {{ nginx.vhosts.managed.get(vhost).get('disabled_name', vhost ~ nginx.vhosts.disabled_postfix) }}
  17. {%- endif -%}
  18. {%- endmacro %}
  19. # Gets the path of a particular vhost
  20. {% macro vhost_path(vhost, state) -%}
  21. {%- if state == True -%}
  22. {{ path_join(vhost, nginx.vhosts.managed.get(vhost).get('enabled_dir', nginx.lookup.vhost_enabled)) }}
  23. {%- elif state == False -%}
  24. {{ path_join(disabled_name(vhost), nginx.vhosts.managed.get(vhost).get('available_dir', nginx.lookup.vhost_available)) }}
  25. {%- else -%}
  26. {{ path_join(vhost, nginx.vhosts.managed.get(vhost).get('available_dir', nginx.lookup.vhost_available)) }}
  27. {%- endif -%}
  28. {%- endmacro %}
  29. # Gets the current canonical name of a vhost
  30. {% macro vhost_curpath(vhost) -%}
  31. {{ vhost_path(vhost, nginx.vhosts.managed.get(vhost).get('available')) }}
  32. {%- endmacro %}
  33. # Creates the sls block that manages symlinking / renaming vhosts
  34. {% macro manage_status(vhost, state) -%}
  35. {%- set anti_state = {True:False, False:True}.get(state) -%}
  36. {% if state == True %}
  37. {%- if nginx.lookup.vhost_use_symlink %}
  38. file.symlink:
  39. {{ sls_block(nginx.vhosts.symlink_opts) }}
  40. - name: {{ vhost_path(vhost, state) }}
  41. - target: {{ vhost_path(vhost, anti_state) }}
  42. {%- else %}
  43. file.rename:
  44. {{ sls_block(nginx.vhosts.rename_opts) }}
  45. - name: {{ vhost_path(vhost, state) }}
  46. - source: {{ vhost_path(vhost, anti_state) }}
  47. {%- endif %}
  48. {%- elif state == False %}
  49. {%- if nginx.lookup.vhost_use_symlink %}
  50. file.absent:
  51. - name: {{ vhost_path(vhost, anti_state) }}
  52. {%- else %}
  53. file.rename:
  54. {{ sls_block(nginx.vhosts.rename_opts) }}
  55. - name: {{ vhost_path(vhost, state) }}
  56. - source: {{ vhost_path(vhost, anti_state) }}
  57. {%- endif -%}
  58. {%- endif -%}
  59. {%- endmacro %}
  60. # Makes sure the enabled directory exists
  61. nginx_vhost_enabled_dir:
  62. file.directory:
  63. {{ sls_block(nginx.vhosts.dir_opts) }}
  64. - name: {{ nginx.lookup.vhost_enabled }}
  65. # If enabled and available are not the same, create available
  66. {% if nginx.lookup.vhost_enabled != nginx.lookup.vhost_available -%}
  67. nginx_vhost_available_dir:
  68. file.directory:
  69. {{ sls_block(nginx.vhosts.dir_opts) }}
  70. - name: {{ nginx.lookup.vhost_available }}
  71. {%- endif %}
  72. # Manage the actual vhost files
  73. {% for vhost, settings in nginx.vhosts.managed.items() %}
  74. {% endfor %}
  75. # Managed enabled/disabled state for vhosts
  76. {% for vhost, settings in nginx.vhosts.managed.items() %}
  77. {% if settings.config != None %}
  78. {% set conf_state_id = 'vhost_conf_' ~ loop.index0 %}
  79. {{ conf_state_id }}:
  80. file.managed:
  81. {{ sls_block(nginx.vhosts.managed_opts) }}
  82. - name: {{ vhost_curpath(vhost) }}
  83. - source: salt://nginx/ng/files/vhost.conf
  84. - template: jinja
  85. - context:
  86. config: {{ settings.config|json() }}
  87. {% if 'overwrite' in settings and settings.overwrite == False %}
  88. - unless:
  89. - test -e {{ vhost_curpath(vhost) }}
  90. {% endif %}
  91. {% do vhost_states.append(conf_state_id) %}
  92. {% endif %}
  93. {% if settings.enabled != None %}
  94. {% set status_state_id = 'vhost_state_' ~ loop.index0 %}
  95. {{ status_state_id }}:
  96. {{ manage_status(vhost, settings.enabled) }}
  97. {% if settings.config != None %}
  98. - require:
  99. - file: {{ conf_state_id }}
  100. {% endif %}
  101. {% do vhost_states.append(status_state_id) %}
  102. {% endif %}
  103. {% endfor %}