Saltstack Official Nginx Formula
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

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