inc.sh 5.04 KB
Newer Older
1 2
#!/bin/bash

3
# set -o errexit tells the shell to exit as soon as a command exits with non-zero status, i.e. fails
Felix's avatar
Felix committed
4
set -o errexit
5

Felix's avatar
Felix committed
6
# :- is an shell operator. If FS_ENV is set and not the empty string, use FS_ENV, otherwise use dev
7 8
export FS_ENV=${FS_ENV:-dev}

Felix's avatar
Felix committed
9
# user identification number of the current user
10 11
export UID

12 13 14
MYSQL_USERNAME=${MYSQL_USERNAME:-root}
MYSQL_PASSWORD=${MYSQL_PASSWORD:-root}

15 16 17 18 19 20 21
# docker-compose arguments:
# -T : do not allocate a TTY: not necessary since we just execute a command
# but need not interactivity
# see: https://docs.docker.com/compose/reference/exec/

# sh -c "..." : run the command "..." in a shell

Felix's avatar
Felix committed
22 23
# BASH_SOURCE is an array with the filenames of the files that were called to get here
# so BASH_SOURCE[0] is the filename (with path) of this file
24
# different to $0 when this file is sourced with "." or source as in many of the scripts
25
dir=$(cd $(dirname ${BASH_SOURCE[0]}) && pwd)
26

27
function log-header() {
28
  # print a log header, take one argument as the printed title
29 30 31 32 33 34 35
  local text=$1;
  echo
  echo "============================================"
  echo "  $text"
  echo "============================================"
}

36 37 38 39
function dc() {
  $dir/docker-compose "$@"
}

40
function sql-query() {
41
  local database=$1 query=$2;
42
  dc exec -T db sh -c "mysql --password=$MYSQL_PASSWORD $database --execute=\"$query\""
43 44 45
}

function sql-file() {
46 47
  local database=$1 filename=$2;
  echo "Executing sql file $FS_ENV/$database $filename"
48
  dc exec -T db sh -c "mysql --password=$MYSQL_PASSWORD $database < /app/$filename"
49 50
}

51
function sql-dump() {
52
  dc exec -T db mysqldump --password=$MYSQL_PASSWORD foodsharing "$@"
53 54
}

55
function exec-in-container() {
56 57
  local container=$1; shift;
  local command=$@;
Felix's avatar
Felix committed
58
  dc exec -T --user $(id -u):$(id -g) $container sh -c "HOME=./ $command"
59 60
}

61 62 63 64 65 66
function exec-in-container-with-image-user() {
  local container=$1; shift;
  local command=$@;
  dc exec -T $container sh -c "HOME=./ $command"
}

67
function run-in-container() {
68 69
  local container=$1; shift;
  local command=$@;
Felix's avatar
Felix committed
70
  dc run --rm --no-deps --user $(id -u):$(id -g) $container sh -c "HOME=./ $command"
71 72
}

73 74 75
function run-in-container-with-service-ports() {
  local container=$1; shift;
  local command=$@;
Felix's avatar
Felix committed
76
  dc run --rm --no-deps --user $(id -u):$(id -g) --service-ports $container sh -c "HOME=./ $command"
77 78
}

79 80 81
function exec-in-container-asroot() {
  local container=$1; shift;
  local command=$@;
82
  dc exec --user root -T $container sh -c "$command"
83 84 85
}

function run-in-container-asroot() {
86 87
  local container=$1; shift;
  local command=$@;
88 89 90 91 92
  # run : create a new container to execute the command
  # --user root : set the user who executes the command
  # --rm : remove the container after executing the command
  # sh -c "..." : what is executed in the container: a shell that
  # interprets "..."
93
  dc run --rm --no-deps --user root $container sh -c "$command"
94 95
}

96
function dropdb() {
97 98 99
  local database=$1;
  echo "Dropping database $FS_ENV/$database"
  sql-query mysql "drop database if exists $database"
100 101 102
}

function createdb() {
103 104
  local database=$1;
  echo "Creating database $FS_ENV/$database"
105 106 107 108
  sql-query mysql "\
    create database if not exists $database; \
    alter database $database character set = utf8mb4 collate = utf8mb4_unicode_ci; \
  "
109 110 111
}

function recreatedb() {
112 113 114
  local database=$1;
  dropdb "$database"
  createdb "$database"
115 116 117
}

function migratedb() {
118 119
  local database=$1;
  echo "Migrating database $FS_ENV/$database"
120 121 122 123 124 125 126 127 128 129 130 131
  dest=migrations/_all.sql
  migration_files="\
      migrations/initial.sql \
      migrations/static.sql \
      migrations/27-profilchange.sql \
      migrations/27-verify.sql \
      migrations/incremental-* \
  "
  echo "" > $dest
  for f in $migration_files; do
    cat $f >> $dest
    echo ';' >> $dest
Matthias Larisch's avatar
Matthias Larisch committed
132
  done
133
  echo "COMMIT;" >> $dest
134 135 136

  # if running in ci we do not have a mounted folder so we need to
  # manually copy the generated migration file into the container
Felix's avatar
Felix committed
137 138 139
  # dc ps = docker container ls: list containers
  # --quiet: only display numeric IDs
  docker cp $dest $(dc ps --quiet db):/app/$dest
140

141
  sql-file $database $dest
142

143 144
  dest=migrations/_reload_data.sql
  echo "set foreign_key_checks=0;" > $dest
145
  for T in `sql-query foodsharing "SHOW TABLES;" | tail -n+2`; do
146
    echo "TRUNCATE TABLE $T;" >> $dest
147
  done
148 149 150
  sql-dump --extended-insert --quick --no-create-info --single-transaction --disable-keys --no-autocommit --skip-add-locks >> $dest
  echo "set foreign_key_checks=1;" >> $dest
  docker cp $dest $(dc ps -q app):/app/$dest
151 152 153 154
}

function purge-db() {
  time sql-file foodsharing migrations/_reload_data.sql
Tilmann Becker's avatar
Tilmann Becker committed
155
}
156 157

function wait-for-mysql() {
158
  exec-in-container-asroot db "while ! mysql --password=$MYSQL_PASSWORD --silent --execute='select 1' >/dev/null 2>&1; do sleep 1; done"
159
}
160

161
function install-chat-dependencies() {
162 163
  # TODO: move this into scripts/mkdirs when MR#97 is merged
  run-in-container-asroot chat \
Felix's avatar
Felix committed
164
    "mkdir --parents node_modules && chown --recursive $(id -u):$(id -g) node_modules"
165 166 167 168

  # have to do run, not exec, as container will not start until
  # node_modules is installed, this will run up a fresh container and
  # just run npm install
169
  run-in-container chat yarn
170
}