github.com/crowdsecurity/crowdsec@v1.6.1/test/lib/db/instance-postgres (about)

     1  #!/usr/bin/env bash
     2  
     3  set -eu
     4  script_name=$0
     5  DB_BACKEND=$(echo "${script_name}" | cut -d- -f2)
     6  export DB_BACKEND
     7  
     8  die() {
     9      echo >&2 "$@"
    10      exit 1
    11  }
    12  
    13  PGHOST=${PGHOST:-127.0.0.1}
    14  PGPORT=${PGPORT:-5432}
    15  PGPASSWORD=${PGPASSWORD:-postgres}
    16  PGUSER=${PGUSER:-postgres}
    17  export PGHOST
    18  export PGPORT
    19  export PGPASSWORD
    20  export PGUSER
    21  
    22  about() {
    23      die "usage: ${script_name} [ config_yaml | setup | dump <backup_file> | restore <backup_file> ]"
    24  }
    25  
    26  check_requirements() {
    27      if ! command -v psql >/dev/null; then
    28          die "missing required program 'psql' as a postgres client (package postgressql-client on debian like system)"
    29      fi
    30      if ! command -v pg_dump >/dev/null; then
    31          die "missing required program 'pg_dump' (package postgresql-client on debian like system)"
    32      fi
    33      if ! command -v pg_restore >/dev/null; then
    34          die "missing required program 'pg_restore' (package postgresql-client on debian like system)"
    35      fi
    36  }
    37  
    38  exec_sql() {
    39      cmd="${1?Missing required sql command}"
    40      psql <<< "${cmd}"
    41  }
    42  
    43  setup() {
    44      exec_sql "DROP DATABASE IF EXISTS crowdsec_test;"
    45      exec_sql "DROP USER IF EXISTS crowdsec_test;"
    46      exec_sql "CREATE USER crowdsec_test WITH ENCRYPTED PASSWORD 'crowdsec_test';"
    47      exec_sql "CREATE DATABASE crowdsec_test OWNER crowdsec_test;"
    48  }
    49  
    50  dump() {
    51      backup_file="${1?Missing file to backup database to}"
    52      pg_dump -Ft --dbname crowdsec_test --clean --create --file "${backup_file}"
    53  }
    54  
    55  restore() {
    56      backup_file="${1?missing file to restore database from}"
    57      [[ -f "${backup_file}" ]] || die "Backup file ${backup_file} doesn't exist"
    58      pg_restore --dbname crowdsec_test --clean "${backup_file}"
    59  }
    60  
    61  config_yaml() {
    62      yq e '
    63          .db_config.type=strenv(DB_BACKEND)|
    64          .db_config.user="crowdsec_test" |
    65          .db_config.password="crowdsec_test" |
    66          .db_config.db_name="crowdsec_test"  |
    67          .db_config.host=strenv(PGHOST) |
    68          .db_config.port=env(PGPORT) |
    69          .db_config.sslmode="disable" |
    70          del(.db_config.db_path)
    71      ' -i "${CONFIG_YAML}"
    72  }
    73  
    74  [[ $# -lt 1 ]] && about
    75  
    76  check_requirements
    77  
    78  case "$1" in
    79      setup)
    80          setup
    81          ;;
    82      config-yaml)
    83          config_yaml
    84          ;;
    85      dump)
    86          shift
    87          dump "$@"
    88          ;;
    89      restore)
    90          shift
    91          restore "$@"
    92          ;;
    93      exec_sql)
    94          shift
    95          exec_sql "$@"
    96          ;;
    97      *)
    98          about
    99          ;;
   100  esac;