Skip to content

Support for PostgresApp on MacOS #178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ Available states
------------

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

``postgres.client``
-------------------

Installs the PostgreSQL client binaries and libraries.
Installs the PostgreSQL client binaries and libraries on Linux.

``postgres.manage``
-------------------
Expand All @@ -33,18 +33,18 @@ See ``pillar.example`` file for details.
``postgres.python``
-------------------

Installs the PostgreSQL adapter for Python.
Installs the PostgreSQL adapter for Python on Linux.

``postgres.server``
-------------------

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

``postgres.server.image``
-------------------------

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

.. code:: yaml
Expand Down Expand Up @@ -77,12 +77,14 @@ The state relies on the ``postgres:use_upstream_repo`` Pillar value which could

* ``True`` (default): adds the upstream repository to install packages from
* ``False``: makes sure that the repository configuration is absent
* ``postgresapp`` (MacOS) uses upstream PostgresApp package repository.

The ``postgres:version`` Pillar controls which version of the PostgreSQL packages should be
installed from the upstream repository. Defaults to ``9.5``.
installed from the upstream Linux repository. Defaults to ``9.5``.

Testing
=======
The postgres state was tested on MacOS (El Capitan 10.11.6)

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

Expand Down
11 changes: 10 additions & 1 deletion pillar.example
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
postgres:

# UPSTREAM REPO
# Set True to configure upstream postgresql.org repository for YUM/APT/ZYPP
use_upstream_repo: False
# Version to install from upstream repository (if upstream_repo: True)
version: '9.6'

### MACOS
# Set to 'postgresapp' to install that upstream MacOS package
#use_upstream_repo: 'postgresapp'

# PACKAGE
# These pillars are typically never required.
# pkg: 'postgresql'
Expand All @@ -14,11 +19,15 @@ postgres:
- postgresql-contrib
- postgresql-plpython


#'Alternatives system' priority incremental. 0 disables feature.
linux:
altpriority: 30

# macos limits
limits:
soft: 64000
hard: 64000

# POSTGRES
# Append the lines under this item to your postgresql.conf file.
# Pay attention to indent exactly with 4 spaces for all lines.
Expand Down
17 changes: 17 additions & 0 deletions postgres/defaults.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ postgres:
pkg_dev: postgresql-devel
pkg_libpq_dev: postgresql-libs
python: python-psycopg2
userhomes: /home
systemuser:
user: postgres
group: postgres

Expand All @@ -21,6 +23,21 @@ postgres:
conf_dir: /var/lib/pgsql/data
postgresconf: ""

macos:
archive: postgres.dmg
tmpdir: /tmp/postgrestmp
postgresapp:
#See: https://github.com/PostgresApp/PostgresApp/releases/
url: https://github.com/PostgresApp/PostgresApp/releases/download/v2.1.1/Postgres-2.1.1.dmg
sum: sha256=ac0656b522a58fd337931313f09509c09610c4a6078fe0b8e469e69af1e1750b
homebrew:
url:
sum:
dl:
opts: -s -L
interval: 60
retries: 2

pg_hba.conf: salt://postgres/templates/pg_hba.conf.j2
acls:
# "local" is for Unix domain socket connections only
Expand Down
54 changes: 51 additions & 3 deletions postgres/dev.sls
Original file line number Diff line number Diff line change
@@ -1,13 +1,61 @@
{% from "postgres/map.jinja" import postgres with context %}

{% if postgres.pkg_dev %}
{% if grains.os not in ('Windows', 'MacOS',) %}

{% if postgres.pkg_dev %}
install-postgres-dev-package:
pkg.installed:
- name: {{ postgres.pkg_dev }}
{% endif %}
{% endif %}

{% if postgres.pkg_libpq_dev %}
{% if postgres.pkg_libpq_dev %}
install-postgres-libpq-dev:
pkg.installed:
- name: {{ postgres.pkg_libpq_dev }}
{% endif %}

{% endif %}


{% if grains.os == 'MacOS' %}

# Darwin maxfiles limits
{% if postgres.limits.soft or postgres.limits.hard %}

postgres_maxfiles_limits_conf:
file.managed:
- name: /Library/LaunchDaemons/limit.maxfiles.plist
- source: salt://postgres/templates/limit.maxfiles.plist
- context:
soft_limit: {{ postgres.limits.soft or postgres.limits.hard }}
hard_limit: {{ postgres.limits.hard or postgres.limits.soft }}
- group: wheel
{% endif %}

# MacOS Shortcut for system user
{% if postgres.systemuser|lower not in (None, '',) %}

postgres-desktop-shortcut-clean:
file.absent:
- name: '{{ postgres.userhomes }}/{{ postgres.systemuser }}/Desktop/postgres'
- require_in:
- file: postgres-desktop-shortcut-add

{% endif %}

postgres-desktop-shortcut-add:
file.managed:
- name: /tmp/mac_shortcut.sh
- source: salt://postgres/templates/mac_shortcut.sh
- mode: 755
- template: jinja
- context:
user: {{ postgres.systemuser }}
homes: {{ postgres.userhomes }}
cmd.run:
- name: /tmp/mac_shortcut.sh {{ postgres.use_upstream_repo }}
- runas: {{ postgres.systemuser }}
- require:
- file: postgres-desktop-shortcut-add

{% endif %}
5 changes: 5 additions & 0 deletions postgres/init.sls
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@

include:
{% if grains.os == 'MacOS' %}
- postgres.macos
{% else %}
- postgres.server
- postgres.client
- postgres.manage
{% endif %}
10 changes: 10 additions & 0 deletions postgres/macos/init.sls
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% from "postgres/map.jinja" import postgres with context %}

include:
{% if postgres.use_upstream_repo == 'postgresapp' %}
- postgres.macos.postgresapp
{% else %}
- postgres.server
- postgres.client
{% endif %}
- postgres.dev
67 changes: 67 additions & 0 deletions postgres/macos/postgresapp.sls
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{% from "postgres/map.jinja" import postgres as pg with context %}

# Cleanup first
pg-remove-prev-archive:
file.absent:
- name: '{{ pg.macos.tmpdir }}/{{ pg.macos.archive }}'
- require_in:
- pg-extract-dirs

pg-extract-dirs:
file.directory:
- names:
- '{{ pg.macos.tmpdir }}'
- makedirs: True
- clean: True
- require_in:
- pg-download-archive

pg-download-archive:
pkg.installed:
- name: curl
cmd.run:
- name: curl {{ pg.macos.dl.opts }} -o '{{ pg.macos.tmpdir }}/{{ pg.macos.archive }}' {{ pg.macos.postgresapp.url }}
{% if grains['saltversioninfo'] >= [2017, 7, 0] %}
- retry:
attempts: {{ pg.macos.dl.retries }}
interval: {{ pg.macos.dl.interval }}
{% endif %}

{%- if pg.macos.postgresapp.sum %}
#Check hashstring for archive downloads
{%- if grains['saltversioninfo'] <= [2016, 11, 6] %}
pg-check-archive-hash:
module.run:
- name: file.check_hash
- path: '{{ pg.macos.tmpdir }}/{{ pg.macos.archive }}'
- file_hash: {{ pg.macos.postgresapp.sum }}
- onchanges:
- cmd: pg-download-archive
- require_in:
- archive: pg-package-install
{%- endif %}
{%- endif %}

pg-package-install:
macpackage.installed:
- name: '{{ pg.macos.tmpdir }}/{{ pg.macos.archive }}'
- store: True
- dmg: True
- app: True
- force: True
- allow_untrusted: True
- onchanges:
- cmd: pg-download-archive
- require_in:
- file: pg-package-install
- file: pg-remove-archive
file.append:
- name: {{ pg.userhomes }}/{{ pg.systemuser }}/.bash_profile
- text: 'export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/latest/bin'

pg-remove-archive:
file.absent:
- name: '{{ pg.macos.tmpdir }}'
- onchanges:
- macpackage: pg-package-install

22 changes: 14 additions & 8 deletions postgres/osmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,23 @@ Suse:
{% endif %}

MacOS:
service: postgresql
{# todo: homebrew postgresql #}
{% if repo.use_upstream_repo == 'homebrew' %}
service: homebrew.mxcl.postgresql
{% elif repo.use_upstream_repo == 'postgresapp' %}
service: com.postgresapp.Postgres2
{% endif %}
pkg: postgresql
pkg_client:
pkg_libpq_dev:
conf_dir: /usr/local/var/postgres
user: _postgres
group: _postgres
userhomes: /Users
user: {{ repo.user }}
group: {{ repo.group }}
conf_dir: /Users/{{ repo.user }}/Library/AppSupport/postgres_{{ repo.use_upstream_repo }}
prepare_cluster:
command: initdb -D /usr/local/var/postgres/
test: test -f /usr/local/var/postgres/PG_VERSION
user: _postgres
group: _postgres
command: initdb -D /Users/{{ repo.user }}/Library/AppSupport/postgres_{{ repo.use_upstream_repo }}
test: test -f /Users/{{ repo.user }}/Library/AppSupport/postgres_{{ repo.use_upstream_repo }}/PG_VERSION
user: {{ repo.user }}
group: {{ repo.group }}

# vim: ft=sls
8 changes: 8 additions & 0 deletions postgres/repo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,12 @@ use_upstream_repo: {{ salt['pillar.get']('postgres:use_upstream_repo',
version: {{ salt['pillar.get']('postgres:version',
defaults.postgres.version) }}

#Early lookup for system user on MacOS
{% if grains.os == 'MacOS' %}
{% set sysuser = salt['pillar.get']('postgres.user') or salt['cmd.run']("stat -f '%Su' /dev/console") %}
{% set sysgroup = salt['pillar.get']('postgres.group') or salt['cmd.run']("stat -f '%Sg' /dev/console") %}
user: {{ sysuser }}
group: {{ sysgroup }}
{% endif %}

# vim: ft=sls
21 changes: 21 additions & 0 deletions postgres/templates/limit.maxfiles.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>limit.maxfiles</string>
<key>ProgramArguments</key>
<array>
<string>launchctl</string>
<string>limit</string>
<string>maxfiles</string>
<string>{{ soft_limit }}</string>
<string>{{ hard_limit }}</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>ServiceIPC</key>
<false/>
</dict>
</plist>
8 changes: 8 additions & 0 deletions postgres/templates/mac_shortcut.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash

vendor=$1
app="postgres.app"
Source="/Applications/$app"
Destination="{{ homes }}/{{ user }}/Desktop"
/usr/bin/osascript -e "tell application \"Finder\" to make alias file to POSIX file \"$Source\" at POSIX file \"$Destination\""

4 changes: 2 additions & 2 deletions postgres/upstream.sls
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
{%- if 'pkg_repo' in postgres -%}

{%- if postgres.use_upstream_repo -%}
# Add upstream repository for your distro

# Add upstream repository for your distro
postgresql-repo:
pkgrepo.managed:
{{- format_kwargs(postgres.pkg_repo) }}

{%- else -%}

# Remove the repo configuration (and GnuPG key) as requested
# Remove the repo configuration (and GnuPG key) as requested
postgresql-repo:
pkgrepo.absent:
- name: {{ postgres.pkg_repo.name }}
Expand Down