Configure rsyslog to listen over a unix domain socket instead of a port

- Add a placeholder rsyslog.conf so it doesn't fail on start
 - Create access restricted directory for unix socket to be created in
 - Create RSyslogHandler to exit early when logging socket doesn't exist
 - Write updated logging settings when dispatcher comes up and restart rsyslog so they  take effect
 - Move rsyslogd to the web container and create rpc supervisor.sock
 - Add env var for supervisor.conf path
This commit is contained in:
Shane McDonald
2020-02-25 19:55:14 -05:00
committed by Christian Adams
parent f8afae308a
commit c0af3c537b
11 changed files with 75 additions and 15 deletions

View File

@@ -141,6 +141,9 @@ def dispatch_startup():
# and Tower fall out of use/support, we can probably just _assume_ that # and Tower fall out of use/support, we can probably just _assume_ that
# everybody has moved to bigint, and remove this code entirely # everybody has moved to bigint, and remove this code entirely
enforce_bigint_pk_migration() enforce_bigint_pk_migration()
# Update Tower's rsyslog.conf file based on loggins settings in the db
reconfigure_rsyslog()
def inform_cluster_of_shutdown(): def inform_cluster_of_shutdown():

View File

@@ -7,6 +7,7 @@ from awx.main.utils.reload import supervisor_service_command
def reconfigure_rsyslog(): def reconfigure_rsyslog():
tmpl = '' tmpl = ''
parts = ['$IncludeConfig /etc/rsyslog.conf']
if settings.LOG_AGGREGATOR_ENABLED: if settings.LOG_AGGREGATOR_ENABLED:
host = getattr(settings, 'LOG_AGGREGATOR_HOST', '') host = getattr(settings, 'LOG_AGGREGATOR_HOST', '')
port = getattr(settings, 'LOG_AGGREGATOR_PORT', '') port = getattr(settings, 'LOG_AGGREGATOR_PORT', '')
@@ -26,11 +27,8 @@ def reconfigure_rsyslog():
except ValueError: except ValueError:
port = settings.LOG_AGGREGATOR_PORT port = settings.LOG_AGGREGATOR_PORT
parts = []
parts.extend([ parts.extend([
'$IncludeConfig /etc/rsyslog.conf', 'input(type="imuxsock" Socket="/var/run/tower/sockets/rsyslog.sock" unlink="on")',
'$ModLoad imudp',
'$UDPServerRun 51414',
'template(name="awx" type="string" string="%msg%")', 'template(name="awx" type="string" string="%msg%")',
]) ])
if protocol.startswith('http'): if protocol.startswith('http'):
@@ -65,8 +63,8 @@ def reconfigure_rsyslog():
parts.append( parts.append(
f'action(type="omfwd" target="{host}" port="{port}" protocol="{protocol}" action.resumeRetryCount="-1" template="awx")' # noqa f'action(type="omfwd" target="{host}" port="{port}" protocol="{protocol}" action.resumeRetryCount="-1" template="awx")' # noqa
) )
tmpl = '\n'.join(parts)
with open('/var/lib/awx/rsyslog.conf', 'w') as f: tmpl = '\n'.join(parts)
with open('/var/lib/awx/rsyslog/rsyslog.conf', 'w') as f:
f.write(tmpl + '\n') f.write(tmpl + '\n')
supervisor_service_command(command='restart', service='awx-rsyslogd') supervisor_service_command(command='restart', service='awx-rsyslogd')

View File

@@ -3,10 +3,20 @@
# Python # Python
import logging import logging
import os.path
# Django # Django
from django.conf import settings from django.conf import settings
class RSysLogHandler(logging.handlers.SysLogHandler):
def emit(self, msg):
if not os.path.exists(settings.LOGGING_SOCK):
return
return super(RSysLogHandler, self).emit(msg)
ColorHandler = logging.StreamHandler ColorHandler = logging.StreamHandler
if settings.COLOR_LOGS is True: if settings.COLOR_LOGS is True:

View File

@@ -4,6 +4,7 @@
# Python # Python
import subprocess import subprocess
import logging import logging
import os
# Django # Django
from django.conf import settings from django.conf import settings
@@ -17,6 +18,11 @@ def supervisor_service_command(command, service='*', communicate=True):
# supervisorctl restart tower-processes:receiver tower-processes:factcacher # supervisorctl restart tower-processes:receiver tower-processes:factcacher
''' '''
args = ['supervisorctl'] args = ['supervisorctl']
supervisor_config_path = os.getenv('SUPERVISOR_WEB_CONFIG_PATH', None)
if supervisor_config_path:
args.extend(['-c', supervisor_config_path])
args.extend([command, ':'.join(['tower-processes', service])]) args.extend([command, ':'.join(['tower-processes', service])])
logger.debug('Issuing command to {} services, args={}'.format(command, args)) logger.debug('Issuing command to {} services, args={}'.format(command, args))
supervisor_process = subprocess.Popen(args, stdin=subprocess.PIPE, supervisor_process = subprocess.Popen(args, stdin=subprocess.PIPE,

View File

@@ -955,6 +955,7 @@ CHANNEL_LAYERS = {
} }
# Logging configuration. # Logging configuration.
LOGGING_SOCK = '/var/run/tower/sockets/rsyslog.sock'
LOGGING = { LOGGING = {
'version': 1, 'version': 1,
'disable_existing_loggers': False, 'disable_existing_loggers': False,
@@ -1011,9 +1012,9 @@ LOGGING = {
'formatter': 'simple', 'formatter': 'simple',
}, },
'external_logger': { 'external_logger': {
'class': 'logging.handlers.SysLogHandler', 'class': 'awx.main.utils.handlers.RSysLogHandler',
'formatter': 'json', 'formatter': 'json',
'address': ('localhost', 51414), 'address': LOGGING_SOCK,
'filters': ['external_log_enabled', 'dynamic_level_filter'], 'filters': ['external_log_enabled', 'dynamic_level_filter'],
}, },
'tower_warnings': { 'tower_warnings': {

View File

@@ -46,8 +46,20 @@ stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0 stderr_logfile_maxbytes=0
[program:awx-rsyslogd]
command = rsyslogd -n -i /var/run/tower/sockets/rsyslog.pid -f /var/lib/awx/rsyslog/rsyslog.conf
autostart = true
autorestart = true
stopwaitsecs = 1
stopsignal=KILL
stopasgroup=true
killasgroup=true
redirect_stderr=true
stdout_logfile=/dev/stderr
stdout_logfile_maxbytes=0
[group:tower-processes] [group:tower-processes]
programs=nginx,uwsgi,daphne,wsbroadcast programs=nginx,uwsgi,daphne,wsbroadcast,awx-rsyslogd
priority=5 priority=5
# TODO: Exit Handler # TODO: Exit Handler
@@ -62,10 +74,10 @@ events=TICK_60
priority=0 priority=0
[unix_http_server] [unix_http_server]
file=/tmp/supervisor.sock file=/var/run/tower/sockets/supervisor.web.sock
[supervisorctl] [supervisorctl]
serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket serverurl=unix:///var/run/tower/sockets/supervisor.web.sock ; use a unix:// URL for a unix socket
[rpcinterface:supervisor] [rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

View File

@@ -99,10 +99,9 @@ RUN cd /usr/local/bin && \
ADD rsyslog.repo /etc/yum.repos.d/ ADD rsyslog.repo /etc/yum.repos.d/
RUN yum install -y rsyslog-omhttp RUN yum install -y rsyslog-omhttp
RUN echo '$IncludeConfig /var/lib/awx/rsyslog.conf' >> /etc/rsyslog.conf
# Pre-create things that we need to write to # Pre-create things that we need to write to
RUN for dir in /home/awx /var/log/tower /var/log/nginx /var/lib/nginx; \ RUN for dir in /home/awx /var/run/supervisor /var/lib/awx/rsyslog /var/run/rsyslog /var/log/tower /var/log/nginx /var/lib/nginx; \
do mkdir -p $dir; chmod -R g+rwx $dir; chgrp -R root $dir; done && \ do mkdir -p $dir; chmod -R g+rwx $dir; chgrp -R root $dir; done && \
\ \
for file in /etc/passwd /var/run/nginx.pid; \ for file in /etc/passwd /var/run/nginx.pid; \

View File

@@ -94,6 +94,10 @@ spec:
ports: ports:
- containerPort: 8052 - containerPort: 8052
volumeMounts: volumeMounts:
- name: sockets
mountPath: "/var/run/tower/sockets/"
- name: rsyslog
mountPath: "/var/lib/awx/rsyslog/"
{% if ca_trust_dir is defined %} {% if ca_trust_dir is defined %}
- name: {{ kubernetes_deployment_name }}-ca-trust-dir - name: {{ kubernetes_deployment_name }}-ca-trust-dir
mountPath: "/etc/pki/ca-trust/source/anchors/" mountPath: "/etc/pki/ca-trust/source/anchors/"
@@ -174,6 +178,10 @@ spec:
- /usr/bin/launch_awx_task.sh - /usr/bin/launch_awx_task.sh
imagePullPolicy: Always imagePullPolicy: Always
volumeMounts: volumeMounts:
- name: sockets
mountPath: "/var/run/tower/sockets/"
- name: rsyslog
mountPath: "/var/lib/awx/rsyslog/"
{% if ca_trust_dir is defined %} {% if ca_trust_dir is defined %}
- name: {{ kubernetes_deployment_name }}-ca-trust-dir - name: {{ kubernetes_deployment_name }}-ca-trust-dir
mountPath: "/etc/pki/ca-trust/source/anchors/" mountPath: "/etc/pki/ca-trust/source/anchors/"
@@ -223,6 +231,8 @@ spec:
- name: {{ kubernetes_deployment_name }}-memcached-socket - name: {{ kubernetes_deployment_name }}-memcached-socket
mountPath: "/var/run/memcached" mountPath: "/var/run/memcached"
env: env:
- name: SUPERVISOR_WEB_CONFIG_PATH
value: "/supervisor.conf"
- name: AWX_SKIP_MIGRATIONS - name: AWX_SKIP_MIGRATIONS
value: "1" value: "1"
- name: MY_POD_UID - name: MY_POD_UID
@@ -313,6 +323,10 @@ spec:
{{ affinity | to_nice_yaml(indent=2) | indent(width=8, indentfirst=True) }} {{ affinity | to_nice_yaml(indent=2) | indent(width=8, indentfirst=True) }}
{% endif %} {% endif %}
volumes: volumes:
- name: sockets
emptyDir: {}
- name: rsyslog
emptyDir: {}
{% if ca_trust_dir is defined %} {% if ca_trust_dir is defined %}
- name: {{ kubernetes_deployment_name }}-ca-trust-dir - name: {{ kubernetes_deployment_name }}-ca-trust-dir
hostPath: hostPath:

View File

@@ -20,6 +20,8 @@ services:
user: root user: root
restart: unless-stopped restart: unless-stopped
volumes: volumes:
- sockets:/var/run/tower/sockets/
- rsyslog:/var/lib/awx/rsyslog/
- "{{ docker_compose_dir }}/SECRET_KEY:/etc/tower/SECRET_KEY" - "{{ docker_compose_dir }}/SECRET_KEY:/etc/tower/SECRET_KEY"
- "{{ docker_compose_dir }}/environment.sh:/etc/tower/conf.d/environment.sh" - "{{ docker_compose_dir }}/environment.sh:/etc/tower/conf.d/environment.sh"
- "{{ docker_compose_dir }}/credentials.py:/etc/tower/conf.d/credentials.py" - "{{ docker_compose_dir }}/credentials.py:/etc/tower/conf.d/credentials.py"
@@ -75,6 +77,8 @@ services:
user: root user: root
restart: unless-stopped restart: unless-stopped
volumes: volumes:
- sockets:/var/run/tower/sockets/
- rsyslog:/var/lib/awx/rsyslog/
- "{{ docker_compose_dir }}/SECRET_KEY:/etc/tower/SECRET_KEY" - "{{ docker_compose_dir }}/SECRET_KEY:/etc/tower/SECRET_KEY"
- "{{ docker_compose_dir }}/environment.sh:/etc/tower/conf.d/environment.sh" - "{{ docker_compose_dir }}/environment.sh:/etc/tower/conf.d/environment.sh"
- "{{ docker_compose_dir }}/credentials.py:/etc/tower/conf.d/credentials.py" - "{{ docker_compose_dir }}/credentials.py:/etc/tower/conf.d/credentials.py"
@@ -114,6 +118,7 @@ services:
http_proxy: {{ http_proxy | default('') }} http_proxy: {{ http_proxy | default('') }}
https_proxy: {{ https_proxy | default('') }} https_proxy: {{ https_proxy | default('') }}
no_proxy: {{ no_proxy | default('') }} no_proxy: {{ no_proxy | default('') }}
SUPERVISOR_WEB_CONFIG_PATH: '/supervisor.conf'
redis: redis:
image: {{ redis_image }} image: {{ redis_image }}
@@ -157,3 +162,6 @@ services:
https_proxy: {{ https_proxy | default('') }} https_proxy: {{ https_proxy | default('') }}
no_proxy: {{ no_proxy | default('') }} no_proxy: {{ no_proxy | default('') }}
{% endif %} {% endif %}
volumes:
sockets:
rsyslog:

View File

@@ -104,7 +104,7 @@ RUN cd /usr/local/bin && \
ADD tools/docker-compose/rsyslog.repo /etc/yum.repos.d/ ADD tools/docker-compose/rsyslog.repo /etc/yum.repos.d/
RUN yum install -y rsyslog-omhttp RUN yum install -y rsyslog-omhttp
RUN echo '$IncludeConfig /var/lib/awx/rsyslog.conf' >> /etc/rsyslog.conf RUN mkdir -p /var/lib/awx/rsyslog/ && echo '$IncludeConfig /etc/rsyslog.conf' >> /var/lib/awx/rsyslog/rsyslog.conf
RUN dnf -y clean all && rm -rf /root/.cache RUN dnf -y clean all && rm -rf /root/.cache
@@ -123,11 +123,20 @@ ADD tools/docker-compose/entrypoint.sh /
ADD tools/scripts/awx-python /usr/bin/awx-python ADD tools/scripts/awx-python /usr/bin/awx-python
# Pre-create things that we need to write to # Pre-create things that we need to write to
<<<<<<< HEAD
RUN for dir in /var/lib/awx/ /var/log/tower/ /var/lib/awx/projects /.ansible /var/log/nginx /var/lib/nginx /.local; \ RUN for dir in /var/lib/awx/ /var/log/tower/ /var/lib/awx/projects /.ansible /var/log/nginx /var/lib/nginx /.local; \
do mkdir -p $dir; chmod -R g+rwx $dir; chgrp -R root $dir; done && \ do mkdir -p $dir; chmod -R g+rwx $dir; chgrp -R root $dir; done && \
\ \
for file in /etc/passwd /etc/supervisord.conf /venv/awx/lib/python3.6/site-packages/awx.egg-link /var/run/nginx.pid; \ for file in /etc/passwd /etc/supervisord.conf /venv/awx/lib/python3.6/site-packages/awx.egg-link /var/run/nginx.pid; \
do touch $file; chmod -R g+rwx $file; chgrp -R root $file; done do touch $file; chmod -R g+rwx $file; chgrp -R root $file; done
=======
RUN for dir in /var/lib/awx/rsyslog /var/run/tower/rsyslog /var/log/tower/ /var/lib/awx/projects /.ansible /var/log/nginx /var/lib/nginx /.local; \
do mkdir -p $dir; chmod -R g+rwx $dir; chgrp -R root $dir; done
RUN for file in /etc/passwd /etc/supervisord.conf \
/venv/awx/lib/python3.6/site-packages/awx.egg-link /var/run/nginx.pid; \
do touch $file; chmod -R g+rwx $file; chgrp -R root $file; done
>>>>>>> Configure rsyslog to listen over a unix domain socket instead of a port
ENV HOME /var/lib/awx ENV HOME /var/lib/awx
ENV PATH="/usr/local/n/versions/node/10.15.0/bin:${PATH}" ENV PATH="/usr/local/n/versions/node/10.15.0/bin:${PATH}"

View File

@@ -72,7 +72,7 @@ stdout_logfile=/dev/fd/1
stdout_logfile_maxbytes=0 stdout_logfile_maxbytes=0
[program:awx-rsyslogd] [program:awx-rsyslogd]
command = rsyslogd -n -i /awx_devel/rsyslog.pid command = rsyslogd -n -i /var/run/tower/rsyslog/rsyslog.pid -f /var/lib/awx/rsyslog/rsyslog.conf
autostart = true autostart = true
autorestart = true autorestart = true
stopwaitsecs = 1 stopwaitsecs = 1