diff --git a/cmd/start/main.go b/cmd/start/main.go index bf971d2b..efa8b52a 100644 --- a/cmd/start/main.go +++ b/cmd/start/main.go @@ -33,7 +33,7 @@ func main() { for range t.C { if err := node.PostInit(); err != nil { - fmt.Printf("failed post-init: %s", err) + fmt.Printf("failed post-init: %s. Retrying...", err) continue } diff --git a/pkg/flypg/node.go b/pkg/flypg/node.go index a3151a65..bbf1cf7c 100644 --- a/pkg/flypg/node.go +++ b/pkg/flypg/node.go @@ -31,6 +31,7 @@ type Node struct { AppName string PrivateIP string DataDir string + Region string PGPort int SUCredentials Credentials @@ -48,6 +49,7 @@ func NewNode() (*Node, error) { DataDir: "/data/postgresql", ManagerDatabaseName: "repmgr", ManagerConfigPath: "/data/repmgr.conf", + Region: os.Getenv("FLY_REGION"), } if appName := os.Getenv("FLY_APP_NAME"); appName != "" { @@ -149,6 +151,13 @@ func (n *Node) Init() error { return nil } +func (n *Node) ValidPrimary() bool { + if n.Region == os.Getenv("PRIMARY_REGION") { + return true + } + return false +} + // PostInit are operations that should be executed against a running Postgres on boot. func (n *Node) PostInit() error { client, err := state.NewConsulClient() @@ -163,6 +172,11 @@ func (n *Node) PostInit() error { switch primaryIP { case "": + // Check if we can be a primary + if !n.ValidPrimary() { + return fmt.Errorf("no primary to follow and can't configure self as primary because primary region is '%s' and we are in '%s'", n.Region, os.Getenv("PRIMARY_REGION")) + } + // Initialize ourselves as the primary. conn, err := n.NewLocalConnection(context.TODO()) if err != nil { diff --git a/pkg/flypg/repmgr.go b/pkg/flypg/repmgr.go index a063d437..5bba3889 100644 --- a/pkg/flypg/repmgr.go +++ b/pkg/flypg/repmgr.go @@ -99,6 +99,7 @@ func writeManagerConf(node Node) error { "follow_command": fmt.Sprintf("'repmgr standby follow -f %s --log-to-file --upstream-node-id=%%n'", node.ManagerConfigPath), "event_notification_command": fmt.Sprintf("'/usr/local/bin/event_handler -node-id %%n -event %%e -success %%s -details \"%%d\"'"), "event_notifications": "'repmgrd_failover_promote,standby_promote'", + "location": node.Region, } for key, value := range conf {