docker-entrypoint.sh 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. set -e
  14. # first arg is `-something` or `+something`
  15. if [ "${1#-}" != "$1" ] || [ "${1#+}" != "$1" ]; then
  16. set -- /opt/couchdb/bin/couchdb "$@"
  17. fi
  18. # first arg is the bare word `couchdb`
  19. if [ "$1" = 'couchdb' ]; then
  20. shift
  21. set -- /opt/couchdb/bin/couchdb "$@"
  22. fi
  23. if [ "$1" = '/opt/couchdb/bin/couchdb' ]; then
  24. # Check that we own everything in /opt/couchdb and fix if necessary. We also
  25. # add the `-f` flag in all the following invocations because there may be
  26. # cases where some of these ownership and permissions issues are non-fatal
  27. # (e.g. a config file owned by root with o+r is actually fine), and we don't
  28. # to be too aggressive about crashing here ...
  29. find /opt/couchdb \! \( -user couchdb -group couchdb \) -exec chown -f couchdb:couchdb '{}' +
  30. # Ensure that data files have the correct permissions. We were previously
  31. # preventing any access to these files outside of couchdb:couchdb, but it
  32. # turns out that CouchDB itself does not set such restrictive permissions
  33. # when it creates the files. The approach taken here ensures that the
  34. # contents of the datadir have the same permissions as they had when they
  35. # were initially created. This should minimize any startup delay.
  36. find /opt/couchdb/data -type d ! -perm 0755 -exec chmod -f 0755 '{}' +
  37. find /opt/couchdb/data -type f ! -perm 0644 -exec chmod -f 0644 '{}' +
  38. # Do the same thing for configuration files and directories. Technically
  39. # CouchDB only needs read access to the configuration files as all online
  40. # changes will be applied to the "docker.ini" file below, but we set 644
  41. # for the sake of consistency.
  42. find /opt/couchdb/etc -type d ! -perm 0755 -exec chmod -f 0755 '{}' +
  43. find /opt/couchdb/etc -type f ! -perm 0644 -exec chmod -f 0644 '{}' +
  44. if [ ! -z "$NODENAME" ] && ! grep "couchdb@" /opt/couchdb/etc/vm.args; then
  45. echo "-name couchdb@$NODENAME" >> /opt/couchdb/etc/vm.args
  46. fi
  47. # Ensure that CouchDB will write custom settings in this file
  48. touch /opt/couchdb/etc/local.d/docker.ini
  49. if [ "$COUCHDB_USER" ] && [ "$COUCHDB_PASSWORD" ]; then
  50. # Create admin only if not already present
  51. if ! grep -Pzoqr "\[admins\]\n$COUCHDB_USER =" /opt/couchdb/etc/local.d/*.ini; then
  52. printf "\n[admins]\n%s = %s\n" "$COUCHDB_USER" "$COUCHDB_PASSWORD" >> /opt/couchdb/etc/local.d/docker.ini
  53. fi
  54. fi
  55. if [ "$COUCHDB_SECRET" ]; then
  56. # Set secret only if not already present
  57. if ! grep -Pzoqr "\[couch_httpd_auth\]\nsecret =" /opt/couchdb/etc/local.d/*.ini; then
  58. printf "\n[couch_httpd_auth]\nsecret = %s\n" "$COUCHDB_SECRET" >> /opt/couchdb/etc/local.d/docker.ini
  59. fi
  60. fi
  61. chown -f couchdb:couchdb /opt/couchdb/etc/local.d/docker.ini || true
  62. # if we don't find an [admins] section followed by a non-comment, display a warning
  63. if ! grep -Pzoqr '\[admins\]\n[^;]\w+' /opt/couchdb/etc/default.d/*.ini /opt/couchdb/etc/local.d/*.ini; then
  64. # The - option suppresses leading tabs but *not* spaces. :)
  65. cat >&2 <<-'EOWARN'
  66. ****************************************************
  67. WARNING: CouchDB is running in Admin Party mode.
  68. This will allow anyone with access to the
  69. CouchDB port to access your database. In
  70. Docker's default configuration, this is
  71. effectively any other container on the same
  72. system.
  73. Use "-e COUCHDB_USER=admin -e COUCHDB_PASSWORD=password"
  74. to set it in "docker run".
  75. ****************************************************
  76. EOWARN
  77. fi
  78. exec gosu couchdb "$@"
  79. fi
  80. exec "$@"