Skip to content

Commit

Permalink
Support json on mongodb
Browse files Browse the repository at this point in the history
  • Loading branch information
yuki777 committed May 6, 2023
1 parent 8b99595 commit e33270e
Show file tree
Hide file tree
Showing 7 changed files with 270 additions and 25 deletions.
28 changes: 25 additions & 3 deletions mongodb/create-start.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
#!/bin/bash

currentDir="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
# Get format option
format=""
while getopts ":f:" opt; do
case ${opt} in
f)
format="$OPTARG"
;;
\?)
echo "Invalid option: -$OPTARG" 1>&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." 1>&2
exit 1
;;
esac
done
shift $((OPTIND - 1))

currentDir="$(
cd "$(dirname "$0")" >/dev/null 2>&1
pwd -P
)"
cd $currentDir
./create.sh $1 $2 $3
./create.sh -f "$format" $1 $2 $3 > /dev/null

set -eu
./start.sh $1
./start.sh -f "$format" $1
56 changes: 48 additions & 8 deletions mongodb/create.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
#!/bin/bash
set -eu

# Get format option
format=""
while getopts ":f:" opt; do
case ${opt} in
f)
format="$OPTARG"
;;
\?)
echo "Invalid option: -$OPTARG" 1>&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." 1>&2
exit 1
;;
esac
done
shift $((OPTIND - 1))

currentDir="$(
cd "$(dirname "$0")" >/dev/null 2>&1
pwd -P
Expand All @@ -9,10 +28,10 @@ cd $currentDir

if [ $# -eq 0 ]; then
cat <<_EOT_
# usage : $currentDir/create.sh {Name} {MongodbVersion} {Port}
# e.g. : $currentDir/create.sh mongo4-foo 4.4.10 27017
# e.g. : $currentDir/create.sh mongo5-bar 5.0.3 37017
# e.g. : $currentDir/create.sh mongo5-baz 5.0.3 random
usage : $currentDir/create.sh {Name} {MongodbVersion} {Port}
e.g. : $currentDir/create.sh mongo4-foo 4.4.10 27017
e.g. : $currentDir/create.sh mongo5-bar 5.0.3 37017
e.g. : $currentDir/create.sh mongo5-baz 5.0.3 random
_EOT_
exit 1
fi
Expand All @@ -37,10 +56,31 @@ mkdir -p $dir/datadir/$optName
extractFile $dir $optFileName

echo $optPort >$dir/datadir/$optName/mongodb.port.init
echo "#mongod.conf" > $dir/datadir/$optName/mongod.conf

echo "#mongod.conf" >$dir/datadir/$optName/mongod.conf
echo "mongod.conf is here. $dir/datadir/$optName/mongod.conf"

echo MongoDB Successfully created. $optName $optVersion $optPort
cd $currentDir
getCommands $optName $optVersion $optPort
commands=$(getCommands $optName $optVersion $optPort $format)

normalOutputs=""
normalOutputs="${normalOutputs}mongod.conf is here. $dir/datadir/$optName/mongod.conf\n"
normalOutputs="${normalOutputs}MongoDB Successfully created. $optName $optVersion $optPort\n"
normalOutputs="${normalOutputs}$commands\n"

jsonOutputs=""
jsonOutputs="$jsonOutputs{
\"message\": \"MongoDB Successfully created.\",
\"name\": \"$optName\",
\"type\": \"mongodb\",
\"version\": \"$optVersion\",
\"port\": \"$optPort\",
\"dataDir\": \"$dir/datadir/$optName\",
\"confPath\": \"$dir/datadir/$optName/mongod.conf\"
}"

# Output
if [ "$format" = "json" ]; then
echo -e "${jsonOutputs}"
else
echo -e "${normalOutputs}"
fi
48 changes: 45 additions & 3 deletions mongodb/delete.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
#!/bin/bash

currentDir="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
# Get format option
format=""
while getopts ":f:" opt; do
case ${opt} in
f)
format="$OPTARG"
;;
\?)
echo "Invalid option: -$OPTARG" 1>&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." 1>&2
exit 1
;;
esac
done
shift $((OPTIND - 1))

currentDir="$(
cd "$(dirname "$0")" >/dev/null 2>&1
pwd -P
)"
cd $currentDir
. functions.sh

Expand All @@ -14,11 +36,31 @@ optPort=$(getPortByName "$optName" "$optVersion")

dir=$currentDir/versions/$optVersion

./stop.sh $optName $optVersion $optPort
./stop.sh -f "$format" $optName $optVersion $optPort > /dev/null
sleep 1

set -eu
exitIfNotExistDir $dir/datadir/$optName
exitIfRunningPort $optPort
[ -d "$dir/datadir/$optName" ] && rm -fr $dir/datadir/$optName
echo MongoDB Successfully deleted. $optName $optVersion $optPort

normalOutputs=""
normalOutputs="${normalOutputs}MongoDB Successfully deleted. $optName $optVersion $optPort"

jsonOutputs=""
jsonOutputs="$jsonOutputs{
\"message\": \"MongoDB Successfully deleted.\",
\"name\": \"$optName\",
\"type\": \"mongodb\",
\"version\": \"$optVersion\",
\"port\": \"$optPort\",
\"dataDir\": \"$dir/datadir/$optName\",
\"confPath\": \"$dir/datadir/$optName/mongod.conf\"
}"

# Output
if [ "$format" = "json" ]; then
echo -e "${jsonOutputs}"
else
echo -e "${normalOutputs}"
fi
39 changes: 37 additions & 2 deletions mongodb/port.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
#!/bin/bash
set -eu

currentDir="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
# Get format option
format=""
while getopts ":f:" opt; do
case ${opt} in
f)
format="$OPTARG"
;;
\?)
echo "Invalid option: -$OPTARG" 1>&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." 1>&2
exit 1
;;
esac
done
shift $((OPTIND - 1))

currentDir="$(
cd "$(dirname "$0")" >/dev/null 2>&1
pwd -P
)"
cd $currentDir
. functions.sh

Expand All @@ -21,4 +43,17 @@ optVersion=$(getVersionByName "$optName")
exitIfNotExistPortFile "$optName" "$optVersion"
optPort=$(getPortByName "$optName" "$optVersion")

echo "$optPort"
normalOutputs=""
normalOutputs="${normalOutputs}$optPort"

jsonOutputs=""
jsonOutputs="$jsonOutputs{
\"port\": \"$optPort\"
}"

# Output
if [ "$format" = "json" ]; then
echo -e "${jsonOutputs}"
else
echo -e "${normalOutputs}"
fi
28 changes: 25 additions & 3 deletions mongodb/restart.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
#!/bin/bash

currentDir="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
# Get format option
format=""
while getopts ":f:" opt; do
case ${opt} in
f)
format="$OPTARG"
;;
\?)
echo "Invalid option: -$OPTARG" 1>&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." 1>&2
exit 1
;;
esac
done
shift $((OPTIND - 1))

currentDir="$(
cd "$(dirname "$0")" >/dev/null 2>&1
pwd -P
)"
cd $currentDir
./stop.sh $1
./stop.sh -f "$format" $1 > /dev/null

sleep 1

set -eu
./start.sh $1
./start.sh -f "$format" $1
50 changes: 46 additions & 4 deletions mongodb/start.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
#!/bin/bash
set -eu

currentDir="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
# Get format option
format=""
while getopts ":f:" opt; do
case ${opt} in
f)
format="$OPTARG"
;;
\?)
echo "Invalid option: -$OPTARG" 1>&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." 1>&2
exit 1
;;
esac
done
shift $((OPTIND - 1))

currentDir="$(
cd "$(dirname "$0")" >/dev/null 2>&1
pwd -P
)"
cd $currentDir
. functions.sh

Expand All @@ -25,7 +47,27 @@ $dir/basedir/bin/mongod \
--logpath $dir/datadir/$optName/mongodb.log \
--pidfilepath $dir/datadir/$optName/mongodb.pid \
--port $optPort \
--fork
--fork 1>&2
echo $optPort > $dir/datadir/$optName/mongodb.port
echo "Your config file is located $dir/datadir/$optName/mongod.conf"
echo MongoDB Successfully started. $optName $optVersion $optPort

normalOutputs=""
normalOutputs="${normalOutputs}MongoDB Successfully started. $optName $optVersion $optPort\n"
normalOutputs="${normalOutputs}Your config file is located $dir/datadir/$optName/mongodb.conf"

jsonOutputs=""
jsonOutputs="$jsonOutputs{
\"message\": \"MongoDB Successfully started.\",
\"name\": \"$optName\",
\"type\": \"mongodb\",
\"version\": \"$optVersion\",
\"port\": \"$optPort\",
\"dataDir\": \"$dir/datadir/$optName\",
\"confPath\": \"$dir/datadir/$optName/mongod.conf\"
}"

# Output
if [ "$format" = "json" ]; then
echo -e "${jsonOutputs}"
else
echo -e "${normalOutputs}"
fi
46 changes: 44 additions & 2 deletions mongodb/stop.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
#!/bin/bash
set -eu

currentDir="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
# Get format option
format=""
while getopts ":f:" opt; do
case ${opt} in
f)
format="$OPTARG"
;;
\?)
echo "Invalid option: -$OPTARG" 1>&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." 1>&2
exit 1
;;
esac
done
shift $((OPTIND - 1))

currentDir="$(
cd "$(dirname "$0")" >/dev/null 2>&1
pwd -P
)"
cd $currentDir
. functions.sh

Expand All @@ -20,4 +42,24 @@ exitIfNotRunningPort $optPort
[ -f "$dir/datadir/$optName/mongodb.pid" ] && kill $(cat $dir/datadir/$optName/mongodb.pid) && rm -f $dir/datadir/$optName/mongodb.pid
[ -f "$dir/datadir/$optName/mongodb.port" ] && cp $dir/datadir/$optName/mongodb.port $dir/datadir/$optName/mongodb.port.last
[ -f "$dir/datadir/$optName/mongodb.port" ] && rm -f $dir/datadir/$optName/mongodb.port
echo MongoDB Successfully stopped. $optName $optVersion $optPort

normalOutputs=""
normalOutputs="${normalOutputs}MongoDB Successfully stopped. $optName $optVersion $optPort"

jsonOutputs=""
jsonOutputs="$jsonOutputs{
\"message\": \"MongoDB Successfully stopped.\",
\"name\": \"$optName\",
\"type\": \"mongodb\",
\"version\": \"$optVersion\",
\"port\": \"$optPort\",
\"dataDir\": \"$dir/datadir/$optName\",
\"confPath\": \"$dir/datadir/$optName/mongod.conf\"
}"

# Output
if [ "$format" = "json" ]; then
echo -e "${jsonOutputs}"
else
echo -e "${normalOutputs}"
fi

0 comments on commit e33270e

Please sign in to comment.