11
11
12
12
logging .basicConfig (format = '%(asctime)s %(levelname)s: %(message)s' , level = LOG_LEVEL )
13
13
14
- f = open (sys .argv [1 ], "r" )
15
- config = yaml .load (f .read ())
16
- f .close ()
17
-
18
- etcd = Etcd (config ["etcd" ])
19
- postgresql = Postgresql (config ["postgresql" ])
20
- ha = Ha (postgresql , etcd )
21
14
22
15
# stop postgresql on script exit
23
- def stop_postgresql ():
16
+ def stop_postgresql (postgresql ):
24
17
postgresql .stop ()
25
- atexit .register (stop_postgresql )
26
18
27
19
# wait for etcd to be available
28
- def wait_for_etcd (message ):
20
+ def wait_for_etcd (message , etcd , postgresql ):
29
21
etcd_ready = False
30
22
while not etcd_ready :
31
23
try :
@@ -35,60 +27,73 @@ def wait_for_etcd(message):
35
27
logging .info ("waiting on etcd: %s" % message )
36
28
time .sleep (5 )
37
29
38
- logging .info ("Governor Starting up" )
30
+ def run (config ):
31
+ etcd = Etcd (config ["etcd" ])
32
+ postgresql = Postgresql (config ["postgresql" ])
33
+ ha = Ha (postgresql , etcd )
34
+
35
+ atexit .register (stop_postgresql , postgresql )
36
+ logging .info ("Governor Starting up" )
39
37
# is data directory empty?
40
- if postgresql .data_directory_empty ():
41
- logging .info ("Governor Starting up: Empty Data Dir" )
42
- # racing to initialize
43
- wait_for_etcd ("cannot initialize member without ETCD" )
44
- if etcd .race ("/initialize" , postgresql .name ):
45
- logging .info ("Governor Starting up: Initialisation Race ... WON!!!" )
46
- logging .info ("Governor Starting up: Initialise Postgres" )
47
- postgresql .initialize ()
48
- logging .info ("Governor Starting up: Initialise Complete" )
49
- etcd .take_leader (postgresql .name )
38
+ if postgresql .data_directory_empty ():
39
+ logging .info ("Governor Starting up: Empty Data Dir" )
40
+ # racing to initialize
41
+ wait_for_etcd ("cannot initialize member without ETCD" , etcd , postgresql )
42
+ if etcd .race ("/initialize" , postgresql .name ):
43
+ logging .info ("Governor Starting up: Initialisation Race ... WON!!!" )
44
+ logging .info ("Governor Starting up: Initialise Postgres" )
45
+ postgresql .initialize ()
46
+ logging .info ("Governor Starting up: Initialise Complete" )
47
+ etcd .take_leader (postgresql .name )
48
+ logging .info ("Governor Starting up: Starting Postgres" )
49
+ postgresql .start ()
50
+ else :
51
+ logging .info ("Governor Starting up: Initialisation Race ... LOST" )
52
+ logging .info ("Governor Starting up: Sync Postgres from Leader" )
53
+ synced_from_leader = False
54
+ while not synced_from_leader :
55
+ leader = etcd .current_leader ()
56
+ if not leader :
57
+ time .sleep (5 )
58
+ continue
59
+ if postgresql .sync_from_leader (leader ):
60
+ logging .info ("Governor Starting up: Sync Completed" )
61
+ postgresql .write_recovery_conf (leader )
62
+ logging .info ("Governor Starting up: Starting Postgres" )
63
+ postgresql .start ()
64
+ synced_from_leader = True
65
+ else :
66
+ time .sleep (5 )
67
+ else :
68
+ logging .info ("Governor Starting up: Existing Data Dir" )
69
+ postgresql .follow_no_leader ()
50
70
logging .info ("Governor Starting up: Starting Postgres" )
51
71
postgresql .start ()
52
- else :
53
- logging .info ("Governor Starting up: Initialisation Race ... LOST" )
54
- logging .info ("Governor Starting up: Sync Postgres from Leader" )
55
- synced_from_leader = False
56
- while not synced_from_leader :
57
- leader = etcd .current_leader ()
58
- if not leader :
59
- time .sleep (5 )
60
- continue
61
- if postgresql .sync_from_leader (leader ):
62
- logging .info ("Governor Starting up: Sync Completed" )
63
- postgresql .write_recovery_conf (leader )
64
- logging .info ("Governor Starting up: Starting Postgres" )
65
- postgresql .start ()
66
- synced_from_leader = True
67
- else :
68
- time .sleep (5 )
69
- else :
70
- logging .info ("Governor Starting up: Existing Data Dir" )
71
- postgresql .follow_no_leader ()
72
- logging .info ("Governor Starting up: Starting Postgres" )
73
- postgresql .start ()
74
72
75
- wait_for_etcd ("running in readonly mode; cannot participate in cluster HA without etcd" )
76
- logging .info ("Governor Running: Starting Running Loop" )
77
- while True :
78
- try :
79
- logging .info ("Governor Running: %s" % ha .run_cycle ())
73
+ wait_for_etcd ("running in readonly mode; cannot participate in cluster HA without etcd" , etcd , postgresql )
74
+ logging .info ("Governor Running: Starting Running Loop" )
75
+ while True :
76
+ try :
77
+ logging .info ("Governor Running: %s" % ha .run_cycle ())
80
78
81
- # create replication slots
82
- if postgresql .is_leader ():
83
- logging .info ("Governor Running: I am the Leader" )
84
- for node in etcd .get_client_path ("/members?recursive=true" )["node" ]["nodes" ]:
85
- member = node ["key" ].split ('/' )[- 1 ]
86
- if member != postgresql .name :
87
- postgresql .query ("DO LANGUAGE plpgsql $$DECLARE somevar VARCHAR; BEGIN SELECT slot_name INTO somevar FROM pg_replication_slots WHERE slot_name = '%(slot)s' LIMIT 1; IF NOT FOUND THEN PERFORM pg_create_physical_replication_slot('%(slot)s'); END IF; END$$;" % {"slot" : member })
88
- etcd .touch_member (postgresql .name , postgresql .connection_string )
79
+ # create replication slots
80
+ if postgresql .is_leader ():
81
+ logging .info ("Governor Running: I am the Leader" )
82
+ for node in etcd .get_client_path ("/members?recursive=true" )["node" ]["nodes" ]:
83
+ member = node ["key" ].split ('/' )[- 1 ]
84
+ if member != postgresql .name :
85
+ postgresql .query ("DO LANGUAGE plpgsql $$DECLARE somevar VARCHAR; BEGIN SELECT slot_name INTO somevar FROM pg_replication_slots WHERE slot_name = '%(slot)s' LIMIT 1; IF NOT FOUND THEN PERFORM pg_create_physical_replication_slot('%(slot)s'); END IF; END$$;" % {"slot" : member })
86
+ etcd .touch_member (postgresql .name , postgresql .connection_string )
89
87
90
- time .sleep (config ["loop_wait" ])
91
- except urllib2 .URLError :
92
- logging .warning ("Lost connection to etcd, setting no leader and waiting on etcd" )
93
- postgresql .follow_no_leader ()
94
- wait_for_etcd ("running in readonly mode; cannot participate in cluster HA without etcd" )
88
+ time .sleep (config ["loop_wait" ])
89
+ except urllib2 .URLError :
90
+ logging .info ("Lost connection to etcd, setting no leader and waiting on etcd" )
91
+ postgresql .follow_no_leader ()
92
+ wait_for_etcd ("running in readonly mode; cannot participate in cluster HA without etcd" , etcd , postgresql )
93
+
94
+ if __name__ == "__main__" :
95
+ f = open (sys .argv [1 ], "r" )
96
+ config = yaml .load (f .read ())
97
+ f .close ()
98
+
99
+ run (config )
0 commit comments