Skip to content

Instantly share code, notes, and snippets.

@travispaul
Last active October 3, 2016 15:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save travispaul/2e95e4338e70ae6ae8d8381247a29357 to your computer and use it in GitHub Desktop.
Save travispaul/2e95e4338e70ae6ae8d8381247a29357 to your computer and use it in GitHub Desktop.
Pid file is created before CouchDB is able to respond to HTTP requests.
# Get couchdb_password from metadata if exists, or use couchdb_pw, or set one.
log "getting couchdb_password"
if [[ $(mdata-get couchdb_password &>/dev/null)$? -eq "0" ]]; then
COUCHDB_PW=$(mdata-get couchdb_password 2>/dev/null);
mdata-put couchdb_pw ${COUCHDB_PW}
elif [[ $(mdata-get couchdb_pw &>/dev/null)$? -eq "0" ]]; then
COUCHDB_PW=$(mdata-get couchdb_pw 2>/dev/null);
else
COUCHDB_PW=$(od -An -N8 -x /dev/random | head -1 | tr -d ' ');
mdata-put couchdb_pw ${COUCHDB_PW}
fi
# Enable CouchDB
log "enabling epmd and couchdb"
svcadm enable -s pkgsrc/epmd
svcadm enable -s pkgsrc/couchdb
# Wait for CouchDB to start
log "waiting for the socket to show up"
COUNT="0";
while [[ $(curl http://127.0.0.1:5984 &>/dev/null)$? -ne "0" ]]; do
#while [[ ! -e /var/spool/couchdb/couchdb.pid ]]; do
sleep 1
((COUNT=COUNT+1))
if [[ ${COUNT} -eq 60 ]]; then
log "ERROR Could not talk to CouchDB after 60 seconds"
ERROR=yes
break 1
fi
done
[[ -n "${ERROR}" ]] && exit 31
log "(it took ${COUNT} seconds to start properly)"
sleep 1
# Create CouchDB admin user
log "creating couchdb user"
curl -X PUT http://127.0.0.1:5984/_config/admins/couchdb -d "\"${COUCHDB_PW}\""
# Configure CouchDB
log "configuring couchdb"
openssl genrsa > /opt/local/etc/couchdb/server_key.pem
openssl req -new -x509 -key /opt/local/etc/couchdb/server_key.pem -out /opt/local/etc/couchdb/server_cert.pem -days 1095 -subj "/C=US/ST=CA/O=Default/"
gsed -i "/^; httpsd =/s/; httpsd =/httpsd =/" /opt/local/etc/couchdb/local.ini
gsed -i "s/;cert_file = \/full\/path\/to\/server_cert.pem/cert_file = \/opt\/local\/etc\/couchdb\/server_cert.pem/g" /opt/local/etc/couchdb/local.ini
gsed -i "s/;key_file = \/full\/path\/to\/server_key.pem/key_file = \/opt\/local\/etc\/couchdb\/server_key.pem/g" /opt/local/etc/couchdb/local.ini
gsed -i "s/; require_valid_user = false/require_valid_user = true/g" /opt/local/etc/couchdb/local.ini
# Restart CouchDB
svcadm restart pkgsrc/couchdb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment