Browse Source

Refactoring go.sh part 1 mongo

Jim Barritt 11 months ago
parent
commit
04f81baf4e
1 changed files with 95 additions and 40 deletions
  1. 95 40
      apps/petitions/decode-app/go.sh

+ 95 - 40
apps/petitions/decode-app/go.sh

@@ -1,6 +1,8 @@
1 1
 #!/bin/sh
2
-# Exit if any command exits with non-zero status
3
-set -e
2
+set -e # Exit script if anything fails
3
+set -u # unset variables cause an error
4
+set -o pipefail # https://coderwall.com/p/fkfaqq/safer-bash-scripts-with-set-euxo-pipefail
5
+#set -x # for debugging each command
4 6
 
5 7
 ECS_REGISTRY="987195267860.dkr.ecr.eu-west-1.amazonaws.com"
6 8
 APP_NAME="decode/app"
@@ -8,18 +10,84 @@ MONGO_HOST="http://localhost:27017"
8 10
 DECODE_APP_HOST="http://localhost:3040"
9 11
 CLUSTER_NAME="petitions-app"
10 12
 
13
+# https://stackoverflow.com/questions/11231937/bash-ignoring-error-for-a-particular-command
14
+# -o makes the output disappear, -s means silent so it doesnt show you what download status
15
+http_status_of() {
16
+    URL=$1
17
+    HTTP_STATUS=$(curl -s -o /dev/null -w "%{http_code}\n" ${URL} || true)
18
+    if [[ ${HTTP_STATUS} == 200 ]]; then
19
+        echo "Running (200 ok)"
20
+        break;
21
+    else
22
+        echo "Not running (${HTTP_STATUS})"
23
+        break;
24
+    fi
25
+}
26
+
27
+
28
+status() {
29
+    echo "Status of mongo docker:\n"
30
+    docker-compose -f docker-compose.mongo.yml ps
31
+    echo "\n"
32
+
33
+    HTTP_STATUS=$(http_status_of ${MONGO_HOST})
34
+
35
+    echo "Status of mongo db @ ${MONGO_HOST}: ${HTTP_STATUS}\n"
36
+}
37
+
38
+wait_for_service() {
39
+  HOST=$1
40
+  ATTEMPTS=0
41
+  echo "\nWaiting for service to appear @ ${HOST} ...\n"
42
+  while :
43
+  do
44
+    echo "Trying to reach ${HOST} [${ATTEMPTS}] ..."
45
+    HTTP_STATUS=$(http_status_of ${HOST})
46
+    if [[ ${HTTP_STATUS} == "Running (200 ok)" ]]; then
47
+      echo "Service @ ${HOST} is available"
48
+      break;
49
+    fi
50
+    ((ATTEMPTS++))
51
+    if [[ ${ATTEMPTS} == 15 ]]; then
52
+      echo "The service ${HOST} is still not available after 15 retries"
53
+      echo "Exiting"
54
+      exit 1
55
+    fi
56
+    sleep 2
57
+  done
58
+}
59
+
11 60
 start_mongo() {
12
-  exec `docker-compose --verbose -f docker-compose.mongo.yml up -d`
61
+    echo "\nStarting docker container with mongo (see docker.log for details)\n"
62
+    docker-compose -f docker-compose.mongo.yml up -d
63
+    wait_for_service $MONGO_HOST
13 64
 }
14 65
 
66
+
15 67
 stop_mongo() {
16
-  mongo decode-dev --eval "db.dropDatabase()"
17
-  exec `docker-compose -f docker-compose.mongo.yml down`
68
+    echo "Stopping mongo docker...\n"
69
+    docker-compose -f docker-compose.mongo.yml down
70
+    echo "\nMongo stopped."
18 71
 }
19 72
 
20
-populate_mongo() {
21
-  mongoimport --host $DB_HOST:27017 -d $DB_NAME -c users --jsonArray --file data/users.json
22
-  mongoimport --host $DB_HOST:27017 -d $DB_NAME -c petitions --jsonArray --file data/petitions.json
73
+
74
+init_db() {
75
+    DB_NAME="decode-dev"
76
+    DB_HOST="localhost"
77
+
78
+    echo "Clearing existing data...\n"
79
+    mongo ${DB_NAME} --eval "db.dropDatabase()"
80
+    echo "\nDatabase is clean."
81
+
82
+    echo "\nInitialising database ${DB_NAME} with static data ...\n"
83
+    mongoimport --host $DB_HOST:27017 -d $DB_NAME -c users --jsonArray --file data/users.json
84
+    mongoimport --host $DB_HOST:27017 -d $DB_NAME -c petitions --jsonArray --file data/petitions.json
85
+    echo "\ndatabase is initialised."
86
+}
87
+
88
+drop_db() {
89
+  echo "Dropping database"
90
+
23 91
 }
24 92
 
25 93
 prepare_decode_app_artefact() {
@@ -28,16 +96,16 @@ prepare_decode_app_artefact() {
28 96
   lein ring uberjar
29 97
 }
30 98
 
99
+
100
+
101
+
102
+
31 103
 start() {
32 104
   case ${ENVIRONMENT} in
33 105
     dev)
34
-      start_mongo
106
+
35 107
       prepare_decode_app_artefact
36
-      SERVICE=mongo
37
-      wait_for_service
38
-      DB_NAME="decode-dev"
39
-      DB_HOST="localhost"
40
-      populate_mongo
108
+
41 109
       ENV=development java -jar target/decode-app-0.1.0-SNAPSHOT-standalone.jar &
42 110
       SERVICE=decode_app
43 111
       wait_for_service
@@ -58,30 +126,6 @@ stop() {
58 126
   exec `ps aux | grep "[t]arget/decode-app-0.1.0-SNAPSHOT-standalone" | awk '{printf $2}' | xargs kill -9`
59 127
 }
60 128
 
61
-wait_for_service() {
62
-  case "$SERVICE" in
63
-    mongo) HOST=${MONGO_HOST} ;;
64
-    decode_app) HOST=${DECODE_APP_HOST} ;;
65
-  esac
66
-  ATTEMPTS=0
67
-  while :
68
-  do
69
-    status=$(curl -w "%{http_code}\n" ${HOST} -o /dev/null)
70
-    if [ ${status} == 200 ]; then
71
-      echo "Service ${SERVICE} is running fine"
72
-      break;
73
-    fi
74
-    ((ATTEMPTS++))
75
-    if [ ${ATTEMPTS} == 15 ]; then
76
-      echo "The service ${SERVICE} is still not available"
77
-      echo "Cleaning up"
78
-      stop_mongo
79
-      echo "Exiting"
80
-      exit 1
81
-    fi
82
-    sleep 2
83
-  done
84
-}
85 129
 
86 130
 unit() {
87 131
   lein resource
@@ -240,16 +284,27 @@ usage() {
240 284
   echo "cluster <create|delete>   : creates or deletes the cluster"
241 285
   echo "populate-db <DNS address> : populates the db in production"
242 286
 }
243
-
287
+ENVIRONMENT=${2:-dev}
244 288
 OPTION="$1"
245 289
   case $OPTION in
290
+    start-db)
291
+      start_mongo
292
+      ;;
293
+    stop-db)
294
+      stop_mongo
295
+      ;;
296
+    init-db)
297
+      init_db
298
+      ;;
246 299
     start)
247
-      ENVIRONMENT=$2
248 300
       start
249 301
       ;;
250 302
     stop)
251 303
       stop
252 304
       ;;
305
+    status)
306
+      status
307
+      ;;
253 308
     unit-test)
254 309
       set -e
255 310
       unit