|
| 1 | +#!/bin/bash |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 3 | +# use this file except in compliance with the License. You may obtain a copy of |
| 4 | +# the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 10 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 11 | +# License for the specific language governing permissions and limitations under |
| 12 | +# the License. |
| 13 | + |
| 14 | +set -e |
| 15 | + |
| 16 | +# first arg is `-something` or `+something` |
| 17 | +if [ "${1#-}" != "$1" ] || [ "${1#+}" != "$1" ]; then |
| 18 | + set -- /opt/couchdb/bin/couchdb "$@" |
| 19 | +fi |
| 20 | + |
| 21 | +# first arg is the bare word `couchdb` |
| 22 | +if [ "$1" = 'couchdb' ]; then |
| 23 | + shift |
| 24 | + set -- /opt/couchdb/bin/couchdb "$@" |
| 25 | +fi |
| 26 | + |
| 27 | +if [ "$1" = '/opt/couchdb/bin/couchdb' ]; then |
| 28 | + # this is where runtime configuration changes will be written. |
| 29 | + # we need to explicitly touch it here in case /opt/couchdb/etc has |
| 30 | + # been mounted as an external volume, in which case it won't exist. |
| 31 | + # If running as the couchdb user (i.e. container starts as root), |
| 32 | + # write permissions will be granted below. |
| 33 | + touch /opt/couchdb/etc/local.d/docker.ini |
| 34 | + |
| 35 | + # if user is root, assume running under the couchdb user (default) |
| 36 | + # and ensure it is able to access files and directories that may be mounted externally |
| 37 | + if [ "$(id -u)" = '0' ]; then |
| 38 | + # Check that we own everything in /opt/couchdb and fix if necessary. We also |
| 39 | + # add the `-f` flag in all the following invocations because there may be |
| 40 | + # cases where some of these ownership and permissions issues are non-fatal |
| 41 | + # (e.g. a config file owned by root with o+r is actually fine), and we don't |
| 42 | + # to be too aggressive about crashing here ... |
| 43 | + find /opt/couchdb \! \( -user couchdb -group couchdb \) -exec chown -f couchdb:couchdb '{}' + |
| 44 | + |
| 45 | + # Ensure that data files have the correct permissions. We were previously |
| 46 | + # preventing any access to these files outside of couchdb:couchdb, but it |
| 47 | + # turns out that CouchDB itself does not set such restrictive permissions |
| 48 | + # when it creates the files. The approach taken here ensures that the |
| 49 | + # contents of the datadir have the same permissions as they had when they |
| 50 | + # were initially created. This should minimize any startup delay. |
| 51 | + find /opt/couchdb/data -type d ! -perm 0755 -exec chmod -f 0755 '{}' + |
| 52 | + find /opt/couchdb/data -type f ! -perm 0644 -exec chmod -f 0644 '{}' + |
| 53 | + |
| 54 | + # Do the same thing for configuration files and directories. Technically |
| 55 | + # CouchDB only needs read access to the configuration files as all online |
| 56 | + # changes will be applied to the "docker.ini" file below, but we set 644 |
| 57 | + # for the sake of consistency. |
| 58 | + find /opt/couchdb/etc -type d ! -perm 0755 -exec chmod -f 0755 '{}' + |
| 59 | + find /opt/couchdb/etc -type f ! -perm 0644 -exec chmod -f 0644 '{}' + |
| 60 | + fi |
| 61 | + |
| 62 | + if [ ! -z "$NODENAME" ] && ! grep "couchdb@" /opt/couchdb/etc/vm.args; then |
| 63 | + echo "-name couchdb@$NODENAME" >> /opt/couchdb/etc/vm.args |
| 64 | + fi |
| 65 | + |
| 66 | + if [ "$COUCHDB_USER" ] && [ "$COUCHDB_PASSWORD" ]; then |
| 67 | + # Create admin only if not already present |
| 68 | + if ! grep -Pzoqr "\[admins\]\n$COUCHDB_USER =" /opt/couchdb/etc/local.d/*.ini /opt/couchdb/etc/local.ini; then |
| 69 | + printf "\n[admins]\n%s = %s\n" "$COUCHDB_USER" "$COUCHDB_PASSWORD" >> /opt/couchdb/etc/local.d/docker.ini |
| 70 | + fi |
| 71 | + fi |
| 72 | + |
| 73 | + if [ "$COUCHDB_SECRET" ]; then |
| 74 | + # Set secret only if not already present |
| 75 | + if ! grep -Pzoqr "\[chttpd_auth\]\nsecret =" /opt/couchdb/etc/local.d/*.ini /opt/couchdb/etc/local.ini; then |
| 76 | + printf "\n[chttpd_auth]\nsecret = %s\n" "$COUCHDB_SECRET" >> /opt/couchdb/etc/local.d/docker.ini |
| 77 | + fi |
| 78 | + fi |
| 79 | + |
| 80 | + if [ "$COUCHDB_ERLANG_COOKIE" ]; then |
| 81 | + cookieFile='/opt/couchdb/.erlang.cookie' |
| 82 | + if [ -e "$cookieFile" ]; then |
| 83 | + if [ "$(cat "$cookieFile" 2>/dev/null)" != "$COUCHDB_ERLANG_COOKIE" ]; then |
| 84 | + echo >&2 |
| 85 | + echo >&2 "warning: $cookieFile contents do not match COUCHDB_ERLANG_COOKIE" |
| 86 | + echo >&2 |
| 87 | + fi |
| 88 | + else |
| 89 | + echo "$COUCHDB_ERLANG_COOKIE" > "$cookieFile" |
| 90 | + fi |
| 91 | + chown couchdb:couchdb "$cookieFile" |
| 92 | + chmod 600 "$cookieFile" |
| 93 | + fi |
| 94 | + |
| 95 | + if [ "$(id -u)" = '0' ]; then |
| 96 | + chown -f couchdb:couchdb /opt/couchdb/etc/local.d/docker.ini || true |
| 97 | + fi |
| 98 | + |
| 99 | + # if we don't find an [admins] section followed by a non-comment, display a warning |
| 100 | + if ! grep -Pzoqr '\[admins\]\n[^;]\w+' /opt/couchdb/etc/default.d/*.ini /opt/couchdb/etc/local.d/*.ini /opt/couchdb/etc/local.ini; then |
| 101 | + # The - option suppresses leading tabs but *not* spaces. :) |
| 102 | + cat >&2 <<-'EOWARN' |
| 103 | +************************************************************* |
| 104 | +ERROR: CouchDB 3.0+ will no longer run in "Admin Party" |
| 105 | + mode. You *MUST* specify an admin user and |
| 106 | + password, either via your own .ini file mapped |
| 107 | + into the container at /opt/couchdb/etc/local.ini |
| 108 | + or inside /opt/couchdb/etc/local.d, or with |
| 109 | + "-e COUCHDB_USER=admin -e COUCHDB_PASSWORD=password" |
| 110 | + to set it via "docker run". |
| 111 | +************************************************************* |
| 112 | +EOWARN |
| 113 | + exit 1 |
| 114 | + fi |
| 115 | + |
| 116 | + if [ "$(id -u)" = '0' ]; then |
| 117 | + export HOME=$(echo ~couchdb) |
| 118 | + exec setpriv --reuid=couchdb --regid=couchdb --clear-groups "$@" |
| 119 | + fi |
| 120 | +fi |
| 121 | + |
| 122 | +exec "$@" |
0 commit comments