Skip to content

Commit 8031f74

Browse files
Merge pull request #191 from noelmcloughlin/postgresApp
PostgresApp and Homebrew Postgres on MacOS
2 parents 36fb8f4 + a34a4c2 commit 8031f74

15 files changed

+253
-31
lines changed

README.rst

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ Available states
1717
------------
1818

1919
Installs and configures both PostgreSQL server and client with creation of various DB objects in
20-
the cluster.
20+
the cluster. This state applies to both Linux and MacOS.
2121

2222
``postgres.client``
2323
-------------------
2424

25-
Installs the PostgreSQL client binaries and libraries.
25+
Installs the PostgreSQL client binaries and libraries on Linux.
2626

2727
``postgres.manage``
2828
-------------------
@@ -33,18 +33,18 @@ See ``pillar.example`` file for details.
3333
``postgres.python``
3434
-------------------
3535

36-
Installs the PostgreSQL adapter for Python.
36+
Installs the PostgreSQL adapter for Python on Linux.
3737

3838
``postgres.server``
3939
-------------------
4040

41-
Installs the PostgreSQL server package, prepares the DB cluster and starts the server using
41+
Installs the PostgreSQL server package on Linux, prepares the DB cluster and starts the server using
4242
packaged init script, job or unit.
4343

4444
``postgres.server.image``
4545
-------------------------
4646

47-
Installs the PostgreSQL server package, prepares the DB cluster and starts the server by issuing
47+
Installs the PostgreSQL server package on Linux, prepares the DB cluster and starts the server by issuing
4848
raw ``pg_ctl`` command. The ``postgres:bake_image`` Pillar toggles this behaviour. For example:
4949

5050
.. code:: yaml
@@ -77,12 +77,15 @@ The state relies on the ``postgres:use_upstream_repo`` Pillar value which could
7777

7878
* ``True`` (default): adds the upstream repository to install packages from
7979
* ``False``: makes sure that the repository configuration is absent
80+
* ``'postgresapp'`` (MacOS) uses upstream PostgresApp package repository.
81+
* ``'homebrew'`` (MacOS) uses Homebrew postgres
8082

8183
The ``postgres:version`` Pillar controls which version of the PostgreSQL packages should be
82-
installed from the upstream repository. Defaults to ``9.5``.
84+
installed from the upstream Linux repository. Defaults to ``9.5``.
8385

8486
Testing
8587
=======
88+
The postgres state was tested on MacOS (El Capitan 10.11.6)
8689

8790
Testing is done with the ``kitchen-salt``.
8891

pillar.example

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ postgres:
55
# Version to install from upstream repository (if upstream_repo: True)
66
version: '9.6'
77

8+
### MACOS
9+
# Set to 'postgresapp' OR 'homebrew' for MacOS
10+
#use_upstream_repo: 'postgresapp'
11+
#use_upstream_repo: 'homebrew'
12+
813
# PACKAGE
914
# These pillars are typically never required.
1015
# pkg: 'postgresql'
@@ -14,11 +19,15 @@ postgres:
1419
- postgresql-contrib
1520
- postgresql-plpython
1621

17-
1822
#'Alternatives system' priority incremental. 0 disables feature.
1923
linux:
2024
altpriority: 30
2125

26+
# macos limits
27+
limits:
28+
soft: 64000
29+
hard: 64000
30+
2231
# POSTGRES
2332
# Append the lines under this item to your postgresql.conf file.
2433
# Pay attention to indent exactly with 4 spaces for all lines.

postgres/client.sls

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
{%- endif %}
88
{%- endfor %}
99
10-
{%- if postgres.use_upstream_repo %}
10+
{%- if postgres.use_upstream_repo == true %}
1111
include:
1212
- postgres.upstream
1313
{%- endif %}
@@ -16,7 +16,7 @@ include:
1616
postgresql-client-libs:
1717
pkg.installed:
1818
- pkgs: {{ pkgs }}
19-
{%- if postgres.use_upstream_repo %}
19+
{%- if postgres.use_upstream_repo == true %}
2020
- refresh: True
2121
- require:
2222
- pkgrepo: postgresql-repo

postgres/codenamemap.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#}
1717

1818
{# use upstream version if configured #}
19-
{% if repo.use_upstream_repo %}
19+
{% if repo.use_upstream_repo == true %}
2020
{% set version = repo.version %}
2121
{% endif %}
2222

@@ -49,7 +49,7 @@
4949
#}
5050

5151
{# use upstream version if configured #}
52-
{% if repo.use_upstream_repo %}
52+
{% if repo.use_upstream_repo == true %}
5353
{% set version = repo.version %}
5454
{% endif %}
5555

postgres/defaults.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ postgres:
99
pkg_dev: postgresql-devel
1010
pkg_libpq_dev: postgresql-libs
1111
python: python-psycopg2
12+
userhomes: /home
13+
systemuser:
1214
user: postgres
1315
group: postgres
1416

@@ -21,6 +23,21 @@ postgres:
2123
conf_dir: /var/lib/pgsql/data
2224
postgresconf: ""
2325

26+
macos:
27+
archive: postgres.dmg
28+
tmpdir: /tmp/postgrestmp
29+
postgresapp:
30+
#See: https://github.com/PostgresApp/PostgresApp/releases/
31+
url: https://github.com/PostgresApp/PostgresApp/releases/download/v2.1.1/Postgres-2.1.1.dmg
32+
sum: sha256=ac0656b522a58fd337931313f09509c09610c4a6078fe0b8e469e69af1e1750b
33+
homebrew:
34+
url:
35+
sum:
36+
dl:
37+
opts: -s -L
38+
interval: 60
39+
retries: 2
40+
2441
pg_hba.conf: salt://postgres/templates/pg_hba.conf.j2
2542
acls:
2643
# "local" is for Unix domain socket connections only

postgres/dev.sls

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,59 @@
11
{% from "postgres/map.jinja" import postgres with context %}
22
3-
{% if postgres.pkg_dev %}
3+
{% if grains.os not in ('Windows', 'MacOS',) %}
4+
5+
{% if postgres.pkg_dev %}
46
install-postgres-dev-package:
57
pkg.installed:
68
- name: {{ postgres.pkg_dev }}
7-
{% endif %}
9+
{% endif %}
810
9-
{% if postgres.pkg_libpq_dev %}
11+
{% if postgres.pkg_libpq_dev %}
1012
install-postgres-libpq-dev:
1113
pkg.installed:
1214
- name: {{ postgres.pkg_libpq_dev }}
15+
{% endif %}
16+
17+
{% endif %}
18+
19+
20+
{% if grains.os == 'MacOS' %}
21+
22+
# Darwin maxfiles limits
23+
{% if postgres.limits.soft or postgres.limits.hard %}
24+
25+
postgres_maxfiles_limits_conf:
26+
file.managed:
27+
- name: /Library/LaunchDaemons/limit.maxfiles.plist
28+
- source: salt://postgres/templates/limit.maxfiles.plist
29+
- context:
30+
soft_limit: {{ postgres.limits.soft or postgres.limits.hard }}
31+
hard_limit: {{ postgres.limits.hard or postgres.limits.soft }}
32+
- group: {{ postgres.group }}
33+
{% endif %}
34+
35+
{% if postgres.use_upstream_repo == 'postgresapp' %}
36+
# Shortcut for PostgresApp
37+
postgres-desktop-shortcut-clean:
38+
file.absent:
39+
- name: '{{ postgres.userhomes }}/{{ postgres.user }}/Desktop/Postgres ({{ postgres.use_upstream_repo }})'
40+
- require_in:
41+
- file: postgres-desktop-shortcut-add
42+
43+
postgres-desktop-shortcut-add:
44+
file.managed:
45+
- name: /tmp/mac_shortcut.sh
46+
- source: salt://postgres/templates/mac_shortcut.sh
47+
- mode: 755
48+
- template: jinja
49+
- context:
50+
user: {{ postgres.user }}
51+
homes: {{ postgres.userhomes }}
52+
cmd.run:
53+
- name: '/tmp/mac_shortcut.sh "Postgres ({{ postgres.use_upstream_repo }})"'
54+
- runas: {{ postgres.user }}
55+
- require:
56+
- file: postgres-desktop-shortcut-add
57+
{% endif %}
58+
1359
{% endif %}

postgres/init.sls

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1+
12
include:
3+
{% if grains.os == 'MacOS' %}
4+
- postgres.macos
5+
{% else %}
26
- postgres.server
37
- postgres.client
48
- postgres.manage
9+
{% endif %}

postgres/macos/init.sls

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{% from "postgres/map.jinja" import postgres with context %}
2+
3+
include:
4+
{% if postgres.use_upstream_repo == 'postgresapp' %}
5+
- postgres.macos.postgresapp
6+
{% elif postgres.use_upstream_repo == 'homebrew' %}
7+
- postgres.server
8+
- postgres.client
9+
{% endif %}
10+
- postgres.dev

postgres/macos/postgresapp.sls

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
{% from "postgres/map.jinja" import postgres as pg with context %}
2+
3+
# Cleanup first
4+
pg-remove-prev-archive:
5+
file.absent:
6+
- name: '{{ pg.macos.tmpdir }}/{{ pg.macos.archive }}'
7+
- require_in:
8+
- pg-extract-dirs
9+
10+
pg-extract-dirs:
11+
file.directory:
12+
- names:
13+
- '{{ pg.macos.tmpdir }}'
14+
- makedirs: True
15+
- clean: True
16+
- require_in:
17+
- pg-download-archive
18+
19+
pg-download-archive:
20+
pkg.installed:
21+
- name: curl
22+
cmd.run:
23+
- name: curl {{ pg.macos.dl.opts }} -o '{{ pg.macos.tmpdir }}/{{ pg.macos.archive }}' {{ pg.macos.postgresapp.url }}
24+
{% if grains['saltversioninfo'] >= [2017, 7, 0] %}
25+
- retry:
26+
attempts: {{ pg.macos.dl.retries }}
27+
interval: {{ pg.macos.dl.interval }}
28+
{% endif %}
29+
30+
{%- if pg.macos.postgresapp.sum %}
31+
pg-check-archive-hash:
32+
module.run:
33+
- name: file.check_hash
34+
- path: '{{ pg.macos.tmpdir }}/{{ pg.macos.archive }}'
35+
- file_hash: {{ pg.macos.postgresapp.sum }}
36+
- onchanges:
37+
- cmd: pg-download-archive
38+
- require_in:
39+
- archive: pg-package-install
40+
{%- endif %}
41+
42+
pg-package-install:
43+
macpackage.installed:
44+
- name: '{{ pg.macos.tmpdir }}/{{ pg.macos.archive }}'
45+
- store: True
46+
- dmg: True
47+
- app: True
48+
- force: True
49+
- allow_untrusted: True
50+
- onchanges:
51+
- cmd: pg-download-archive
52+
- require_in:
53+
- file: pg-package-install
54+
- file: pg-remove-archive
55+
file.append:
56+
- name: {{ pg.userhomes }}/{{ pg.user }}/.bash_profile
57+
- text: 'export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/latest/bin'
58+
59+
pg-remove-archive:
60+
file.absent:
61+
- name: '{{ pg.macos.tmpdir }}'
62+
- onchanges:
63+
- macpackage: pg-package-install
64+

postgres/osmap.yaml

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ RedHat:
3333
gpgcheck: 1
3434
gpgkey: 'https://download.postgresql.org/pub/repos/yum/RPM-GPG-KEY-PGDG-{{ release }}'
3535

36-
{% if repo.use_upstream_repo %}
36+
{% if repo.use_upstream_repo == true %}
3737

3838
{% set data_dir = '/var/lib/pgsql/' ~ repo.version ~ '/data' %}
3939

@@ -104,7 +104,7 @@ Suse:
104104
gpgcheck: 1
105105
gpgautoimport: True
106106

107-
{% if repo.use_upstream_repo %}
107+
{% if repo.use_upstream_repo == true %}
108108
{# Pillars needed are 'use_upstream_repo: True' and 'version: n.n'. #}
109109
{# Avoid setting package names as pillars, as may corrupt postgres. #}
110110
{% set lib_dir = '/var/lib/pgsql/' ~ repo.version ~ '/data' %}
@@ -133,18 +133,26 @@ Suse:
133133

134134
{% endif %}
135135

136+
{%- if grains.os == 'MacOS' %}
137+
## jinja check avoids rendering noise/failure on Linux
136138
MacOS:
137-
service: postgresql
139+
{%- if repo.use_upstream_repo == 'homebrew' %}
140+
service: homebrew.mxcl.postgresql
141+
{%- elif repo.use_upstream_repo == 'postgresapp' %}
142+
service: com.postgresapp.Postgres2
143+
{%- endif %}
138144
pkg: postgresql
139145
pkg_client:
140146
pkg_libpq_dev:
141-
conf_dir: /usr/local/var/postgres
142-
user: _postgres
143-
group: _postgres
147+
userhomes: /Users
148+
user: {{ repo.user }}
149+
group: {{ repo.group }}
150+
conf_dir: /Users/{{ repo.user }}/Library/AppSupport/postgres_{{ repo.use_upstream_repo }}
144151
prepare_cluster:
145-
command: initdb -D /usr/local/var/postgres/
146-
test: test -f /usr/local/var/postgres/PG_VERSION
147-
user: _postgres
148-
group: _postgres
152+
command: initdb -D /Users/{{ repo.user }}/Library/AppSupport/postgres_{{ repo.use_upstream_repo }}
153+
test: test -f /Users/{{ repo.user }}/Library/AppSupport/postgres_{{ repo.use_upstream_repo }}/PG_VERSION
154+
user: {{ repo.user }}
155+
group: {{ repo.group }}
156+
{%- endif %}
149157

150158
# vim: ft=sls

postgres/repo.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,12 @@ use_upstream_repo: {{ salt['pillar.get']('postgres:use_upstream_repo',
88
version: {{ salt['pillar.get']('postgres:version',
99
defaults.postgres.version) }}
1010

11+
#Early lookup for system user on MacOS
12+
{% if grains.os == 'MacOS' %}
13+
{% set sysuser = salt['pillar.get']('postgres.user') or salt['cmd.run']("stat -f '%Su' /dev/console") %}
14+
{% set sysgroup = salt['pillar.get']('postgres.group') or salt['cmd.run']("stat -f '%Sg' /dev/console") %}
15+
user: {{ sysuser }}
16+
group: {{ sysgroup }}
17+
{% endif %}
18+
1119
# vim: ft=sls

0 commit comments

Comments
 (0)