Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support XDG_DATA_HOME #56

Merged
merged 8 commits into from
May 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,16 @@ jobs:
strategy:
matrix:
os: [macos-latest, ubuntu-latest]
test_script: [mongodb-test.sh, mysql-test.sh, postgresql-12-test.sh, postgresql-13-test.sh, redis-test.sh]
test_script: [
mongodb-4.4.10.sh,
mongodb-5.0.3.sh,
mysql-5.7.31.sh,
mysql-8.0.30.sh,
postgresql-12.4.sh,
postgresql-13.2.sh,
redis-6.0.16.sh,
redis-6.2.6.sh
]
steps:
- uses: actions/checkout@v3
- name: Run test
Expand Down
14 changes: 1 addition & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,19 +158,7 @@ cd dbdb
- You can use `dbdb.sh` for that.

```
/path/to/dbdb/dbdb.sh

mongodb.4.4.3.mongo4 is stopped.
/path/to/dbdb/mongodb/start.sh mongo4
/path/to/dbdb/mongodb/stop.sh mongo4
/path/to/dbdb/mongodb/restart.sh mongo4

mysql.5.7.31.mysql5-foo is running.
/path/to/dbdb/mysql/start.sh mysql5-foo
/path/to/dbdb/mysql/stop.sh mysql5-foo
/path/to/dbdb/mysql/restart.sh mysql5-foo

...
./dbdb.sh
```

### How to start a database server when my server booted?
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v1.2.12
v1.2.13
47 changes: 33 additions & 14 deletions dbdb.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
#!/bin/bash
set -eu

getInstallDir() {
suffix=dbdb
if [ -z "${XDG_DATA_HOME+x}" ]; then
echo "$HOME/.local/share/$suffix"
else
echo "$XDG_DATA_HOME/$suffix"
fi
}

# Get format option
format=""
while getopts ":f:" opt; do
Expand All @@ -25,34 +34,34 @@ RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m'

# Get current directory
currentDir="$(
cd "$(dirname "$0")" >/dev/null 2>&1
pwd -P
)"
cd "$currentDir"
installDir=$(getInstallDir)

normalOutputs=""
jsonOutputs=""
dbTypes=(mongodb mysql postgresql redis)
for dbType in "${dbTypes[@]}"; do
for dbVersion in $(ls "$currentDir/$dbType/versions" 2>/dev/null); do
if [ -d "$currentDir/$dbType/versions/$dbVersion" ]; then
for dbServerName in $(ls "$currentDir/$dbType/versions/$dbVersion/datadir" 2>/dev/null); do
if [ -d "$currentDir/$dbType/versions/$dbVersion/basedir" ]; then
if [ -d "$currentDir/$dbType/versions/$dbVersion/datadir/$dbServerName" ]; then
for dbVersion in $(ls "$installDir/$dbType/versions" 2>/dev/null); do
if [ -d "$installDir/$dbType/versions/$dbVersion" ]; then
for dbServerName in $(ls "$installDir/$dbType/versions/$dbVersion/datadir" 2>/dev/null); do
if [ -d "$installDir/$dbType/versions/$dbVersion/basedir" ]; then
if [ -d "$installDir/$dbType/versions/$dbVersion/datadir/$dbServerName" ]; then
# port
dbPort="{port}"
if [ -f "$currentDir/$dbType/versions/$dbVersion/datadir/$dbServerName/$dbType.port" ]; then
dbPort=$(cat "$currentDir/$dbType/versions/$dbVersion/datadir/$dbServerName/$dbType.port")
elif [ -f "$currentDir/$dbType/versions/$dbVersion/datadir/$dbServerName/$dbType.port.last" ]; then
dbPort=$(cat "$currentDir/$dbType/versions/$dbVersion/datadir/$dbServerName/$dbType.port.last")
elif [ -f "$currentDir/$dbType/versions/$dbVersion/datadir/$dbServerName/$dbType.port.init" ]; then
dbPort=$(cat "$currentDir/$dbType/versions/$dbVersion/datadir/$dbServerName/$dbType.port.init")
if [ -f "$installDir/$dbType/versions/$dbVersion/datadir/$dbServerName/$dbType.port" ]; then
dbPort=$(cat "$installDir/$dbType/versions/$dbVersion/datadir/$dbServerName/$dbType.port")
elif [ -f "$installDir/$dbType/versions/$dbVersion/datadir/$dbServerName/$dbType.port.last" ]; then
dbPort=$(cat "$installDir/$dbType/versions/$dbVersion/datadir/$dbServerName/$dbType.port.last")
elif [ -f "$installDir/$dbType/versions/$dbVersion/datadir/$dbServerName/$dbType.port.init" ]; then
dbPort=$(cat "$installDir/$dbType/versions/$dbVersion/datadir/$dbServerName/$dbType.port.init")
fi

# pid
pidFile="$currentDir/$dbType/versions/$dbVersion/datadir/$dbServerName/$dbType.pid"
pidFile="$installDir/$dbType/versions/$dbVersion/datadir/$dbServerName/$dbType.pid"

# status
if [ -f "$pidFile" ] && pgrep -F "$pidFile" >/dev/null; then
Expand Down Expand Up @@ -87,7 +96,17 @@ for dbType in "${dbTypes[@]}"; do

# jsonOutputs
availableCommands='["start.sh", "stop.sh", "restart.sh", "port.sh", "status.sh", "connect.sh", "delete.sh"]'
jsonOutputs="$jsonOutputs{\"name\": \"$dbServerName\", \"type\": \"$dbType\", \"version\": \"$dbVersion\", \"port\": \"$dbPort\", \"status\": \"$status\", \"commandPath\": \"$currentDir/$dbType/\", \"availableCommands\": $availableCommands, \"dataDir\": \"$currentDir/$dbType/versions/$dbVersion/datadir/$dbServerName\", \"confPath\": \"$currentDir/$dbType/versions/$dbVersion/datadir/$dbServerName/$confName\"},"
jsonOutputs="$jsonOutputs{
\"name\": \"$dbServerName\",
\"type\": \"$dbType\",
\"version\": \"$dbVersion\",
\"port\": \"$dbPort\",
\"status\": \"$status\",
\"commandPath\": \"$currentDir/$dbType/\",
\"availableCommands\": $availableCommands,
\"dataDir\": \"$installDir/$dbType/versions/$dbVersion/datadir/$dbServerName\",
\"confPath\": \"$installDir/$dbType/versions/$dbVersion/datadir/$dbServerName/$confName\"
},"
fi
fi
done
Expand Down
87 changes: 53 additions & 34 deletions lib/functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -76,52 +76,52 @@ getUrlFileAs() {

extractFile() {
if [ ! -d $1/basedir ]; then
echo "Extracting... $2" 1>&2
echo "Extracting..." 1>&2
mkdir -p $1/basedir
cd $1/basedir
cp ../../../$2.tar.gz .
cp $1/$2.tar.gz .
tar zxf $2.tar.gz --strip-components 1
rm -f $2.tar.gz
fi
}

getCommands() {
currentDir=$(getCurrentDir)
type=$(getType)

format="${4:-}"
if [ "$format" = "json" ]; then
jsonOutputs="{
\"start\": \"$currentDir/start.sh $1\",
\"stop\": \"$currentDir/stop.sh $1\",
\"restart\": \"$currentDir/restart.sh $1\",
\"port\": \"$currentDir/port.sh $1\",
\"status\": \"$currentDir/status.sh $1\",
\"connect\": \"$currentDir/connect.sh $1\",
\"delete\": \"$currentDir/delete.sh $1\"
\"start\": \"./$type/start.sh $1\",
\"stop\": \"./$type/stop.sh $1\",
\"restart\": \"./$type/restart.sh $1\",
\"port\": \"./$type/port.sh $1\",
\"status\": \"./$type/status.sh $1\",
\"connect\": \"./$type/connect.sh $1\",
\"delete\": \"./$type/delete.sh $1\"
}"
echo -e $jsonOutputs
else
echo ""
echo "# Start"
echo "$currentDir/start.sh $1"
echo "./$type/start.sh $1"
echo ""
echo "# Stop"
echo "$currentDir/stop.sh $1"
echo "./$type/stop.sh $1"
echo ""
echo "# Restart"
echo "$currentDir/restart.sh $1"
echo "./$type/restart.sh $1"
echo ""
echo "# Port"
echo "$currentDir/port.sh $1"
echo "./$type/port.sh $1"
echo ""
echo "# Status"
echo "$currentDir/status.sh $1"
echo "./$type/status.sh $1"
echo ""
echo "# Connect"
echo "$currentDir/connect.sh $1"
echo "./$type/connect.sh $1"
echo ""
echo "# Delete"
echo "$currentDir/delete.sh $1"
echo "./$type/delete.sh $1"
echo ""
fi

Expand All @@ -138,21 +138,21 @@ getRandomPort() {
# if $1 name is duplicated in this datastore, exit(1)
exitIfDuplicatedName() {
name=$1
currentDir=$(getCurrentDir)
found=$(find "$currentDir" -type d -name "$name" | wc -l | tr -d '[:space:]')
installDir=$(getInstallDir $(getType))
found=$(find "$installDir" -type d -name "$name" | wc -l | tr -d '[:space:]')
if [ "$found" = "0" ]; then
:
else
echo "The name '$name' is already in use." 1>&2
#find "$currentDir" -type d -name "$name" | grep -E "datadir/$name$" 1>&2
#find "$installDir" -type d -name "$name" | grep -E "datadir/$name$" 1>&2
exit 1
fi
}

exitIfNotExistVersion() {
name=$1
currentDir=$(getCurrentDir)
version=$(find "$currentDir" -type d -name "$name" | grep -E "datadir/$name$" | awk -F "/datadir/" '{print $1}' | awk -F "/versions/" '{print $2}')
installDir=$(getInstallDir $(getType))
version=$(find "$installDir" -type d -name "$name" | grep -E "datadir/$name$" | awk -F "/datadir/" '{print $1}' | awk -F "/versions/" '{print $2}')
if [ "$version" = "" ]; then
echo "There is no version for the given '$name'." 1>&2
exit 1
Expand All @@ -161,36 +161,46 @@ exitIfNotExistVersion() {

getVersionByName() {
name=$1
currentDir=$(getCurrentDir)
version=$(find "$currentDir" -type d -name "$name" | grep -E "datadir/$name$" | awk -F "/datadir/" '{print $1}' | awk -F "/versions/" '{print $2}')
installDir=$(getInstallDir $(getType))
version=$(find "$installDir" -type d -name "$name" | grep -E "datadir/$name$" | awk -F "/datadir/" '{print $1}' | awk -F "/versions/" '{print $2}')
echo "$version"
}

exitIfNotExistPortFile() {
name=$1
version=$2
currentDir=$(getCurrentDir)
portFiles=$(find "$currentDir/versions/$version" -type f -name "*.port*" | grep -E "/datadir/$name/" || true)
installDir=$(getInstallDir $(getType))
portFiles=$(find "$installDir/versions/$version" -type f -name "*.port*" | grep -E "/datadir/$name/" || true)
if [ "$portFiles" = "" ]; then
echo "There are no port files for the given '$name'." 1>&2
exit 1
fi
}

getService() {
service=$(basename $(getCurrentDir))
echo "$service"
getType() {
if [[ "$(getCurrentDir)" == *"/dbdb/mongodb"* ]]; then
echo "mongodb"
elif [[ "$(getCurrentDir)" == *"/dbdb/mysql"* ]]; then
echo "mysql"
elif [[ "$(getCurrentDir)" == *"/dbdb/postgresql"* ]]; then
echo "postgresql"
elif [[ "$(getCurrentDir)" == *"/dbdb/redis"* ]]; then
echo "redis"
else
echo "unknown database type" 1>&2
exit 1
fi
}

getPortByName() {
service=$(getService)
type=$(getType)
name=$1
version=$2
currentDir=$(getCurrentDir)
installDir=$(getInstallDir $(getType))

runningPort=$(cat "$currentDir/versions/$version/datadir/$name/$service.port" 2>/dev/null || true)
lastPort=$(cat "$currentDir/versions/$version/datadir/$name/$service.port.last" 2>/dev/null || true)
initPort=$(cat "$currentDir/versions/$version/datadir/$name/$service.port.init" 2>/dev/null || true)
runningPort=$(cat "$installDir/versions/$version/datadir/$name/$type.port" 2>/dev/null || true)
lastPort=$(cat "$installDir/versions/$version/datadir/$name/$type.port.last" 2>/dev/null || true)
initPort=$(cat "$installDir/versions/$version/datadir/$name/$type.port.init" 2>/dev/null || true)

if [ "$runningPort" ]; then
echo $runningPort
Expand All @@ -209,3 +219,12 @@ getOptPort() {
echo "$optPort"
fi
}

getInstallDir() {
suffix=dbdb/$1
if [ -z "${XDG_DATA_HOME+x}" ]; then
echo "$HOME/.local/share/$suffix"
else
echo "$XDG_DATA_HOME/$suffix"
fi
}
3 changes: 2 additions & 1 deletion mongodb/connect.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ optVersion=$(getVersionByName "$optName")
exitIfNotExistPortFile "$optName" "$optVersion"
optPort=$(getPortByName "$optName" "$optVersion")

dir=$currentDir/versions/$optVersion
installDir=$(getInstallDir $(getType))
dir=$installDir/versions/$optVersion

exitIfNotExistDir $dir/datadir/$optName
exitIfNotRunningPort $optPort
Expand Down
8 changes: 5 additions & 3 deletions mongodb/create.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ optPort=$(getOptPort $3)
optFileName=mongodb-${optVersion}-${os}
optUser=_dbdb_mongodb
optSocket=/tmp/dbdb_mongodb_$optPort.sock
dir=$currentDir/versions/$optVersion
installDir=$(getInstallDir $(getType))
dir=$installDir/versions/$optVersion

mkdir -p "$dir"
cd $dir

exitIfDuplicatedName $optName
exitIfExistDir $dir/datadir/$optName
Expand All @@ -58,8 +62,6 @@ extractFile $dir $optFileName
echo $optPort >$dir/datadir/$optName/mongodb.port.init
echo "#mongod.conf" > $dir/datadir/$optName/mongod.conf


cd $currentDir
commands=$(getCommands $optName $optVersion $optPort $format)

normalOutputs=""
Expand Down
3 changes: 2 additions & 1 deletion mongodb/delete.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ optVersion=$(getVersionByName "$optName")
exitIfNotExistPortFile "$optName" "$optVersion"
optPort=$(getPortByName "$optName" "$optVersion")

dir=$currentDir/versions/$optVersion
installDir=$(getInstallDir $(getType))
dir=$installDir/versions/$optVersion

./stop.sh -f "$format" $optName $optVersion $optPort > /dev/null
sleep 1
Expand Down
2 changes: 1 addition & 1 deletion mongodb/restart.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ currentDir="$(
pwd -P
)"
cd $currentDir
./stop.sh -f "$format" $1 > /dev/null

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

set -eu
Expand Down
3 changes: 2 additions & 1 deletion mongodb/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ optVersion=$(getVersionByName "$optName")
exitIfNotExistPortFile "$optName" "$optVersion"
optPort=$(getPortByName "$optName" "$optVersion")

dir=$currentDir/versions/$optVersion
installDir=$(getInstallDir $(getType))
dir=$installDir/versions/$optVersion

exitIfNotExistDir $dir/datadir/$optName
exitIfRunningPort $optPort
Expand Down
3 changes: 2 additions & 1 deletion mongodb/status.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ optVersion=$(getVersionByName "$optName")
exitIfNotExistPortFile "$optName" "$optVersion"
optPort=$(getPortByName "$optName" "$optVersion")

dir=$currentDir/versions/$optVersion
installDir=$(getInstallDir $(getType))
dir=$installDir/versions/$optVersion

exitIfNotExistDir $dir/datadir/$optName
exitIfNotRunningPort $optPort
Expand Down
3 changes: 2 additions & 1 deletion mongodb/stop.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ optVersion=$(getVersionByName "$optName")
exitIfNotExistPortFile "$optName" "$optVersion"
optPort=$(getPortByName "$optName" "$optVersion")

dir=$currentDir/versions/$optVersion
installDir=$(getInstallDir $(getType))
dir=$installDir/versions/$optVersion

exitIfNotExistDir $dir/datadir/$optName
exitIfNotRunningPort $optPort
Expand Down
3 changes: 2 additions & 1 deletion mysql/connect.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ exitIfNotExistPortFile "$optName" "$optVersion"
optPort=$(getPortByName "$optName" "$optVersion")

optSocket=/tmp/dbdb_mysql_$optPort.sock
dir=$currentDir/versions/$optVersion
installDir=$(getInstallDir $(getType))
dir=$installDir/versions/$optVersion

exitIfNotExistDir $dir/datadir/$optName
exitIfNotRunningPort $optPort
Expand Down
Loading