summaryrefslogtreecommitdiff
path: root/docker/demo/images/merchant/startup.sh
blob: feaca16e8912401770f153d4b38e275e4cc2020f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/bin/bash

set -o pipefail
set -eu

export LD_LIBRARY_PATH=/usr/local/lib
export GNUNET_FORCE_LOG=";;;;WARNING"

# Values from config file mounted at run time:
CURRENCY=`taler-config -c /config/deployment.conf -s taler-deployment -o currency`
BACKEND_APIKEY=`taler-config -c /config/deployment.conf -s taler-deployment -o merchant-apikey`
BACKEND_URL=`taler-config -c /config/deployment.conf -s taler-deployment -o merchant-url`
SYNC_URL=`taler-config -c /config/deployment.conf -s taler-deployment -o sync-url`
EXCHANGE_URL=`taler-config -c /config/deployment.conf -s taler-deployment -o default-exchange`
DB_PASSWORD=`taler-config -c /config/deployment.conf -s taler-deployment -o db-password`

BLOG_IBAN=DE940993
POS_IBAN=DE445094
GNUNET_IBAN=DE463312
DEFAULT_IBAN=DE474361
TOR_IBAN=DE358263
TALER_IBAN=DE102893
SURVEY_IBAN=DE731371

while ! pg_isready -h talerdb -d taler; do
  echo DB not ready yet.
  sleep 2
done
echo Now DB is ready.

# FIXME: wallets external to the containers put localhost'ed
# exchanges along a /pay request.  That breaks here, since the
# exchange listens from another container.  The following
# command routes every request to 5555 (port on the host
# system that points to a contained exchange AND where the
# merchant tries to /deposit), to the container where the exchange listens.
socat TCP-LISTEN:5555,fork,reuseaddr TCP:exchange:80 &

# FIXME: browsers can only get redirected to merchant backends
# as they appear outside of the container (port 5556).  OTOH,
# merchant frontends can only talk to backends as they appear
# _inside_ the container (port 80).  Config, ultimately, must
# specify backends as they appear outside, otherwise frontends
# would redirect browsers with in-container addresses, that
# would make the backend not reached.  The following redirection
# allows to bridge the external merchant port to the internal,
# to make frontends reach the backend.
socat TCP-LISTEN:5556,fork,reuseaddr TCP:localhost:80 &

# sync HTTPD redirect:
socat TCP-LISTEN:5563,fork,reuseaddr TCP:localhost:8080 &

# $2 might have Authorization header.
is_serving () {
set +u # tolerate missing $2
echo Checking $1
for n in `seq 1 50`
  do
    echo "."
    sleep 0.5
    OK=1
    # auth case.
    if test -n "$2"; then
      wget --header "$2" $1 -t 1 -o /dev/null -O /dev/null >/dev/null && break
    else
      wget $1 -t 1 -o /dev/null -O /dev/null >/dev/null && break
    fi
    OK=0
  done
  if [ 1 != $OK ]
  then
      echo "ERROR: $1 unreachable."
      exit 1
  fi
  echo Now available: $1
  set -u
}

is_serving ${EXCHANGE_URL}

EXCHANGE_MASTER_PUB=$(curl -s ${EXCHANGE_URL}keys | jq -r .master_public_key)
echo Found Exchange Pub: $EXCHANGE_MASTER_PUB
sed -i "s;__EXCHANGE_URL__;${EXCHANGE_URL};" /config/taler.conf
sed -i "s/__EXCHANGE_PUB__/${EXCHANGE_MASTER_PUB}/" /config/taler.conf
sed -i "s/__CURRENCY__/${CURRENCY}/" /config/taler.conf
sed -i "s/__BACKEND_APIKEY__/${BACKEND_APIKEY}/" /config/taler.conf
sed -i "s;__BACKEND_URL__;${BACKEND_URL};" /config/taler.conf
sed -i "s;__DB_PASSWORD__;${DB_PASSWORD};" /config/taler.conf

echo "Init database... "
taler-merchant-dbinit -L WARNING -c /config/taler.conf
echo DONE
echo -n "Launch merchant backend..."
taler-merchant-httpd -L WARNING -a $BACKEND_APIKEY -c /config/taler.conf 2>&1 | \
  rotatelogs -e /logs/taler-merchant-httpd-%Y-%m-%d.log 86400 &
echo DONE
sleep 1

is_serving "${BACKEND_URL}config"

# If the witness instance exists or has wrong auth,
# then all the others do.
echo -n "Checking instances existence..."
INSTANCES_STATUS=$(curl -s -o /dev/null \
  -w "%{http_code}" \
  -H "Authorization: Bearer $BACKEND_APIKEY" \
  "${BACKEND_URL}instances/Taler/private")
echo "DONE ($INSTANCES_STATUS)"

case $INSTANCES_STATUS in
  "404")
    echo "Taler (witness) instance not found, assuming none is."
    source /create_instances.sh;
    ;;
  "401")
    echo "Taler (witness) instance had wrong auth, assuming API key is new."
    source /update_instances_auth.sh;
    ;;
  *)
    echo "Taler (witness) instance found, API key correct, do nothing."
    ;;
esac
export TALER_ENV_URL_MERCHANT_BLOG=`taler-config -c /config/deployment.conf -s taler-deployment -o blog-url`
export TALER_ENV_URL_MERCHANT_DONATIONS=`taler-config -c /config/deployment.conf -s taler-deployment -o donations-url`
export TALER_ENV_URL_MERCHANT_SURVEY=`taler-config -c /config/deployment.conf -s taler-deployment -o survey-url`
export TALER_ENV_URL_INTRO=`taler-config -c /config/deployment.conf -s taler-deployment -o landing-url`
export TALER_ENV_URL_BANK=`taler-config -c /config/deployment.conf -s taler-deployment -o bank-url`

echo -n "Launch blog..."
${HOME}/.local/bin/taler-merchant-demos -c /config/taler.conf --http-port 8080 blog 2>&1 | rotatelogs -e /logs/blog-%Y-%m-%d.log 86400 &
echo DONE
echo -n "Launch donations..."
${HOME}/.local/bin/taler-merchant-demos -c /config/taler.conf --http-port 8081 donations 2>&1 | rotatelogs -e /logs/donations-%Y-%m-%d.log 86400 &
echo DONE
echo -n "Launch Survey..."
${HOME}/.local/bin/taler-merchant-demos -c /config/taler.conf --http-port 8082 survey 2>&1 | rotatelogs -e /logs/survey-%Y-%m-%d.log 86400 &
echo DONE
echo -n "Launch Landing..."
${HOME}/.local/bin/taler-merchant-demos -c /config/taler.conf --http-port 8083 landing 2>&1 | rotatelogs -e /logs/landing-%Y-%m-%d.log 86400 &
echo DONE

echo -n Creating a reserve for tips...
PAYTO_RESERVE=$(
  taler-merchant-setup-reserve \
    --amount ${CURRENCY}:20 \
    --exchange-url ${EXCHANGE_URL} \
    --merchant-url http://localhost/instances/survey/ \
    --apikey "Bearer ${BACKEND_APIKEY}" \
    --wire-method iban
)

SANDBOX_URL="http://bank:15000"
is_serving "${SANDBOX_URL}/demobanks/default/integration-api/config"
SURVEY_USERNAME=`taler-config -c /config/deployment.conf -s taler-deployment -o survey-sandbox-username`
SURVEY_PASSWORD=`taler-config -c /config/deployment.conf -s taler-deployment -o survey-sandbox-password`
# Check/wait that the Survey site got its bank account.
curl "${SANDBOX_URL}/demobanks/default/access-api/public-accounts"
is_serving "${SANDBOX_URL}/demobanks/default/access-api/accounts/${SURVEY_USERNAME}" \
  "Authorization: Basic $(echo -n $SURVEY_USERNAME:$SURVEY_PASSWORD | base64)"
export LIBEUFIN_SANDBOX_USERNAME=${SURVEY_USERNAME}
export LIBEUFIN_SANDBOX_PASSWORD=${SURVEY_PASSWORD}
libeufin-cli sandbox \
  --sandbox-url ${SANDBOX_URL} \
  demobank new-transaction --bank-account ${LIBEUFIN_SANDBOX_USERNAME} \
  --payto-with-subject ${PAYTO_RESERVE} --amount 20
unset LIBEUFIN_SANDBOX_USERNAME
unset LIBEUFIN_SANDBOX_PASSWORD
echo DONE

echo -n "Init sync database..."
sync-dbinit -L WARNING -c /config/taler.conf
echo DONE

echo -n "Launching sync..."
sync-httpd -L WARNING -c /config/taler.conf 2>&1 | \
  rotatelogs -e /logs/sync-httpd-%Y-%m-%d.log 86400 &
echo DONE

is_serving $SYNC_URL

wait -n