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.

152 lines
5.2KB

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