Saltstack Official Nginx Formula
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

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