1+ #! /bin/bash
2+ # shellcheck disable=SC2016,SC2086,SC2046
3+
4+ # Script configuration
5+ set -euo pipefail # Exit on error, undefined vars, and pipe failures
6+ trap ' echo "Error on line $LINENO. Exit code: $?"' ERR
7+
8+ # Configuration variables
9+ REPO_URL=
" [email protected] :Countly/countly-server.git" 10+ REPO_NAME=" countly-community"
11+ BRANCH=" master"
12+ LOG_FILE=" community_setup_$( date +%Y%m%d_%H%M%S) .log"
13+ DIR=" $( pwd) /$REPO_NAME "
14+
15+ # Plugins that will be disabled by default
16+ DISABLED_PLUGINS=(
17+ " browser"
18+ " empty"
19+ " enterpriseinfo"
20+ " hooks"
21+ " ip_store"
22+ " ldap"
23+ " old-ui-compatibility"
24+ " push"
25+ " recaptcha"
26+ " tracker"
27+ " two-factor-auth"
28+ " vue-example"
29+ " white-labeling"
30+ )
31+
32+ # Logging function
33+ log () {
34+ local level=$1
35+ shift
36+ local message=$*
37+ local timestamp
38+ timestamp=$( date ' +%Y-%m-%d %H:%M:%S' )
39+ echo -e " ${timestamp} [${level} ] ${message} " | tee -a " $LOG_FILE "
40+ }
41+
42+ # Check if command exists
43+ command_exists () {
44+ command -v " $1 " > /dev/null 2>&1
45+ }
46+
47+ # Prerequisites check
48+ check_prerequisites () {
49+ log " INFO" " Checking prerequisites..."
50+
51+ local prerequisites=(git node npm)
52+ for cmd in " ${prerequisites[@]} " ; do
53+ if ! command_exists " $cmd " ; then
54+ log " ERROR" " Required command '$cmd ' not found. Please install it and try again."
55+ exit 1
56+ fi
57+ done
58+
59+ # Create nodejs symlink if it doesn't exist and node is not already available as nodejs
60+ if ! command_exists nodejs && command_exists node; then
61+ log " INFO" " Creating nodejs symlink..."
62+ sudo ln -s " $( command -v node) " /usr/local/bin/nodejs || {
63+ log " WARNING" " Could not create nodejs symlink, but continuing as node is available"
64+ }
65+ fi
66+
67+ log " INFO" " All prerequisites verified"
68+ }
69+
70+ # Repository setup
71+ setup_repository () {
72+ log " INFO" " Setting up repository..."
73+
74+ if [ -d " $REPO_NAME " ]; then
75+ local answer
76+ read -r -p " Directory $REPO_NAME already exists. Backup (b), remove (r), or exit (e)? " answer
77+ case $answer in
78+ b|B)
79+ log " INFO" " Backing up existing directory..."
80+ mv " $REPO_NAME " " ${REPO_NAME} _backup_$( date +%Y%m%d_%H%M%S) "
81+ ;;
82+ r|R)
83+ log " INFO" " Removing existing directory..."
84+ rm -rf " $REPO_NAME "
85+ ;;
86+ * )
87+ log " INFO" " Exiting as per user request"
88+ exit 0
89+ ;;
90+ esac
91+ fi
92+
93+ git clone " $REPO_URL " " $REPO_NAME " || {
94+ log " ERROR" " Failed to clone repository. Please check your SSH key and repository access."
95+ exit 1
96+ }
97+
98+ cd " $REPO_NAME " || exit 1
99+
100+ # Git configuration
101+ git config --global --add safe.directory " $DIR "
102+ git config core.fileMode false
103+ git checkout " $BRANCH "
104+
105+ # Submodules setup if any
106+ log " INFO" " Setting up submodules..."
107+ git submodule status
108+ git pull
109+ git submodule init
110+ git submodule update
111+ }
112+
113+ # Configuration setup
114+ setup_configuration () {
115+ log " INFO" " Setting up configuration files..."
116+
117+ # Copy config files if they don't exist
118+ cp -n " $DIR /api/config.sample.js" " $DIR /api/config.js" || log " WARNING" " Skipping api config - file may already exist"
119+ cp -n " $DIR /plugins/plugins.default.json" " $DIR /plugins/plugins.json" || log " WARNING" " Skipping plugins config - file may already exist"
120+ cp -n " $DIR /frontend/express/config.sample.js" " $DIR /frontend/express/config.js" || log " WARNING" " Skipping express config - file may already exist"
121+ cp -n " $DIR /frontend/express/public/javascripts/countly/countly.config.sample.js" " $DIR /frontend/express/public/javascripts/countly/countly.config.js" || log " WARNING" " Skipping frontend config - file may already exist"
122+
123+ # Use platform-independent sed syntax
124+ if [[ " $OSTYPE " == " darwin" * ]]; then
125+ # macOS
126+ sed -i ' ' ' s#countlyGlobal.path#"http://localhost:3001"#g' " $DIR /frontend/express/public/javascripts/countly/countly.config.js"
127+ sed -i ' ' ' s/max_pool_size: 500/max_pool_size: 20/g' " $DIR /api/config.js"
128+ else
129+ # Linux and others
130+ sed -i ' s#countlyGlobal.path#"http://localhost:3001"#g' " $DIR /frontend/express/public/javascripts/countly/countly.config.js"
131+ sed -i ' s/max_pool_size: 500/max_pool_size: 20/g' " $DIR /api/config.js"
132+ fi
133+ }
134+
135+ # Plugin management
136+ manage_plugins () {
137+ log " INFO" " Managing plugins..."
138+
139+ PLUGIN_JS_PATH=" $DIR /bin/commands/scripts/plugin.js"
140+ for dir in " $DIR /plugins" /* /; do
141+ plugin=$( basename " $dir " )
142+
143+ # Using a for loop to check array membership
144+ is_disabled=0
145+ for disabled_plugin in " ${DISABLED_PLUGINS[@]} " ; do
146+ if [ " $plugin " = " $disabled_plugin " ]; then
147+ is_disabled=1
148+ break
149+ fi
150+ done
151+
152+ if [ " $is_disabled " -eq 1 ]; then
153+ log " INFO" " Disabling $plugin ..."
154+ if node " $PLUGIN_JS_PATH " disable " $plugin " ; then
155+ log " INFO" " Successfully disabled $plugin "
156+ else
157+ log " ERROR" " Failed to disable $plugin "
158+ fi
159+ else
160+ log " INFO" " Enabling $plugin ..."
161+ if node " $PLUGIN_JS_PATH " enable " $plugin " ; then
162+ log " INFO" " Successfully enabled $plugin "
163+ else
164+ log " ERROR" " Failed to enable $plugin "
165+ fi
166+ fi
167+ done
168+ }
169+
170+ # Main execution
171+ main () {
172+ log " INFO" " Starting community setup script..."
173+
174+ check_prerequisites
175+ setup_repository
176+
177+ # Set permissions - but don't fail if we don't have sudo
178+ sudo chmod -R 766 " $DIR " || {
179+ log " WARNING" " Could not set full permissions on $DIR . You may need to adjust permissions manually."
180+ }
181+
182+ # Install dependencies
183+ cd " $DIR " || exit 1
184+ npm install
185+
186+ setup_configuration
187+ manage_plugins
188+
189+ # build locales and sass
190+ npx grunt dist-all
191+
192+ log " INFO" " Community setup completed successfully!"
193+ log " INFO" " You can now start Countly by using the debugger"
194+ }
195+
196+ # Execute main function
197+ main
0 commit comments