{% from "nginx/map.jinja" import nginx as nginx_map with context %} | |||||
{% set nginx = pillar.get('nginx', {}) -%} | {% set nginx = pillar.get('nginx', {}) -%} | ||||
{% set home = nginx.get('home', '/var/www') -%} | {% set home = nginx.get('home', '/var/www') -%} | ||||
{% set conf_dir = nginx.get('conf_dir', '/etc/nginx') -%} | {% set conf_dir = nginx.get('conf_dir', '/etc/nginx') -%} | ||||
{{ home }}: | {{ home }}: | ||||
file: | file: | ||||
- directory | - directory | ||||
- user: www-data | |||||
- group: www-data | |||||
- user: {{ nginx_map.default_user }} | |||||
- group: {{ nginx_map.default_user }} | |||||
- mode: 0755 | - mode: 0755 | ||||
- makedirs: True | - makedirs: True | ||||
- source: {{ conf_template }} | - source: {{ conf_template }} | ||||
- require: | - require: | ||||
- file: {{ conf_dir }} | - file: {{ conf_dir }} | ||||
- context: | |||||
default_user: {{ nginx_map.default_user }} | |||||
default_group: {{ nginx_map.default_group }} | |||||
{% for dir in ('sites-enabled', 'sites-available') %} | |||||
/etc/nginx/{{ dir }}: | |||||
file.directory: | |||||
- user: root | |||||
- group: root | |||||
{% endfor -%} |
include: | include: | ||||
- nginx.common | - nginx.common | ||||
# Only upstart OR sysvinit should default to true. | |||||
{% if pillar.get('nginx', {}).get('use_upstart', true) %} | {% if pillar.get('nginx', {}).get('use_upstart', true) %} | ||||
- nginx.upstart | - nginx.upstart | ||||
{% elif pillar.get('nginx', {}).get('use_sysvinit', true) %} | |||||
{% elif pillar.get('nginx', {}).get('use_sysvinit', false) %} | |||||
- nginx.sysvinit | - nginx.sysvinit | ||||
{% endif %} | {% endif %} | ||||
{% if pillar.get('nginx', {}).get('user_auth_enabled', true) %} | {% if pillar.get('nginx', {}).get('user_auth_enabled', true) %} |
{% set nginx = salt['grains.filter_by']({ | {% set nginx = salt['grains.filter_by']({ | ||||
'Debian': { | 'Debian': { | ||||
'apache_utils': 'apache2-utils', | 'apache_utils': 'apache2-utils', | ||||
'package': 'nginx-full' | |||||
'package': 'nginx-full', | |||||
'default_user': 'www-data', | |||||
'default_group': 'www-data', | |||||
'disable_before_rename': False, | |||||
'old_init_disable': 'update-rc.d -f nginx remove' | |||||
}, | }, | ||||
'RedHat': { | 'RedHat': { | ||||
'apache_utils': 'httpd-tools', | 'apache_utils': 'httpd-tools', | ||||
'package': 'nginx', | |||||
'default_user': 'nginx', | |||||
'default_group': 'nginx', | |||||
'disable_before_rename': True, | |||||
'old_init_disable': 'chkconfig --del nginx' | |||||
}, | }, | ||||
}, merge=salt['pillar.get']('nginx:lookup')) %} | |||||
}, merge=salt['pillar.get']('nginx:lookup'), default='Debian') %} |
- source: /etc/init.d/nginx | - source: /etc/init.d/nginx | ||||
- require_in: | - require_in: | ||||
- file: nginx | - file: nginx | ||||
- require: | |||||
- pkg: nginx | |||||
{% if grains.get('os_family') == 'Debian' %} | |||||
# Don't dpkg-divert if we are not Debian based! | |||||
cmd: | cmd: | ||||
- wait | - wait | ||||
- name: dpkg-divert --divert /usr/share/nginx/init.d --add /etc/init.d/nginx | - name: dpkg-divert --divert /usr/share/nginx/init.d --add /etc/init.d/nginx | ||||
- file: nginx-old-init | - file: nginx-old-init | ||||
- require_in: | - require_in: | ||||
- file: nginx | - file: nginx | ||||
{% endif %} | |||||
module: | module: | ||||
- wait | - wait | ||||
- name: cmd.run | - name: cmd.run | ||||
- require_in: | - require_in: | ||||
- file: nginx | - file: nginx | ||||
# RedHat requires the init file in place to chkconfig off | |||||
{% if nginx['disable_before_rename'] %} | |||||
{% set _in = '_in' %} | |||||
{% else %} | |||||
{% set _in = '' %} | |||||
{% endif %} | |||||
nginx-old-init-disable: | nginx-old-init-disable: | ||||
cmd: | cmd: | ||||
- wait | |||||
- name: update-rc.d -f nginx remove | |||||
- require: | |||||
- run | |||||
- name: {{ nginx.old_init_disable }} | |||||
- require{{ _in }}: | |||||
- module: nginx-old-init | - module: nginx-old-init | ||||
- watch: | |||||
- file: nginx-old-init | |||||
- unless: [ ! -f /etc/init.d/nginx ] | |||||
{% endif %} | {% endif %} | ||||
{% if grains.get('os_family') == 'Debian' %} | {% if grains.get('os_family') == 'Debian' %} | ||||
- require: | - require: | ||||
- pkg: nginx | - pkg: nginx | ||||
- file: nginx-old-init | - file: nginx-old-init | ||||
- module: nginx-old-init | |||||
- module: nginx-old-init | |||||
{% endif %} | {% endif %} | ||||
service: | service: | ||||
- running | - running | ||||
{% if use_upstart %} | {% if use_upstart %} | ||||
- file: nginx | - file: nginx | ||||
{% endif %} | {% endif %} | ||||
- file: /etc/nginx/nginx.conf | |||||
- file: /etc/nginx/conf.d/default.conf | |||||
- file: /etc/nginx/conf.d/example_ssl.conf | |||||
{% set conf_dir = salt['pillar.get']('nginx:conf_dir', '/etc/nginx') %} | |||||
- file: {{ conf_dir }}/nginx.conf | |||||
- file: {{ conf_dir }}/conf.d/default.conf | |||||
- file: {{ conf_dir }}/conf.d/example_ssl.conf | |||||
- pkg: nginx | - pkg: nginx | ||||
# Create 'service' symlink for tab completion. | # Create 'service' symlink for tab completion. | ||||
{% if use_upstart %} | |||||
# This is not supported in os_family RedHat and likely only works in | |||||
# Debian-based distros | |||||
{% if use_upstart and grains['os_family'] == 'Debian' %} | |||||
/etc/init.d/nginx: | /etc/init.d/nginx: | ||||
file.symlink: | file.symlink: | ||||
- target: /lib/init/upstart-job | - target: /lib/init/upstart-job |
# Source currently requires package 'build-essential' which is Debian based. | |||||
# Will not work with os_family RedHat! You have been warned. | |||||
{% set nginx = pillar.get('nginx', {}) -%} | {% set nginx = pillar.get('nginx', {}) -%} | ||||
{% set version = nginx.get('version', '1.6.2') -%} | {% set version = nginx.get('version', '1.6.2') -%} | ||||
{% set checksum = nginx.get('checksum', 'sha256=b5608c2959d3e7ad09b20fc8f9e5bd4bc87b3bc8ba5936a513c04ed8f1391a18') -%} | {% set checksum = nginx.get('checksum', 'sha256=b5608c2959d3e7ad09b20fc8f9e5bd4bc87b3bc8ba5936a513c04ed8f1391a18') -%} | ||||
{% for name, module in nginx.get('modules', {}).items() -%} | {% for name, module in nginx.get('modules', {}).items() -%} | ||||
- file: get-nginx-{{name}} | - file: get-nginx-{{name}} | ||||
{% endfor %} | {% endfor %} | ||||
- watch_in: | |||||
{% set logger_types = ('access', 'error') %} | |||||
{% for log_type in logger_types %} | |||||
- service: nginx-logger-{{ log_type }} | |||||
{% endfor %} | |||||
- require: | - require: | ||||
- cmd: get-nginx | - cmd: get-nginx | ||||
{% for name, module in nginx.get('modules', {}).items() -%} | {% for name, module in nginx.get('modules', {}).items() -%} |
- user: root | - user: root | ||||
- group: root | - group: root | ||||
- mode: 755 | - mode: 755 | ||||
- source: salt://nginx/templates/sysvinit-logger.jinja | |||||
- source: | |||||
- salt://nginx/templates/{{ grains['os_family'] }}-sysvinit-logger.jinja | |||||
- salt://nginx/templates/sysvinit-logger.jinja | |||||
- context: | - context: | ||||
type: {{ log_type }} | type: {{ log_type }} | ||||
service: | service: | ||||
- running | - running | ||||
- enable: True | - enable: True | ||||
- restart: True | - restart: True | ||||
- watch: | |||||
- cmd: nginx | |||||
- require: | - require: | ||||
- file: nginx-logger-{{ log_type }} | - file: nginx-logger-{{ log_type }} | ||||
- require_in: | - require_in: | ||||
- service: nginx | - service: nginx | ||||
# Not supported in os_family other than Debian | |||||
{% if grains['os_family'] == 'Debian' %} | |||||
cmd: | |||||
- wait | |||||
- name: /usr/sbin/update-rc.d nginx-logger-{{ log_type }} defaults | |||||
{% endif %} | |||||
{% endfor %} | {% endfor %} | ||||
/etc/logrotate.d/nginx: | /etc/logrotate.d/nginx: |
#!/bin/bash | |||||
# /etc/init.d/nginx-logger-{{ type }} | |||||
# | |||||
# chkconfig: 345 84 16 | |||||
# description: Nginx logger for {{ type }} | |||||
# processname: nginx-logger-{{ type }} | |||||
NAME=nginx-logger-{{ type }} | |||||
DESC="syslog forwarder for nginx {{type}} logs" | |||||
DAEMON=/usr/bin/logger | |||||
DAEMON_ARGS=" -f /var/log/nginx/{{ type }}.fifo -t nginx -p {% if type == 'error' %}warn{% else %}debug{% endif %}" | |||||
PIDFILE=/var/run/$NAME.pid | |||||
SCRIPTNAME=/etc/init.d/$NAME | |||||
# Exit if the daemon program isn't installed | |||||
[ -x "$DAEMON" ] || exit 0 | |||||
# Read configuration variable file if it is present | |||||
[ -r /etc/default/$NAME ] && . /etc/default/$NAME | |||||
. /etc/init.d/functions | |||||
do_start() { | |||||
# Return | |||||
# 0 if daemon has been started | |||||
# 1 if daemon was already running | |||||
# 2 if daemon could not be started | |||||
echo -n "Starting $NAME" | |||||
pid=$(cat $PIDFILE 2>/dev/null) | |||||
if [ -n "$pid" ]; then | |||||
failure | |||||
echo | |||||
return 1; | |||||
fi | |||||
if [ ! -r /var/log/nginx/{{ type }}.fifo ]; then | |||||
mkdir -p /var/log/nginx | |||||
mkfifo /var/log/nginx/{{ type }}.fifo | |||||
chown root.root /var/log/nginx/{{ type }}.fifo | |||||
chmod 660 /var/log/nginx/{{ type }}.fifo | |||||
fi | |||||
$DAEMON $DAEMON_ARGS & | |||||
ERROR=$? | |||||
PID=$! | |||||
if [ $ERROR -eq 0 ]; then | |||||
success | |||||
echo | |||||
echo $PID > $PIDFILE | |||||
else | |||||
failure | |||||
echo | |||||
exit 2 | |||||
fi | |||||
} | |||||
do_stop() { | |||||
# Return | |||||
# 0 if daemon has been stopped | |||||
# 1 if daemon was already stopped | |||||
# 2 if daemon could not be stopped | |||||
# other if a failure occurred | |||||
echo -n Stopping $NAME | |||||
pid=$(cat $PIDFILE 2>/dev/null) | |||||
if [ $? -eq 0 ]; then | |||||
echo $pid | xargs kill 2&1>/dev/null | |||||
success | |||||
RETVAL=0 | |||||
else | |||||
failure | |||||
RETVAL=1 | |||||
fi | |||||
echo | |||||
[ "$RETVAL" = 2 ] && return 2 | |||||
rm -f $PIDFILE | |||||
return "$RETVAL" | |||||
} | |||||
case "$1" in | |||||
start) | |||||
do_start | |||||
;; | |||||
stop) | |||||
do_stop | |||||
;; | |||||
status) | |||||
status -p "$PIDFILE" "$DAEMON" && exit 0 || exit $? | |||||
;; | |||||
restart|force-reload) | |||||
do_stop | |||||
do_start | |||||
;; | |||||
*) | |||||
echo "Usage: /etc/init.d/nginx-logger-{{ type }} {start|stop|status|restart|force-reload}" >&2 | |||||
exit 3 | |||||
;; | |||||
esac | |||||
exit 0 |
{% set nginx = pillar.get('nginx', {}) -%} | {% set nginx = pillar.get('nginx', {}) -%} | ||||
{% set user = nginx.get('user', 'www-data') -%} | |||||
{% set group = nginx.get('group', 'www-data') -%} | |||||
# defaults passed via context from the map.jinja | |||||
{% set user = nginx.get('user', default_user) -%} | |||||
{% set group = nginx.get('group', default_group) -%} | |||||
user {{ user }} {{ group }}; | user {{ user }} {{ group }}; | ||||
worker_processes {{ nginx.get('worker_processes', 1) }}; | worker_processes {{ nginx.get('worker_processes', 1) }}; | ||||
{% set worker_rlimit_nofile = nginx.get('worker_rlimit_nofile', '') -%} | {% set worker_rlimit_nofile = nginx.get('worker_rlimit_nofile', '') -%} |