Merge pull request #52 from apache/postrm_remove_databases

Add extra protection to databases during package purge
diff --git a/README-SNAP.md b/README-SNAP.md
index c528dfa..28628fb 100644
--- a/README-SNAP.md
+++ b/README-SNAP.md
@@ -61,10 +61,34 @@
 
 ## Monitoring CouchDB 
 
-The logs, by default, are captured by journald. View the logs with:
+The logs, by default, are captured by journald. View the logs with either command:
 
 ```bash
-$ journalctl -u snap.couchdb* -f`
+$ snap logs couchdb -f
+$ journalctl -u snap.couchdb* -f
+```
+
+## Removing CouchDB
+
+There are several difference between installation via 'apt' and 'snap'. One important 
+difference is when removing couchdb. When calling 'apt remove couchdb', the binaries 
+are removed but the configuration and the couch database files remain, leaving the 
+user to clean up any databases latter. 
+
+Calling 'snap remove couchdb' *will* remove binaries, configurations and the database.
+
+On newer versions of snapd (snapd 2.39+) a snapshot is made of the SNAP_DATA 
+and SNAP_COMMON directories and this is stored (subject to disc space) for about 30 days. 
+On these newer version a 'snap remove' followed by a 'snap install' may restore the 
+database; but you are best to make your own backup before removing couchdb.
+If you do not want to keep the configuration or database files you can delete the 
+snapshot by calling snap remove with the --purge parameter. 
+
+To remove your installation either:
+
+```bash
+$ sudo snap remove couchdb
+$ sudo snap remove couchdb --purge
 ```
 
 -----
@@ -89,7 +113,7 @@
 couchdb-c1> snap install couchdb --edge
 couchdb-c1> snap connect couchdb:mount-observe
 couchdb-c1> snap connect couchdb:process-control
-couchdb-c1> curl -X PUT http://localhost:5984/_node/_local/_config/httpd/bind_address -d '"0.0.0.0"'
+couchdb-c1> curl -X PUT http://localhost:5984/_node/_local/_config/chttpd/bind_address -d '"0.0.0.0"'
 couchdb-c1> curl -X PUT http://localhost:5984/_node/_local/_config/admins/admin -d '"Be1stDB"'
 couchdb-c1> exit
 ```
diff --git a/js/rpm/SPECS/js.spec b/js/rpm/SPECS/js.spec
index 944fa11..130b7ec 100644
--- a/js/rpm/SPECS/js.spec
+++ b/js/rpm/SPECS/js.spec
@@ -39,7 +39,7 @@
 BuildRequires:	python3
 %else
 Buildrequires:	nspr-devel >= 4.7
-BuildRequires:	python
+BuildRequires:	python2
 %endif
 BuildRequires:	perl
 BuildRequires:	zip
diff --git a/snap/hooks/install b/snap/hooks/install
index 98807bc..e6e8e21 100755
--- a/snap/hooks/install
+++ b/snap/hooks/install
@@ -12,13 +12,15 @@
 # License for the specific language governing permissions and limitations under
 # the License.
 
+mkdir -p ${SNAP_COMMON}/data
+
 mkdir -p ${SNAP_DATA}/etc/local.d
 
 if [ ! -s ${SNAP_DATA}/etc/vm.args ]; then
-   cp ${SNAP}/opt/couchdb/etc/vm.args.dist ${SNAP_DATA}/etc/vm.args
+   cat ${SNAP}/opt/couchdb/etc/vm.args.dist > ${SNAP_DATA}/etc/vm.args
 fi
 
 if [ ! -s ${SNAP_DATA}/etc/local.ini ]; then
-   cp ${SNAP}/opt/couchdb/etc/local.ini.dist ${SNAP_DATA}/etc/local.ini
+   cat ${SNAP}/opt/couchdb/etc/local.ini.dist > ${SNAP_DATA}/etc/local.ini
 fi
 
diff --git a/snap/hooks/pre-refresh b/snap/hooks/pre-refresh
new file mode 100755
index 0000000..248e6b3
--- /dev/null
+++ b/snap/hooks/pre-refresh
@@ -0,0 +1,22 @@
+#!/bin/sh
+
+# Licensed under the Apache License, Version 2.0 (the "License"); you may not
+# use this file except in compliance with the License. You may obtain a copy of
+# the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations under
+# the License.
+
+# If the data directory does not exisit in /var/couchdb/common; but does exisit in /var/couchdb/current; move it to common.
+
+if [ ! -d ${SNAP_COMMON}/data ]; then
+   if [ -d ${SNAP_DATA}/data ]; then
+       mv ${SNAP_DATA}/data ${SNAP_COMMON}/data
+   fi
+fi
+
diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml
index b8e24b4..9137f12 100644
--- a/snap/snapcraft.yaml
+++ b/snap/snapcraft.yaml
@@ -82,9 +82,9 @@
 layout:
   # Database and log files are common across upgrades
   $SNAP/opt/couchdb/data:
-    bind: $SNAP_DATA/data
+    bind: $SNAP_COMMON/data
   $SNAP/opt/couchdb/var/log:
-    bind: $SNAP_DATA/log
+    bind: $SNAP_COMMON/log
   # Local configuration files may change across upgrades
   $SNAP/opt/couchdb/etc/vm.args:
     bind-file: $SNAP_DATA/etc/vm.args
@@ -93,6 +93,10 @@
   $SNAP/opt/couchdb/etc/local.ini:
     bind-file: $SNAP_DATA/etc/local.ini
   # We do not bind default.ini or default.d/ as these are intended to be immutable
+  
+environment:
+  COUCHDB_ARGS_FILE: ${SNAP_DATA}/etc/vm.args
+  ERL_FLAGS: "-couch_ini ${SNAP}/opt/couchdb/etc/default.ini ${SNAP}/opt/couchdb/etc/default.d ${SNAP_DATA}/etc/local.ini ${SNAP_DATA}/etc/local.d"
 
 apps:
   couchdb:
@@ -106,7 +110,10 @@
     plugs: [network, network-bind, process-control, mount-observe]
   remsh:
     command: opt/couchdb/bin/remsh
-    plugs: [network, network-bind, process-control]
+    plugs: [network, network-bind]
   couchup:
     command: opt/couchdb/bin/couchup
-    plugs: [network, network-bind, process-control]
+    plugs: [network, network-bind]
+  couchjs:
+    command: opt/couchdb/bin/couchjs
+    plugs: [network, network-bind]