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.

162 lines
5.5KB

  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. - 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. - clean: {{ nginx.servers.purge_servers_config }}
  84. # If enabled and available are not the same, create available
  85. {% if nginx.lookup.server_enabled != nginx.lookup.server_available -%}
  86. nginx_server_available_dir:
  87. file.directory:
  88. {{ sls_block(nginx.servers.dir_opts) }}
  89. - name: {{ nginx.lookup.server_available }}
  90. - clean: {{ nginx.servers.purge_servers_config }}
  91. {%- endif %}
  92. # Manage the actual server files
  93. {% for server, settings in nginx.servers.managed.items() %}
  94. {% endfor %}
  95. # Managed enabled/disabled state for servers
  96. {% for server, settings in nginx.servers.managed.items() %}
  97. {% set conf_state_id = 'server_conf_' ~ loop.index0 %}
  98. {% if 'deleted' in settings and settings.deleted %}
  99. {{ conf_state_id }}:
  100. file.absent:
  101. - name: {{ server_curpath(server) }}
  102. {% else %}
  103. {% if settings.enabled == True %}
  104. {{ conf_state_id }}:
  105. file.managed:
  106. {{ sls_block(nginx.servers.managed_opts) }}
  107. - name: {{ server_curpath(server) }}
  108. - source:
  109. {%- if 'source_path' in settings.config %}
  110. - {{ settings.config.source_path }}
  111. {%- endif %}
  112. {{ files_switch([server, 'server.conf'],
  113. 'server_conf_file_managed'
  114. )
  115. }}
  116. - makedirs: True
  117. - template: jinja
  118. {% if 'source_path' not in settings.config %}
  119. - context:
  120. config: {{ settings.config|json(sort_keys=False) }}
  121. nginx: {{ _nginx|json() }}
  122. {% endif %}
  123. {% if 'overwrite' in settings and settings.overwrite == False %}
  124. - unless:
  125. - test -e {{ server_curpath(server) }}
  126. {% endif %}
  127. {% endif %}
  128. {% endif %}
  129. {% do server_states.append(conf_state_id) %}
  130. {% if settings.enabled != None %}
  131. {% set status_state_id = 'server_state_' ~ loop.index0 %}
  132. {%- set enabled_dir = path_join(server, nginx.servers.managed.get(server).get('enabled_dir', nginx.lookup.server_enabled)) -%}
  133. {%- set available_dir = path_join(server, nginx.servers.managed.get(server).get('available_dir', nginx.lookup.server_available)) -%}
  134. {%- if enabled_dir != available_dir %}
  135. {{ status_state_id }}:
  136. {% if 'deleted' in settings and settings.deleted %}
  137. {{ manage_status(server, False, True) }}
  138. {% else %}
  139. {{ manage_status(server, settings.enabled, False) }}
  140. {% endif %}
  141. {% if settings.enabled == True %}
  142. - require:
  143. - file: {{ conf_state_id }}
  144. {% endif %}
  145. {% do server_states.append(status_state_id) %}
  146. {%- endif %} {# enabled != available_dir #}
  147. {% endif %}
  148. {% endfor %}