Browse Source

Almost done with mongo db init

Jim Barritt 11 months ago
parent
commit
dd2350b376

+ 6 - 1
apps/petitions/decode-app/do.sh

@@ -8,7 +8,7 @@ set -u # unset variables cause an error
8 8
 set -o pipefail # https://coderwall.com/p/fkfaqq/safer-bash-scripts-with-set-euxo-pipefail
9 9
 #set -x # for debugging each command
10 10
 
11
-source ./ops/local/lib.sh
11
+source ops/local/_lib.sh
12 12
 
13 13
 function install {
14 14
     echo -e "'nInstalling 'do' alias so you just have to type 'do ...' instead of './do.sh' ...\n"
@@ -21,8 +21,13 @@ function mongo {
21 21
     ./ops/local/mongo.sh $@
22 22
 }
23 23
 
24
+function decode-app {
25
+    ./ops/local/decode-app.sh $@
26
+}
27
+
24 28
 function status {
25 29
     mongo status
30
+    decode-app status
26 31
 }
27 32
 
28 33
 

+ 3 - 1
apps/petitions/decode-app/ops/local/env.sh

@@ -1,6 +1,8 @@
1 1
 #!/usr/bin/env bash
2 2
 
3
+
4
+
3 5
 APP_NAME="decode/app"
4
-DECODE_APP_HOST="http://localhost:3040"
6
+
5 7
 CLUSTER_NAME="petitions-app"
6 8
 

+ 7 - 0
apps/petitions/decode-app/ops/local/lib.sh

@@ -2,6 +2,7 @@
2 2
 
3 3
 # https://stackoverflow.com/questions/59895/getting-the-source-directory-of-a-bash-script-from-within
4 4
 # https://www.ostricher.com/2014/10/the-right-way-to-get-the-directory-of-a-bash-script/
5
+# http://mywiki.wooledge.org/BashFAQ/031
5 6
 
6 7
 # TODO: make it so you have to prefix the commands with cmd_ or something so not all functions are availbale
7 8
 function lib/process_commands {
@@ -44,3 +45,9 @@ function lib/wait_for_service {
44 45
     sleep 2
45 46
   done
46 47
 }
48
+
49
+function lib/command_exists {
50
+    CMD=$1
51
+    INSTRUCTIONS=$2
52
+    command -v ${CMD} >/dev/null 2>&1 || { echo >&2 -e "\n[ERROR] This script requires the command '${CMD}' but it's not installed. ${INSTRUCTIONS}.  Exiting.\n"; exit 1; }
53
+}

+ 68 - 0
apps/petitions/decode-app/ops/local/decode-app.sh

@@ -0,0 +1,68 @@
1
+#!/usr/bin/env bash
2
+
3
+set -e # Exit script if anything fails
4
+set -u # unset variables cause an error
5
+set -o pipefail # https://coderwall.com/p/fkfaqq/safer-bash-scripts-with-set-euxo-pipefail
6
+#set -x # for debugging each command
7
+
8
+SCRIPT_HOME=$(cd $(dirname ${BASH_SOURCE[0]}) && pwd)
9
+
10
+source ${SCRIPT_HOME}/_env.sh
11
+source ${SCRIPT_HOME}/_lib.sh
12
+
13
+source ${SCRIPT_HOME}/mongo.lib.sh
14
+
15
+DECODE_APP_PORT=3040
16
+DECODE_APP_URL="http://localhost:${DECODE_APP_PORT}"
17
+DECODE_APP_DB_NAME="decode_sample"
18
+
19
+DECODE_APP_LOG_FILE=/var/log/decode-app/system.log
20
+
21
+function check_pre_requisites {
22
+    lib/command_exists sass "Install using 'brew install sass/sass/sass' or visit https://sass-lang.com/install"
23
+
24
+    echo "All pre requisites are accounted for."
25
+}
26
+
27
+
28
+function status {
29
+    
30
+    HTTP_STATUS=$(lib/http_status_of ${DECODE_APP_URL})
31
+
32
+
33
+    echo -e "\nStatus of decode-app @ ${DECODE_APP_URL}: ${HTTP_STATUS}\n"
34
+}
35
+
36
+function logs {
37
+    FOLLOW=${1:-}
38
+    tail -n200 ${FOLLOW} ${DECODE_APP_LOG_FILE}
39
+}
40
+
41
+package() {
42
+  echo -e "\nCompiling and packagaing decode-app using lein...\n"
43
+
44
+  check_pre_requisites
45
+
46
+  lein resource
47
+  lein sass once
48
+  lein ring uberjar
49
+  echo -e "\nPackage Complete and should be in target dir\n"
50
+}
51
+
52
+function init-db {
53
+    echo -e "\nCleaning and initialising a sample database in [${DECODE_APP_DB_NAME}]...\n"
54
+
55
+    echo -e "Mongo Connection: [$(mongo/connection ${DECODE_APP_DB_NAME})]"
56
+
57
+    mongo/drop-db ${DECODE_APP_DB_NAME}
58
+    echo -e "Database is clean."
59
+
60
+    echo -e "\nImporting sample data to database [${DECODE_APP_DB_NAME}]...\n"
61
+    mongo/import ${DECODE_APP_DB_NAME} ${SCRIPT_HOME}/../sample-data/users.json
62
+    mongo/import ${DECODE_APP_DB_NAME} ${SCRIPT_HOME}/../sample-data/petitions.json
63
+
64
+    echo -e "\nDatabase is initialised."
65
+}
66
+
67
+
68
+lib/process_commands $@

+ 56 - 0
apps/petitions/decode-app/ops/local/mongo.lib.sh

@@ -0,0 +1,56 @@
1
+#!/usr/bin/env bash
2
+
3
+SCRIPT_HOME=$(cd $(dirname ${BASH_SOURCE[0]}) && pwd)
4
+
5
+source ${SCRIPT_HOME}/_env.sh
6
+source ${SCRIPT_HOME}/_lib.sh
7
+
8
+DOCKER_COMPOSE_CONFIG=${SCRIPT_HOME}/mongo.docker-compose.yml
9
+
10
+MONGO_HOST="localhost"
11
+MONGO_PORT=27017
12
+MONGO_URL="http://${MONGO_HOST}:${MONGO_PORT}"
13
+
14
+DOCKER_COMMAND="docker-compose -f ${DOCKER_COMPOSE_CONFIG}"
15
+
16
+
17
+function mongo/connection {
18
+    DB_NAME=${1:-}
19
+    echo "mongodb://${MONGO_HOST}:${MONGO_PORT}/${DB_NAME}"
20
+}
21
+
22
+function mongo/eval {
23
+    DB_NAME=$1
24
+    MONGO_CMD=$2
25
+    echo -e "mongo/eval ${MONGO_CMD}"
26
+    mongo $(mongo/connection ${DB_NAME}) --eval ${MONGO_CMD}
27
+}
28
+
29
+function mongo/db-exists? {
30
+    DB_NAME=$1
31
+    MONGO_CMD="db.getMongo().getDBNames().indexOf(\"${DB_NAME}\")"
32
+    echo "$(mongo $(mongo/connection) --eval ${MONGO_CMD} --quiet)"
33
+}
34
+
35
+function mongo/drop-db {
36
+    DB_NAME=$1
37
+
38
+    echo -e "Dropping db [${DB_NAME}]..."
39
+
40
+    if [[ $(mongo/db-exists? ${DB_NAME}) -lt 0 ]]; then
41
+        echo -e "${DB_NAME} does not exist, no need to drop."
42
+    else
43
+        mongo/eval ${DB_NAME} "db.dropDatabase()"
44
+    fi
45
+}
46
+
47
+function mongo/import {
48
+    DB_NAME=$1
49
+    IMPORT_FILE=$2
50
+    echo "Importing to db [${DB_NAME}] from file [${IMPORT_FILE}]..."
51
+# mongoimport --host ${MONGO_HOST} : ${MONGO_PORT} -d ${DECODE_APP_DB_NAME} -c users --jsonArray --file ${SCRIPT_HOME}/../sample-data/users.json
52
+   # mongoimport --host ${MONGO_HOST} : ${MONGO_PORT} -d ${DECODE_APP_DB_NAME} -c petitions --jsonArray --file ${SCRIPT_HOME}/../sample-data/petitions.json
53
+
54
+
55
+
56
+}

+ 5 - 9
apps/petitions/decode-app/ops/local/mongo.sh

@@ -7,15 +7,10 @@ set -o pipefail # https://coderwall.com/p/fkfaqq/safer-bash-scripts-with-set-eux
7 7
 
8 8
 SCRIPT_HOME=$(cd $(dirname ${BASH_SOURCE[0]}) && pwd)
9 9
 
10
-source ${SCRIPT_HOME}/env.sh
11
-source ${SCRIPT_HOME}/lib.sh
10
+source ${SCRIPT_HOME}/_env.sh
11
+source ${SCRIPT_HOME}/_lib.sh
12 12
 
13
-DOCKER_COMPOSE_CONFIG=${SCRIPT_HOME}/mongo.docker-compose.yml
14
-
15
-# File-local config
16
-MONGO_URL="http://localhost:27017"
17
-
18
-DOCKER_COMMAND="docker-compose -f ${DOCKER_COMPOSE_CONFIG}"
13
+source ${SCRIPT_HOME}/mongo.lib.sh
19 14
 
20 15
 function status {
21 16
     echo -e "Status of local mongo docker:\n"
@@ -24,7 +19,6 @@ function status {
24 19
 
25 20
     HTTP_STATUS=$(lib/http_status_of ${MONGO_URL})
26 21
 
27
-
28 22
     echo -e "\nStatus of mongo db @ ${MONGO_URL}: ${HTTP_STATUS}\n"
29 23
 }
30 24
 
@@ -60,4 +54,6 @@ function down {
60 54
     echo -e "\nMongo stopped."
61 55
 }
62 56
 
57
+
63 58
 lib/process_commands $@
59
+

apps/petitions/decode-app/data/petitions.json → apps/petitions/decode-app/ops/sample-data/petitions.json


apps/petitions/decode-app/data/users.json → apps/petitions/decode-app/ops/sample-data/users.json