Saltstack Official Nginx Formula
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123 lines
4.0KB

  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) -%}
  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. file.rename:
  44. {{ sls_block(nginx.servers.rename_opts) }}
  45. - name: {{ server_path(server, state) }}
  46. - source: {{ server_path(server, anti_state) }}
  47. {%- endif %}
  48. {%- elif state == False %}
  49. {%- if nginx.lookup.server_use_symlink %}
  50. file.absent:
  51. - name: {{ server_path(server, anti_state) }}
  52. {%- else %}
  53. file.rename:
  54. {{ sls_block(nginx.servers.rename_opts) }}
  55. - name: {{ server_path(server, state) }}
  56. - source: {{ server_path(server, anti_state) }}
  57. {%- endif -%}
  58. {%- endif -%}
  59. {%- endmacro %}
  60. # Makes sure the enabled directory exists
  61. nginx_server_enabled_dir:
  62. file.directory:
  63. {{ sls_block(nginx.servers.dir_opts) }}
  64. - name: {{ nginx.lookup.server_enabled }}
  65. # If enabled and available are not the same, create available
  66. {% if nginx.lookup.server_enabled != nginx.lookup.server_available -%}
  67. nginx_server_available_dir:
  68. file.directory:
  69. {{ sls_block(nginx.servers.dir_opts) }}
  70. - name: {{ nginx.lookup.server_available }}
  71. {%- endif %}
  72. # Manage the actual server files
  73. {% for server, settings in nginx.servers.managed.items() %}
  74. {% endfor %}
  75. # Managed enabled/disabled state for servers
  76. {% for server, settings in nginx.servers.managed.items() %}
  77. {% if settings.config != None %}
  78. {% if 'source_path' in settings.config %}
  79. {% set source_path = settings.config.source_path %}
  80. {% else %}
  81. {% set source_path = 'salt://nginx/ng/files/server.conf' %}
  82. {% endif %}
  83. {% set conf_state_id = 'server_conf_' ~ loop.index0 %}
  84. {{ conf_state_id }}:
  85. file.managed:
  86. {{ sls_block(nginx.servers.managed_opts) }}
  87. - name: {{ server_curpath(server) }}
  88. - source: {{ source_path }}
  89. - template: jinja
  90. {% if 'source_path' not in settings.config %}
  91. - context:
  92. config: {{ settings.config|json() }}
  93. {% endif %}
  94. {% if 'overwrite' in settings and settings.overwrite == False %}
  95. - unless:
  96. - test -e {{ server_curpath(server) }}
  97. {% endif %}
  98. {% do server_states.append(conf_state_id) %}
  99. {% endif %}
  100. {% if settings.enabled != None %}
  101. {% set status_state_id = 'server_state_' ~ loop.index0 %}
  102. {{ status_state_id }}:
  103. {{ manage_status(server, settings.enabled) }}
  104. {% if settings.config != None %}
  105. - require:
  106. - file: {{ conf_state_id }}
  107. {% endif %}
  108. {% do server_states.append(status_state_id) %}
  109. {% endif %}
  110. {% endfor %}