Set up a replicated store with server init --patroni, and watch it
init --patroni user@host ... installs PostgreSQL, Patroni and etcd on the given machines over ssh, with this one as the third etcd vote, makes the role and database, and moves the store in use into the cluster. Each step checks first, so it changes nothing on a cluster that is up. For a store of several hosts the control plane publishes each member's role, state and lag from Patroni, a stopped member as missing, prints it in ctl status, and alerts on it. Making an administrator on PostgreSQL sent a bool to an integer column; it sends an integer now. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
7a562ac1e7
commit
975c93994f
22 files changed
+1495
-43
No files matched your search
@@ -0,0 +1,44 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// SplitPostgres reads a PostgreSQL address that may name several hosts —
|
||||
// "postgres://u:p@h1:5432,h2:5432/db?…", the members of a replicated cluster,
|
||||
// as pgx and libpq both accept — into its hosts, their ports, and the rest of
|
||||
// the address with one placeholder host, which url.Parse can read. url.Parse
|
||||
// cannot read the list itself: it took everything before the last colon for
|
||||
// one host name.
|
||||
func SplitPostgres(dsn string) (hosts, ports []string, rest *url.URL, err error) {
|
||||
scheme, after, ok := strings.Cut(dsn, "://")
|
||||
if !ok {
|
||||
return nil, nil, nil, fmt.Errorf("the store address is not a URL")
|
||||
}
|
||||
authority, tail := after, ""
|
||||
if i := strings.IndexAny(after, "/?"); i >= 0 {
|
||||
authority, tail = after[:i], after[i:]
|
||||
}
|
||||
userinfo, hostlist := "", authority
|
||||
if i := strings.LastIndexByte(authority, '@'); i >= 0 {
|
||||
userinfo, hostlist = authority[:i+1], authority[i+1:]
|
||||
}
|
||||
for _, hp := range strings.Split(hostlist, ",") {
|
||||
u, err := url.Parse("x://" + hp)
|
||||
if err != nil || u.Hostname() == "" {
|
||||
return nil, nil, nil, fmt.Errorf("the store address has a host that is not one: %q", hp)
|
||||
}
|
||||
port := u.Port()
|
||||
if port == "" {
|
||||
port = "5432"
|
||||
}
|
||||
hosts, ports = append(hosts, u.Hostname()), append(ports, port)
|
||||
}
|
||||
rest, err = url.Parse(scheme + "://" + userinfo + "host" + tail)
|
||||
if err != nil {
|
||||
return nil, nil, nil, fmt.Errorf("the store address is not a URL")
|
||||
}
|
||||
return hosts, ports, rest, nil
|
||||
}
|
||||
@@ -252,3 +252,27 @@ func TestPostgresBucketsUsageByEveryGrouping(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgresMakesAnAdministrator(t *testing.T) {
|
||||
// init --postgres and --patroni create the first administrator straight
|
||||
// in PostgreSQL, which refused the bool SetAdmin sent.
|
||||
ctx := context.Background()
|
||||
s := openPG(t)
|
||||
if err := s.CreateUser(ctx, User{ID: "u1", Email: "a@x", Name: "a", PasswordHash: "h",
|
||||
State: UserActive, CreatedAt: time.Now()}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.SetAdmin(ctx, "u1", true); err != nil {
|
||||
t.Fatalf("making an administrator: %v", err)
|
||||
}
|
||||
u, err := s.UserByEmail(ctx, "a@x")
|
||||
if err != nil || !u.IsAdmin {
|
||||
t.Fatalf("%+v, %v", u, err)
|
||||
}
|
||||
if err := s.SetAdmin(ctx, "u1", false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if u, _ := s.UserByEmail(ctx, "a@x"); u.IsAdmin {
|
||||
t.Error("still an administrator")
|
||||
}
|
||||
}
|
||||
@@ -175,7 +175,14 @@ func (s *DB) SetUserState(ctx context.Context, id string, state UserState) error
|
||||
// account is what closes sessions, and that is the lever for someone who should
|
||||
// not be here at all.
|
||||
func (s *DB) SetAdmin(ctx context.Context, id string, admin bool) error {
|
||||
res, err := s.db.ExecContext(ctx, `UPDATE users SET is_admin = ? WHERE id = ?`, admin, id)
|
||||
// An integer, not a bool: the column is an integer on both dialects,
|
||||
// and PostgreSQL refuses a bool for it where SQLite took one — so making
|
||||
// an administrator worked in every test and failed on a fresh install.
|
||||
flag := 0
|
||||
if admin {
|
||||
flag = 1
|
||||
}
|
||||
res, err := s.db.ExecContext(ctx, `UPDATE users SET is_admin = ? WHERE id = ?`, flag, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user