Merge "README update"
diff --git a/.travis.yml b/.travis.yml
index 17ef32d..b7d0d97 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,3 +1,6 @@
+language: python
+python:
+- "2.7.13"
 sudo: required
 services:
   - docker
diff --git a/keystone/files/logging.conf b/keystone/files/logging.conf
deleted file mode 100644
index 9bd6435..0000000
--- a/keystone/files/logging.conf
+++ /dev/null
@@ -1,71 +0,0 @@
-{%- set log_handlers = [] -%}
-{%- for log_handler_name, log_handler_attrs in values.logging.log_handlers.items() %}
-  {%- if log_handler_attrs.get('enabled', False) %}
-    {%- do log_handlers.append(log_handler_name) -%}
-  {%- endif %}
-{%- endfor %}
-[loggers]
-keys = root, keystone, access, eventletwsgi
-
-[handlers]
-keys = {{ log_handlers | join(", ") }}
-
-[formatters]
-keys = context, default{% if values.logging.log_handlers.get('fluentd', {}).get('enabled', False) %}, fluentd{% endif %}
-
-[logger_root]
-level = WARNING
-handlers = {{ log_handlers | join(", ") }}
-
-[logger_keystone]
-level = INFO
-handlers = {{ log_handlers | join(", ") }}
-qualname = keystone
-
-[logger_access]
-level = INFO
-qualname = access
-handlers = {{ log_handlers | join(", ") }}
-
-[logger_eventletwsgi]
-level = INFO
-handlers = {{ log_handlers | join(", ") }}
-qualname = eventlet.wsgi.server
-
-[logger_sqlalchemy]
-level = WARNING
-handlers = {{ log_handlers | join(", ") }}
-qualname = sqlalchemy
-
-{%- if values.logging.log_handlers.get('fluentd', {}).get('enabled', False) %}
-[handler_fluentd]
-class = fluent.handler.FluentHandler
-args = ('openstack.keystone', 'localhost', 24224)
-formatter = fluentd
-{%- endif %}
-
-{%- if values.logging.log_handlers.watchedfile.enabled %}
-[handler_watchedfile]
-class = handlers.WatchedFileHandler
-args = ('/var/log/keystone/keystone.log',)
-formatter = context
-{%- endif %}
-
-{% if values.logging.log_handlers.get('ossyslog', {}).get('enabled', False) -%}
-{%- set ossyslog_args = values.logging.log_handlers.ossyslog.get('args', {}) -%}
-[handler_ossyslog]
-class = oslo_log.handlers.OSSysLogHandler
-args = ( handlers.SysLogHandler.{{ ossyslog_args.get('facility', 'LOG_USER') }}, )
-formatter = context
-{%- endif %}
-
-[formatter_context]
-class = oslo_log.formatters.ContextFormatter
-
-[formatter_default]
-format = %(message)s
-
-{%- if values.logging.log_handlers.get('fluentd', {}).get('enabled', False) %}
-[formatter_fluentd]
-class = oslo_log.formatters.FluentFormatter
-{%- endif %}
diff --git a/keystone/map.jinja b/keystone/map.jinja
index d65b795..aef6028 100644
--- a/keystone/map.jinja
+++ b/keystone/map.jinja
@@ -27,6 +27,7 @@
         'roles': ['admin', 'Member'],
         'cacert': '/etc/ssl/certs/ca-certificates.crt',
         'logging': {
+          'app_name': 'keystone',
           'log_appender': false,
           'log_handlers': {
             'watchedfile': {
@@ -51,6 +52,7 @@
         'roles': ['admin', 'Member'],
         'cacert': '/etc/pki/tls/certs/ca-bundle.crt',
         'logging': {
+          'app_name': 'keystone',
           'log_appender': false,
           'log_handlers': {
             'watchedfile': {
@@ -59,7 +61,7 @@
           },
         },
     },
-}, merge=pillar.keystone.get('server', {}), base='BaseDefaults') %}
+}, merge=pillar.get('keystone', {}).get('server', {}), base='BaseDefaults') %}
 
 {% set client = salt['grains.filter_by']({
     'Debian': {
@@ -74,7 +76,7 @@
         'pkgs': ['python-keystoneclient'],
         'roles': ['admin', 'Member'],
     },
-}, merge=pillar.keystone.get('client', {})) %}
+}, merge=pillar.get('keystone', {}).get('client', {})) %}
 
 {% set control = salt['grains.filter_by']({
     'Debian': {
@@ -83,7 +85,7 @@
     'RedHat': {
         'pkgs': [],
     },
-}, merge=pillar.keystone.get('control', {})) %}
+}, merge=pillar.get('keystone', {}).get('control', {})) %}
 
 {% set monitoring = salt['grains.filter_by']({
     'default': {
diff --git a/keystone/meta/fluentd.yml b/keystone/meta/fluentd.yml
index 8319670..e61ef44 100644
--- a/keystone/meta/fluentd.yml
+++ b/keystone/meta/fluentd.yml
@@ -29,7 +29,7 @@
               time_format: '%d/%b/%Y:%H:%M:%S %z'
               keep_time_key: false
               # Apache keystone format: https://regex101.com/r/KkL3my/1/
-              format: '/(?<hostname>[\w\.\-]+)\:(?<port>\d+)\s(-\s){0,1}(?<http_client_ip_address>[\d\.]+)\s\-\s\-\s\[(?<Timestamp>.*)\]\s(?<Payload>\"(?<http_method>[A-Z]+)\s(?<http_url>\S+)\s(?<http_version>[.\/\dHTFSP]+)\"\s(?<http_status>\d{3})\s(?<http_response_time>\d+)\s(?<http_response_size>\d+)\s\"(?<http_referer>.*)\"\s\"(?<user_agent>.*)\")/'
+              format: '/(?<hostname>[\w\.\-]+)\:(?<port>\d+)\s(?<http_x_forwarded_for_address>[\d\.]+|-)\s(?<http_client_ip_address>[\d\.]+)\s\-\s\-\s\[(?<Timestamp>.*)\]\s(?<Payload>\"(?<http_method>[A-Z]+)\s(?<http_url>\S+)\s(?<http_version>[.\/\dHTFSP]+)\"\s(?<http_status>\d{3})\s(?<http_response_time>\d+)\s(?<http_response_size>\d+)\s\"(?<http_referer>.*)\"\s\"(?<user_agent>.*)\")/'
         filter:
           add_keystone_wsgi_record_fields:
             tag: openstack.keystone
diff --git a/keystone/meta/sphinx.yml b/keystone/meta/sphinx.yml
index 7ce69dd..0155e7f 100644
--- a/keystone/meta/sphinx.yml
+++ b/keystone/meta/sphinx.yml
@@ -23,8 +23,9 @@
           address: http://{{ server.bind.address }}:{{ server.bind.public_port }}
           protocol: http
       param:
-        bind:
+        private_bind:
           value: {{ server.bind.address }}:{{ server.bind.private_port }}
+        bind:
           value: {{ server.bind.address }}:{{ server.bind.public_port }}
         token_engine:
           value: {{ server.tokens.engine }}
diff --git a/keystone/server.sls b/keystone/server.sls
index 4402735..cab1980 100644
--- a/keystone/server.sls
+++ b/keystone/server.sls
@@ -129,10 +129,11 @@
   file.managed:
     - user: keystone
     - group: keystone
-    - source: salt://keystone/files/logging.conf
+    - source: salt://oslo_templates/files/logging/_logging.conf
     - template: jinja
     - defaults:
-        values: {{ server }}
+        service_name: keystone
+        _data: {{ server.logging }}
     - require:
       - pkg: keystone_packages
 {%- if server.logging.log_handlers.get('fluentd', {}).get('enabled', False) %}
@@ -245,6 +246,9 @@
     - require:
       - file: /root/keystonercv3
       - service: {{ keystone_service }}
+      {%- if not grains.get('noservices', False) %}
+      - cmd: keystone_syncdb
+      {%- endif %}
 
 {%- endfor %}
 
diff --git a/tests/run_tests.sh b/tests/run_tests.sh
index af5ccb3..9761585 100755
--- a/tests/run_tests.sh
+++ b/tests/run_tests.sh
@@ -6,11 +6,13 @@
 CURDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
 METADATA=${CURDIR}/../metadata.yml
 FORMULA_NAME=$(cat $METADATA | python -c "import sys,yaml; print yaml.load(sys.stdin)['name']")
+FORMULA_META_DIR=${CURDIR}/../${FORMULA_NAME}/meta
 
 ## Overrideable parameters
 PILLARDIR=${PILLARDIR:-${CURDIR}/pillar}
 BUILDDIR=${BUILDDIR:-${CURDIR}/build}
 VENV_DIR=${VENV_DIR:-${BUILDDIR}/virtualenv}
+MOCK_BIN_DIR=${MOCK_BIN_DIR:-${CURDIR}/mock_bin}
 DEPSDIR=${BUILDDIR}/deps
 
 SALT_FILE_DIR=${SALT_FILE_DIR:-${BUILDDIR}/file_root}
@@ -40,6 +42,15 @@
     python -m pip install salt${PIP_SALT_VERSION}
 }
 
+setup_mock_bin() {
+    # If some state requires a binary, a lightweight replacement for
+    # such binary can be put into MOCK_BIN_DIR for test purposes
+    if [ -d "${MOCK_BIN_DIR}" ]; then
+        PATH="${MOCK_BIN_DIR}:$PATH"
+        export PATH
+    fi
+}
+
 setup_pillar() {
     [ ! -d ${SALT_PILLAR_DIR} ] && mkdir -p ${SALT_PILLAR_DIR}
     echo "base:" > ${SALT_PILLAR_DIR}/top.sls
@@ -121,6 +132,7 @@
     [ -d ${BUILDDIR} ] && mkdir -p ${BUILDDIR}
 
     which salt-call || setup_virtualenv
+    setup_mock_bin
     setup_pillar
     setup_salt
     install_dependencies
@@ -130,7 +142,26 @@
     for pillar in ${PILLARDIR}/*.sls; do
         grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
         state_name=$(basename ${pillar%.sls})
+        salt_run grains.set 'noservices' False force=True
+
+        echo "Checking state ${FORMULA_NAME}.${state_name} ..."
         salt_run --id=${state_name} state.show_sls ${FORMULA_NAME} || (log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1)
+
+        # Check that all files in 'meta' folder can be rendered using any valid pillar
+        for meta in `find ${FORMULA_META_DIR} -type f`; do
+            meta_name=$(basename ${meta})
+            echo "Checking meta ${meta_name} ..."
+            salt_run --out=quiet --id=${state_name} cp.get_template ${meta} ${SALT_CACHE_DIR}/${meta_name} \
+              || (log_err "Failed to render meta ${meta} using pillar ${FORMULA_NAME}.${state_name}"; exit 1)
+            cat ${SALT_CACHE_DIR}/${meta_name}
+        done
+    done
+}
+
+real_run() {
+    for pillar in ${PILLARDIR}/*.sls; do
+        state_name=$(basename ${pillar%.sls})
+        salt_run --id=${state_name} state.sls ${FORMULA_NAME} || (log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1)
     done
 }
 
@@ -147,22 +178,23 @@
 }
 
 ## Main
-[[ "$0" != "$BASH_SOURCE"  ]] || {
-  trap _atexit INT TERM EXIT
+trap _atexit INT TERM EXIT
 
-  case $1 in
-      clean)
-          clean
-          ;;
-      prepare)
-          prepare
-          ;;
-      run)
-          run
-          ;;
-      *)
-          prepare
-          run
-          ;;
-  esac
-}
+case $1 in
+    clean)
+        clean
+        ;;
+    prepare)
+        prepare
+        ;;
+    run)
+        run
+        ;;
+    real-run)
+        real_run
+        ;;
+    *)
+        prepare
+        run
+        ;;
+esac