[cubicweb/postgres] several refactorings of the cubicweb.postgres state
authorDavid Douard <david.douard@logilab.fr>
Wed, 22 Jul 2015 14:50:06 +0200
changeset 54 9d76def7890a
parent 53 fe7208b31d56
child 55 13148a44982f
[cubicweb/postgres] several refactorings of the cubicweb.postgres state - add a map to handle default values for postgresql, - add support for locale configuration in the pillar data, - put postgresql configuration options in a 'postgresql' dictionnary (instead of several pg_xxx vars) - fix postgres version comparison - ensure /etc/postgresql/{{ postgresql.version }}/main/conf.d/
cubicweb/postgres/init.sls
cubicweb/postgres/map.jinja
pillar.example
--- a/cubicweb/postgres/init.sls	Tue Jul 21 12:16:13 2015 +0200
+++ b/cubicweb/postgres/init.sls	Wed Jul 22 14:50:06 2015 +0200
@@ -1,21 +1,30 @@
-{% set psql_version=pillar.get('postgresql_version', 9.1) %}
-
-# TODO: allow to declare locales in pillar
+{% from "cubicweb/postgres/map.jinja" import postgresql with context %}
 
 locales:
   pkg.installed
 
-fr_FR.UTF-8:
+{% for locale in pillar.get('locales', {}).get('present', []) %}
+locale-{{locale}}-present:
   locale.present
-en_US.UTF-8:
-  locale.system
+{% endfor %}
+
+{% set systemlocale=pillar.get('locales', {}).get('system', 'en_US.UTF-8') %}
 
+locale-system-present:
+  locale.present:
+    - name: {{systemlocale}}
+
+locale-system:
+  locale.system:
+    - name: {{systemlocale}}
+
+# XXX not compatible with centos
 pg-server:
   pkg.installed:
     - pkgs:
-      - postgresql-{{ psql_version }}
-      - postgresql-contrib-{{ psql_version }}
-      - postgresql-plpython-{{ psql_version }}
+      - postgresql-{{ postgresql.version }}
+      - postgresql-contrib-{{ postgresql.version }}
+      - postgresql-plpython-{{ postgresql.version }}
   {% if grains['oscodename'] == 'wheezy' %}
   file.managed:
     - name: /etc/init.d/postgresql
@@ -25,17 +34,17 @@
   {% endif %}
   postgres_cluster.present:
     - name: 'main'
-    - version: {{ psql_version }}
+    - version: {{ postgresql.version }}
     - encoding: UTF8
   service.running:
     - name: postgresql
     - require:
       - postgres_cluster: pg-server
 
-postgresql-plpython-{{ psql_version }}:
+postgresql-plpython-{{ postgresql.version }}:
   pkg.installed
-
-/etc/postgresql/{{ psql_version }}/main/pg_hba.conf:
+  
+/etc/postgresql/{{ postgresql.version }}/main/pg_hba.conf:
   file.managed:
     {% set psql_users=[] %}
     {% for vhostname, vhost in pillar['cubicweb_apps'].iteritems() %}
@@ -45,11 +54,11 @@
     {% endif %}
     {% endfor %}
     {% endfor %}
-    - name: /etc/postgresql/{{ psql_version }}/main/pg_hba.conf
-    - source: salt://cubicweb/postgres/pg_hba.conf.jinja
+    - name: /etc/postgresql/{{ postgresql.version }}/main/pg_hba.conf
     - user: postgres
     - group: postgres
     - mode: 644
+    - source: salt://cubicweb/postgres/pg_hba.conf.jinja
     - template: jinja
     - context:
         users: {{ psql_users }}
@@ -58,22 +67,26 @@
     - watch_in:
         service: pg-server
 
-{% if pillar.get('pg_listen', False) %}
-{% if psql_version >= '9.4' %}
-/etc/postgresql/{{ psql_version }}/main/postgresql.conf:
+
+{% if postgresql.listen_tcp %}
+{% if salt['pkg.version_cmp'](postgresql.version, '9.4') >= 0 %}
+/etc/postgresql/{{ postgresql.version }}/main/postgresql.conf:
   file.append:
     - text: 
       - "# appended by salt"
       - "include_dir 'conf.d'"
 
-/etc/postgresql/{{ psql_version }}/main/conf.d/listen.conf:
+/etc/postgresql/{{ postgresql.version }}/main/conf.d/:
+  file.directory
+
+/etc/postgresql/{{ postgresql.version }}/main/conf.d/listen.conf:
   file.managed:
     - contents: |
        listen_addresses = '*'
     - watch_in:
        service: pg-server
 {% else %}
-/etc/postgresql/{{ psql_version }}/main/postgresql.conf:
+/etc/postgresql/{{ postgresql.version }}/main/postgresql.conf:
   file.append:
     - text:
        - listen_addresses = '*'
@@ -107,7 +120,8 @@
     - require:
       - postgres_user: pg-{{vhostname}}-{{name}}
       - service: pg-server
-      - locale: fr_FR.UTF-8
+        # locale-system-present: poor man safety belt...
+      - locale: locale-system-present
   postgres_extension.present:
     - name: plpythonu
   postgres_user:
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cubicweb/postgres/map.jinja	Wed Jul 22 14:50:06 2015 +0200
@@ -0,0 +1,12 @@
+{% set postgresql = salt['grains.filter_by']({
+'jessie': {
+  'version': '9.4',
+  'listen_tcp': False,
+  'pg_hba': "",
+ },
+ 'wheezy': {
+   'version': '9.1',
+   'listen_tcp': False,
+   'pg_hba': "",
+   },
+}, grain='oscodename', merge=salt['pillar.get']('postgresql')) %}
--- a/pillar.example	Tue Jul 21 12:16:13 2015 +0200
+++ b/pillar.example	Wed Jul 22 14:50:06 2015 +0200
@@ -8,12 +8,18 @@
     # insert inline configuration
     # lines here, valid for all vhosts
 
+locales:
+  present:
+    - fr_FR.UTF-8
+    - en_US.UTF-8
+  system: en_US.UTF-8
+    
 postgresql:
-  enable: True # set to True to install postgresql on same host
-#make postresql listen on the network
-#pg_listen: False
-#Insert pg_hdba lines 
-#custom_pg_hba_lines: | 
+#  version: 9.4
+#set true to make postresql listen on the network
+#  listen_tcp: False
+#pg_hdba lines to insert 
+#   pg_hba_lines: | 
 #    host    all             all             10.1.1.3/32            md5
 
 cubicweb_repo: production # set to acceptance if you want more recent versions of cubicweb 
@@ -22,15 +28,10 @@
 ## 'cubicweb_repo' option is ignored
 #cubicweb_version: 3.20.5-1
 
-## you may specify a precise postgresql version (package version). If set, the
-## 'postgresql_version' option is ignored
-# postgresql_version: 9.4
-
 ## You can add extra repository sources
 ## when extra_sources is defined and debian_pkgrepos is undefined the "all dependencies are 
 ## availble in that extra source" scenario is used, no public cubicweb debian repository 
 ## will be configured. Changes pending from https://www.cubicweb.org/ticket/5714468
-
 #extra_sources:
 #  - deb https://user:password@domain.com/path/dists/ wheezy/
 ## you can specify your own repository sources from .sls definitions instead of logilab default