-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo-docker.sh
executable file
·163 lines (123 loc) · 4.71 KB
/
mongo-docker.sh
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
#!/bin/bash
RED='\033[0;31m'
GREEN='\033[0;32m'
PURPLE="\033[0;35m"
LIGHTBLUE="\033[0;36m"
LIGHTBLUEB="\033[1;36m"
YELLOWB="\033[1;33m"
NC='\033[0m' # No Color
PASSWORD=""
VOLUMEPATH=""
DIRECTORY=""
usage() {
printf "${PURPLE}MongoDB Docker Deployment${NC}\n\n"
printf "The script will create a docker container of MongoDB.\n\nThe path that you pass should be an absolute path.\n\n"
printf "${YELLOWB}NOTE - Docker changes ownership of your data folder to root. \nIf you need to run the cleanup script, run this script with${NC} ${LIGHTBLUEB}sudo${NC}\n\n"
printf "Options:\n\n"
echo "-o i | -o install ] - Create a docker container & volume to attach to"
echo "-o c | -o cleanup ] - Clean up installation, including docker image"
printf "\nArguments (install):\n\n"
echo "-p - Add a user assigned password for your DB"
echo "-d - Add a path to the directory that you want to create the mongo-data folder in"
printf "\nArguments (cleanup):\n\n"
echo "-d - Add the path of your mongo-data directory that you want the script to delete"
exit 1;
}
# Done
function folderCheck() {
if [ -d "${VOLUMEPATH}/mongo-data" ];
then
printf "Directory mongo-data exists. Assigning as volume to docker image.\n"
else
echo "Error: Directory mongo-data was not found. Creating directory now. ${NC}"
mkdir "${VOLUMEPATH}"/mongo-data
fi
}
function dockerContainerSetup() {
echo -e "${GREEN}########################################################${NC}"
echo -e "${GREEN}############# PULLING MONGODB DOCKER IMAGE #############${NC}"
echo -e "${GREEN}########################################################${NC}\n"
docker pull mongo
echo -e "${GREEN}############################################################${NC}"
echo -e "${GREEN}############# STARTING MONGODB DOCKER CONTAINER ############${NC}"
echo -e "${GREEN}############################################################${NC}\n"
docker run -it -v "${VOLUMEPATH}"/mongo-data:/data/db --name mongo -d -p 27017:27017 mongo mongod --replSet my-mongo-set
}
function dockerExecCommands() {
isDockerRunning=$(docker inspect -f "{{.State.Running}}" mongo)
sleep 1
if [ "$isDockerRunning" = true ];
then
docker exec -it mongo mongo --eval "rs.initiate()"
sleep 1
docker exec -it mongo mongo --eval "cdr = db.getSiblingDB('cdr');cdr.createUser({ user: 'mongo', pwd: '$PASSWORD', roles: [ { role: 'readWrite', db: 'cdr' } ]})"
echo -e "${LIGHTBLUEB}Your connection url is${YELLOWB} mongodb://localhost:27017/cdr${NC}. Your username is${LIGHTBLUEB} mongo${NC} & your password is ${LIGHTBLUEB}$PASSWORD${NC} "
else
printf "${RED}There was an error starting mongo. Check the logs or error output from Docker."
fi
}
function checkLastCharacterOfVolumePath() {
if [[ -n "$VOLUMEPATH" ]]; then
lastCharacter=${VOLUMEPATH: -1}
if [[ $lastCharacter = "/" ]]; then
cleanedText=${VOLUMEPATH%/*}
VOLUMEPATH=$cleanedText
fi
fi
}
function cleanup() {
printf "Running cleanup\n"
echo ""
docker stop mongo
echo ""
docker rm mongo
echo ""
docker rmi mongo
echo ""
if [ -d "${VOLUMEPATH}/mongo-data" ];
then
echo "Directory mongo-data exists. Removing."
rm -rf "${VOLUMEPATH}/mongo-data"
else
echo "Directory mongo-data was not found. No further action required."
fi
echo "done."
}
while getopts o:p:d: flag
do
case "${flag}" in
o) OPTION=${OPTARG};;
p) ARGUMENT=${OPTARG};;
d) DIRECTORY=${OPTARG};;
# *) usage;;
esac
done
if [[ -z "$OPTION" && -z "$DIRECTORY" ]]; then
echo "No directory or option was given"
exit 1;
fi
if [[ $OPTION =~ ^i(nstall)?$ ]];then
if [[ -z $ARGUMENT ]]; then
printf "${RED}No password was given. The format should resemble the following: ./mongo-docker.sh -o install -p mypassword -d /home/user \nExiting.\n"
exit 1;
elif [[ -z $DIRECTORY ]]; then
printf "${RED}No directory was given. The format should resemble the following: ./mongo-docker.sh -o install -p mypassword -d /home/user \nExiting.\n"
exit 1;
fi
fi
if [[ $OPTION =~ ^i(nstall)?$ ]] && [[ -z "$ARGUMENT" ]]; then
echo "${RED}Missing arguments. Exiting."
exit 1;
fi
if [[ -n "$OPTION" ]]; then
VOLUMEPATH=$DIRECTORY
checkLastCharacterOfVolumePath
if [[ $OPTION =~ ^i(nstall)?$ ]] && [[ -n "$ARGUMENT" ]]; then
folderCheck
dockerContainerSetup
PASSWORD=$ARGUMENT
dockerExecCommands
elif [[ $OPTION =~ ^c(leanup)?$ ]] && [[ -n "$DIRECTORY" ]]; then
cleanup
fi
fi