Saltstack Official Nginx Formula
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. {% set conf_state_id = 'server_conf_' ~ loop.index0 %}
  79. {{ conf_state_id }}:
  80. file.managed:
  81. {{ sls_block(nginx.servers.managed_opts) }}
  82. - name: {{ server_curpath(server) }}
  83. - source: salt://nginx/ng/files/server.conf
  84. - template: jinja
  85. - context:
  86. config: {{ settings.config|json() }}
  87. {% if 'overwrite' in settings and settings.overwrite == False %}
  88. - unless:
  89. - test -e {{ server_curpath(server) }}
  90. {% endif %}
  91. {% do server_states.append(conf_state_id) %}
  92. {% endif %}
  93. {% if settings.enabled != None %}
  94. {% set status_state_id = 'server_state_' ~ loop.index0 %}
  95. {{ status_state_id }}:
  96. {{ manage_status(server, settings.enabled) }}
  97. {% if settings.config != None %}
  98. - require:
  99. - file: {{ conf_state_id }}
  100. {% endif %}
  101. {% do server_states.append(status_state_id) %}
  102. {% endif %}
  103. {% endfor %}