Skip to content

Commit

Permalink
Merge pull request #5461 from ant-media/add_redis_support
Browse files Browse the repository at this point in the history
Add Redis support
  • Loading branch information
mekya committed Mar 18, 2024
2 parents 6ce6337 + b053df8 commit 128fb76
Show file tree
Hide file tree
Showing 13 changed files with 382 additions and 202 deletions.
9 changes: 6 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ before_install:
#install mongodb ffmpeg wondershaper
- sudo apt-get install mongodb-org ffmpeg wondershaper -y -qq
- sudo mongod --dbpath /var/lib/mongodb &>/dev/null & # start mongo db - don' write log
#install chrome
- wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
- sudo apt install ./google-chrome-stable_current_amd64.deb -y

- sudo git clone --depth=1 https://github.com/ant-media/utilities.git /usr/local/onvif
# get parent project
- (if [ $(git ls-remote https://github.com/ant-media/ant-media-server-parent.git $TRAVIS_BRANCH | wc -l) == "1" ];
Expand Down Expand Up @@ -174,13 +178,12 @@ cache:

after_failure:
- sudo cat /usr/local/antmedia/log/ant-media-server.log
- echo "----------------------------------------------"
- sudo cat /usr/local/antmedia/log/antmedia-error.log
- sudo service mongod status
- sudo service redis-server status
- sudo cat /var/log/mongodb/mongod.log
- sudo cat /usr/local/antmedia/hs_err_pid*.log # cat hs_err_pid file if it exists
- sudo cat hs_err_pid*.log # cat hs_err_pid file if it exists
- sudo service mongod status
- sudo service redis-server status


deploy:
Expand Down
70 changes: 29 additions & 41 deletions src/main/java/io/antmedia/console/AdminApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.concurrent.ConcurrentLinkedQueue;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
Expand All @@ -41,6 +42,7 @@
import io.antmedia.AntMediaApplicationAdapter;
import io.antmedia.cluster.IClusterNotifier;
import io.antmedia.console.datastore.ConsoleDataStoreFactory;
import io.antmedia.datastore.db.DataStoreFactory;
import io.vertx.core.Vertx;
import jakarta.annotation.Nullable;

Expand Down Expand Up @@ -334,18 +336,11 @@ public boolean createApplication(String appName, String warFileFullPath) {
return false;
}

if(isCluster) {
String dbConnectionURL = getDataStoreFactory().getDbHost();
String mongoUser = getDataStoreFactory().getDbUser();
String mongoPass = getDataStoreFactory().getDbPassword();

boolean result = runCreateAppScript(appName, true, dbConnectionURL, mongoUser, mongoPass, warFileFullPath);
success = result;
}
else {
boolean result = runCreateAppScript(appName, warFileFullPath);
success = result;
}
String dbConnectionURL = getDataStoreFactory().getDbHost();
String mongoUser = getDataStoreFactory().getDbUser();
String mongoPass = getDataStoreFactory().getDbPassword();
success = runCreateAppScript(appName, isCluster, dbConnectionURL, mongoUser, mongoPass, warFileFullPath);


vertx.executeBlocking(() -> {
try {
Expand Down Expand Up @@ -461,48 +456,41 @@ public synchronized boolean deleteApplication(String appName, boolean deleteDB)
return success;
}


public boolean runCreateAppScript(String appName, String warFilePath) {
return runCreateAppScript(appName, false, null, null, null, warFilePath);
}

public boolean runCreateAppScript(String appName) {
return runCreateAppScript(appName, false, null, null, null, null);
}

public boolean runCreateAppScript(String appName, boolean isCluster,
String mongoHost, String mongoUser, String mongoPass, String warFileName) {
String dbConnectionUrl, String dbUser, String dbPass, String warFileName) {
Path currentRelativePath = Paths.get("");
String webappsPath = currentRelativePath.toAbsolutePath().toString();

String command;

if(warFileName != null && !warFileName.isEmpty())
{
command = "/bin/bash create_app.sh"
+ " -n " + appName
+ " -w true"
+ " -p " + webappsPath
+ " -c " + isCluster
+ " -f " + warFileName;

String command = "/bin/bash create_app.sh"
+ " -n " + appName
+ " -w true"
+ " -p " + webappsPath
+ " -c " + isCluster;

if (!DataStoreFactory.DB_TYPE_MAPDB.equals(getDataStoreFactory().getDbType())) {
//add db connection url, user and pass if it's not mapdb
if (StringUtils.isNotBlank(dbConnectionUrl)) {
command += " -m " + dbConnectionUrl;
}
if (StringUtils.isNotBlank(dbUser)) {
command += " -u " + dbUser;
}
if (StringUtils.isNotBlank(dbPass)) {
command += " -s " + dbPass;
}
}
else
{
command = "/bin/bash create_app.sh"
+ " -n " + appName
+ " -w true"
+ " -p " + webappsPath
+ " -c " + isCluster;
}

if(isCluster)
if(StringUtils.isNotBlank(warFileName))
{
command += " -m " + mongoHost
+ " -u " + mongoUser
+ " -s " + mongoPass;
}
command += " -f " + warFileName;

}

log.info("Creating application with command: {}", command);
return runCommand(command);
}
Expand Down
12 changes: 5 additions & 7 deletions src/main/server/change_server_mode.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,19 @@ usage() {
echo ""
echo "Usage: "
echo "Change server mode to cluster"
echo "$0 cluster {DB_SERVER}"
echo "$0 cluster {DB_SERVER} {MONGO_DB_USERNAME} {MONGO_DB_PASSWORD}"
echo "$0 cluster {DB_CONNECTION_URL}"
echo ""
echo "You can use connection string directly as {DB_SERVER} that starts with mongodb:// or mongodb+srv:// or give the ip address"
echo "You can also use REDIS url or redis configuration file as {DB_SERVER}"
echo "You can use connection string directly as {DB_CONNECTION_URL} that starts with mongodb:// or mongodb+srv:// or give the ip address"
echo "You can also use REDIS url or redis configuration file as {DB_CONNECTION_URL}"
echo "Please don't forget quote your connection string. Take a look at the sample below."
echo "Usage:"
echo "$0 cluster \"mongodb://[username:password@]host1[:port1][,...hostN[:portN]][/[defaultauthdb][?options]]\" "
echo "$0 cluster \"redis://[username:password@]host1:port1]\" "
echo ""
echo "Change server mode to standalone that uses mapdb"
echo "$0 standalone"
echo "Change server mode to standalone that uses {DB_SERVER}"
echo "$0 standalone {DB_SERVER}"
echo "Pay attention: If you use connection string directly - contains username and password - as {MONGO_DB_SERVER} parameter like above, then Ant Media Server ignores {MONGO_DB_USERNAME} and {MONGO_DB_PASSWORD}. Lastly {MONGO_DB_USERNAME} and {MONGO_DB_PASSWORD} are deprecated"
echo "Change server mode to standalone that uses {DB_CONNECTION_URL} for mongo or redis"
echo "$0 standalone {DB_CONNECTION_URL}"
echo ""
echo ""
echo "Change server mode to standalone"
Expand Down
5 changes: 3 additions & 2 deletions src/main/server/conf/functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ change_server_mode() {
MODE=$1
if [ $MODE = "cluster" ]; then
echo "Mode: cluster"
#default db type
DB_TYPE=mongodb
DB_URL=$2

Expand Down Expand Up @@ -54,9 +55,9 @@ change_server_mode() {
DB_URL=$2
if [ -z "$DB_URL" ]; then #backward compatible if no DB_URL it's mapdb
DB_TYPE=mapdb
DB_URL=localhost
DB_URL=""
echo "DB type is mapdb"
elif [[ $DB_URL =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ || $DB_URL =~ ^localhost$ || $DB_URL =~ ^mongo.*$ ]]; then
elif [[ $DB_HOST =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+(:[0-9]{1,5})?$ || $DB_HOST =~ ^localhost(:[0-9]{1,5})?$ || $DB_HOST =~ ^mongo.*$ ]]; then
# it should be ^mongo.*$ not ^mongodb.*$ becaue kubernetes deployment give -h mongo parameter
DB_TYPE=mongodb
echo "DB type is mongodb"
Expand Down
56 changes: 33 additions & 23 deletions src/main/server/create_app.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ usage() {
echo "Usage:"
echo "$0 -n APPLICATION_NAME [-p INSTALLATION_PATH] [-w true|false] [-c true|false]"
echo "Options:"
echo "-n: Name of the application that you want to have. It's mandatory"
echo "-n: Name of the application that you want to have. It's mandatory"
echo "-m: Database URI including username and password. It can be MongoDB or RedisDB connection url. If it's a cluster, it's mandatory. Otherwise optional"
echo "-f: war file path for custom app deployment"
echo "-p: (Optional) Path is the install location of Ant Media Server which is /usr/local/antmedia by default."
echo "-w: (Optional) The flag to deploy application as war file. Default value is false"
echo "-c: (Optional) The flag to deploy application in cluster mode. Default value is false"
echo "-m: Mongo DB host. If it's a cluster, it's mandatory. Otherwise optional"
echo "-u: Mongo DB user. If it's a cluster, it's mandatory. Otherwise optional"
echo "-s: Mongo DB password. If it's a cluster, it's mandatory. Otherwise optional"
echo "-u: (Deprecated: Add username to Database URI with -m option)Mongo DB user or Redis user. If it's a cluster, it's mandatory. Otherwise optional"
echo "-s: (Deprecated: Add password to Database URI with -m option)Mongo DB or Redis password. If it's a cluster, it's mandatory. Otherwise optional"
echo "-h: print this usage"
echo "-f: war file path for custom app deployment"

echo " "
echo "Example: "
echo "$0 -n live -w"
Expand All @@ -31,6 +32,7 @@ ERROR_MESSAGE="Error: App is not created. Please check the error in the terminal
AMS_DIR=/usr/local/antmedia
AS_WAR=false
IS_CLUSTER=false
DB_TYPE=mapdb

while getopts 'n:p:w:h:c:m:u:s:f:' option
do
Expand All @@ -39,9 +41,9 @@ do
p) AMS_DIR=${OPTARG};;
w) AS_WAR=${OPTARG};;
c) IS_CLUSTER=${OPTARG};;
m) MONGO_HOST=${OPTARG};;
u) MONGO_USER=${OPTARG};;
s) MONGO_PASS=${OPTARG};;
m) DB_HOST=${OPTARG};;
u) DB_USER=${OPTARG};;
s) DB_PASS=${OPTARG};;
f) WAR_FILE=${OPTARG};;
h) usage
exit 1;;
Expand Down Expand Up @@ -79,14 +81,6 @@ if [[ -z "$AS_WAR" ]]; then
AS_WAR="false"
fi

if [[ "$IS_CLUSTER" == "true" ]]; then
if [[ -z "$MONGO_HOST" ]]; then
echo "Please set mongodb host, username and password for cluster mode. "
usage
exit 1
fi
fi

case $AMS_DIR in
/*) AMS_DIR=$AMS_DIR;;
*) AMS_DIR=$PWD/$AMS_DIR;;
Expand Down Expand Up @@ -134,16 +128,32 @@ check_result
sed -i $SED_COMPATIBILITY 's^<param-value>/StreamApp^<param-value>/'$APP_NAME'^' $WEB_XML_FILE
check_result

if [[ $MONGO_HOST == \'*\' ]]; then
MONGO_HOST="${MONGO_HOST:1:-1}"
#remove single quotes if exists
if [[ $DB_HOST == \'*\' ]]; then
DB_HOST="${DB_HOST:1:-1}"
fi

#remove double quotes if exists
if [[ $DB_HOST == \"*\" ]]; then
DB_HOST="${DB_HOST:1:-1}"
fi

#Automatically find which DB it is
if [[ $DB_HOST =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+(:[0-9]{1,5})?$ || $DB_HOST =~ ^localhost(:[0-9]{1,5})?$ || $DB_HOST =~ ^mongo.*$ ]]; then
DB_TYPE=mongodb
elif [[ $DB_HOST =~ ^redis.*$ ]]; then
DB_TYPE=redisdb
fi


sed -i $SED_COMPATIBILITY 's/db.type=.*/db.type='$DB_TYPE'/' $RED5_PROPERTIES_FILE
sed -i $SED_COMPATIBILITY 's#db.host=.*#db.host='$DB_HOST'#' $RED5_PROPERTIES_FILE
sed -i $SED_COMPATIBILITY 's/db.user=.*/db.user='$DB_USER'/' $RED5_PROPERTIES_FILE
sed -i $SED_COMPATIBILITY 's/db.password=.*/db.password='$DB_PASS'/' $RED5_PROPERTIES_FILE


if [[ "$IS_CLUSTER" == "true" ]]; then
echo "Cluster mode"
sed -i $SED_COMPATIBILITY 's/db.type=.*/db.type='mongodb'/' $RED5_PROPERTIES_FILE
sed -i $SED_COMPATIBILITY 's#db.host=.*#db.host='$MONGO_HOST'#' $RED5_PROPERTIES_FILE
sed -i $SED_COMPATIBILITY 's/db.user=.*/db.user='$MONGO_USER'/' $RED5_PROPERTIES_FILE
sed -i $SED_COMPATIBILITY 's/db.password=.*/db.password='$MONGO_PASS'/' $RED5_PROPERTIES_FILE
ln -s $WAR_FILE $AMS_DIR/webapps/root/$APP_NAME.war
else
echo "Not cluster mode."
Expand All @@ -160,4 +170,4 @@ else
chown -R antmedia:antmedia $APP_DIR -f
fi

echo "$APP_NAME is created."
echo "$APP_NAME is created."
Loading

0 comments on commit 128fb76

Please sign in to comment.