Update snap for 2.3.1 + core18 (#50)

diff --git a/README-SNAP.md b/README-SNAP.md
new file mode 100644
index 0000000..c528dfa
--- /dev/null
+++ b/README-SNAP.md
@@ -0,0 +1,247 @@
+# CouchDB "in a snap"
+
+# Table of Contents
+1. [Installation](#installation)
+1. [Configuration](#configuration)
+1. [Clustering](#clustering)
+1. [Building](#building)
+
+-----
+
+# Installation <a name="installation"></a>
+
+## Downloading from the snap store
+
+The snap can be installed from a file or directly from the snap store:
+
+```bash
+$ sudo snap install couchdb
+```  
+
+## Enable snap permissions
+
+The snap installation uses AppArmor to protect your system. CouchDB requests access to two
+interfaces: mount-observe, which is used by the disk compactor to know when to initiate a
+cleanup; and process-control, which is used by the indexer to set the priority of couchjs
+to 'nice'. These two interfaces are required for CouchDB to run correctly.
+
+To connect the interfaces type:
+
+```bash
+$ sudo snap connect couchdb:mount-observe
+$ sudo snap connect couchdb:process-control
+```
+
+# Configuration <a name="configuration"></a>
+
+Be sure to read the [CouchDB documentation](http://docs.couchdb.org/en/stable/) first.
+
+CouchDB defaults are stored **read-only** in `/snap/couchdb/current/opt/couchdb/etc/`.
+This includes `default.ini` and any `default.d/*` files added in the snap build process.
+These are all read-only and should never be changed.
+
+User-configurable files are stored in `/var/snap/couchdb/current/etc/` and are writeable.
+Changes may be made to `local.ini` or placed in any `local.d/*.ini` file. Configuration
+management tools (like puppet, chef, ansible, and salt) can be used to manage these files.
+
+Erlang settings are stored in the `/var/snap/couchdb/current/etc/vm.args` file.  The snap
+configuration tool can be used to quickly change the node name and security cookie:
+
+```bash
+$ sudo snap set couchdb name=couchdb@1.2.3.4 setcookie=cutter
+```
+
+Be sure to read `vm.args` to understand what these settings do before changing them.
+
+*Any configuration file changes require restarting CouchDB before they are effective:*
+
+```bash
+$ sudo snap restart couchdb
+```
+
+## Monitoring CouchDB 
+
+The logs, by default, are captured by journald. View the logs with:
+
+```bash
+$ journalctl -u snap.couchdb* -f`
+```
+
+-----
+
+# Clustering <a name="clustering"></a>
+
+You can set up a snap-based cluster on your desktop in no time using the couchdb snap.
+
+## Create three nodes
+
+In the example below, we are going to set up a three node CouchDB cluster. (Three is the
+minimum number needed to support clustering features.) We'll also set up a separate,
+single machine for making backups. In this example we will be using LXD.
+
+We launch a (single) new container, install couchdb via snap from the store and enable
+interfaces, open up the bind address and set a admin password.
+
+```bash
+localhost> lxc launch ubuntu:18.04 couchdb-c1
+localhost> lxc exec couchdb-c1 bash
+couchdb-c1> apt update
+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/admins/admin -d '"Be1stDB"'
+couchdb-c1> exit
+```
+
+Back on localhost, we can then use the LXD copy function to speed up installation:
+
+```bash
+$ lxc copy couchdb-c1 couchdb-c2
+$ lxc copy couchdb-c1 couchdb-c3
+$ lxc copy couchdb-c1 couchdb-bkup
+$ lxc start couchdb-c2
+$ lxc start couchdb-c3
+$ lxc start couchdb-bkup
+```
+
+## Configure CouchDB using the snap tool
+
+We are going to need the IP addresses of each container:
+
+```bash
+$ lxc list
+```
+
+For this example, let's say the IP addresses are `10.210.199.10`, `.11` and `.12`.
+
+Now, again from localhost, and using the `lxc exec` commond, we will use the snap
+configuration tool to set the various configuration files.
+
+```bash
+$ lxc exec couchdb-c1 snap set couchdb name=couchdb@10.210.199.10 setcookie=monster
+$ lxc exec couchdb-c2 snap set couchdb name=couchdb@10.210.199.11 setcookie=monster
+$ lxc exec couchdb-c3 snap set couchdb name=couchdb@10.210.199.12 setcookie=monster
+```
+
+The backup machine we will configure as a single instance (`n=1, q=1`). 
+
+```bash
+  $ lxc exec couchdb-bkup snap set couchdb name=couchdb@127.0.0.1 setcookie=monster
+  $ lxc exec couchdb-bkup -- curl -X PUT http://admin:Be1stDB@localhost:5984/_node/_local/_config/cluster/n -d '"1"'
+  $ lxc exec couchdb-bkup -- curl -X PUT http://admin:Be1stDB@localhost:5984/_node/_local/_config/cluster/q -d '"1"'
+```
+
+Each snap must be restarted for the new configurations to take affect. 
+
+```bash
+$ lxc exec couchdb-c1 snap restart couchdb
+$ lxc exec couchdb-c2 snap restart couchdb
+$ lxc exec couchdb-c3 snap restart couchdb
+$ lxc exec couchdb-bkup snap restart couchdb
+```
+
+The configuration files are stored here:
+
+```bash
+$ lxc exec couchdb-bkup cat /var/snap/couchdb/current/etc/vm.args
+```
+
+Any changes to couchdb via curl are stored here:
+
+```bash
+$ lxc exec couchdb-bkup cat /var/snap/couchdb/current/etc/local.ini
+```
+
+## Configure CouchDB Cluster (using the http interface)
+
+Now we set up the cluster via the http front-end. This only needs to be run once on the
+first machine. The last command syncs with the other nodes and creates the standard
+databases.
+
+```bash
+$ curl -X POST -H "Content-Type: application/json" \
+    http://admin:Be1stDB@10.210.199.10:5984/_cluster_setup \
+    -d '{"action": "add_node", "host":"10.210.199.11", "port": "5984", "username": "admin", "password":"Be1stDB"}'
+$ curl -X POST -H "Content-Type: application/json" \
+    http://admin:Be1stDB@10.210.199.10:5984/_cluster_setup \
+    -d '{"action": "add_node", "host":"10.210.199.12", "port": "5984", "username": "admin", "password":"Be1stDB"}'
+$ curl -X POST -H "Content-Type: application/json" \
+    http://admin:Be1stDB@10.210.199.10:5984/_cluster_setup \
+    -d '{"action": "finish_cluster"}'
+```
+
+Now we have a functioning three node cluster. 
+
+## An Example Database
+
+Let's create an example database ...
+
+```bash
+$ curl -X PUT http://admin:Be1stDB@10.210.199.10:5984/example
+$ curl -X PUT http://admin:Be1stDB@10.210.199.10:5984/example/aaa -d '{"test":1}' -H "Content-Type: application/json"
+$ curl -X PUT http://admin:Be1stDB@10.210.199.10:5984/example/aab -d '{"test":2}' -H "Content-Type: application/json"
+$ curl -X PUT http://admin:Be1stDB@10.210.199.10:5984/example/aac -d '{"test":3}' -H "Content-Type: application/json"
+```
+
+... and verify that it is created on all three nodes:
+
+```bash
+$ curl -X GET http://admin:Be1stDB@10.210.199.10:5984/example/_all_docs
+$ curl -X GET http://admin:Be1stDB@10.210.199.11:5984/example/_all_docs
+$ curl -X GET http://admin:Be1stDB@10.210.199.12:5984/example/_all_docs
+```
+
+## Backing Up CouchDB
+
+Our backup server is on 10.210.199.242. We will manually replicate to this from one (can be any one) of the nodes.
+
+```bash
+$ curl -X POST http://admin:Be1stDB@10.210.199.242:5984/_replicate \
+    -d '{"source":"http://10.210.199.10:5984/example","target":"example","continuous":false,"create_target":true}' \
+    -H "Content-Type: application/json"
+$ curl -X GET http://admin:Be1stDB@10.210.199.242:5984/example/_all_docs
+```
+
+Whereas the data store for the clusters nodes is sharded:
+
+```bash
+  $ lxc exec couchdb-c1 ls /var/snap/couchdb/common/data/shards/
+```
+
+The backup database is a single directory:
+
+```bash
+  $ lxc exec couchdb-bkup ls /var/snap/couchdb/common/data/shards/
+```
+
+-----
+
+# Building this snap <a name="building"></a>
+
+This build requires Ubuntu 18.04, the `core18` core, and the `snapcraft` tool.  The
+CouchDB team builds this using the
+[`yakshaveinc/snapcraft`](https://hub.docker.com/r/yakshaveinc/snapcraft) image, which is
+the [official `snapcore/snapcraft` Docker
+image](https://snapcraft.io/docs/build-on-docker) patched for Ubuntu 18.04. (When the
+upstream image is fully patched for `core18`, we'll move to it instead.)
+
+From an Ubuntu 18.04 machine with Docker installed:
+
+```bash
+$ git clone https://github.com/couchdb/couchdb-pkg && cd couchdb-pkg`
+$ docker pull yakshaveinc/snapcraft:core18-edge`
+$ docker run -it -v "$PWD":/build:Z -w /build yakshaveinc/snapcraft:core18-edge snapcraft
+```
+
+The self-built snap will need to be installed using `--dangerous`:
+
+```bash
+sudo snap install ./couchdb_2.3.1_amd64.snap --dangerous
+```
+
+Clean up with:
+
+```bash
+$docker run -it -v "$PWD":/build:Z -w /build yakshaveinc/snapcraft:core18-edge snapcraft clean
+```
diff --git a/README.md b/README.md
index 2a5f36e..ca70e64 100644
--- a/README.md
+++ b/README.md
@@ -112,17 +112,11 @@
 
 -----
 
-# Building snaps
+# Snap packages
 
-## Prerequisites
+See [README-SNAP.md](README-SNAP.md).
 
-1. Ubuntu 16.04
-1. `sudo apt install snapd snapcraft`
-
-## How to do it
-
-1. Edit `snap/snapcraft.yaml` to point to the correct tag (e.g. `2.3.0`)
-1. `snapcraft`
+-----
 
 # Feedback, Issues, Contributing
 
diff --git a/snap/BUILD.md b/snap/BUILD.md
deleted file mode 100644
index 694b0af..0000000
--- a/snap/BUILD.md
+++ /dev/null
@@ -1,30 +0,0 @@
-# Building snaps
-
-## Prerequisites
-
-CouchDB requires Ubuntu 16.04. If building on 18.04, then LXD might be useful.
-
-1. `lxc launch ubuntu:16.04 couchdb-pkg`
-1. `lxc exec couchdb-pkg bash`
-1. `sudo apt update`
-1. `sudo apt install snapd snapcraft`
-
-1. `git clone https://github.com/couchdb/couchdb-pkg.git`
-1. `cd couchdb-pkg`
-
-## How to do it
-
-1. Edit `snap/snapcraft.yaml` to point to the correct tag (e.g. `2.2.0`)
-1. `snapcraft`
-
-## Instalation
-
-You may need to pull the LXD file to the host system.
-
-    $ lxc file pull couchdb-pkg/root/couchdb-pkg/couchdb_2.2.0_amd64.snap /tmp/couchdb_2.2.0_amd64.snap
-
-The self crafted snap will need to be installed in devmode
-
-    $ sudo snap install /tmp/couchdb_2.2.0_amd64.snap --devmode
-
-
diff --git a/snap/HOWTO.md b/snap/HOWTO.md
deleted file mode 100644
index a217af9..0000000
--- a/snap/HOWTO.md
+++ /dev/null
@@ -1,112 +0,0 @@
-# HOW TO install a cluster using snap
-
-## Create three nodes
-
-In the example below, we are going to set up a three node CouchDB cluster. (Three is the minimum number needed to support clustering features.) We'll also set up a separate, single machine for making backups. In this example we will be using LXD.
-
-We launch a (single) new container, install couchdb via snap from the store and enable interfaces, open up the bind address and set a admin password.
-```bash
-  1. localhost> lxc launch ubuntu:18.04 couchdb-c1
-  1. localhost> lxc exec couchdb-c1 bash
-  1. couchdb-c1> apt update
-  1. couchdb-c1> snap install couchdb --edge
-  1. couchdb-c1> snap connect couchdb:mount-observe
-  1. couchdb-c1> snap connect couchdb:process-control
-  1. couchdb-c1> curl -X PUT http://localhost:5984/_node/_local/_config/httpd/bind_address -d '"0.0.0.0"'
-  1. couchdb-c1> curl -X PUT http://localhost:5984/_node/_local/_config/admins/admin -d '"Be1stDB"'
-  1. couchdb-c1> exit
-```
-Back on localhost, we can then use the LXD copy function to speed up installation:
-```bash
-  $ lxc copy couchdb-c1 couchdb-c2
-  $ lxc copy couchdb-c1 couchdb-c3
-  $ lxc copy couchdb-c1 couchdb-bkup
-  $ lxc start couchdb-c2
-  $ lxc start couchdb-c3
-  $ lxc start couchdb-bkup
-```
-
-## Configure CouchDB using the snap tool
-
-We are going to need the IP addresses:
-```bash
-  $ lxc list
-```
-Now, again from localhost, and using the `lxc exec` commond, we will use the snap configuration tool to set the 
-various configuration files.
-```bash
-  $ lxc exec couchdb-c1 snap set couchdb name=couchdb@10.210.199.73 setcookie=monster
-  $ lxc exec couchdb-c2 snap set couchdb name=couchdb@10.210.199.221 setcookie=monster
-  $ lxc exec couchdb-c3 snap set couchdb name=couchdb@10.210.199.121 setcookie=monster
-```
-The backup machine we will configure as a single instance (n=1). 
-```bash
-  $ lxc exec couchdb-bkup snap set couchdb name=couchdb@127.0.0.1 setcookie=monster
-  $ lxc exec couchdb-bkup -- curl -X PUT http://admin:Be1stDB@localhost:5984/_node/_local/_config/cluster/n -d '"1"'
-  $ lxc exec couchdb-bkup -- curl -X PUT http://admin:Be1stDB@localhost:5984/_node/_local/_config/cluster/q -d '"1"'
-
-```
-Each snap must be restarted for the new configurations to take affect. 
-```bash
-  $ lxc exec couchdb-c1 snap restart couchdb
-  $ lxc exec couchdb-c2 snap restart couchdb
-  $ lxc exec couchdb-c3 snap restart couchdb
-  $ lxc exec couchdb-bkup snap restart couchdb
-```
-The configuration files are stored here.
-```bash
-  $ lxc exec couchdb-bkup cat /var/snap/couchdb/current/etc/vm.args
-```
-Any changes to couchdb from the http configutation tool are made here
-```bash
-  $ lxc exec couchdb-bkup cat /var/snap/couchdb/current/etc/local.ini
-```
-
-## Configure CouchDB Cluster (using the http interface)
-
-Now we set up the cluster via the http front-end. This only needs to be run once on the first machine. The last command 
-syncs with the other nodes and creates the standard databases.
-```bash
-  $ curl -X POST -H "Content-Type: application/json" http://admin:Be1stDB@10.210.199.73:5984/_cluster_setup -d '{"action": "add_node", "host":"10.210.199.221", "port": "5984", "username": "admin", "password":"Be1stDB"}'
-  $ curl -X POST -H "Content-Type: application/json" http://admin:Be1stDB@10.210.199.73:5984/_cluster_setup -d '{"action": "add_node", "host":"10.210.199.121", "port": "5984", "username": "admin", "password":"Be1stDB"}'
-  $ curl -X POST -H "Content-Type: application/json" http://admin:Be1stDB@10.210.199.73:5984/_cluster_setup -d '{"action": "finish_cluster"}'
-```
-Now we have a functioning three node cluster. 
-
-## An Example Database
-
-Let's create an example database ...
-```bash
-  $ curl -X PUT http://admin:Be1stDB@10.210.199.73:5984/example
-  $ curl -X PUT http://admin:Be1stDB@10.210.199.73:5984/example/aaa -d '{"test":1}' -H "Content-Type: application/json"
-  $ curl -X PUT http://admin:Be1stDB@10.210.199.73:5984/example/aab -d '{"test":2}' -H "Content-Type: application/json"
-  $ curl -X PUT http://admin:Be1stDB@10.210.199.73:5984/example/aac -d '{"test":3}' -H "Content-Type: application/json"
-```
-... And see that it is created on all three nodes.
-```bash
-  $ curl -X GET http://admin:Be1stDB@10.210.199.73:5984/example/_all_docs
-  $ curl -X GET http://admin:Be1stDB@10.210.199.221:5984/example/_all_docs
-  $ curl -X GET http://admin:Be1stDB@10.210.199.121:5984/example/_all_docs
-```
-## Backing Up CouchDB
-
-Our backup server is on 10.210.199.242. We will manually replicate to this from one (can be any one) of the nodes.
-```bash
-  $ curl -X POST http://admin:Be1stDB@10.210.199.242:5984/_replicate -d '{"source":"http://10.210.199.73:5984/example", "target":"example", "continuous":false,"create_target":true}' -H "Content-Type: application/json"
-  $ curl -X GET http://admin:Be1stDB@10.210.199.242:5984/example/_all_docs
-```
-Whereas the data store for the clusters nodes is sharded:
-```bash
-  $ lxc exec couchdb-c1 ls /var/snap/couchdb/common/data/shards/
-```
-The backup database is a single directory:
-```bash
-  $ lxc exec couchdb-bkup ls /var/snap/couchdb/common/data/shards/
-```
-
-## Monitoring CouchDB 
-
-The logs, by default, are captured by journald. First connect to the node in question:
-  `$ lxc exec couchdb-c1 bash`
-Then, show logs as usual. couchdb is likely prefixed with 'snap' and suffix may vary depending on the version of snap.
-  `$ journalctl -u snap.couchdb* -f`
diff --git a/snap/README.md b/snap/README.md
deleted file mode 100644
index 3be05d3..0000000
--- a/snap/README.md
+++ /dev/null
@@ -1,70 +0,0 @@
-# Snap Instalation
-
-## Downloading from the snap store
-
-The snap can be installed from a file or directly from the snap store. It is, for the moment, listed in the edge channel.
-
-```
-    $ sudo snap install couchdb --edge
-```  
-## Enable snap permissions
-
-The snap installation uses AppArmor to protect your system. CouchDB requests access to two interfaces: mount-observe, which
-is used by the disk compactor to know when to initiate a cleanup; and process-control, which is used by the indexer to set
-the priority of couchjs to 'nice'. These two interfaces, while not required, are useful. If they are not enabled, CouchDB will
-still run, but you will need to run the compactor manually and couchjs may put a heavy load on the system when indexing. 
-
-To connect the interfaces type:
-   ```
-   $ sudo snap connect couchdb:mount-observe
-   $ sudo snap connect couchdb:process-control
-   ```
-## Snap configuration
-
-There are two levels of hierarchy within couchdb configuration. 
-
-The default layer is stored in /snap/couchdb/current/rel/couchdb/etc/ the default.ini is
-first consulted and then any file default.d directory. In the snap installation 
-this is mounted read-only.
-
-The local layer is stored in /var/snap/couchdb/current/etc/ on the writable /var mount. 
-Within this second layer, configurations are set with-in local.ini or superseded by any 
-file within local.d. Configuration management tools (like puppet, chef, ansible, salt) operate here.
-
-The name of the erlang process and the security cookie used is set within vm.args file.
-This can be set suing the snap native configuration. For example, when setting up 
-a cluster over several machines the convention is to set the erlang name to couchdb@your.ip.address. 
-
-```
-    $ sudo snap set couchdb name=couchdb@216.3.128.12 setcookie=cutter
-```
-
-Snap Native Configuration changes only come into effect after a restart
-
-```
-    $ sudo snap restart couchdb
-```
-
-CouchDB options can be set via configuration over HTTP, as below.
-
-```
-    $ curl -X PUT http://localhost:5984/_node/_local/_config/httpd/bind_address -d '"0.0.0.0"'
-    $ curl -X PUT http://localhost:5984/_node/_local/_config/couchdb/delayed-commits -d '"true"'
-```
-
-Changes here do not require a restart.
-
-For anything else in vm.args or configuration not white listed over http, you can edit 
-the /var/snap/couchdb/current/etc files by hand and restart CouchDB. 
-
-## Example Cluster
-
-See the [HOWTO][1] file to see an example of a three node cluster and further notes. 
-
-## Building a Private Snap
-
-If you want to build your own snap file from source see the [BUILD][2] for instructions.
-
-[1]: HOWTO.md
-[2]: BUILD.md
-
diff --git a/snap/couchdb.ini b/snap/couchdb.ini
deleted file mode 100644
index a384ff3..0000000
--- a/snap/couchdb.ini
+++ /dev/null
@@ -1,3 +0,0 @@
-[couchdb]
-database_dir = /var/snap/couchdb/common/data
-view_index_dir = /var/snap/couchdb/common/data
diff --git a/snap/hooks/configure b/snap/hooks/configure
new file mode 100755
index 0000000..5076e5a
--- /dev/null
+++ b/snap/hooks/configure
@@ -0,0 +1,45 @@
+#!/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.
+
+set -e
+
+VM_ARGS=$SNAP_DATA/etc/vm.args
+
+
+## add or replace for the vm.arg file
+_modify_vm_args() {
+  opt=$1
+  value="$2"
+  replace_line="-$opt $value"
+  if $(grep -q "^-$opt " $VM_ARGS); then
+    sed "s/^-$opt .*/$replace_line/" $VM_ARGS 2>/dev/null >${VM_ARGS}.new
+    mv -f ${VM_ARGS}.new $VM_ARGS 2>/dev/null
+  else
+    echo $replace_line >> $VM_ARGS
+  fi
+}
+
+# The vm_args file can only be changed from the filesystem
+# configutaion vm.args file
+
+VM_ARGS_OPTIONS="name setcookie"
+for key in $VM_ARGS_OPTIONS
+do
+  val=$(snapctl get $key)
+  if [ ! -z "$val" ]; then
+    _modify_vm_args $key $val
+    sleep 0.125
+  fi
+done
+
diff --git a/snap/hooks/install b/snap/hooks/install
new file mode 100755
index 0000000..98807bc
--- /dev/null
+++ b/snap/hooks/install
@@ -0,0 +1,24 @@
+#!/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.
+
+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
+fi
+
+if [ ! -s ${SNAP_DATA}/etc/local.ini ]; then
+   cp ${SNAP}/opt/couchdb/etc/local.ini.dist ${SNAP_DATA}/etc/local.ini
+fi
+
diff --git a/snap/meta/hooks/configure b/snap/meta/hooks/configure
deleted file mode 100755
index d6af526..0000000
--- a/snap/meta/hooks/configure
+++ /dev/null
@@ -1,33 +0,0 @@
-#!/bin/sh 
-
-set -e
-
-VM_ARGS=$SNAP_DATA/etc/vm.args
-
-
-## add or replace for the vm.arg file
-_modify_vm_args() {
-  opt=$1
-  value="$2"
-  replace_line="-$opt $value"
-  if $(grep -q "^-$opt " $VM_ARGS); then
-    sed "s/^-$opt .*/$replace_line/" $VM_ARGS 2>/dev/null >${VM_ARGS}.new
-    mv -f ${VM_ARGS}.new $VM_ARGS 2>/dev/null
-  else
-    echo $replace_line >> $VM_ARGS
-  fi
-}
-
-# The vm_args file can only be changed from the filesystem
-# configutaion vm.args file
-
-VM_ARGS_OPTIONS="name setcookie"
-for key in $VM_ARGS_OPTIONS
-do
-  val=$(snapctl get $key)
-  if [ ! -z "$val" ]; then
-    _modify_vm_args $key $val
-    sleep 0.125
-  fi
-done
-
diff --git a/snap/meta/hooks/install b/snap/meta/hooks/install
deleted file mode 100755
index 0a2195c..0000000
--- a/snap/meta/hooks/install
+++ /dev/null
@@ -1,12 +0,0 @@
-#!/bin/sh 
-
-mkdir -p ${SNAP_DATA}/etc/local.d 
-
-if [ ! -f ${SNAP_DATA}/etc/vm.args ]; then
-   cp ${SNAP}/rel/couchdb/etc/vm.args ${SNAP_DATA}/etc/vm.args
-fi
-
-if [ ! -f ${SNAP_DATA}/etc/local.ini ]; then
-   cp ${SNAP}/rel/couchdb/etc/local.ini ${SNAP_DATA}/etc/local.ini
-fi
-
diff --git a/snap/snap_run b/snap/snap_run
deleted file mode 100755
index e608086..0000000
--- a/snap/snap_run
+++ /dev/null
@@ -1,35 +0,0 @@
-#!/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.
-
-# The purpose of this script is to initialize environment variables and Erlang
-# configuration files so that couchdb will run inside a Snap package's confinement
-
-export HOME=$SNAP_DATA
-export COUCHDB_ARGS_FILE=${SNAP_DATA}/etc/vm.args
-export ERL_FLAGS="-couch_ini ${SNAP}/rel/couchdb/etc/default.ini ${SNAP}/rel/couchdb/etc/default.d ${SNAP_DATA}/etc/local.ini ${SNAP_DATA}/etc/local.d"
-
-mkdir -p ${SNAP_DATA}/etc 
-
-if [ ! -e ${SNAP_DATA}/etc/vm.args ]; then
-    cp ${SNAP}/rel/couchdb/etc/vm.args ${SNAP_DATA}/etc/vm.args
-fi
-
-if [ ! -d ${SNAP_DATA}/etc/local.d ]; then
-    mkdir ${SNAP_DATA}/etc/local.d
-fi
-
-if [ ! -e ${SNAP_DATA}/etc/local.ini ]; then
-    touch ${SNAP_DATA}/etc/local.ini
-fi
-
-exec ${SNAP}/rel/couchdb/bin/couchdb
diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml
index 1eadf03..b8e24b4 100644
--- a/snap/snapcraft.yaml
+++ b/snap/snapcraft.yaml
@@ -1,74 +1,112 @@
 name: couchdb
-version: 2.2.0
-summary: Document based database
-description: CouchDB is a database that completely embraces the web. Store your data with JSON documents. Access your documents and query your indexes with your web browser, via HTTP. Index, combine, and transform your documents with JavaScript. 
-confinement: strict
+version: 2.3.1
+summary: Official Apache CouchDB snap - a clustering document oriented database
+description: |
+  CouchDB is a database that completely embraces the web. Store your data with
+  JSON documents. Access your documents and query your indexes with your web
+  browser, via HTTP. Index, combine, and transform your documents with
+  JavaScript. 
+
+architectures:
+  - build-on: amd64
+    run-on: amd64
+assumes: [command-chain, common-data-dir]
+base: core18
 grade: stable
+confinement: strict
+
+parts:
+  add-repo:
+    plugin: nil
+    override-pull: |
+      apt-get update
+      apt-get upgrade -yy
+      apt-get install -y --no-install-recommends apt-transport-https gnupg ca-certificates
+      echo "deb https://apache.bintray.com/couchdb-deb bionic main" | tee /etc/apt/sources.list.d/custom.list
+      apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 8756C4F765C9AC3CB6B85D62379CE192D401AB61
+      apt-get update
+  couchdb: 
+    after: [add-repo]
+    plugin: dump
+    source: https://apache.bintray.com/couchdb-deb/pool/C/CouchDB/couchdb_2.3.1~bionic_amd64.deb
+    source-type: deb
+    # because this doesn't use apt, we have to manually list all of our dependencies :(
+    # the following are all in core18, and warning output can safely be ignored:
+    # lib/x86_64-linux-gnu/libbz2.so.1.0
+    # lib/x86_64-linux-gnu/libc.so.6
+    # lib/x86_64-linux-gnu/libcrypt.so.1
+    # lib/x86_64-linux-gnu/libdl.so.2
+    # lib/x86_64-linux-gnu/libgcrypt.so.20
+    # lib/x86_64-linux-gnu/libgpg-error.so.0
+    # lib/x86_64-linux-gnu/liblzma.so.5
+    # lib/x86_64-linux-gnu/libm.so.6
+    # lib/x86_64-linux-gnu/libncurses.so.5
+    # lib/x86_64-linux-gnu/libncursesw.so.5
+    # lib/x86_64-linux-gnu/libnsl.so.1
+    # lib/x86_64-linux-gnu/libpthread.so.0
+    # lib/x86_64-linux-gnu/libresolv.so.2
+    # lib/x86_64-linux-gnu/librt.so.1
+    # lib/x86_64-linux-gnu/libsystemd.so.0
+    # lib/x86_64-linux-gnu/libutil.so.1
+    # lib/x86_64-linux-gnu/libz.so.1
+    # usr/lib/x86_64-linux-gnu/libdb-5.3.so
+    # usr/lib/x86_64-linux-gnu/liblz4.so.1
+    # usr/lib/x86_64-linux-gnu/libpanelw.so.5
+    # usr/lib/x86_64-linux-gnu/libstdc++.so.6
+    stage-packages:
+      - ca-certificates
+      - adduser
+      - curl
+      - debconf
+      - init-system-helpers
+      - couch-libmozjs185-1.0
+      - lsb-base
+      - procps
+      - python3
+      - python3-requests
+      - libcurl4
+      - libgcc1
+      - libicu60
+      - libssl1.0.0
+      - libtinfo5
+    override-pull: |
+      snapcraftctl pull
+      rm -f opt/couchdb/data opt/couchdb/var/log opt/couchdb/etc/default.d/*
+      mkdir -p opt/couchdb/etc/default.d.dist/
+      mv opt/couchdb/etc/vm.args opt/couchdb/etc/vm.args.dist
+      mv opt/couchdb/etc/local.ini opt/couchdb/etc/local.ini.dist
+    override-build: |
+      echo "couchdb couchdb/mode select none" | debconf-set-selections
+      snapcraftctl build
+
+layout:
+  # Database and log files are common across upgrades
+  $SNAP/opt/couchdb/data:
+    bind: $SNAP_DATA/data
+  $SNAP/opt/couchdb/var/log:
+    bind: $SNAP_DATA/log
+  # Local configuration files may change across upgrades
+  $SNAP/opt/couchdb/etc/vm.args:
+    bind-file: $SNAP_DATA/etc/vm.args
+  $SNAP/opt/couchdb/etc/local.d:
+    bind: $SNAP_DATA/etc/local.d
+  $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
 
 apps:
-    server:
-        daemon: simple
-        command: rel/couchdb/bin/snap_run
-        plugs: [network-bind, process-control]
-    couchdb:
-        command: rel/couchdb/bin/snap_run
-        plugs: [network-bind, mount-observe, browser-support]
-parts:
-    couchdb: 
-        plugin: make
-        source: http://www-us.apache.org/dist/couchdb/source/2.2.0/apache-couchdb-2.2.0.tar.gz
-        #source: https://github.com/apache/couchdb.git
-        #source-type: git
-        #source-tag: 2.2.0
-        override-build: |
-          ./configure --disable-docs
-          make release
-          cp -ra ./rel $SNAPCRAFT_PART_INSTALL
-          cp -ra ./bin $SNAPCRAFT_PART_INSTALL
-        build-packages:
-          - binutils
-          - libc6-dev
-          - gcc
-          - g++
-          - make
-          - erlang-dev
-          - erlang-base
-          - erlang-reltool
-          - libcurl4-openssl-dev
-          - libmozjs185-dev
-          - libnspr4-dev
-          - libicu-dev
-          - icu-devtools
-          # For erlang/rebar processing
-          - erlang-nox
-          - erlang-os-mon
-          - erlang-syntax-tools
-          # For fauxton
-          - nodejs-dev
-          - nodejs-legacy
-          - npm
-        stage:
-            - rel
-            - bin
-        prime:
-            - rel
-            - bin
-
-    snap-config:
-        plugin: dump
-        source: ./snap/
-        organize:
-            couchdb.ini: rel/couchdb/etc/default.d/couchdb.ini
-            snap_run: rel/couchdb/bin/snap_run
-
-    packages:
-        plugin: nil
-        stage-packages:
-            - libicu55
-            - libmozjs185-1.0
-        filesets:
-            exclusion:
-              - "-usr/share/doc/*"
-              - "-usr/share/man/*"
-        prime:
-            - "$exclusion"
+  couchdb:
+    adapter: full
+    command: opt/couchdb/bin/couchdb
+    plugs: [network, network-bind, process-control, mount-observe]
+  server:
+    daemon: simple
+    adapter: full
+    command: opt/couchdb/bin/couchdb
+    plugs: [network, network-bind, process-control, mount-observe]
+  remsh:
+    command: opt/couchdb/bin/remsh
+    plugs: [network, network-bind, process-control]
+  couchup:
+    command: opt/couchdb/bin/couchup
+    plugs: [network, network-bind, process-control]