-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_app.sh
148 lines (122 loc) · 4.62 KB
/
create_app.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
#!/bin/bash
# Script to create an application in Ant Media Server
# Function to display usage instructions
usage() {
echo "Usage:"
echo "$0 -n APPLICATION_NAME [-p INSTALLATION_PATH] [-w true|false] [-h DATABASE_HOST] [-f WAR_FILE]"
echo "Options:"
echo "-n: Name of the application that you want to create. (Mandatory)"
echo "-p: Path is the installation location of Ant Media Server. Default: /usr/local/antmedia"
echo "-w: Flag to deploy the application as a war file. Default: false"
echo "-h: Database host with optional username and password (e.g., mongodb://username:password@host:port)."
echo "-f: War file path for custom app deployment"
echo "-q: Print this usage"
echo " "
echo "Example: "
echo "$0 -n live -w"
echo "$0 -n live -h mongodb://localhost:27017"
echo " "
echo "If you have any questions, send an email to [email protected]"
}
# Print all parameters passed to the script
echo "All parameters: $@"
ERROR_MESSAGE="Error: Application creation failed. Please check the error in the terminal and refer to the instructions."
AMS_DIR=/usr/local/antmedia
AS_WAR=false
MONGO_PORT=27017
# Parse command-line options
while getopts 'n:p:w:h:f:q' option; do
case "${option}" in
n) APP_NAME=${OPTARG} ;;
p) AMS_DIR=${OPTARG} ;;
w) AS_WAR=${OPTARG} ;;
h) DATABASE_HOST=${OPTARG} ;;
f) WAR_FILE=${OPTARG} ;;
q) usage
exit 1 ;;
esac
done
# Check if APPLICATION_NAME is provided as an argument
if [ -z "$APP_NAME" ]; then
APP_NAME=$1
if [ ! -z "$2" ]; then
AMS_DIR=$2
fi
fi
# Check if APPLICATION_NAME is still missing
if [[ -z "$APP_NAME" ]]; then
echo "Error: Missing parameter APPLICATION_NAME. Check the instructions below."
usage
exit 1
fi
# Set the WAR file path if not provided
if [[ -z "$WAR_FILE" ]]; then
WAR_FILE=$AMS_DIR/StreamApp*.war
fi
# Set AS_WAR flag to false if not provided
if [[ -z "$AS_WAR" ]]; then
AS_WAR=false
fi
# Format the AMS_DIR path
case $AMS_DIR in
/*) AMS_DIR=$AMS_DIR ;;
*) AMS_DIR=$PWD/$AMS_DIR ;;
esac
APP_NAME_LOWER=$(echo $APP_NAME | awk '{print tolower($0)}')
APP_DIR=$AMS_DIR/webapps/$APP_NAME
RED5_PROPERTIES_FILE=$APP_DIR/WEB-INF/red5-web.properties
WEB_XML_FILE=$APP_DIR/WEB-INF/web.xml
# Create the application directory
mkdir -p $APP_DIR
# Copy the WAR file to the application directory
cp $WAR_FILE $APP_DIR
cd $APP_DIR
WAR_FILE_NAME=$(basename $WAR_FILE)
# Unzip the WAR file
unzip $WAR_FILE_NAME
# Remove the WAR file
rm $WAR_FILE_NAME
OS_NAME=$(uname)
if [[ "$OS_NAME" == 'Darwin' ]]; then
SED_COMPATIBILITY='.bak'
fi
# Update database configuration in red5-web.properties
if [[ -n "$DATABASE_HOST" ]]; then
# Extract database type, username, password, host, and port from DATABASE_HOST
DATABASE_TYPE=$(echo $DATABASE_HOST | cut -d ':' -f 1)
HOST_PORT=$(echo $DATABASE_HOST | cut -d '@' -f 2)
if [[ "$DATABASE_TYPE" == "mongodb" ]]; then
# Extract username, password, host, and port for MongoDB
MONGO_USERNAME=$(echo $HOST_PORT | cut -d ':' -f 1 | cut -d '/' -f 3)
MONGO_PASSWORD=$(echo $MONGO_USERNAME | cut -d ':' -f 2)
MONGO_HOST=$(echo $HOST_PORT | cut -d '@' -f 2 | cut -d ':' -f 1)
MONGO_PORT=$(echo $HOST_PORT | cut -d '@' -f 2 | cut -d ':' -f 2)
if [ -n "$MONGO_USERNAME" ]; then
# If MongoDB username is provided, add it to the connection string
MONGO_CONNECTION="mongodb://$MONGO_USERNAME:$MONGO_PASSWORD@$MONGO_HOST:$MONGO_PORT"
else
MONGO_CONNECTION="mongodb://$MONGO_HOST:$MONGO_PORT"
fi
sed -i $SED_COMPATIBILITY 's#db.type=.*#db.type=mongodb#' $RED5_PROPERTIES_FILE
sed -i $SED_COMPATIBILITY 's#db.host=.*#db.host='$MONGO_CONNECTION'#' $RED5_PROPERTIES_FILE
fi
fi
# Update other properties in red5-web.properties
sed -i $SED_COMPATIBILITY 's#webapp.dbName=.*#webapp.dbName='$APP_NAME_LOWER'.db#' $RED5_PROPERTIES_FILE
sed -i $SED_COMPATIBILITY 's#webapp.contextPath=.*#webapp.contextPath=/'$APP_NAME'#' $RED5_PROPERTIES_FILE
sed -i $SED_COMPATIBILITY 's#db.app.name=.*#db.app.name='$APP_NAME'#' $RED5_PROPERTIES_FILE
sed -i $SED_COMPATIBILITY 's#db.name=.*#db.name='$APP_NAME_LOWER'#' $RED5_PROPERTIES_FILE
# Update display name and context path in web.xml
sed -i $SED_COMPATIBILITY 's#<display-name>StreamApp#<display-name>'$APP_NAME'#' $WEB_XML_FILE
sed -i $SED_COMPATIBILITY 's#<param-value>/StreamApp#<param-value>/'$APP_NAME'#' $WEB_XML_FILE
# Perform additional operations based on AS_WAR flag
if [[ $AS_WAR == "true" ]]; then
echo "Application will be deployed as a WAR file"
cd $APP_DIR
zip -r ../$APP_NAME.war *
rm -r $APP_DIR
else
echo "Application is deployed as a directory."
chown -R antmedia:antmedia $APP_DIR -f
fi
echo "$APP_NAME is created."