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.

165 line
5.6KB

  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') %}
  12. {%- do _nginx.pop('servers') %}
  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. - target: {{ server_path(server, anti_state) }}
  49. {%- else %}
  50. {%- if deleted == True %}
  51. file.absent:
  52. - name: {{ server_path(server, state) }}
  53. {%- else %}
  54. file.rename:
  55. {{ sls_block(nginx.servers.rename_opts) }}
  56. - name: {{ server_path(server, state) }}
  57. - source: {{ server_path(server, anti_state) }}
  58. {%- endif %}
  59. {%- endif %}
  60. {%- elif state == False %}
  61. {%- if nginx.lookup.server_use_symlink %}
  62. file.absent:
  63. - name: {{ server_path(server, anti_state) }}
  64. {%- else %}
  65. {%- if deleted == True %}
  66. file.absent:
  67. - name: {{ server_path(server, state) }}
  68. {%- else %}
  69. file.rename:
  70. {{ sls_block(nginx.servers.rename_opts) }}
  71. - name: {{ server_path(server, state) }}
  72. - source: {{ server_path(server, anti_state) }}
  73. {%- endif %}
  74. {%- endif -%}
  75. {%- endif -%}
  76. {%- endmacro %}
  77. # Makes sure the enabled directory exists
  78. nginx_server_enabled_dir:
  79. file.directory:
  80. {{ sls_block(nginx.servers.dir_opts) }}
  81. - name: {{ nginx.lookup.server_enabled }}
  82. - clean: {{ nginx.servers.purge_servers_config }}
  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. - clean: {{ nginx.servers.purge_servers_config }}
  90. {%- endif %}
  91. # Manage the actual server files
  92. {% for server, settings in nginx.servers.managed.items() %}
  93. {% endfor %}
  94. # Managed enabled/disabled state for servers
  95. {% for server, settings in nginx.servers.managed.items() %}
  96. {% set conf_state_id = 'server_conf_' ~ loop.index0 %}
  97. {% if 'deleted' in settings and settings.deleted %}
  98. {{ conf_state_id }}:
  99. file.absent:
  100. - name: {{ server_curpath(server) }}
  101. {% else %}
  102. {% if settings.enabled == True %}
  103. {{ conf_state_id }}:
  104. file.managed:
  105. {{ sls_block(nginx.servers.managed_opts) }}
  106. - name: {{ server_curpath(server) }}
  107. - source:
  108. {%- if 'source_path' in settings.config %}
  109. - {{ settings.config.source_path }}
  110. {%- endif %}
  111. {{ files_switch([server, 'server.conf'],
  112. 'server_conf_file_managed'
  113. )
  114. }}
  115. - makedirs: True
  116. - template: jinja
  117. - require_in:
  118. - service: nginx_service
  119. {% if 'source_path' not in settings.config %}
  120. - context:
  121. config: {{ settings.config|json(sort_keys=False) }}
  122. nginx: {{ _nginx|json() }}
  123. {% endif %}
  124. {% if 'overwrite' in settings and settings.overwrite == False %}
  125. - unless:
  126. - test -e {{ server_curpath(server) }}
  127. {% endif %}
  128. {% do server_states.append(conf_state_id) %}
  129. {% endif %}
  130. {% endif %}
  131. {% if settings.enabled != None %}
  132. {% set status_state_id = 'server_state_' ~ loop.index0 %}
  133. {%- set enabled_dir = path_join(server, nginx.servers.managed.get(server).get('enabled_dir', nginx.lookup.server_enabled)) -%}
  134. {%- set available_dir = path_join(server, nginx.servers.managed.get(server).get('available_dir', nginx.lookup.server_available)) -%}
  135. {%- if enabled_dir != available_dir %}
  136. {{ status_state_id }}:
  137. {% if 'deleted' in settings and settings.deleted %}
  138. {{ manage_status(server, False, True) }}
  139. {% else %}
  140. {{ manage_status(server, settings.enabled, False) }}
  141. {% endif %}
  142. {% if settings.enabled == True %}
  143. - require:
  144. - file: {{ conf_state_id }}
  145. {% endif %}
  146. {% if 'deleted' not in settings or ( 'deleted' in settings and settings.deleted == False ) %}
  147. {% do server_states.append(status_state_id) %}
  148. {% endif %}
  149. {%- endif %} {# enabled != available_dir #}
  150. {% endif %}
  151. {% endfor %}