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.

167 line
5.7KB

  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('streams') if nginx.streams is defined %}
  13. {%- do _nginx.pop('snippets') if nginx.snippets is defined %}
  14. {%- do _nginx.pop('servers') if nginx.servers is defined %}
  15. # Simple path concatenation.
  16. # Needs work to make this function on windows.
  17. {% macro path_join(file, root) -%}
  18. {{ root ~ '/' ~ file }}
  19. {%- endmacro %}
  20. # Retrieves the disabled name of a particular server
  21. {% macro disabled_name(server) -%}
  22. {%- if nginx.lookup.server_use_symlink -%}
  23. {{ nginx.servers.managed.get(server).get('disabled_name', server) }}
  24. {%- else -%}
  25. {{ nginx.servers.managed.get(server).get('disabled_name', server ~ nginx.servers.disabled_postfix) }}
  26. {%- endif -%}
  27. {%- endmacro %}
  28. # Gets the path of a particular server
  29. {% macro server_path(server, state) -%}
  30. {%- if state == True -%}
  31. {{ path_join(server, nginx.servers.managed.get(server).get('enabled_dir', nginx.lookup.server_enabled)) }}
  32. {%- elif state == False -%}
  33. {{ path_join(disabled_name(server), nginx.servers.managed.get(server).get('available_dir', nginx.lookup.server_available)) }}
  34. {%- else -%}
  35. {{ path_join(server, nginx.servers.managed.get(server).get('available_dir', nginx.lookup.server_available)) }}
  36. {%- endif -%}
  37. {%- endmacro %}
  38. # Gets the current canonical name of a server
  39. {% macro server_curpath(server) -%}
  40. {{ server_path(server, nginx.servers.managed.get(server).get('available_dir')) }}
  41. {%- endmacro %}
  42. # Creates the sls block that manages symlinking / renaming servers
  43. {% macro manage_status(server, state, deleted) -%}
  44. {%- set anti_state = {True:False, False:True}.get(state) -%}
  45. {% if state == True %}
  46. {%- if nginx.lookup.server_use_symlink %}
  47. file.symlink:
  48. {{ sls_block(nginx.servers.symlink_opts) }}
  49. - name: {{ server_path(server, state) }}
  50. - makedirs: True
  51. - target: {{ server_path(server, anti_state) }}
  52. {%- else %}
  53. {%- if deleted == True %}
  54. file.absent:
  55. - name: {{ server_path(server, state) }}
  56. {%- else %}
  57. file.rename:
  58. {{ sls_block(nginx.servers.rename_opts) }}
  59. - name: {{ server_path(server, state) }}
  60. - source: {{ server_path(server, anti_state) }}
  61. {%- endif %}
  62. {%- endif %}
  63. {%- elif state == False %}
  64. {%- if nginx.lookup.server_use_symlink %}
  65. file.absent:
  66. - name: {{ server_path(server, anti_state) }}
  67. {%- else %}
  68. {%- if deleted == True %}
  69. file.absent:
  70. - name: {{ server_path(server, state) }}
  71. {%- else %}
  72. file.rename:
  73. {{ sls_block(nginx.servers.rename_opts) }}
  74. - name: {{ server_path(server, state) }}
  75. - source: {{ server_path(server, anti_state) }}
  76. {%- endif %}
  77. {%- endif -%}
  78. {%- endif -%}
  79. {%- endmacro %}
  80. # Makes sure the enabled directory exists
  81. nginx_server_enabled_dir:
  82. file.directory:
  83. {{ sls_block(nginx.servers.dir_opts) }}
  84. - name: {{ nginx.lookup.server_enabled }}
  85. - clean: {{ nginx.servers.purge_servers_config }}
  86. # If enabled and available are not the same, create available
  87. {% if nginx.lookup.server_enabled != nginx.lookup.server_available -%}
  88. nginx_server_available_dir:
  89. file.directory:
  90. {{ sls_block(nginx.servers.dir_opts) }}
  91. - name: {{ nginx.lookup.server_available }}
  92. - clean: {{ nginx.servers.purge_servers_config }}
  93. {%- endif %}
  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. {% do server_states.append(conf_state_id) %}
  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 'requires' in settings %}
  119. - require:
  120. {%- for k, v in settings.requires.items() %}
  121. - {{ k }}: {{ v }}
  122. {%- endfor %}
  123. {%- endif %}
  124. {% if 'source_path' not in settings.config %}
  125. - context:
  126. config: {{ settings.config|json(sort_keys=False) }}
  127. nginx: {{ _nginx|json() }}
  128. {% endif %}
  129. {% if 'overwrite' in settings and settings.overwrite == False %}
  130. - unless:
  131. - test -e {{ server_curpath(server) }}
  132. {% endif %}
  133. {% do server_states.append(conf_state_id) %}
  134. {% endif %}
  135. {% endif %}
  136. {% if settings.enabled != None %}
  137. {% set status_state_id = 'server_state_' ~ loop.index0 %}
  138. {%- set enabled_dir = path_join(server, nginx.servers.managed.get(server).get('enabled_dir', nginx.lookup.server_enabled)) -%}
  139. {%- set available_dir = path_join(server, nginx.servers.managed.get(server).get('available_dir', nginx.lookup.server_available)) -%}
  140. {%- if enabled_dir != available_dir %}
  141. {{ status_state_id }}:
  142. {% if 'deleted' in settings and settings.deleted %}
  143. {{ manage_status(server, False, True) }}
  144. {% else %}
  145. {{ manage_status(server, settings.enabled, False) }}
  146. {% endif %}
  147. {% if settings.enabled == True %}
  148. - require:
  149. - file: {{ conf_state_id }}
  150. {% endif %}
  151. {% do server_states.append(status_state_id) %}
  152. {%- endif %} {# enabled != available_dir #}
  153. {% endif %}
  154. {% endfor %}