diff --git a/README.md b/README.md index c6f1786980..8a6974b6d7 100644 --- a/README.md +++ b/README.md @@ -95,8 +95,10 @@ You can then set up the [expected tables](storage/mysql/storage.sql) in a ```bash ./scripts/resetdb.sh -Completely wipe and reset database 'test'. +Warning: about to destroy and reset database 'test' Are you sure? y +> Resetting DB... +> Reset Complete ``` ### Integration Tests diff --git a/scripts/resetdb.sh b/scripts/resetdb.sh index 5de24884ed..350418d03a 100755 --- a/scripts/resetdb.sh +++ b/scripts/resetdb.sh @@ -1,16 +1,59 @@ #!/bin/bash -readonly TRILLIAN_PATH=$(go list -f '{{.Dir}}' github.com/google/trillian) - -echo "Completely wipe and reset database 'test'." -read -p "Are you sure? " -n 1 -r -if [[ $REPLY =~ ^[Yy]$ ]] -then - # User-supplied arguments must be first. This is because some flags, such - # as --defaults-extra-file, must be at the start. - mysql "$@" -u root -e 'DROP DATABASE IF EXISTS test;' - mysql "$@" -u root -e 'CREATE DATABASE test;' - mysql "$@" -u root -e "GRANT ALL ON test.* TO 'test'@'localhost' IDENTIFIED BY 'zaphod';" - mysql "$@" -u root -D test < ${TRILLIAN_PATH}/storage/mysql/storage.sql -fi -echo +set -e + +usage() { + echo "$0 [--force] [--verbose] ..." + echo "accepts environment variables:" + echo " - DB_NAME" + echo " - DB_USER" + echo " - DB_PASSWORD" +} + +collect_vars() { + # set unset environment variables to defaults + [ -z ${DB_USER+x} ] && DB_USER="root" + [ -z ${DB_NAME+x} ] && DB_NAME="test" + # format reused supplied environment variables + FLAGS="" + [ -z ${DB_PASSWORD+x} ] || FLAGS="${FLAGS} -p$DB_PASSWORD" + + # handle flags + FORCE=false + VERBOSE=false + while [[ $# -gt 0 ]]; do + case "$1" in + --force) FORCE=true ;; + --verbose) VERBOSE=true ;; + *) FLAGS="${FLAGS} $1" + esac + shift 1 + done +} + +main() { + collect_vars "$@" + + readonly TRILLIAN_PATH=$(go list -f '{{.Dir}}' github.com/google/trillian) + + # what we're about to do + if [[ ${VERBOSE} = 'true' ]] + then + echo "-- using DB_USER: ${DB_USER}" + fi + echo "Warning: about to destroy and reset database '${DB_NAME}'" + + [[ ${FORCE} = true ]] || read -p "Are you sure? " -n 1 -r + + if [ -z ${REPLY+x} ] || [[ $REPLY =~ ^[Yy]$ ]] + then + echo "Resetting DB..." + mysql $FLAGS -u $DB_USER -e "DROP DATABASE IF EXISTS ${DB_NAME};" + mysql $FLAGS -u $DB_USER -e "CREATE DATABASE ${DB_NAME};" + mysql $FLAGS -u $DB_USER -e "GRANT ALL ON ${DB_NAME}.* TO '${DB_NAME}'@'localhost' IDENTIFIED BY 'zaphod';" + mysql $FLAGS -u $DB_USER -D ${DB_NAME} < ${TRILLIAN_PATH}/storage/mysql/storage.sql + echo "Reset Complete" + fi +} + +main "$@" \ No newline at end of file diff --git a/server/trillian_db_client/Dockerfile b/server/trillian_db_client/Dockerfile new file mode 100644 index 0000000000..b88b36a0f5 --- /dev/null +++ b/server/trillian_db_client/Dockerfile @@ -0,0 +1,15 @@ +FROM golang:1.8 + +RUN apt-get update && \ + apt-get install -y mysql-client + +ADD . /go/src/github.com/google/trillian +WORKDIR /go/src/github.com/google/trillian + +RUN go get -v ./server/trillian_log_server + +ENV DB_USER=test \ + DB_PASSWORD=zaphod \ + DB_DATABASE=test + +CMD [ 'mysql' ] diff --git a/server/trillian_log_server/Dockerfile b/server/trillian_log_server/Dockerfile index 40b7d1dffc..ba84300cd2 100644 --- a/server/trillian_log_server/Dockerfile +++ b/server/trillian_log_server/Dockerfile @@ -1,4 +1,4 @@ -FROM golang +FROM golang:1.8 ENV DB_USER=test \ DB_PASSWORD=zaphod \ @@ -26,4 +26,4 @@ EXPOSE $RPC_PORT EXPOSE $HTTP_PORT HEALTHCHECK --interval=5m --timeout=3s \ - CMD curl -f http://localhost:$HTTP_PORT/debug/vars || exit 1 + CMD curl -f http://localhost:$HTTP_PORT/debug/vars || exit 1 \ No newline at end of file diff --git a/server/trillian_log_signer/Dockerfile b/server/trillian_log_signer/Dockerfile index 7893e5d17d..6c792723bb 100644 --- a/server/trillian_log_signer/Dockerfile +++ b/server/trillian_log_signer/Dockerfile @@ -1,4 +1,4 @@ -FROM golang +FROM golang:1.8 ENV DB_USER=test \ DB_PASSWORD=zaphod \ @@ -11,7 +11,6 @@ ENV HOST=0.0.0.0 \ ENV SEQUENCER_GUARD_WINDOW=0s \ FORCE_MASTER=true - ADD . /go/src/github.com/google/trillian WORKDIR /go/src/github.com/google/trillian @@ -28,4 +27,4 @@ ENTRYPOINT /go/bin/trillian_log_signer \ EXPOSE $HTTP_PORT HEALTHCHECK --interval=5m --timeout=3s \ - CMD curl -f http://localhost:$HTTP_PORT/debug/vars || exit 1 + CMD curl -f http://localhost:$HTTP_PORT/debug/vars || exit 1 \ No newline at end of file