1 /*
2  * repmgr-client-global.h
3  * Copyright (c) 2ndQuadrant, 2010-2020
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef _REPMGR_CLIENT_GLOBAL_H_
20 #define _REPMGR_CLIENT_GLOBAL_H_
21 
22 #include "configfile.h"
23 
24 /* values for --copy-external-config-files */
25 #define CONFIG_FILE_SAMEPATH 1
26 #define CONFIG_FILE_PGDATA 2
27 
28 /* default value for "cluster event --limit"*/
29 #define CLUSTER_EVENT_LIMIT 20
30 
31 
32 
33 typedef struct
34 {
35 	/* configuration metadata */
36 	bool		conninfo_provided;
37 	bool		connection_param_provided;
38 	bool		host_param_provided;
39 	bool		limit_provided;
40 	bool		wait_provided;
41 
42 	/* general configuration options */
43 	char		config_file[MAXPGPATH];
44 	bool		dry_run;
45 	bool		force;
46 	char		pg_bindir[MAXLEN];	/* overrides setting in repmgr.conf */
47 	int			wait;
48 	bool		no_wait;
49 	bool		compact;
50 	bool		detail;
51 	bool		dump_config;
52 
53 	/* logging options */
54 	char		log_level[MAXLEN];	/* overrides setting in repmgr.conf */
55 	bool		log_to_file;
56 	bool		quiet;
57 	bool		terse;
58 	bool		verbose;
59 
60 	/* output options */
61 	bool		csv;
62 	bool		nagios;
63 	bool		optformat;
64 
65 	/* standard connection options */
66 	char		dbname[MAXLEN];
67 	char		host[MAXLEN];
68 	char		username[MAXLEN];
69 	char		port[MAXLEN];
70 
71 	/* other connection options */
72 	char		remote_user[MAXLEN];
73 	char		superuser[MAXLEN];
74 
75 	/* general node options */
76 	int			node_id;
77 	char		node_name[NAMEDATALEN];
78 	char		data_dir[MAXPGPATH];
79 	int			remote_node_id;
80 
81 	/* "standby clone" options */
82 	bool		copy_external_config_files;
83 	int			copy_external_config_files_destination;
84 	bool		fast_checkpoint;
85 	bool		rsync_only;
86 	bool		no_upstream_connection;
87 	char		recovery_min_apply_delay[MAXLEN];
88 	char		replication_user[MAXLEN];
89 	char		upstream_conninfo[MAXLEN];
90 	bool		without_barman;
91 	bool		replication_conf_only;
92 	bool		verify_backup;
93 
94 	/* "standby clone"/"standby follow" options */
95 	int			upstream_node_id;
96 
97 	/* "standby register" options */
98 	bool		wait_register_sync;
99 	int			wait_register_sync_seconds;
100 	int			wait_start;
101 
102 	/* "standby switchover" options */
103 	bool		always_promote;
104 	bool		force_rewind_used;
105 	char		force_rewind_path[MAXPGPATH];
106 	bool		siblings_follow;
107 	bool		repmgrd_no_pause;
108 	bool		repmgrd_force_unpause;
109 
110 	/* "node status" options */
111 	bool		is_shutdown_cleanly;
112 
113 	/* "node check" options */
114 	bool		archive_ready;
115 	bool		downstream;
116 	bool		upstream;
117 	bool		replication_lag;
118 	bool		role;
119 	bool		slots;
120 	bool		missing_slots;
121 	bool		has_passfile;
122 	bool		replication_connection;
123 	bool		data_directory_config;
124 	bool		replication_config_owner;
125 	bool		db_connection;
126 
127 	/* "node rejoin" options */
128 	char		config_files[MAXLEN];
129 
130 	/* "node service" options */
131 	char		action[MAXLEN];
132 	bool		check;
133 	bool		list_actions;
134 	bool		checkpoint;
135 
136 	/* "cluster event" options */
137 	bool		all;
138 	char		event[MAXLEN];
139 	int			limit;
140 
141 	/* "cluster cleanup" options */
142 	int			keep_history;
143 
144 	/* following options for internal use */
145 	char		config_archive_dir[MAXPGPATH];
146 	OutputMode	output_mode; /* set through provision of --csv, --nagios or --optformat */
147 	bool		disable_wal_receiver;
148 	bool		enable_wal_receiver;
149 } t_runtime_options;
150 
151 #define T_RUNTIME_OPTIONS_INITIALIZER { \
152 		/* configuration metadata */ \
153 		false, false, false, false, false,	\
154 		/* general configuration options */	\
155 		"", false, false, "", -1, false, false, false, false, \
156 		/* logging options */ \
157 		"", false, false, false, false,	\
158 		/* output options */ \
159 		false, false, false,  \
160 		/* database connection options */ \
161 		"", "", "",	"", \
162 		/* other connection options */ \
163 		"",	"", \
164 		/* general node options */ \
165 		UNKNOWN_NODE_ID, "", "", UNKNOWN_NODE_ID, \
166 		/* "standby clone" options */ \
167 		false, CONFIG_FILE_SAMEPATH, false, false, false, "", "", "", \
168 		false, false, false, \
169 		/* "standby clone"/"standby follow" options */ \
170 		NO_UPSTREAM_NODE, \
171 		/* "standby register" options */ \
172 		false, -1, DEFAULT_WAIT_START,   \
173 		/* "standby switchover" options */ \
174 		false, false, "", false, false, false,	\
175 		/* "node status" options */ \
176 		false, \
177 		/* "node check" options */ \
178 		false, false, false, false, false, false, false, false,	false, false, false, false, \
179 		/* "node rejoin" options */ \
180 		"", \
181 		/* "node service" options */ \
182 		"", false, false, false,  \
183 		/* "cluster event" options */ \
184 		false, "", CLUSTER_EVENT_LIMIT,	\
185 		/* "cluster cleanup" options */ \
186 		0, \
187 		/* following options for internal use */ \
188 		"/tmp", OM_TEXT, false, false \
189 }
190 
191 
192 typedef enum
193 {
194 	barman,
195 	pg_basebackup
196 } standy_clone_mode;
197 
198 typedef enum
199 {
200 	ACTION_UNKNOWN = -1,
201 	ACTION_NONE,
202 	ACTION_START,
203 	ACTION_STOP,
204 	ACTION_STOP_WAIT,
205 	ACTION_RESTART,
206 	ACTION_RELOAD,
207 	ACTION_PROMOTE
208 } t_server_action;
209 
210 
211 typedef enum
212 {
213 	USER_TYPE_UNKNOWN = -1,
214 	REPMGR_USER,
215 	REPLICATION_USER_OPT,
216 	REPLICATION_USER_NODE,
217 	SUPERUSER
218 } t_user_type;
219 
220 typedef enum
221 {
222 	JOIN_SUCCESS,
223 	JOIN_FAIL_NO_PING,
224 	JOIN_FAIL_NO_REPLICATION
225 } standy_join_status;
226 
227 typedef enum
228 {
229 	REMOTE_ERROR_UNKNOWN = -1,
230 	REMOTE_ERROR_NONE,
231 	REMOTE_ERROR_DB_CONNECTION,
232 	REMOTE_ERROR_CONNINFO_PARSE
233 } t_remote_error_type;
234 
235 typedef struct ColHeader
236 {
237 	char		title[MAXLEN];
238 	int			max_length;
239 	int			cur_length;
240 	bool		display;
241 } ColHeader;
242 
243 
244 
245 /* globally available configuration structures */
246 extern t_runtime_options runtime_options;
247 extern t_conninfo_param_list source_conninfo;
248 extern t_node_info target_node_info;
249 
250 /* global variables */
251 extern bool config_file_required;
252 extern char pg_bindir[MAXLEN];
253 
254 /* global functions */
255 extern int	check_server_version(PGconn *conn, char *server_type, bool exit_on_error, char *server_version_string);
256 extern bool create_repmgr_extension(PGconn *conn);
257 extern int	test_ssh_connection(char *host, char *remote_user);
258 
259 extern standy_clone_mode get_standby_clone_mode(void);
260 
261 extern int copy_remote_files(char *host, char *remote_user, char *remote_path,
262 				  char *local_path, bool is_directory, int server_version_num);
263 
264 extern void print_error_list(ItemList *error_list, int log_level);
265 
266 extern void make_pg_path(PQExpBufferData *buf, const char *file);
267 
268 extern void get_superuser_connection(PGconn **conn, PGconn **superuser_conn, PGconn **privileged_conn);
269 
270 extern void make_remote_repmgr_path(PQExpBufferData *outputbuf, t_node_info *remote_node_record);
271 extern void make_repmgrd_path(PQExpBufferData *output_buf);
272 
273 /* display functions */
274 extern bool format_node_status(t_node_info *node_info, PQExpBufferData *node_status, PQExpBufferData *upstream, ItemList *warnings);
275 extern void print_help_header(void);
276 extern void print_status_header(int cols, ColHeader *headers);
277 
278 /* server control functions */
279 extern void get_server_action(t_server_action action, char *script, char *data_dir);
280 extern bool data_dir_required_for_action(t_server_action action);
281 extern void get_node_config_directory(char *config_dir_buf);
282 extern void get_node_data_directory(char *data_dir_buf);
283 extern void init_node_record(t_node_info *node_record);
284 extern bool can_use_pg_rewind(PGconn *conn, const char *data_directory, PQExpBufferData *reason);
285 extern void make_standby_signal_path(char *buf);
286 extern bool write_standby_signal(void);
287 
288 extern bool create_replication_slot(PGconn *conn, char *slot_name, t_node_info *upstream_node_record, PQExpBufferData *error_msg);
289 extern bool drop_replication_slot_if_exists(PGconn *conn, int node_id, char *slot_name);
290 
291 extern standy_join_status check_standby_join(PGconn *primary_conn, t_node_info *primary_node_record, t_node_info *standby_node_record);
292 extern bool check_replication_slots_available(int node_id, PGconn* conn);
293 extern bool check_node_can_attach(TimeLineID local_tli, XLogRecPtr local_xlogpos, PGconn *follow_target_conn, t_node_info *follow_target_node_record, bool is_rejoin);
294 extern bool check_replication_config_owner(int pg_version, const char *data_directory, PQExpBufferData *error_msg, PQExpBufferData *detail_msg);
295 
296 extern void check_shared_library(PGconn *conn);
297 extern bool is_repmgrd_running(PGconn *conn);
298 extern int parse_repmgr_version(const char *version_string);
299 
300 #endif							/* _REPMGR_CLIENT_GLOBAL_H_ */
301