8
8
{% set dbuser = settings.get(' dbuser' , defaults.dbuser) -% }
9
9
{% set dbpassword = settings.get(' dbpassword' , defaults.dbpassword) -% }
10
10
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) -% }
29
12
30
13
include:
31
14
- zabbix.pgsql.conf
32
15
16
+ # Check is there any tables in database.
17
+ # returns changed if there are zero tables in the db
33
18
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 }}
39
28
40
29
{% if ' sql_file' in settings -% }
41
30
upload_sql_dump:
@@ -51,7 +40,7 @@ upload_sql_dump:
51
40
52
41
import_sql:
53
42
cmd.run:
54
- - name: zcat {{ sql_file }} | psql | head - 5
43
+ - name: zcat {{ sql_file }} | psql | { head - 5 ; cat > / dev / null; }
55
44
- runas: {{ zabbix.user }}
56
45
- env:
57
46
- PGUSER : {{ dbuser }}
@@ -61,4 +50,4 @@ import_sql:
61
50
- require:
62
51
- pkg: zabbix- server
63
52
- onchanges:
64
- - test : check_db_pgsql
53
+ - cmd : check_db_pgsql
0 commit comments