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.

253 lines
8.0KB

  1. {% from "nginx/map.jinja" import nginx as nginx_map with context %}
  2. # Source currently requires package 'build-essential' which is Debian based.
  3. # Will not work with os_family RedHat!
  4. # TODO- Someone with a RedHat system please update this to work on RedHat
  5. {% set nginx = pillar.get('nginx', {}) -%}
  6. {% set use_sysvinit = nginx.get('use_sysvinit', nginx_map['use_sysvinit']) %}
  7. {% set version = nginx.get('version', '1.6.2') -%}
  8. {% set tarball_url = nginx.get('tarball_url', 'http://nginx.org/download/nginx-' + version + '.tar.gz') -%}
  9. {% set checksum = nginx.get('checksum', 'sha256=b5608c2959d3e7ad09b20fc8f9e5bd4bc87b3bc8ba5936a513c04ed8f1391a18') -%}
  10. {% set home = nginx.get('home', nginx_map['home']) -%}
  11. {% set base_temp_dir = nginx.get('base_temp_dir', '/tmp') -%}
  12. {% set source = nginx.get('source_root', '/usr/local/src') -%}
  13. {% set conf_dir = nginx.get('conf_dir', nginx_map['conf_dir']) -%}
  14. {% set conf_only = nginx.get('conf_only', false) -%}
  15. {% set log_dir = nginx.get('log_dir', nginx_map['log_dir']) -%}
  16. {% set pid_path = nginx.get('pid_path', '/var/run/nginx.pid') -%}
  17. {% set lock_path = nginx.get('lock_path', '/var/lock/nginx.lock') -%}
  18. {% set sbin_dir = nginx.get('sbin_dir', nginx_map['sbin_dir']) -%}
  19. {% set install_prefix = nginx.get('install_prefix', nginx_map['install_prefix']) -%}
  20. {% set with_items = nginx.get('with', ['debug', 'http_dav_module', 'http_stub_status_module', 'pcre', 'ipv6']) -%}
  21. {% set without_items = nginx.get('without', []) -%}
  22. {% set make_flags = nginx.get('make_flags', nginx_map['make_flags']) -%}
  23. {% set service_name = nginx.get('service_name', 'nginx') %}
  24. {% set nginx_package = source + '/nginx-' + version + '.tar.gz' -%}
  25. {% set nginx_source = source + "/nginx-" + version -%}
  26. {% set nginx_modules_dir = source + "/nginx-modules" -%}
  27. include:
  28. - nginx.common
  29. {% if nginx.get('with_luajit', false) %}
  30. - nginx.luajit2
  31. {% endif -%}
  32. {% if nginx.get('with_openresty', false) %}
  33. - nginx.openresty
  34. {% endif -%}
  35. nginx_group:
  36. group.present:
  37. - name: {{ nginx_map.default_group }}
  38. nginx_user:
  39. file.directory:
  40. - name: {{ home }}
  41. - user: {{ nginx_map.default_user }}
  42. - group: {{ nginx_map.default_group }}
  43. - mode: 0755
  44. - require:
  45. - user: nginx_user
  46. - group: nginx_group
  47. user.present:
  48. - name: {{ nginx_map.default_user }}
  49. - home: {{ home }}
  50. - groups:
  51. - {{ nginx_map.default_group }}
  52. - require:
  53. - group: nginx_group
  54. {{ nginx_modules_dir }}:
  55. file:
  56. - directory
  57. - makedirs: True
  58. get-nginx:
  59. pkg.installed:
  60. - names:
  61. - libpcre3-dev
  62. - build-essential
  63. - libssl-dev
  64. file.managed:
  65. - name: {{ nginx_package }}
  66. - source: {{ tarball_url }}
  67. - source_hash: {{ checksum }}
  68. - require:
  69. - file: {{ nginx_modules_dir }}
  70. cmd.wait:
  71. - cwd: {{ source }}
  72. - name: tar -zxf {{ nginx_package }}
  73. - require:
  74. - pkg: get-nginx
  75. - file: get-nginx
  76. - watch:
  77. - file: get-nginx
  78. {% for name, module in nginx.get('modules', {}).items() -%}
  79. get-nginx-{{name}}:
  80. file.managed:
  81. - name: {{ nginx_modules_dir }}/{{name}}.tar.gz
  82. - source: {{ module['source'] }}
  83. - source_hash: {{ module['source_hash'] }}
  84. cmd.wait:
  85. - cwd: {{ nginx_modules_dir }}
  86. - names:
  87. - tar --transform "s,^$(tar --list -zf {{name}}.tar.gz | head -n 1),{{name}}/," -zxf {{name}}.tar.gz
  88. - watch:
  89. - file: get-nginx-{{name}}
  90. - require_in:
  91. - cmd: nginx
  92. {% endfor -%}
  93. {% if nginx.get('ngx_devel_kit', true) -%}
  94. get-ngx_devel_kit:
  95. file.managed:
  96. - name: {{ source }}/ngx_devel_kit.tar.gz
  97. - source: https://github.com/simpl/ngx_devel_kit/archive/v0.2.18.tar.gz
  98. - source_hash: sha1=e21ba642f26047661ada678b21eef001ee2121d8
  99. cmd.wait:
  100. - cwd: {{ source }}
  101. - name: tar -zxf {{ source }}/ngx_devel_kit.tar.gz -C {{ source }}
  102. - watch:
  103. - file: get-ngx_devel_kit
  104. {% endif %}
  105. nginx-source-modified:
  106. cmd.run:
  107. - cwd: {{ nginx_source }}
  108. - stateful: True
  109. - names:
  110. - m=$(find . \! -name "build.*" -newer {{ sbin_dir }}/nginx -print -quit);
  111. r=$?;
  112. if [ x$r != x0 ]; then
  113. echo "changed=yes comment='binary file does not exist or other find error'";
  114. exit 0;
  115. fi;
  116. if [ x$m != "x" ]; then
  117. echo "changed=yes comment='source files are newer than binary'";
  118. exit 0;
  119. fi;
  120. echo "changed=no comment='source files are older than binary'"
  121. {% for name, module in nginx.get('modules', {}).items() -%}
  122. nginx-module-modified-{{name}}:
  123. cmd.run:
  124. - cwd: {{ nginx_modules_dir }}/{{name}}
  125. - stateful: True
  126. - names:
  127. - m=$(find . \! -name "build.*" -newer {{ sbin_dir }}/nginx -print -quit);
  128. r=$?;
  129. if [ x$r != x0 ]; then
  130. echo "changed=yes comment='binary file does not exist or other find error'";
  131. exit 0;
  132. fi;
  133. if [ x$m != "x" ]; then
  134. echo "changed=yes comment='module source files are newer than binary'";
  135. exit 0;
  136. fi;
  137. echo "changed=no comment='module source files are older than binary'"
  138. {% endfor -%}
  139. nginx:
  140. cmd.wait:
  141. - cwd: {{ nginx_source }}
  142. - names:
  143. - (
  144. ./configure --conf-path={{ conf_dir }}/nginx.conf
  145. --sbin-path={{ sbin_dir }}/nginx
  146. --user={{ nginx_map.default_user }}
  147. --group={{ nginx_map.default_group }}
  148. --prefix={{ install_prefix }}
  149. --http-log-path={{ log_dir }}/access.log
  150. --error-log-path={{ log_dir }}/error.log
  151. --pid-path={{ pid_path }}
  152. --lock-path={{ lock_path }}
  153. --http-client-body-temp-path={{ base_temp_dir }}/body
  154. --http-proxy-temp-path={{ base_temp_dir }}/proxy
  155. --http-fastcgi-temp-path={{ base_temp_dir }}/fastcgi
  156. --http-uwsgi-temp-path={{ base_temp_dir }}/temp_uwsgi
  157. --http-scgi-temp-path={{ base_temp_dir }}/temp_scgi
  158. {%- for name, module in nginx.get('modules', {}).items() %}
  159. --add-module={{nginx_modules_dir}}/{{name}}
  160. {%- endfor %}
  161. {%- for name in with_items %}
  162. --with-{{ name }}
  163. {%- endfor %}
  164. {%- for name in without_items %}
  165. --without-{{ name }}
  166. {%- endfor %}
  167. && make {{ make_flags }}
  168. && make install
  169. )
  170. {#- If they want to silence the compiler output, then save it to file so we can reference it later if needed #}
  171. {%- if nginx.get('silence_compiler', true) %}
  172. > {{ nginx_source }}/build.out 2> {{ nginx_source }}/build.err;
  173. {#- If the build process failed, write stderr to stderr and exit with the error code #}
  174. r=$?;
  175. if [ x$r != x0 ]; then
  176. cat {{ nginx_source }}/build.err 1>&2; {#- copy err output to stderr #}
  177. exit $r;
  178. fi;
  179. {% endif %}
  180. - watch:
  181. - cmd: get-nginx
  182. - cmd: nginx-source-modified
  183. {% for name, module in nginx.get('modules', {}).items() -%}
  184. - cmd: nginx-module-modified-{{name}}
  185. - file: get-nginx-{{name}}
  186. {% endfor %}
  187. {% if use_sysvinit %}
  188. - watch_in:
  189. {% set logger_types = ('access', 'error') %}
  190. {% for log_type in logger_types %}
  191. - service: nginx-logger-{{ log_type }}
  192. {% endfor %}
  193. {% endif %}
  194. - require:
  195. - cmd: get-nginx
  196. {% for name, module in nginx.get('modules', {}).items() -%}
  197. - file: get-nginx-{{name}}
  198. {% endfor %}
  199. file:
  200. - managed
  201. - template: jinja
  202. - name: /etc/init.d/{{ service_name }}
  203. - source: salt://nginx/templates/nginx.init.jinja
  204. - user: root
  205. - group: root
  206. - mode: 0755
  207. - context:
  208. sbin_dir: {{ sbin_dir }}
  209. pid_path: {{ pid_path }}
  210. service:
  211. - running
  212. - enable: True
  213. - restart: True
  214. - name: {{ service_name }}
  215. - watch:
  216. - cmd: nginx
  217. - file: {{ conf_dir }}/nginx.conf
  218. - require:
  219. - cmd: nginx
  220. - file: {{ conf_dir }}/nginx.conf
  221. {% for file in nginx.get('delete_confs', []) %}
  222. {{ conf_dir }}/{{ file }}:
  223. file:
  224. - absent
  225. - require_in:
  226. - service: nginx
  227. {% endfor %}
  228. {% for file in nginx.get('delete_htdocs', []) %}
  229. {{ install_prefix }}/html/{{ file }}:
  230. file:
  231. - absent
  232. - require_in:
  233. - service: nginx
  234. {% endfor %}