copy etcd from images

when server.image is set then binary is copied from docker image

Change-Id: I59c220deabfdf861613f16cf757ba0da44976a9c
diff --git a/README.rst b/README.rst
index cf05053..9478886 100644
--- a/README.rst
+++ b/README.rst
@@ -1,10 +1,15 @@
 
 ==================================
-ETCD Formula
+etcd Formula
 ==================================
 
 Service etcd description
 
+Possible `source.engine`:
+
+- **pkg** - install etcd package (default)
+- **docker_hybrid** - copy binaries from docker image (specified in `server.image`)
+
 Sample pillars
 ==============
 
@@ -66,7 +71,7 @@
           name: etcd03
           port: 4001
 
-ETCD proxy
+etcd proxy
 -------------
 
 .. code-block:: yaml
@@ -85,7 +90,7 @@
         - host: 10.0.175.103
           name: etcd03
 
-run ETCD on k8s
+Run etcd on k8s
 ---------------
 
 .. code-block:: yaml
@@ -94,10 +99,19 @@
       server:
         engine: kubernetes
 
+Copy etcd binary from container
+---------------
+
+.. code-block:: yaml
+
+    etcd:
+      server:
+        image: quay.io/coreos/etcd:latest
+
 Read more
 =========
 
-* links
+* https://github.com/coreos/etcd
 
 Documentation and Bugs
 ======================
diff --git a/etcd/files/etcd.manifest b/etcd/files/etcd.manifest
index 10305e5..a8dbb17 100644
--- a/etcd/files/etcd.manifest
+++ b/etcd/files/etcd.manifest
@@ -11,7 +11,7 @@
 "containers":[
     {
     "name": "etcd-container",
-    "image": "{{ server.registry }}/etcd:{{ server.version }}",
+    "image": "{{ server.get('image', 'quay.io/coreos/etcd:latest') }}",
     "resources": {
       "requests": {
         "cpu": "200m"      }
@@ -33,7 +33,7 @@
     "ports":[
       { "name": "serverport",
         "containerPort": 2380,
-        "hostPort": 2380 
+        "hostPort": 2380
       },{
        "name": "clientport",
         "containerPort": 4001,
@@ -62,4 +62,4 @@
         "path": "/var/log/etcd.log"}
   }
 ]
-}}
\ No newline at end of file
+}}
diff --git a/etcd/files/systemd/etcd.service b/etcd/files/systemd/etcd.service
new file mode 100644
index 0000000..6414481
--- /dev/null
+++ b/etcd/files/systemd/etcd.service
@@ -0,0 +1,23 @@
+[Unit]
+Description=etcd - highly-available key value store
+Documentation=https://github.com/coreos/etcd
+After=network.target
+Wants=network-online.target
+
+[Service]
+Environment=DAEMON_ARGS=
+Environment=ETCD_NAME=%H
+Environment=ETCD_DATA_DIR=/var/lib/etcd/default
+EnvironmentFile=-/etc/default/%p
+Type=notify
+User=etcd
+PermissionsStartOnly=true
+#ExecStart=/bin/sh -c "GOMAXPROCS=$(nproc) /usr/bin/etcd $DAEMON_ARGS"
+ExecStart=/usr/local/bin/etcd $DAEMON_ARGS
+Restart=on-abnormal
+#RestartSec=10s
+LimitNOFILE=65536
+
+[Install]
+WantedBy=multi-user.target
+Alias=etcd2.service
diff --git a/etcd/server/service.sls b/etcd/server/service.sls
index 618e76b..c067cf1 100644
--- a/etcd/server/service.sls
+++ b/etcd/server/service.sls
@@ -1,14 +1,68 @@
 {%- from "etcd/map.jinja" import server with context %}
 {%- if server.enabled %}
 
+{%- if server.get('source', {}).get('engine', 'pkg') == 'pkg' %}
+
 etcd_packages:
   pkg.installed:
   - names: {{ server.pkgs }}
 {%- if server.get('engine', 'systemd') %}
   - require:
     - file: /etc/default/etcd
+  - watch_in:
+    - service: etcd
 {%- endif %}
 
+
+{% elif server.get('source', {}).get('engine') == 'docker_hybrid' %}
+
+user_etcd:
+  user.present:
+    - name: etcd
+    - shell: /bin/false
+    - home: /var/lib/etcd
+    - gid_from_name: True
+
+/tmp/etcd:
+  file.directory:
+      - user: root
+      - group: root
+
+copy-etcd-binaries:
+  dockerng.running:
+    - image: {{ server.get('image', 'quay.io/coreos/etcd:latest') }}
+    - entrypoint: cp
+    - command: -vr /usr/local/bin/ /tmp/etcd/
+    - binds:
+      - /tmp/etcd/:/tmp/etcd/
+    - force: True
+    - require:
+      - file: /tmp/etcd
+
+{%- for filename in ['etcd', 'etcdctl'] %}
+
+/usr/local/bin/{{ filename }}:
+  file.managed:
+    - source: /tmp/etcd/bin/{{ filename }}
+    - mode: 755
+    - user: root
+    - group: root
+    - require:
+      - dockerng: copy-etcd-binaries
+    - watch_in:
+      - service: etcd
+
+{% endfor %}
+
+/etc/systemd/system/etcd.service:
+  file.managed:
+    - source: salt://etcd/files/systemd/etcd.service
+    - template: jinja
+    - user: root
+    - group: root
+
+{% endif %}
+
 {%- if server.get('engine', 'systemd') == 'kubernetes' %}
 
 etcd_service:
@@ -44,11 +98,12 @@
 {%- else %}
         initial_cluster_state: existing
 {%- endif %}
+    - watch_in:
+      - service: etcd
 
 /var/lib/etcd/:
   file.directory:
     - user: etcd
-    - group: etcd
 
 /var/lib/etcd/configenv:
   file.managed:
@@ -61,8 +116,6 @@
   service.running:
   - enable: True
   - name: {{ server.services }}
-  - watch:
-    - file: /etc/default/etcd
 
 {%- endif %}
 
diff --git a/tests/pillar/cluster.sls b/tests/pillar/cluster.sls
index 730b768..a91825f 100644
--- a/tests/pillar/cluster.sls
+++ b/tests/pillar/cluster.sls
@@ -1,5 +1,6 @@
 etcd:
   server:
+    image: quay.io/coreos/etcd:latest
     enabled: true
     bind:
       host: 10.0.175.101
@@ -13,4 +14,4 @@
       port: 4001
     - host: 10.0.175.103
       name: etcd03
-      port: 4001
\ No newline at end of file
+      port: 4001
diff --git a/tests/pillar/proxy.sls b/tests/pillar/proxy.sls
index b440098..5313ab7 100644
--- a/tests/pillar/proxy.sls
+++ b/tests/pillar/proxy.sls
@@ -1,5 +1,6 @@
 etcd:
   server:
+    image: quay.io/coreos/etcd:latest
     enabled: true
     bind:
       host: 10.0.175.101
@@ -10,4 +11,4 @@
     - host: 10.0.175.102
       name: etcd02
     - host: 10.0.175.103
-      name: etcd03
\ No newline at end of file
+      name: etcd03
diff --git a/tests/pillar/single.sls b/tests/pillar/single.sls
index cf2e30d..2f830cd 100644
--- a/tests/pillar/single.sls
+++ b/tests/pillar/single.sls
@@ -1,5 +1,6 @@
 etcd:
   server:
+    image: quay.io/coreos/etcd:latest
     enabled: true
     bind:
       host: 10.0.175.101
@@ -7,4 +8,4 @@
     members:
     - host: 10.0.175.101
       name: etcd01
-      port: 4001
\ No newline at end of file
+      port: 4001