diff --git a/.daemonless/config.yaml b/.daemonless/config.yaml new file mode 100644 index 0000000..49ffe13 --- /dev/null +++ b/.daemonless/config.yaml @@ -0,0 +1,24 @@ +# dbuild configuration file +# Full reference: https://github.com/daemonless/dbuild + +icon: ':simple-apachecouchdb:' + +build: + architectures: + - amd64 + - aarch64 + variants: + - tag: pkg + containerfile: Containerfile.pkg + aliases: ["latest"] + default: true + - tag: pkg-latest + containerfile: Containerfile.pkg + args: + BASE_VERSION: "15.1-latest" + +cit: + mode: health + port: 5984 + health: /_up + ready: "Apache CouchDB has started" diff --git a/.daemonless/logo.svg b/.daemonless/logo.svg new file mode 100644 index 0000000..117a227 --- /dev/null +++ b/.daemonless/logo.svg @@ -0,0 +1 @@ +Apache CouchDB diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml new file mode 100644 index 0000000..f8e227f --- /dev/null +++ b/.github/workflows/build.yaml @@ -0,0 +1,28 @@ +name: Build FreeBSD Container + +on: + push: + branches: [main] + paths-ignore: ['*.md', 'LICENSE', '.gitignore'] + pull_request: + branches: [main] + workflow_dispatch: + inputs: + variant: + description: 'Tag to rebuild (blank = all)' + required: false + type: string + +permissions: + contents: write + packages: write + id-token: write + actions: read + +jobs: + build: + uses: daemonless/workflows/.github/workflows/daemonless-build.yaml@v1 + with: + image_name: couchdb + variant: ${{ inputs.variant }} + secrets: inherit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5093a29 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +scripts/ diff --git a/Containerfile.pkg b/Containerfile.pkg new file mode 100644 index 0000000..84e1ab3 --- /dev/null +++ b/Containerfile.pkg @@ -0,0 +1,56 @@ +# -------------------------------------------------------------------------- +# THIS FILE IS AUTOGENERATED - DO NOT EDIT MANUALLY +# +# Source: Containerfile.pkg.j2 +# -------------------------------------------------------------------------- + +ARG BASE_VERSION=15.1 +FROM ghcr.io/daemonless/base-exec:${BASE_VERSION} + +ARG FREEBSD_ARCH=amd64 +ARG PKG_NAME="couchdb3" +ARG PACKAGES="${PKG_NAME} ca_root_nss" +ARG HEALTHCHECK_ENDPOINT="http://127.0.0.1:5984/_up" + +ENV HEALTHCHECK_URL="${HEALTHCHECK_ENDPOINT}" + +LABEL org.opencontainers.image.title="CouchDB" \ + org.opencontainers.image.description="Seamless multi-primary syncing JSON document database with an HTTP API, MapReduce views, and Couch Replication Protocol support." \ + org.opencontainers.image.source="https://github.com/daemonless/couchdb" \ + org.opencontainers.image.url="https://couchdb.apache.org/" \ + org.opencontainers.image.documentation="https://docs.couchdb.org/" \ + org.opencontainers.image.licenses="Apache-2.0" \ + org.opencontainers.image.vendor="daemonless" \ + org.opencontainers.image.authors="daemonless" \ + io.daemonless.category="Databases" \ + io.daemonless.port="5984" \ + io.daemonless.volumes="/config" \ + io.daemonless.arch="${FREEBSD_ARCH}" \ + io.daemonless.pkg-source="pkg" \ + io.daemonless.packages="${PACKAGES}" \ + io.daemonless.pkg-name="${PKG_NAME}" \ + io.daemonless.healthcheck-url="${HEALTHCHECK_ENDPOINT}" + +# Install Apache CouchDB from FreeBSD packages +RUN pkg update && \ + pkg install -y ${PACKAGES} && \ + mkdir -p /app /config/data /config/etc/local.d \ + /usr/local/libexec/couchdb3/etc/default.d && \ + pkg query '%v' ${PKG_NAME} > /app/version && \ + ln -sf /usr/local/libexec/couchdb3/bin/couchdb /usr/local/bin/couchdb && \ + ln -sf /usr/local/libexec/couchdb3/bin/remsh /usr/local/bin/remsh && \ + ln -sf /usr/local/libexec/couchdb3/bin/weatherreport /usr/local/bin/weatherreport && \ + chmod -R a+rX /usr/local/etc/couchdb3 && \ + chown -R bsd:bsd /config && \ + pkg clean -ay && \ + rm -rf /var/cache/pkg/* /var/db/pkg/repos/* + +COPY root/ / +RUN chmod +x /daemonless/libexec/couchdb + +WORKDIR /config +CMD ["/daemonless/libexec/couchdb"] + +# 5984: HTTP API and Fauxton UI; 4369: epmd; 9100: Erlang cluster distribution +EXPOSE 5984 4369 9100 +VOLUME /config \ No newline at end of file diff --git a/Containerfile.pkg.j2 b/Containerfile.pkg.j2 new file mode 100644 index 0000000..3126ab6 --- /dev/null +++ b/Containerfile.pkg.j2 @@ -0,0 +1,54 @@ +ARG BASE_VERSION=15.1 +FROM ghcr.io/daemonless/base-exec:${BASE_VERSION} + +ARG FREEBSD_ARCH=amd64 +ARG PKG_NAME="couchdb3" +ARG PACKAGES="${PKG_NAME} ca_root_nss" +ARG HEALTHCHECK_ENDPOINT="http://127.0.0.1:5984/_up" + +ENV HEALTHCHECK_URL="${HEALTHCHECK_ENDPOINT}" + +LABEL org.opencontainers.image.title="{{ title }}" \ + org.opencontainers.image.description="{{ description }}" \ + org.opencontainers.image.source="{{ repo_url }}" \ + org.opencontainers.image.url="{{ web_url }}" \ + org.opencontainers.image.documentation="https://docs.couchdb.org/" \ + org.opencontainers.image.licenses="Apache-2.0" \ + org.opencontainers.image.vendor="daemonless" \ + org.opencontainers.image.authors="daemonless" \ + io.daemonless.category="{{ category }}" \ +{%- if ports %} + io.daemonless.port="{{ ports[0].port }}" \ +{%- endif %} +{%- if volumes %} + io.daemonless.volumes="{{ volumes | map(attribute='path') | join(',') }}" \ +{%- endif %} + io.daemonless.arch="${FREEBSD_ARCH}" \ + io.daemonless.pkg-source="pkg" \ + io.daemonless.packages="${PACKAGES}" \ + io.daemonless.pkg-name="${PKG_NAME}" \ + io.daemonless.healthcheck-url="${HEALTHCHECK_ENDPOINT}" + +# Install Apache CouchDB from FreeBSD packages +RUN pkg update && \ + pkg install -y ${PACKAGES} && \ + mkdir -p /app /config/data /config/etc/local.d \ + /usr/local/libexec/couchdb3/etc/default.d && \ + pkg query '%v' ${PKG_NAME} > /app/version && \ + ln -sf /usr/local/libexec/couchdb3/bin/couchdb /usr/local/bin/couchdb && \ + ln -sf /usr/local/libexec/couchdb3/bin/remsh /usr/local/bin/remsh && \ + ln -sf /usr/local/libexec/couchdb3/bin/weatherreport /usr/local/bin/weatherreport && \ + chmod -R a+rX /usr/local/etc/couchdb3 && \ + chown -R bsd:bsd /config && \ + pkg clean -ay && \ + rm -rf /var/cache/pkg/* /var/db/pkg/repos/* + +COPY root/ / +RUN chmod +x /daemonless/libexec/couchdb + +WORKDIR /config +CMD ["/daemonless/libexec/couchdb"] + +# 5984: HTTP API and Fauxton UI; 4369: epmd; 9100: Erlang cluster distribution +EXPOSE 5984 4369 9100 +VOLUME /config diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..e9f4949 --- /dev/null +++ b/LICENSE @@ -0,0 +1,25 @@ +BSD 2-Clause License + +Copyright (c) 2026, daemonless contributors +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..eb6cb56 --- /dev/null +++ b/README.md @@ -0,0 +1,209 @@ + + +# CouchDB + +[![Build Status](https://img.shields.io/github/actions/workflow/status/daemonless/couchdb/build.yaml?style=flat-square&label=Build&color=green)](https://github.com/daemonless/couchdb/actions) +[![Last Commit](https://img.shields.io/github/last-commit/daemonless/couchdb?style=flat-square&label=Last+Commit&color=blue)](https://github.com/daemonless/couchdb/commits) +[![OCI Pulls](https://img.shields.io/docker/pulls/daemonless/couchdb?style=flat-square&label=OCI+Pulls&color=blue)](https://hub.docker.com/r/daemonless/couchdb) + +Seamless multi-primary syncing JSON document database with an HTTP API, MapReduce views, and Couch Replication Protocol support. + +| | | +|---|---| +| **Port** | 5984 | +| **Registry** | `ghcr.io/daemonless/couchdb` | +| **Source** | [https://github.com/apache/couchdb](https://github.com/apache/couchdb) | +| **Website** | [https://couchdb.apache.org/](https://couchdb.apache.org/) | + +## Version Tags +| Tag | Description | Best For | +| :--- | :--- | :--- | +| `latest` / `pkg` | **FreeBSD Quarterly**. Uses stable, tested packages. | Most users — recommended. | +| `pkg-latest` | **FreeBSD Latest**. Rolling package updates. | Staying current. | + +## Prerequisites +Before deploying, ensure your host environment is ready. See the [Quick Start Guide](https://daemonless.io/guides/quick-start) for host setup instructions. + +## Deployment + +### Podman Compose + +```yaml +services: + couchdb: + image: "ghcr.io/daemonless/couchdb:latest" + container_name: couchdb + environment: + - PUID=1000 # User ID for the application process + - PGID=1000 # Group ID for the application process + - TZ=UTC # Timezone for the container + - COUCHDB_USER=admin # Administrator username created on first start + - COUCHDB_PASSWORD=changeme # Password for COUCHDB_USER (hashed into /config/etc/local.ini on first start; if unset, a random password is saved to /config/admin_password) + - COUCHDB_SINGLE_NODE=true # Automatically create _users and _replicator system databases on startup (set false for multi-node cluster setup) + # - COUCHDB_SECRET= # Cookie authentication signing secret (generated and persisted in /config/etc/local.d/10-secret.ini if unset) + # - NODENAME= # Erlang node host or IP for multi-node clustering (-name couchdb@NODENAME) + # - COUCHDB_ERLANG_COOKIE= # Shared Erlang distribution cookie written to /config/.erlang.cookie for clustering + volumes: + - "/containers/couchdb:/config" + ports: + - "5984:5984" + # always (not unless-stopped) so FreeBSD's podman rc.d auto-starts it at boot + restart: always +``` + +Save as `compose.yaml`, then run `podman-compose up -d`. + +### AppJail Director +**.env**: + +``` +# .env + +DIRECTOR_PROJECT=couchdb +PUID=1000 +PGID=1000 +TZ=UTC +COUCHDB_USER=admin +COUCHDB_PASSWORD=changeme +COUCHDB_SINGLE_NODE=true +``` + +**appjail-director.yml**: + +```yaml +# appjail-director.yml + +options: + - virtualnet: ': default' + - nat: +services: + couchdb: + name: couchdb + options: + - container: 'args:--pull' + - expose: '5984:5984 proto:tcp' + oci: + user: root + environment: + - PUID: !ENV '${PUID}' + - PGID: !ENV '${PGID}' + - TZ: !ENV '${TZ}' + - COUCHDB_USER: !ENV '${COUCHDB_USER}' + - COUCHDB_PASSWORD: !ENV '${COUCHDB_PASSWORD}' + - COUCHDB_SINGLE_NODE: !ENV '${COUCHDB_SINGLE_NODE}' + volumes: + - couchdb: /config +volumes: + couchdb: + device: '/containers/couchdb' +``` + +**Makejail**: + +``` +# Makejail + +ARG tag=latest + +OPTION container=boot +OPTION overwrite=force +OPTION from=ghcr.io/daemonless/couchdb:${tag} +``` + +Save the files above, then run `appjail-director up`. + + +> [!WARNING] +> Exposing ports in AppJail means that your service can be reached from remote hosts. If that is not your intention, do not expose the ports and communicate with the service using the jail's IPv4 address or hostname assigned by the virtual network. +> +> To avoid exposing ports, just remove the `expose` option in your `appjail-director.yml` or from your command-line arguments. + +### Bastille + +> [!WARNING] +> Bastille's OCI support is **experimental**. It requires `buildah` and shares the host network stack (`inherit`). Mount volumes with `--volume HOST JAIL`; without it, image-declared volumes are stored under `${bastille_volumesdir}/${jail}`. + +```yaml +services: + couchdb: + name: couchdb + image: "ghcr.io/daemonless/couchdb:latest" + network: + - mode: host + environment: + - PUID=1000 + - PGID=1000 + - TZ=UTC + - COUCHDB_USER=admin + - COUCHDB_PASSWORD=changeme + - COUCHDB_SINGLE_NODE=true + volumes: + - "/containers/couchdb:/config" +``` + +Save as `bastille-compose.yml`, then run `bastille up`. +Open `http://:5984` from another machine (the server's own browser can't use a published port). + +## Parameters + +### Environment Variables + +| Variable | Default | Description | +|----------|---------|-------------| +| `PUID` | `1000` | User ID for the application process | +| `PGID` | `1000` | Group ID for the application process | +| `TZ` | `UTC` | Timezone for the container | +| `COUCHDB_USER` | `admin` | Administrator username created on first start | +| `COUCHDB_PASSWORD` | `changeme` | Password for COUCHDB_USER (hashed into /config/etc/local.ini on first start; if unset, a random password is saved to /config/admin_password) | +| `COUCHDB_SINGLE_NODE` | `true` | Automatically create _users and _replicator system databases on startup (set false for multi-node cluster setup) | +| `COUCHDB_SECRET` | `` | Cookie authentication signing secret (generated and persisted in /config/etc/local.d/10-secret.ini if unset) | +| `NODENAME` | `` | Erlang node host or IP for multi-node clustering (-name couchdb@NODENAME) | +| `COUCHDB_ERLANG_COOKIE` | `` | Shared Erlang distribution cookie written to /config/.erlang.cookie for clustering | + +### Volumes + +| Path | Description | +|------|-------------| +| `/config` | Database files (/config/data) and persistent configuration (/config/etc) | + +### Ports + +| Port | Protocol | Description | +|------|----------|-------------| +| `5984` | TCP | HTTP API and Fauxton web UI (/_utils/) | + +## First run and Fauxton UI + +CouchDB 3.x requires an administrator account. Set `COUCHDB_USER` and +`COUCHDB_PASSWORD` before first start; CouchDB hashes the password in place +in `/config/etc/local.ini` on boot. If `COUCHDB_PASSWORD` is omitted, +a random password is generated on first start and written to +`/config/admin_password` (mode `0600`). + +Sign in to the Fauxton web interface at `http://:5984/_utils/`. + +## Configuration and persistence + +Both database shards (`/config/data`) and configuration files +(`/config/etc/local.ini`, `/config/etc/local.d/*.ini`, and +`/config/etc/vm.args`) live under `/config`, so settings changed through +Fauxton or `/_config` (such as CORS rules for PouchDB or Obsidian LiveSync) +persist across container upgrades without extra bind mounts. + +`COUCHDB_SINGLE_NODE=true` is enabled by default so standalone deployments +create `_users` and `_replicator` automatically on first boot. For a +multi-node cluster, set `COUCHDB_SINGLE_NODE=false`, `NODENAME`, and a +shared `COUCHDB_ERLANG_COOKIE`, and publish ports `4369` (`epmd`) and +`9100` alongside `5984`. + + +**Architectures:** amd64, aarch64 +**User:** `bsd` (UID/GID via PUID/PGID, defaults to 1000:1000) +**Base:** FreeBSD 15.1 + +--- + +Need help? Join our [Discord](https://discord.gg/Kb9tkhecZT) community. \ No newline at end of file diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 0000000..f4c7976 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,95 @@ +name: couchdb + +x-daemonless: + title: "CouchDB" + icon: ":simple-apachecouchdb:" + category: "Databases" + description: "Seamless multi-primary syncing JSON document database with an HTTP API, MapReduce views, and Couch Replication Protocol support." + upstream_url: "https://github.com/apache/couchdb" + web_url: "https://couchdb.apache.org/" + freshports_url: "https://www.freshports.org/databases/couchdb3/" + user: "bsd" + mlock: false + appjail: true + community: "Discord:https://discord.gg/Kb9tkhecZT" + + docs: + env: + PUID: "User ID for the application process" + PGID: "Group ID for the application process" + TZ: "Timezone for the container" + COUCHDB_USER: + desc: "Administrator username created on first start" + level: primary + COUCHDB_PASSWORD: + desc: "Password for COUCHDB_USER (hashed into /config/etc/local.ini on first start; if unset, a random password is saved to /config/admin_password)" + type: secret + level: primary + COUCHDB_SINGLE_NODE: + desc: "Automatically create _users and _replicator system databases on startup (set false for multi-node cluster setup)" + type: boolean + optional: true + level: options + COUCHDB_SECRET: + desc: "Cookie authentication signing secret (generated and persisted in /config/etc/local.d/10-secret.ini if unset)" + type: secret + optional: true + level: advanced + NODENAME: + desc: "Erlang node host or IP for multi-node clustering (-name couchdb@NODENAME)" + optional: true + level: advanced + COUCHDB_ERLANG_COOKIE: + desc: "Shared Erlang distribution cookie written to /config/.erlang.cookie for clustering" + type: secret + optional: true + level: advanced + volumes: + /config: "Database files (/config/data) and persistent configuration (/config/etc)" + ports: + 5984: "HTTP API and Fauxton web UI (/_utils/)" + + notes: | + ## First run and Fauxton UI + + CouchDB 3.x requires an administrator account. Set `COUCHDB_USER` and + `COUCHDB_PASSWORD` before first start; CouchDB hashes the password in place + in `/config/etc/local.ini` on boot. If `COUCHDB_PASSWORD` is omitted, + a random password is generated on first start and written to + `/config/admin_password` (mode `0600`). + + Sign in to the Fauxton web interface at `http://:5984/_utils/`. + + ## Configuration and persistence + + Both database shards (`/config/data`) and configuration files + (`/config/etc/local.ini`, `/config/etc/local.d/*.ini`, and + `/config/etc/vm.args`) live under `/config`, so settings changed through + Fauxton or `/_config` (such as CORS rules for PouchDB or Obsidian LiveSync) + persist across container upgrades without extra bind mounts. + + `COUCHDB_SINGLE_NODE=true` is enabled by default so standalone deployments + create `_users` and `_replicator` automatically on first boot. For a + multi-node cluster, set `COUCHDB_SINGLE_NODE=false`, `NODENAME`, and a + shared `COUCHDB_ERLANG_COOKIE`, and publish ports `4369` (`epmd`) and + `9100` alongside `5984`. + +services: + couchdb: + image: ghcr.io/daemonless/couchdb:latest + container_name: couchdb + restart: unless-stopped + + environment: + - PUID=1000 + - PGID=1000 + - TZ=UTC + - COUCHDB_USER=admin + - COUCHDB_PASSWORD=changeme + - COUCHDB_SINGLE_NODE=true + + volumes: + - /path/to/containers/couchdb:/config + + ports: + - "5984:5984" diff --git a/root/daemonless/libexec/couchdb b/root/daemonless/libexec/couchdb new file mode 100755 index 0000000..2abdb38 --- /dev/null +++ b/root/daemonless/libexec/couchdb @@ -0,0 +1,152 @@ +#!/bin/sh +# Start Apache CouchDB; /init drops to the run user during container boot, and +# dsu drops root if invoked directly via podman exec. + +if [ "$(id -u)" -eq 0 ]; then + run_user="${DAEMONLESS_USER:-bsd}" + run_home=$(getent passwd "${run_user}" | cut -d: -f6) + export HOME="${run_home:-/config}" USER="${run_user}" LOGNAME="${run_user}" + exec dsu "${run_user}" "$0" "$@" +fi + +# Load secrets from *_FILE environment variables (must run in the launcher, not +# init.d, so exported variables reach the Erlang VM). +for var in $(env | sed -n 's/^\([A-Za-z_][A-Za-z0-9_]*\)_FILE=.*/\1_FILE/p'); do + base_var="${var%_FILE}" + eval "file_path=\${${var}}" + if [ -n "${file_path}" ] && [ -f "${file_path}" ]; then + export "${base_var}=$(cat "${file_path}")" + else + echo "[couchdb] Warning: secret file '${file_path}' for ${var} not found" >&2 + fi +done + +export HOME=/config +export ERL_CRASH_DUMP=/config/erl_crash.dump + +mkdir -p /config/data /config/etc/local.d + +# Seed local.ini once; CouchDB writes its server UUID and runtime /_config +# updates into the last file in COUCHDB_INI_FILES (/config/etc/local.ini). +if [ ! -f /config/etc/local.ini ] && [ -f /usr/local/etc/couchdb3/local.ini.sample ]; then + cp /usr/local/etc/couchdb3/local.ini.sample /config/etc/local.ini + chmod 640 /config/etc/local.ini +fi + +# Seed vm.args once. The FreeBSD port patches -detached into vm.args.sample for +# rc.d, which would background beam.smp and exit PID 1 immediately; strip it and +# disable scheduler busy-waiting inside containers. +if [ ! -f /config/etc/vm.args ] && [ -f /usr/local/etc/couchdb3/vm.args.sample ]; then + sed -e '/^[[:space:]]*-detached[[:space:]]*$/d' \ + -e 's/^#+sbwt /+sbwt /' \ + -e 's/^#+sbwtdcpu /+sbwtdcpu /' \ + -e 's/^#+sbwtdio /+sbwtdio /' \ + /usr/local/etc/couchdb3/vm.args.sample > /config/etc/vm.args + chmod 640 /config/etc/vm.args +fi + +if [ -n "${NODENAME}" ] && [ -f /config/etc/vm.args ]; then + if grep -q '^-name couchdb@127\.0\.0\.1$' /config/etc/vm.args; then + sed -i '' \ + -e "s/^-name couchdb@127\.0\.0\.1$/-name couchdb@${NODENAME}/" \ + -e 's/^-kernel inet_dist_use_interface {127,0,0,1}$/#&/' \ + /config/etc/vm.args + elif ! grep -Eq '^-(s?name)[[:space:]]' /config/etc/vm.args; then + printf '\n-name couchdb@%s\n' "${NODENAME}" >> /config/etc/vm.args + fi + if ! grep -q 'inet_dist_listen_min' /config/etc/vm.args; then + printf '\n-kernel inet_dist_listen_min 9100\n-kernel inet_dist_listen_max 9100\n' >> /config/etc/vm.args + fi +fi + +if [ -n "${COUCHDB_ERLANG_COOKIE}" ]; then + cookie_file=/config/.erlang.cookie + if [ ! -e "${cookie_file}" ]; then + ( + umask 077 + printf '%s' "${COUCHDB_ERLANG_COOKIE}" > "${cookie_file}" + ) + elif [ "$(cat "${cookie_file}" 2>/dev/null)" != "${COUCHDB_ERLANG_COOKIE}" ]; then + echo "[couchdb] Warning: ${cookie_file} does not match COUCHDB_ERLANG_COOKIE" >&2 + fi + chmod 600 "${cookie_file}" 2>/dev/null || true +fi + +has_ini_key() { + section="$1" + key_re="$2" + awk -v sec="[${section}]" -v re="${key_re}" ' + /^[[:space:]]*\[/ { + line = $0 + sub(/^[[:space:]]*/, "", line) + sub(/[[:space:]]*$/, "", line) + in_sec = (line == sec) + next + } + in_sec && $0 !~ /^[[:space:]]*(;|#|$)/ && $0 ~ re { + found = 1 + exit 0 + } + END { exit(found ? 0 : 1) } + ' /config/etc/local.d/*.ini /config/etc/local.ini 2>/dev/null +} + +# Standalone deployments need _users and _replicator created on boot with n=1 +# replica; setting COUCHDB_SINGLE_NODE=false leaves 3-node cluster setup open. +write_single_node() { + if [ "$1" = "true" ]; then + printf '[couchdb]\nsingle_node = true\n\n[cluster]\nn = 1\n' > /config/etc/local.d/05-single-node.ini + else + printf '[couchdb]\nsingle_node = %s\n' "$1" > /config/etc/local.d/05-single-node.ini + fi +} + +if [ -n "${COUCHDB_SINGLE_NODE}" ]; then + write_single_node "${COUCHDB_SINGLE_NODE}" +elif [ ! -f /config/etc/local.d/05-single-node.ini ] && \ + ! has_ini_key "couchdb" "^[[:space:]]*single_node[[:space:]]*="; then + write_single_node "true" +fi + +# CouchDB 3.x refuses to start with no [admins] entry ("Admin Party" removed), +# and hashes plaintext passwords in place only in the last COUCHDB_INI_FILES +# entry (/config/etc/local.ini); local.ini.sample ends with an [admins] header. +append_admin() { + chmod 600 /config/etc/local.ini 2>/dev/null || true + if grep -Eq '^[[:space:]]*\[admins\][[:space:]]*$' /config/etc/local.ini 2>/dev/null; then + printf '%s = %s\n' "$1" "$2" >> /config/etc/local.ini + else + printf '\n[admins]\n%s = %s\n' "$1" "$2" >> /config/etc/local.ini + fi +} + +admin_user="${COUCHDB_USER:-admin}" +if [ -n "${COUCHDB_PASSWORD}" ]; then + if ! has_ini_key "admins" "^[[:space:]]*${admin_user}[[:space:]]*="; then + append_admin "${admin_user}" "${COUCHDB_PASSWORD}" + fi +elif ! has_ini_key "admins" "="; then + gen_pw=$(openssl rand -hex 16) + append_admin "${admin_user}" "${gen_pw}" + ( + umask 077 + printf '%s\n' "${gen_pw}" > /config/admin_password + ) + echo "[couchdb] COUCHDB_PASSWORD not set; generated password for '${admin_user}' in /config/admin_password" >&2 +fi + +# Persist a stable cookie signing secret so Fauxton sessions survive restarts. +if ! has_ini_key "chttpd_auth" "^[[:space:]]*secret[[:space:]]*="; then + secret_val="${COUCHDB_SECRET:-$(openssl rand -hex 32)}" + ( + umask 077 + printf '[chttpd_auth]\nsecret = %s\n' "${secret_val}" > /config/etc/local.d/10-secret.ini + ) +fi + +export COUCHDB_FAUXTON_DOCROOT="/usr/local/www/couchdb3" +export COUCHDB_ARGS_FILE="/config/etc/vm.args" +export COUCHDB_INI_FILES="/usr/local/libexec/couchdb3/etc/default.ini /usr/local/libexec/couchdb3/etc/default.d /config/etc/local.d /config/etc/local.ini" + +cd /config/data || exit 1 +exec /usr/local/libexec/couchdb3/bin/couchdb "$@" diff --git a/root/usr/local/libexec/couchdb3/etc/default.d/10-container.ini b/root/usr/local/libexec/couchdb3/etc/default.d/10-container.ini new file mode 100644 index 0000000..a7f8089 --- /dev/null +++ b/root/usr/local/libexec/couchdb3/etc/default.d/10-container.ini @@ -0,0 +1,18 @@ +; Container defaults loaded after default.ini and before /config/etc/*. +; User overrides in /config/etc/local.d/*.ini and /config/etc/local.ini win. + +[couchdb] +database_dir = /config/data +view_index_dir = /config/data + +[smoosh] +state_dir = /config/data + +[nouveau] +index_dir = /config/data/nouveau + +[chttpd] +bind_address = 0.0.0.0 + +[log] +writer = stderr