...

Text file src/github.com/google/certificate-transparency-go/scripts/resetctdb.sh

Documentation: github.com/google/certificate-transparency-go/scripts

     1#!/bin/bash
     2
     3set -e
     4
     5usage() {
     6  cat <<EOF
     7$(basename $0) [--force] [--verbose] ...
     8All unrecognised arguments will be passed through to the 'mysql' command.
     9Accepts environment variables:
    10- MYSQL_ROOT_USER: A user with sufficient rights to create/reset the CT
    11  database (default: root).
    12- MYSQL_ROOT_PASSWORD: The password for \$MYSQL_ROOT_USER (default: none).
    13- MYSQL_HOST: The hostname of the MySQL server (default: localhost).
    14- MYSQL_PORT: The port the MySQL server is listening on (default: 3306).
    15- MYSQL_DATABASE: The name to give to the new CT user and database
    16  (default: cttest).
    17- MYSQL_USER: The name to give to the new CT user (default: cttest).
    18- MYSQL_PASSWORD: The password to use for the new CT user
    19  (default: beeblebrox).
    20- MYSQL_USER_HOST: The host that the CT user will connect from; use '%' as
    21  a wildcard (default: localhost).
    22EOF
    23}
    24
    25die() {
    26  echo "$*" > /dev/stderr
    27  exit 1
    28}
    29
    30collect_vars() {
    31  # set unset environment variables to defaults
    32  [ -z ${MYSQL_ROOT_USER+x} ] && MYSQL_ROOT_USER="root"
    33  [ -z ${MYSQL_HOST+x} ] && MYSQL_HOST="localhost"
    34  [ -z ${MYSQL_PORT+x} ] && MYSQL_PORT="3306"
    35  [ -z ${MYSQL_DATABASE+x} ] && MYSQL_DATABASE="cttest"
    36  [ -z ${MYSQL_USER+x} ] && MYSQL_USER="cttest"
    37  [ -z ${MYSQL_PASSWORD+x} ] && MYSQL_PASSWORD="beeblebrox"
    38  [ -z ${MYSQL_USER_HOST+x} ] && MYSQL_USER_HOST="localhost"
    39  FLAGS=()
    40
    41  # handle flags
    42  FORCE=false
    43  VERBOSE=false
    44  while [[ $# -gt 0 ]]; do
    45    case "$1" in
    46      --force) FORCE=true ;;
    47      --verbose) VERBOSE=true ;;
    48      --help) usage; exit ;;
    49      *) FLAGS+=("$1")
    50    esac
    51    shift 1
    52  done
    53
    54  FLAGS+=(-u "${MYSQL_ROOT_USER}")
    55  FLAGS+=(--host "${MYSQL_HOST}")
    56  FLAGS+=(--port "${MYSQL_PORT}")
    57
    58  # Optionally print flags (before appending password)
    59  [[ ${VERBOSE} = 'true' ]] && echo "- Using MySQL Flags: ${FLAGS[@]}"
    60
    61  # append password if supplied
    62  [ -z ${MYSQL_ROOT_PASSWORD+x} ] || FLAGS+=(-p"${MYSQL_ROOT_PASSWORD}")
    63}
    64
    65main() {
    66  collect_vars "$@"
    67
    68  readonly CT_GO_PATH=$(go list -f '{{.Dir}}' github.com/google/certificate-transparency-go)
    69
    70  echo "Warning: about to destroy and reset database '${MYSQL_DATABASE}'"
    71
    72  [[ ${FORCE} = true ]] || read -p "Are you sure? [Y/N]: " -n 1 -r
    73  echo # Print newline following the above prompt
    74
    75  if [ -z ${REPLY+x} ] || [[ $REPLY =~ ^[Yy]$ ]]
    76  then
    77      echo "Resetting DB..."
    78      mysql "${FLAGS[@]}" -e "DROP DATABASE IF EXISTS ${MYSQL_DATABASE};" || \
    79        die "Error: Failed to drop database '${MYSQL_DATABASE}'."
    80      mysql "${FLAGS[@]}" -e "CREATE DATABASE ${MYSQL_DATABASE};" || \
    81        die "Error: Failed to create database '${MYSQL_DATABASE}'."
    82      mysql "${FLAGS[@]}" -e "CREATE USER IF NOT EXISTS ${MYSQL_USER}@'${MYSQL_USER_HOST}' IDENTIFIED BY '${MYSQL_PASSWORD}';" || \
    83        die "Error: Failed to create user '${MYSQL_USER}@${MYSQL_USER_HOST}'."
    84      mysql "${FLAGS[@]}" -e "GRANT ALL ON ${MYSQL_DATABASE}.* TO ${MYSQL_USER}@'${MYSQL_USER_HOST}'" || \
    85        die "Error: Failed to grant '${MYSQL_USER}' user all privileges on '${MYSQL_DATABASE}'."
    86      echo "Reset Complete"
    87  fi
    88}
    89
    90main "$@"

View as plain text