Skip to content

Startup an arangod container with initial data #22

Closed
@m0ppers

Description

@m0ppers

suggestion by @iamacup on the community channel

useful for application bootstrapping (initial data). import data from a mounted directory.

postgres is doing that:

https://github.com/docker-library/postgres/blob/8e867c8ba0fc8fd347e43ae53ddeba8e67242a53/9.5/docker-entrypoint.sh#L79

downside: more complexity in the dockerfile (while it could easily be implemented in a derived docker image)
upside: very convenient and very beginner friendly

needs discussion

#!/bin/bash
set -eo pipefail
​
DISABLE_AUTHENTICATION="true"
​
function rwfail {
  echo "We seem to not have proper rw access to $1. Please make sure that every mounted volume has full rw access for user arangodb ($(id arangodb))"
  exit 55
}
​
# if command starts with an option, prepend arangod
if [ "${1:0:1}" = '-' ]; then
  set -- arangod "$@"
fi
​
​
if [ "$1" = 'arangod' ]; then
  DATADIR=/var/lib/arangodb

        # ensure proper uid and gid (for example when volume is mounted from the outside)
        # do NOT chown or stuff like that. when using shared folders on the mac chown is VERY likely to fail due to docker => vm => host issues
        touch "$DATADIR"/_rwcheck_$HOSTNAME || rwfail $DATADIR
        rm "$DATADIR"/_rwcheck_"$HOSTNAME"
        touch /var/lib/arangodb-apps/_rwcheck_"$HOSTNAME" || rwfail /var/lib/arangodb-apps/
        rm /var/lib/arangodb-apps/_rwcheck_"$HOSTNAME"
  if [ ! -f "$DATADIR"/SERVER ]; then
    if [ -z "$ARANGO_ROOT_PASSWORD" -a -z "$ARANGO_NO_AUTH" -a -z "$ARANGO_RANDOM_ROOT_PASSWORD" ]; then
      echo >&2 'error: database is uninitialized and password option is not specified '
      echo >&2 '  You need to specify one of $ARANGO_ROOT_PASSWORD, $ARANGO_NO_AUTH and $ARANGO_RANDOM_ROOT_PASSWORD'
      exit 1
    fi

                echo "Initializing database...Hang on..."
                if [ ! -z "$ARANGO_RANDOM_ROOT_PASSWORD" ]; then
                  ARANGO_ROOT_PASSWORD=$(pwgen -s -1 16)
                  echo "==========================================="
                  echo "GENERATED ROOT PASSWORD: $ARANGO_ROOT_PASSWORD"
                  echo "==========================================="
                fi

                if [ ! -z "$ARANGO_ROOT_PASSWORD" ]; then
                  (
                    echo "require(\"org/arangodb/users\").replace(\"root\", \"$ARANGO_ROOT_PASSWORD\");"
                  ) |
                  /usr/sbin/arangod --console --log.tty "" &> /dev/null
                  DISABLE_AUTHENTICATION="false"
                fi
                # Initialize if not already done
                /usr/sbin/arangod --console --upgrade
  fi
fi
​
if [ "$1" == "arangod" ]; then
  # if we really want to start arangod and not bash or any other thing
  # prepend --disable-authentication as the FIRST argument
  # (so it is overridable via command line as well)
  shift
  set -- arangod --server.disable-authentication="$DISABLE_AUTHENTICATION" "$@"
fi
​
#this overrides the file here: https://github.com/arangodb/arangodb-docker/blob/official/jessie/2.8.7/docker-entrypoint.sh and is an exact copy apart from this block - Added to load initial data from /opt/arango-bare into the database
( { 
    function runner 
    { 
        echo "Checking to see if db is up..."; 

        STR="use strict; 
        use warnings; 
        use IO::Socket; 

        my \$socket = IO::Socket::INET->new( 
            PeerAddr => \"127.0.0.1:8529\", 
            PeerPort => \"http(8529)\", 
            Proto => \"tcp\") or print \"not ok\"; 
        print \"ok\""; 

        OUTVAR=$(/usr/bin/perl -e "$STR"); 

        if [ "$OUTVAR" == "ok" ]; then 
          echo "Database is up. Running startup scrips." && arangorestore --server.password $ARANGO_ROOT_PASSWORD --server.username 'root' --input-directory '/opt/arango-bare'; 
        else 
          sleep 2 && runner;
        fi 
    }; 

    runner; 
} )&
​
exec "$@"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions