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.

servers_config.sls 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. # nginx.ng.servers_config
  2. #
  3. # Manages the configuration of virtual host files.
  4. {% from 'nginx/ng/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')) }}
  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/ng/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. - template: jinja
  105. {% if 'source_path' not in settings.config %}
  106. - context:
  107. config: {{ settings.config|json() }}
  108. {% endif %}
  109. {% if 'overwrite' in settings and settings.overwrite == False %}
  110. - unless:
  111. - test -e {{ server_curpath(server) }}
  112. {% endif %}
  113. {% do server_states.append(conf_state_id) %}
  114. {% endif %}
  115. {% endif %}
  116. {% if settings.enabled != None %}
  117. {% set status_state_id = 'server_state_' ~ loop.index0 %}
  118. {%- set enabled_dir = path_join(server, nginx.servers.managed.get(server).get('enabled_dir', nginx.lookup.server_enabled)) -%}
  119. {%- set available_dir = path_join(server, nginx.servers.managed.get(server).get('available_dir', nginx.lookup.server_available)) -%}
  120. {%- if enabled_dir != available_dir %}
  121. {{ status_state_id }}:
  122. {% if 'deleted' in settings and settings.deleted %}
  123. {{ manage_status(server, False, True) }}
  124. {% else %}
  125. {{ manage_status(server, settings.enabled, False) }}
  126. {% endif %}
  127. {% if settings.config != None and settings.enabled == True %}
  128. - require:
  129. - file: {{ conf_state_id }}
  130. {% endif %}
  131. {% if 'deleted' not in settings or ( 'deleted' in settings and settings.deleted == False ) %}
  132. {% do server_states.append(status_state_id) %}
  133. {% endif %}
  134. {%- endif %} {# enabled != available_dir #}
  135. {% endif %}
  136. {% endfor %}