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.

183 lines
6.1KB

  1. # nginx.servers_config
  2. #
  3. # Manages the configuration of virtual host files.
  4. {#- Get the `tplroot` from `tpldir` #}
  5. {%- set tplroot = tpldir.split('/')[0] %}
  6. {%- from tplroot ~ '/map.jinja' import nginx, sls_block with context %}
  7. {%- from tplroot ~ '/libtofs.jinja' import files_switch with context %}
  8. {% set server_states = [] %}
  9. {#- _nginx is a lightened copy of nginx map intended to passed in templates #}
  10. {%- set _nginx = nginx.copy() %}
  11. {%- do _nginx.pop('snippets') if nginx.snippets is defined %}
  12. {%- do _nginx.pop('servers') if nginx.servers is defined %}
  13. # Simple path concatenation.
  14. # Needs work to make this function on windows.
  15. {% macro path_join(file, root) -%}
  16. {{ root ~ '/' ~ file }}
  17. {%- endmacro %}
  18. # Retrieves the disabled name of a particular server
  19. {% macro disabled_name(server) -%}
  20. {%- if nginx.lookup.server_use_symlink -%}
  21. {{ nginx.servers.managed.get(server).get('disabled_name', server) }}
  22. {%- else -%}
  23. {{ nginx.servers.managed.get(server).get('disabled_name', server ~ nginx.servers.disabled_postfix) }}
  24. {%- endif -%}
  25. {%- endmacro %}
  26. # Gets the path of a particular server
  27. {% macro server_path(server, state) -%}
  28. {%- if state == True -%}
  29. {{ path_join(server, nginx.servers.managed.get(server).get('enabled_dir', nginx.lookup.server_enabled)) }}
  30. {%- elif state == False -%}
  31. {{ path_join(disabled_name(server), nginx.servers.managed.get(server).get('available_dir', nginx.lookup.server_available)) }}
  32. {%- else -%}
  33. {{ path_join(server, nginx.servers.managed.get(server).get('available_dir', nginx.lookup.server_available)) }}
  34. {%- endif -%}
  35. {%- endmacro %}
  36. # Gets the current canonical name of a server
  37. {% macro server_curpath(server) -%}
  38. {{ server_path(server, nginx.servers.managed.get(server).get('available_dir')) }}
  39. {%- endmacro %}
  40. # Creates the sls block that manages symlinking / renaming servers
  41. {% macro manage_status(server, state, deleted) -%}
  42. {%- set anti_state = {True:False, False:True}.get(state) -%}
  43. {% if state == True %}
  44. {%- if nginx.lookup.server_use_symlink %}
  45. file.symlink:
  46. {{ sls_block(nginx.servers.symlink_opts) }}
  47. - name: {{ server_path(server, state) }}
  48. - makedirs: True
  49. - target: {{ server_path(server, anti_state) }}
  50. {%- else %}
  51. {%- if deleted == True %}
  52. file.absent:
  53. - name: {{ server_path(server, state) }}
  54. {%- else %}
  55. file.rename:
  56. {{ sls_block(nginx.servers.rename_opts) }}
  57. - name: {{ server_path(server, state) }}
  58. - source: {{ server_path(server, anti_state) }}
  59. {%- endif %}
  60. {%- endif %}
  61. {%- elif state == False %}
  62. {%- if nginx.lookup.server_use_symlink %}
  63. file.absent:
  64. - name: {{ server_path(server, anti_state) }}
  65. {%- else %}
  66. {%- if deleted == True %}
  67. file.absent:
  68. - name: {{ server_path(server, state) }}
  69. {%- else %}
  70. file.rename:
  71. {{ sls_block(nginx.servers.rename_opts) }}
  72. - name: {{ server_path(server, state) }}
  73. - source: {{ server_path(server, anti_state) }}
  74. {%- endif %}
  75. {%- endif -%}
  76. {%- endif -%}
  77. {%- endmacro %}
  78. # Makes sure the enabled directory exists
  79. nginx_server_enabled_dir:
  80. file.directory:
  81. {{ sls_block(nginx.servers.dir_opts) }}
  82. - name: {{ nginx.lookup.server_enabled }}
  83. # If enabled and available are not the same, create available
  84. {% if nginx.lookup.server_enabled != nginx.lookup.server_available -%}
  85. nginx_server_available_dir:
  86. file.directory:
  87. {{ sls_block(nginx.servers.dir_opts) }}
  88. - name: {{ nginx.lookup.server_available }}
  89. {%- endif %}
  90. # Managed enabled/disabled state for servers
  91. {% for server, settings in nginx.servers.managed.items() %}
  92. {% set conf_state_id = 'server_conf_' ~ loop.index0 %}
  93. {% if 'deleted' in settings and settings.deleted %}
  94. {{ conf_state_id }}:
  95. file.absent:
  96. - name: {{ server_curpath(server) }}
  97. {% do server_states.append(conf_state_id) %}
  98. {% else %}
  99. {% if settings.enabled == True %}
  100. {{ conf_state_id }}:
  101. file.managed:
  102. {{ sls_block(nginx.servers.managed_opts) }}
  103. - name: {{ server_curpath(server) }}
  104. - source:
  105. {%- if 'source_path' in settings.config %}
  106. - {{ settings.config.source_path }}
  107. {%- endif %}
  108. {{ files_switch([server, 'server.conf'],
  109. 'server_conf_file_managed'
  110. )
  111. }}
  112. - makedirs: True
  113. - template: jinja
  114. {%- if 'requires' in settings %}
  115. - require:
  116. {%- for k, v in settings.requires.items() %}
  117. - {{ k }}: {{ v }}
  118. {%- endfor %}
  119. {%- endif %}
  120. {% if 'source_path' not in settings.config %}
  121. - context:
  122. config: {{ settings.config|json(sort_keys=False) }}
  123. nginx: {{ _nginx|json() }}
  124. {% endif %}
  125. {% if 'overwrite' in settings and settings.overwrite == False %}
  126. - unless:
  127. - test -e {{ server_curpath(server) }}
  128. {% endif %}
  129. {% do server_states.append(conf_state_id) %}
  130. {% endif %}
  131. {% endif %}
  132. {% if settings.enabled != None %}
  133. {% set status_state_id = 'server_state_' ~ loop.index0 %}
  134. {%- set enabled_dir = path_join(server, nginx.servers.managed.get(server).get('enabled_dir', nginx.lookup.server_enabled)) -%}
  135. {%- set available_dir = path_join(server, nginx.servers.managed.get(server).get('available_dir', nginx.lookup.server_available)) -%}
  136. {%- if enabled_dir != available_dir %}
  137. {{ status_state_id }}:
  138. {% if 'deleted' in settings and settings.deleted %}
  139. {{ manage_status(server, False, True) }}
  140. {% else %}
  141. {{ manage_status(server, settings.enabled, False) }}
  142. {% endif %}
  143. {% if settings.enabled == True %}
  144. - require:
  145. - file: {{ conf_state_id }}
  146. {% endif %}
  147. {% do server_states.append(status_state_id) %}
  148. {%- endif %} {# enabled != available_dir #}
  149. {% endif %}
  150. {% endfor %}
  151. {# Add . and .. to make it easier to not clean those #}
  152. {% set valid_sites = ['.', '..', ] %}
  153. {# Take sites from nginx.servers.managed #}
  154. {% for server, settings in salt['pillar.get']('nginx:servers:managed', {}).items() %}
  155. {% do valid_sites.append(server) %}
  156. {% endfor %}
  157. {% if salt['file.directory_exists'](nginx.lookup.server_enabled) %}
  158. {% for filename in salt['file.readdir'](nginx.lookup.server_enabled) %}
  159. {% if filename not in valid_sites %}
  160. {{ nginx.lookup.server_enabled ~ '/' ~ filename }}:
  161. file.absent
  162. {% endif %}
  163. {% endfor %}
  164. {% endif %}