github.com/decred/politeia@v1.4.0/politeiawww/scripts/userdb-cockroachdb-setup.sh (about)

     1  #!/usr/bin/env bash
     2  
     3  # This script sets up the CockroachDB databases for the politeiawww user data
     4  # and assigns user privileges.
     5  # This script requires that you have already created CockroachDB certificates
     6  # using the cockroachcerts.sh script and that you have a CockroachDB instance
     7  # listening on the default port localhost:26257.
     8  
     9  set -ex
    10  
    11  # COCKROACHDB_DIR must be the same directory that was used with the
    12  # cockroachcerts.sh script.
    13  COCKROACHDB_DIR=$1
    14  if [ "${COCKROACHDB_DIR}" == "" ]; then
    15    COCKROACHDB_DIR="${HOME}/.cockroachdb"
    16  fi
    17  
    18  # ROOT_CERTS_DIR must contain client.root.crt, client.root.key, and ca.crt.
    19  readonly ROOT_CERTS_DIR="${COCKROACHDB_DIR}/certs/clients/root"
    20  
    21  if [ ! -f "${ROOT_CERTS_DIR}/client.root.crt" ]; then
    22    >&2 echo "error: file not found ${ROOT_CERTS_DIR}/client.root.crt"
    23    exit
    24  elif [ ! -f "${ROOT_CERTS_DIR}/client.root.key" ]; then
    25    >&2 echo "error: file not found ${ROOT_CERTS_DIR}/client.root.key"
    26    exit
    27  elif [ ! -f "${ROOT_CERTS_DIR}/ca.crt" ]; then
    28    >&2 echo "error: file not found ${ROOT_CERTS_DIR}/ca.crt"
    29    exit
    30  fi
    31  
    32  # Database names
    33  readonly DB_MAINNET="users_mainnet"
    34  readonly DB_TESTNET="users_testnet3"
    35  
    36  # Database usernames
    37  readonly USER_POLITEIAWWW="politeiawww"
    38  
    39  # Create the mainnet and testnet databases for the politeiawww user data.
    40  cockroach sql \
    41    --certs-dir="${ROOT_CERTS_DIR}" \
    42    --execute "CREATE DATABASE IF NOT EXISTS ${DB_MAINNET}"
    43  
    44  cockroach sql \
    45    --certs-dir="${ROOT_CERTS_DIR}" \
    46    --execute "CREATE DATABASE IF NOT EXISTS ${DB_TESTNET}"
    47  
    48  # Create politeiawww user and assign privileges.
    49  cockroach sql \
    50    --certs-dir="${ROOT_CERTS_DIR}" \
    51    --execute "CREATE USER IF NOT EXISTS ${USER_POLITEIAWWW}"
    52  
    53  cockroach sql \
    54    --certs-dir="${ROOT_CERTS_DIR}" \
    55    --execute "GRANT CREATE, SELECT, DROP, INSERT, DELETE, UPDATE \
    56    ON DATABASE ${DB_MAINNET} TO  ${USER_POLITEIAWWW}"
    57  
    58  cockroach sql \
    59    --certs-dir="${ROOT_CERTS_DIR}" \
    60    --execute "GRANT CREATE, SELECT, DROP, INSERT, DELETE, UPDATE \
    61    ON DATABASE ${DB_TESTNET} TO  ${USER_POLITEIAWWW}"