Skip to content

Commit db9bf8d

Browse files
committed
fix(pgsql): fix pgsql import - pgsql fixes, table detection, defaults
1 parent 5c81ca1 commit db9bf8d

File tree

3 files changed

+16
-25
lines changed

3 files changed

+16
-25
lines changed

zabbix/defaults.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,4 @@ zabbix:
7777
dbname: zabbix
7878
dbuser: zabbix
7979
dbpassword: zabbix
80+
sql_file: /usr/share/doc/zabbix-server-pgsql/create.sql.gz

zabbix/osfamilymap.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ Suse:
167167
pgsql:
168168
pkgs:
169169
- postgresql
170+
- gzip
170171
sql_file: /usr/share/doc/packages/zabbix-server-pgsql/create.sql.gz
171172

172173

zabbix/pgsql/schema.sls

+14-25
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,23 @@
88
{% set dbuser = settings.get('dbuser', defaults.dbuser) -%}
99
{% set dbpassword = settings.get('dbpassword', defaults.dbpassword) -%}
1010
11-
{% set dbroot_user = settings.get('dbroot_user') -%}
12-
{% set dbroot_pass = settings.get('dbroot_pass') -%}
13-
14-
{% set sql_file = settings.get('sql_file', '/usr/share/doc/zabbix-server-pgsql/create.sql.gz') -%}
15-
16-
# Connection args required only if dbroot_user and dbroot_pass defined.
17-
{% set connection_args = {} -%}
18-
{% if dbroot_user and dbroot_pass -%}
19-
{% set connection_args = {'runas': 'nobody', 'host': dbhost, 'user': dbroot_user, 'password': dbroot_pass} -%}
20-
{% endif -%}
21-
22-
# Check is there any tables in database.
23-
# salt.postgres.psql_query return empty result if there is no tables or 'False' on any error i.e. failed auth.
24-
{% set list_tables = "SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname != 'pg_catalog' AND schemaname != 'information_schema' LIMIT 1;" %}
25-
{% set is_db_empty = True -%}
26-
{% if salt.postgres.psql_query(query=list_tables, maintenance_db=dbname, **connection_args) -%}
27-
{% set is_db_empty = False -%}
28-
{% endif -%}
11+
{% set sql_file = settings.get('sql_file', defaults.sql_file) -%}
2912
3013
include:
3114
- zabbix.pgsql.conf
3215
16+
# Check is there any tables in database.
17+
# returns changed if there are zero tables in the db
3318
check_db_pgsql:
34-
test.configurable_test_state:
35-
- name: Is there any tables in '{{ dbname }}' database?
36-
- changes: {{ is_db_empty }}
37-
- result: True
38-
- comment: If changes is 'True' data import required.
19+
cmd.run:
20+
- name: "[[ $(psql -X -A -t -c \"SELECT count(tablename) FROM pg_catalog.pg_tables WHERE schemaname != 'pg_catalog' AND schemaname != 'information_schema';\" || echo \"-1\") -eq \"0\" ]] && echo \"changed=yes comment='DB needs schema import.'\" || echo \"changed=no comment='No DB import needed or possible.'\""
21+
- runas: {{ zabbix.user }}
22+
- stateful: True
23+
- env:
24+
- PGUSER: {{ dbuser }}
25+
- PGPASSWORD: {{ dbpassword }}
26+
- PGDATABASE: {{ dbname }}
27+
- PGHOST: {{ dbhost }}
3928
4029
{% if 'sql_file' in settings -%}
4130
upload_sql_dump:
@@ -51,7 +40,7 @@ upload_sql_dump:
5140
5241
import_sql:
5342
cmd.run:
54-
- name: zcat {{ sql_file }} | psql | head -5
43+
- name: zcat {{ sql_file }} | psql | { head -5; cat >/dev/null; }
5544
- runas: {{ zabbix.user }}
5645
- env:
5746
- PGUSER: {{ dbuser }}
@@ -61,4 +50,4 @@ import_sql:
6150
- require:
6251
- pkg: zabbix-server
6352
- onchanges:
64-
- test: check_db_pgsql
53+
- cmd: check_db_pgsql

0 commit comments

Comments
 (0)