Skip to content

Instantly share code, notes, and snippets.

@anaqreon
Last active Oct 15, 2019
Embed
What would you like to do?
Nextcloud Maps localhost development environment

Nextcloud Maps local development environment

Overview

Develop Nextcloud Maps locally using Docker, with a custom git-based snapshot system for easily saving and restoring the complete state of the local Nextcloud server instance.

The MySQL database and Nextcloud server web root are stored as Docker bind mounts to local directories (see docker-compose.yml):

/var/local/volumes/maps_db
/var/local/volumes/maps_www

Modifications to these Nextcloud files while developing code take immediate effect for an efficient development workflow.

Snapshots

Snapshots are stored in a local git repo at /var/local/snapshots/maps. To take a snapshot, execute

maps_snapshot_create 'Your commit message'

To restore a snapshot, execute

maps_snapshot_restore 3a6af611

Instructions

  1. Create a directory named maps in your home folder (i.e. ~/maps).
  2. Copy the files below into this folder (unless they are already present from a git clone of this repo).
  3. Create the directories /var/local/volumes/ and /var/local/snapshots/maps.
  4. Run the maps/init.sh script to construct the Nextcloud instance and install the Maps app.
  5. Visit http://localhost:8000 in a web browser to log in to the fresh server. The default username is admin with password password.

Files

The files below should reside in ~/maps. The code below assumes the containers are automatically named maps_app_1 and maps_db_1, which should be the case if docker-compose up -d is executed from a working directory called maps using the docker-compose.yml file included.

Dockerfile

Use the official Nextcloud Docker image for a specific version. Installs additional dependencies needed to run the make command for the Maps app installation.

docker-compose.yml

Define the two containers and the bind mounts for the dynamic, persistent data storage. Specify the Apache listening port 8000.

maps_snapshot_create

Copy the persistent storage "volumes" to the snapshot git repo and commit the changes.

map_snapshot_restore

Restore the specified snapshot (by git commit hash) to the Docker bind mount directories, overwriting any existing files.

init.sh

Script to automate the creation of the Docker containers and to provision the new Nextcloud server with the latest Nextcloud Maps app (master branch).

version: '2'
networks:
internal:
external: false
services:
db:
image: mariadb
command: --transaction-isolation=READ-COMMITTED --binlog-format=ROW
restart: always
volumes:
- /var/local/volumes/maps_db:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=password
- MYSQL_PASSWORD=password
- MYSQL_DATABASE=nextcloud
- MYSQL_USER=nextcloud
networks:
- internal
app:
image: nextcloud_maps_dev
build: .
links:
- db
volumes:
- /var/local/volumes/maps_www:/var/www/html
restart: always
networks:
- internal
ports:
- 8000:80
FROM nextcloud:17.0.0
# entrypoint.sh and cron.sh dependencies
RUN set -ex; \
\
apt-get update; \
apt-get install -y --no-install-recommends \
git \
nodejs \
npm \
; \
rm -rf /var/lib/apt/lists/*;
#!/usr/bin/env bash
commit="$1"
cd "$(dirname $(readlink -f "$0"))"
if [[ "${commit}" == "" ]]; then
commit="master"
fi
# Build containers
docker-compose up -d --build --force-recreate
echo "Wait while Docker builds and launches containers..."
while ! curl -L localhost:8000 >/dev/null 2>&1; do
echo "Waiting for Apache server to come online..."
sleep 3
done
echo "Apache server online. Copy in Maps app files and set permissions..."
set -x
# Git clone Nextcloud Maps
docker exec -it -u www-data -w /var/www/html/ maps_app_1 git clone https://github.com/nextcloud/maps.git apps/maps
docker exec -it -u www-data -w /var/www/html/apps/maps maps_app_1 git checkout "${commit}"
docker exec -it -u www-data -w /var/www/html/apps/maps maps_app_1 make
# Allow read/write for "other" so that user on host can edit live files
docker exec -it maps_app_1 chmod -R a+rwX /var/www/html/
set +x
while ! docker exec maps_db_1 mysql --user=nextcloud --password=password -e "SELECT 1" >/dev/null 2>&1; do
echo "Waiting for mysql database to come online..."
sleep 5
done
echo "mysql database online. Proceeding with automated installation..."
# Complete Nextcloud installation
docker exec -it --user www-data maps_app_1 php occ maintenance:install \
--database "mysql" \
--database-name "nextcloud" \
--database-user "nextcloud" \
--database-pass "password" \
--database-host "maps_db_1" \
--admin-user "admin" \
--admin-pass "password" \
--data-dir "/var/www/html/data"
# Enable apps
echo "Enabling Contacts and Maps apps..."
docker exec -it --user www-data maps_app_1 php occ app:enable maps contacts
echo "Nextcloud instance is online. Happy coding!"
exit 0
#!/bin/bash
message="$1"
if [[ "${message}" == "" ]]; then
echo "ERROR: No commit message provided"
exit 1
fi
echo "Copy maps_db and maps_www to snapshot folder ..."
if [[ ! -d /var/local/snapshots/maps/.git ]]; then
sudo mkdir -p /var/local/snapshots/maps/
sudo chown -R www-data:www-data /var/local/snapshots/maps/
fi
sudo rsync -a --delete --exclude=.git* /var/local/volumes/maps_db /var/local/snapshots/maps/
sudo rsync -a --delete --exclude=.git* /var/local/volumes/maps_www /var/local/snapshots/maps/
echo "Commit the updates to the git repo ..."
if [[ ! -d /var/local/snapshots/maps/.git ]]; then
sudo su - root -c "cd /var/local/snapshots/maps && git init"
sudo su - root -c "chmod -R a+rX /var/local/snapshots/maps"
fi
sudo su - root -c "cd /var/local/snapshots/maps && git add -A >/dev/null"
sudo su - root -c "cd /var/local/snapshots/maps && git commit -m \"${message}\" >/dev/null"
echo
echo "Git status and log :"
echo
sudo su - root -c 'cd /var/local/snapshots/maps && git status'
sudo su - root -c 'cd /var/local/snapshots/maps && git log --oneline'
echo "Snapshot complete."
#!/bin/bash
commit="$1"
if [[ "${commit}" == "" ]]; then
echo "ERROR: No commit specified"
exit 1
fi
sudo su - root -c "rm -rf /tmp/maps_restore && git clone /var/local/snapshots/maps /tmp/maps_restore && cd /tmp/maps_restore && git reset --hard ${commit}"
echo "Copy maps_db and maps_www to volume folders ..."
sudo rsync -a --delete --exclude=.git* /tmp/maps_restore/maps_db/ /var/local/volumes/maps_db/
sudo rsync -a --delete --exclude=.git* /tmp/maps_restore/maps_www/ /var/local/volumes/maps_www/
echo "Restore file ownersihip ..."
sudo chown -R 99:99 /var/local/volumes/maps_db
sudo chown -R www-data:www-data /var/local/volumes/maps_www
echo "Restart docker containers ..."
cd ~/maps
docker-compose down && docker-compose up -d
echo "Cleanup temp files ..."
sudo su - root -c "rm -rf /tmp/maps_restore"
echo "Restoration complete."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment