Skip to content

Commit

Permalink
Merge pull request #2 from vadv/fix-pgbouncer-1.17
Browse files Browse the repository at this point in the history
Fix pgbouncer 1.17
  • Loading branch information
vadv authored Sep 20, 2023
2 parents fe7e871 + cc493ba commit 4367741
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
41 changes: 37 additions & 4 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"sort"
"strings"
Expand All @@ -20,9 +21,11 @@ import (
)

var (
connectionString = flag.String("connection", "", "connection string to database")
filePath = flag.String("path", "/etc/pgbouncer/userlist.txt", "path to userlist.txt file")
excludeAccounts = flag.String("exclude", "postgres,replicator,monitor", "exclude users from userlist.txt file")
connectionString = flag.String("connection", "", "connection string to database")
filePath = flag.String("path", "/etc/pgbouncer/userlist.txt", "path to userlist.txt file")
excludeAccounts = flag.String("exclude", "postgres,replicator,monitor", "exclude users from userlist.txt file")
reloadTriggerFile = flag.String("reload-trigger-file", "/tmp/pgbouncer-userlist-generator.trigger", "path to trigger file")
reloadCommand = flag.String("reload-command", "systemctl reload pgbouncer", "command to reload")
)

func main() {
Expand All @@ -36,6 +39,10 @@ func main() {
if errGenerate := generateUserList(ctx, db, *filePath, strings.Split(*excludeAccounts, ",")); errGenerate != nil {
log.Fatalf("generate userlist: %s\n", errGenerate)
}
// if trigger file exists - run reload.
if err := processTriggerFile(); err != nil {
log.Fatalf("process trigger file: %s\n", err)
}
}

func generateUserList(ctx context.Context, db *sql.DB, path string, exclude []string) error {
Expand All @@ -53,7 +60,7 @@ select distinct
from pg_authid as id
left join pg_catalog.pg_auth_members m on id.oid = m.member
left join pg_catalog.pg_roles r on m.roleid = r.oid
where (r.rolname is null or not(r.rolname::text=ANY($1))) and id.rolpassword is not null
where (r.rolname is null or not(r.rolname::TEXT=any($1))) and id.rolpassword is not null
`, pq.Array(exclude))
if errRows != nil {
return errRows
Expand Down Expand Up @@ -95,6 +102,10 @@ where (r.rolname is null or not(r.rolname::text=ANY($1))) and id.rolpassword is
return errBackup
}
}
// before rename - write trigger file.
if err := writeTriggerFile(); err != nil {
return err
}
return os.Rename(tmpConfigPath, path)
}

Expand Down Expand Up @@ -139,3 +150,25 @@ func copyFile(src, dst string) error {
}
return out.Close()
}

func writeTriggerFile() error {
return os.WriteFile(*reloadTriggerFile, nil, 0600)
}

// processTriggerFile:
// if trigger file doesnt not exists:
// - exit
//
// if trigger file exist:
// - run reload command
// - remove trigger file
func processTriggerFile() error {
_, errStat := os.Stat(*reloadTriggerFile)
if errStat != nil {
return nil
}
if err := exec.Command("/bin/bash", "-ec", *reloadCommand).Run(); err != nil {
return err
}
return os.Remove(*reloadTriggerFile)
}

0 comments on commit 4367741

Please sign in to comment.