diff --git a/neo4j/.env.template b/neo4j/.env.template new file mode 100644 index 000000000..c58edee0e --- /dev/null +++ b/neo4j/.env.template @@ -0,0 +1,2 @@ +NEO4J_USERNAME=neo4j +NEO4J_PASSWORD=letmein diff --git a/neo4j/.gitignore b/neo4j/.gitignore new file mode 100644 index 000000000..4c49bd78f --- /dev/null +++ b/neo4j/.gitignore @@ -0,0 +1 @@ +.env diff --git a/neo4j/Dockerfile b/neo4j/Dockerfile index e94a89431..79347d937 100644 --- a/neo4j/Dockerfile +++ b/neo4j/Dockerfile @@ -1,3 +1,6 @@ FROM neo4j:3.5.5 RUN wget https://github.com/neo4j-contrib/neo4j-apoc-procedures/releases/download/3.5.0.1/apoc-3.5.0.1-all.jar -P plugins/ -COPY migrate.sh /usr/local/bin/migrate +RUN apk add --no-cache --quiet procps +COPY db_setup.sh /usr/local/bin/db_setup +COPY entrypoint.sh /docker-entrypoint-wrapper.sh +ENTRYPOINT ["/docker-entrypoint-wrapper.sh"] diff --git a/neo4j/db_setup.sh b/neo4j/db_setup.sh new file mode 100755 index 000000000..21ed54571 --- /dev/null +++ b/neo4j/db_setup.sh @@ -0,0 +1,42 @@ +#!/usr/bin/env bash + +ENV_FILE=$(dirname "$0")/.env +[[ -f "$ENV_FILE" ]] && source "$ENV_FILE" + +if [ -z "$NEO4J_USERNAME" ] || [ -z "$NEO4J_PASSWORD" ]; then + echo "Please set NEO4J_USERNAME and NEO4J_PASSWORD environment variables." + echo "Setting up database constraints and indexes will probably fail because of authentication errors." + echo "E.g. you could \`cp .env.template .env\` unless you run the script in a docker container" +fi + +until echo 'RETURN "Connection successful" as info;' | cypher-shell +do + echo "Connecting to neo4j failed, trying again..." + sleep 1 +done + +echo ' +RETURN "Here is a list of indexes and constraints BEFORE THE SETUP:" as info; +CALL db.indexes(); +' | cypher-shell + +echo ' +CALL db.index.fulltext.createNodeIndex("full_text_search",["Post"],["title", "content"]); +CREATE CONSTRAINT ON (p:Post) ASSERT p.id IS UNIQUE; +CREATE CONSTRAINT ON (c:Comment) ASSERT c.id IS UNIQUE; +CREATE CONSTRAINT ON (c:Category) ASSERT c.id IS UNIQUE; +CREATE CONSTRAINT ON (u:User) ASSERT u.id IS UNIQUE; +CREATE CONSTRAINT ON (o:Organization) ASSERT o.id IS UNIQUE; +CREATE CONSTRAINT ON (t:Tag) ASSERT t.id IS UNIQUE; + + +CREATE CONSTRAINT ON (p:Post) ASSERT p.slug IS UNIQUE; +CREATE CONSTRAINT ON (c:Category) ASSERT c.slug IS UNIQUE; +CREATE CONSTRAINT ON (u:User) ASSERT u.slug IS UNIQUE; +CREATE CONSTRAINT ON (o:Organization) ASSERT o.slug IS UNIQUE; +' | cypher-shell + +echo ' +RETURN "Setting up all the indexes and constraints seems to have been successful. Here is a list AFTER THE SETUP:" as info; +CALL db.indexes(); +' | cypher-shell diff --git a/neo4j/entrypoint.sh b/neo4j/entrypoint.sh new file mode 100755 index 000000000..f9c1afbe1 --- /dev/null +++ b/neo4j/entrypoint.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +# credits: https://github.com/javamonkey79 +# https://github.com/neo4j/docker-neo4j/issues/166 + +# turn on bash's job control +set -m + +# Start the primary process and put it in the background +/docker-entrypoint.sh neo4j & + +# Start the helper process +db_setup + +# the my_helper_process might need to know how to wait on the +# primary process to start before it does its work and returns + + +# now we bring the primary process back into the foreground +# and leave it there +fg %1 diff --git a/neo4j/migrate.sh b/neo4j/migrate.sh deleted file mode 100755 index 6f3361b8a..000000000 --- a/neo4j/migrate.sh +++ /dev/null @@ -1,33 +0,0 @@ -#!/usr/bin/env bash - -# If the user has the password `neo4j` this is a strong indicator, that we are -# the initial default user. Before we can create constraints, we have to change -# the default password. This is a security feature of neo4j. -if echo ":exit" | cypher-shell --password neo4j 2> /dev/null ; then - if [[ -z "${NEO4J_PASSWORD}" ]]; then - echo "NEO4J_PASSWORD environment variable is undefined. I cannot set the initial password." - else - echo "CALL dbms.security.changePassword('${NEO4J_PASSWORD}');" | cypher-shell --password neo4j - fi -fi - -set -e - -echo ' -CALL db.index.fulltext.createNodeIndex("full_text_search",["Post"],["title", "content"]); -CREATE CONSTRAINT ON (p:Post) ASSERT p.id IS UNIQUE; -CREATE CONSTRAINT ON (c:Comment) ASSERT c.id IS UNIQUE; -CREATE CONSTRAINT ON (c:Category) ASSERT c.id IS UNIQUE; -CREATE CONSTRAINT ON (u:User) ASSERT u.id IS UNIQUE; -CREATE CONSTRAINT ON (o:Organization) ASSERT o.id IS UNIQUE; -CREATE CONSTRAINT ON (t:Tag) ASSERT t.id IS UNIQUE; - - -CREATE CONSTRAINT ON (p:Post) ASSERT p.slug IS UNIQUE; -CREATE CONSTRAINT ON (c:Category) ASSERT c.slug IS UNIQUE; -CREATE CONSTRAINT ON (u:User) ASSERT u.slug IS UNIQUE; -CREATE CONSTRAINT ON (o:Organization) ASSERT o.slug IS UNIQUE; -' | cypher-shell - -echo "Successfully created all indices and unique constraints:" -echo 'CALL db.indexes();' | cypher-shell