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.

158 line
5.3KB

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