Support for dockerized aptly
diff --git a/aptly/files/aptly b/aptly/files/aptly
new file mode 100644
index 0000000..cc4dabc
--- /dev/null
+++ b/aptly/files/aptly
@@ -0,0 +1,59 @@
+#!/bin/bash
+# =================================================
+# This file is managed by SaltStack aptly formula.
+# Manual changes may be overwritten.
+# =================================================
+#
+# Run aptly in a container
+#
+# This script will attempt to mirror the host paths by using volumes for the
+# following paths:
+#   * {{ aptly_home }}
+#   * {{ aptly_root }}
+#   * /etc/aptly-conf (read only)
+#   * $(pwd) (read-only)
+#   * $HOME if it's set (read-only)
+#
+# You can add additional volumes (or any docker run options) using
+# the $APTLY_OPTIONS environment variable.
+#
+
+
+set -e
+
+IMAGE=${IMAGE:-"{{ image }}"}
+APTLY_HOME=${APTLY_HOME:-"{{ aptly_home }}"}
+APTLY_ROOT=${APTLY_ROOT:-"{{ aptly_root }}"}
+
+id -u aptly && APTLY_ENV="${APTLY_ENV} -e LOCAL_USER_ID=$(id -u aptly)" || true
+
+# Setup volume mounts for compose config and context
+if [ "$(pwd)" != '/' ]; then
+    VOLUMES="-v $(pwd):$(pwd):ro"
+fi
+
+if [ -e /etc/aptly.conf ]; then
+    VOLUMES="-v /etc/aptly.conf:/etc/aptly.conf:ro"
+fi
+
+if [ -d "$APTLY_HOME" ]; then
+    VOLUMES="-v $APTLY_HOME:$APTLY_HOME"
+fi
+
+if [ -d "$APTLY_ROOT" ]; then
+    VOLUMES="-v $APTLY_ROOT:$APTLY_ROOT"
+fi
+
+if [ -n "$HOME" ]; then
+    VOLUMES="$VOLUMES -v $HOME:$HOME:ro"
+fi
+
+# Only allocate tty if we detect one
+if [ -t 1 ]; then
+    DOCKER_RUN_OPTIONS="-t"
+fi
+if [ -t 0 ]; then
+    DOCKER_RUN_OPTIONS="$DOCKER_RUN_OPTIONS -i"
+fi
+
+exec docker run --rm $DOCKER_RUN_OPTIONS $APTLY_OPTIONS $APTLY_ENV $VOLUMES -w "$(pwd)" $IMAGE "$@"
diff --git a/aptly/files/aptly-api.service b/aptly/files/aptly-api.service
index d4476c3..011dd19 100644
--- a/aptly/files/aptly-api.service
+++ b/aptly/files/aptly-api.service
@@ -6,7 +6,7 @@
 [Service]
 User=aptly
 EnvironmentFile=/etc/default/aptly-api
-ExecStart=/usr/bin/aptly api serve -listen=${LISTEN_HOST}:${LISTEN_PORT}
+ExecStart=${COMMAND} api serve -listen=${LISTEN_HOST}:${LISTEN_PORT}
 Restart=on-failure
 
 [Install]
diff --git a/aptly/files/default-aptly-api b/aptly/files/default-aptly-api
index 630e396..a4440af 100644
--- a/aptly/files/default-aptly-api
+++ b/aptly/files/default-aptly-api
@@ -1,3 +1,8 @@
 {%- from "aptly/map.jinja" import server with context %}
+{%- if server.source.engine == 'docker' %}
+COMMAND=/usr/local/bin/aptly
+{%- else %}
+COMMAND=/usr/bin/aptly
+{%- endif %}
 LISTEN_HOST={{ server.api.bind.host }}
 LISTEN_PORT={{ server.api.bind.port }}
diff --git a/aptly/map.jinja b/aptly/map.jinja
index 4e260c7..1894767 100644
--- a/aptly/map.jinja
+++ b/aptly/map.jinja
@@ -1,7 +1,10 @@
 
 {%- set server = salt['grains.filter_by']({
   'Debian': {
-    'pkgs': ['aptly', 'bzip2'],
+    'source': {
+      'engine': 'pkg',
+      'pkgs': ['aptly', 'bzip2'],
+    },
     'home_dir': '/var/lib/aptly',
     'root_dir': '/srv/aptly',
     'secure': True,
diff --git a/aptly/server/api.sls b/aptly/server/api.sls
index 4cf59ab..70e0dcc 100644
--- a/aptly/server/api.sls
+++ b/aptly/server/api.sls
@@ -12,7 +12,7 @@
     - user: root
     - group: root
     - require:
-      - pkg: aptly_packages
+      - cmd: aptly_installed
 {%- else %}
 aptly_api_service_file:
   file.managed:
@@ -23,7 +23,7 @@
     - mode: 755
     - template: jinja
     - require:
-      - pkg: aptly_packages
+      - cmd: aptly_installed
 {%- endif %}
 
 aptly_api_config:
@@ -42,6 +42,6 @@
     - file: aptly_api_service_file
     - file: aptly_api_config
     - file: aptly_conf
-    - pkg: aptly_packages
+    - cmd: aptly_installed
 
 {%- endif %}
diff --git a/aptly/server/init.sls b/aptly/server/init.sls
index 41c8446..2945e0b 100644
--- a/aptly/server/init.sls
+++ b/aptly/server/init.sls
@@ -8,18 +8,53 @@
 - aptly.server.repos
 - aptly.server.mirrors
 
+{%- if server.source.engine == 'pkg' %}
+
 aptly_packages:
   pkg.installed:
   - names: {{ server.pkgs }}
   - refresh: true
 
+aptly_installed:
+  cmd.wait:
+    - name: "aptly version"
+    - watch:
+      - file: aptly_packages
+    - require:
+      - user: aptly_user
+
+{%- elif publisher.source.engine == 'docker' %}
+
+aptly_wrapper:
+  file.managed:
+    - name: /usr/local/bin/aptly
+    - source: salt://docker/files/aptly
+    - template: jinja
+    - defaults:
+        image: {{ client.compose.source.image|default('tcpcloud/aptly') }}
+        aptly_home: {{ server.home_dir }}
+        aptly_root: {{ server.root_dir }}
+    - mode: 755
+
+aptly_installed:
+  cmd.wait:
+    - name: "/usr/local/bin/aptly version"
+    - watch:
+      - file: aptly_wrapper
+    - require:
+      - user: aptly_user
+      - file: aptly_root_dir
+      - file: aptly_home_dir
+
+{%- endif %}
+
 aptly_user:
   user.present:
   - name: aptly
   - shell: /bin/bash
   - home: {{ server.home_dir }}
   - require:
-    - pkg: aptly_packages
+    - cmd: aptly_installed
 
 aptly_home_dir:
   file.directory:
@@ -113,6 +148,8 @@
   - unless: gpg --no-tty --list-secret-keys | grep '{{ server.gpg_keypair_id }}'
   - require:
     - file: aptly_gpg_key_dir
+  - require_in:
+    - cmd: aptly_installed
 
 {%- endif %}