diff --git a/libs/pgbouncer/Makefile b/libs/pgbouncer/Makefile new file mode 100644 index 00000000000000..d0ad2253000201 --- /dev/null +++ b/libs/pgbouncer/Makefile @@ -0,0 +1,72 @@ +include $(TOPDIR)/rules.mk + +PKG_NAME:=pgbouncer +PKG_VERSION:=1.25.1 +PKG_RELEASE:=1 + +PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz +PKG_SOURCE_URL:=https://www.pgbouncer.org/downloads/files/$(PKG_VERSION)/ +PKG_HASH:=6e566ae92fe3ef7f6a1b9e26d6049f7d7ca39c40e29e7b38f6d5500ae15d8465 + +PKG_MAINTAINER:= Carson Wang +PKG_LICENSE:=ISC +PKG_LICENSE_FILES:=COPYRIGHT + +PKG_INSTALL:=1 +PKG_BUILD_PARALLEL:=1 + +include $(INCLUDE_DIR)/package.mk + +define Package/pgbouncer + SECTION:=net + CATEGORY:=Network + SUBMENU:=Database + TITLE:=Lightweight connection pooler for PostgreSQL + URL:=https://www.pgbouncer.org/ + DEPENDS:=+libevent2 +libc +libpthread +libopenssl +libpq +pgsql-cli +pgsql-server +endef + +define Package/pgbouncer/description + PgBouncer is a lightweight connection pooler for PostgreSQL. + It can pool connections to one or more databases and serve clients + over TCP or Unix sockets. +endef + +CONFIGURE_ARGS += \ + --disable-debug \ + --without-systemd \ + --with-libevent=$(STAGING_DIR)/usr \ + +TARGET_CFLAGS += -std=gnu99 + +define Build/Compile + $(MAKE) -C $(PKG_BUILD_DIR) \ + $(TARGET_CONFIGURE_OPTS) \ + CFLAGS="$(TARGET_CFLAGS)" \ + LDFLAGS="$(TARGET_LDFLAGS)" \ + pgbouncer +endef + +define Build/Install + $(INSTALL_DIR) $(PKG_BUILD_DIR)/ipkg-install/usr/bin + $(INSTALL_BIN) $(PKG_BUILD_DIR)/pgbouncer $(PKG_BUILD_DIR)/ipkg-install/usr/bin/ +endef + +define Package/pgbouncer/conffiles +/etc/pgbouncer/pgbouncer.ini +/etc/pgbouncer/userlist.txt +endef + +define Package/pgbouncer/install + $(INSTALL_DIR) $(1)/usr/bin + $(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/bin/pgbouncer $(1)/usr/bin/ + + $(INSTALL_DIR) $(1)/etc/pgbouncer + $(INSTALL_CONF) ./files/pgbouncer.ini $(1)/etc/pgbouncer/ + $(INSTALL_CONF) ./files/userlist.txt $(1)/etc/pgbouncer/ + + $(INSTALL_DIR) $(1)/etc/init.d + $(INSTALL_BIN) ./files/pgbouncer.init $(1)/etc/init.d/pgbouncer +endef + +$(eval $(call BuildPackage,pgbouncer)) diff --git a/libs/pgbouncer/files/pgbouncer.ini b/libs/pgbouncer/files/pgbouncer.ini new file mode 100644 index 00000000000000..93e08b70595603 --- /dev/null +++ b/libs/pgbouncer/files/pgbouncer.ini @@ -0,0 +1,49 @@ +; Please refer to https://www.pgbouncer.org/config.html +[databases] +; database_name = host=localhost port=5432 dbname=database_name +* = host=127.0.0.1 port=5432 + +[pgbouncer] +listen_addr = 0.0.0.0 +listen_port = 6432 + +; Unix socket (Optional) +;unix_socket_dir = /var/run/pgbouncer + +auth_type = scram-sha-256 +auth_file = /etc/pgbouncer/userlist.txt +auth_user = pgbouncer_auth + +admin_users = pgbouncer_admin +stats_users = pgbouncer_admin + +pool_mode = transaction +max_client_conn = 100 +default_pool_size = 20 +min_pool_size = 5 +reserve_pool_size = 5 +reserve_pool_timeout = 5 + +logfile = /var/log/pgbouncer/pgbouncer.log +pidfile = /var/run/pgbouncer/pgbouncer.pid + +max_db_connections = 50 +max_user_connections = 50 + +server_lifetime = 3600 +server_idle_timeout = 600 +query_timeout = 0 +query_wait_timeout = 120 +client_idle_timeout = 0 + +server_reset_query = DISCARD ALL +server_check_delay = 30 +server_check_query = select 1 + +; DNS setting +dns_max_ttl = 15 +dns_zone_check_period = 0 + +; TLS (if need be) +;client_tls_sslmode = disable +;server_tls_sslmode = disable diff --git a/libs/pgbouncer/files/pgbouncer.init b/libs/pgbouncer/files/pgbouncer.init new file mode 100644 index 00000000000000..816fc7ba7a4b55 --- /dev/null +++ b/libs/pgbouncer/files/pgbouncer.init @@ -0,0 +1,79 @@ +#!/bin/sh /etc/rc.common + +START=95 +STOP=10 + +USE_PROCD=1 +PROG=/usr/bin/pgbouncer +CONFIGFILE=/etc/pgbouncer/pgbouncer.ini +PGBOUNCER_USER=pgbouncer +PGBOUNCER_GROUP=pgbouncer + +boot() { + setup_user + start "$@" +} + +setup_user() { + if ! grep -q "^${PGBOUNCER_GROUP}:" /etc/group; then + echo "${PGBOUNCER_GROUP}:x:888:" >> /etc/group + fi + + if ! grep -q "^${PGBOUNCER_USER}:" /etc/passwd; then + echo "${PGBOUNCER_USER}:x:888:888:PgBouncer:/var/lib/pgbouncer:/bin/false" >> /etc/passwd + fi + + mkdir -p /var/log/pgbouncer + mkdir -p /var/run/pgbouncer + mkdir -p /var/lib/pgbouncer + + chown -R ${PGBOUNCER_USER}:${PGBOUNCER_GROUP} /var/log/pgbouncer + chown -R ${PGBOUNCER_USER}:${PGBOUNCER_GROUP} /var/run/pgbouncer + chown -R ${PGBOUNCER_USER}:${PGBOUNCER_GROUP} /var/lib/pgbouncer + chown -R ${PGBOUNCER_USER}:${PGBOUNCER_GROUP} /etc/pgbouncer + + chmod 755 /var/log/pgbouncer + chmod 755 /var/run/pgbouncer + chmod 755 /var/lib/pgbouncer + chmod 755 /etc/pgbouncer + chmod 644 /etc/pgbouncer/pgbouncer.ini + chmod 600 /etc/pgbouncer/userlist.txt +} + +start_service() { + setup_user + + if [ -f /var/run/pgbouncer/pgbouncer.pid ]; then + local pid=$(cat /var/run/pgbouncer/pgbouncer.pid 2>/dev/null) + if [ -n "$pid" ] && ! kill -0 $pid 2>/dev/null; then + rm -f /var/run/pgbouncer/pgbouncer.pid + fi + fi + + procd_open_instance + procd_set_param command $PROG $CONFIGFILE + procd_set_param user ${PGBOUNCER_USER} + procd_set_param respawn ${respawn_threshold:-3600} ${respawn_timeout:-5} ${respawn_retry:-5} + procd_set_param stdout 1 + procd_set_param stderr 1 + procd_close_instance +} + +stop_service() { + if [ -f /var/run/pgbouncer/pgbouncer.pid ]; then + kill $(cat /var/run/pgbouncer/pgbouncer.pid) 2>/dev/null + rm -f /var/run/pgbouncer/pgbouncer.pid + fi +} + +reload_service() { + if [ -f /var/run/pgbouncer/pgbouncer.pid ]; then + kill -HUP $(cat /var/run/pgbouncer/pgbouncer.pid) + else + start + fi +} + +service_triggers() { + procd_add_reload_trigger "pgbouncer" +} diff --git a/libs/pgbouncer/files/userlist.txt b/libs/pgbouncer/files/userlist.txt new file mode 100644 index 00000000000000..96b9e28e81ca08 --- /dev/null +++ b/libs/pgbouncer/files/userlist.txt @@ -0,0 +1,9 @@ +; PgBouncer User list +; Format: "username" "password_hash" +; +; Generate password hash: +; echo -n "passwordusername" | md5sum +; add "md5" before password string +; +; Example: +; "myuser" "md5d8578edf8458ce06fbc5bb76a58c5ca4"