Saltstack Official Nginx Formula
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

160 líneas
5.4KB

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