forked from cdr-stats/cdr-stats
-
Notifications
You must be signed in to change notification settings - Fork 10
/
cdr-stats-functions.sh
executable file
·861 lines (725 loc) · 28.9 KB
/
cdr-stats-functions.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
#!/bin/bash
#
# CDR-Stats License
# http://www.cdr-stats.org
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Copyright (C) 2011-2013 Star2Billing S.L.
#
# The Initial Developer of the Original Code is
# Arezqui Belaid <info@star2billing.com>
#
#Install mode can me either CLONE or DOWNLOAD
INSTALL_MODE='CLONE'
INSTALL_DIR='/usr/share/cdrstats'
INSTALL_DIR_WELCOME='/var/www/cdr-stats'
DATABASENAME='cdrstats_db'
DB_USERSALT=`</dev/urandom tr -dc 0-9| (head -c $1 > /dev/null 2>&1 || head -c 5)`
DB_USERNAME="cdr_stats_$DB_USERSALT"
DB_PASSWORD=`</dev/urandom tr -dc A-Za-z0-9| (head -c $1 > /dev/null 2>&1 || head -c 20)`
DB_HOSTNAME='localhost'
DB_PORT='5432'
CDRSTATS_USER='cdr_stats'
CELERYD_USER="celery"
CELERYD_GROUP="celery"
CDRSTATS_ENV="cdr-stats"
HTTP_PORT="8008"
BRANCH='master'
DB_BACKEND="POSTGRESQL"
DATETIME=$(date +"%Y%m%d%H%M%S")
#Django bug https://code.djangoproject.com/ticket/16017
export LANG="en_US.UTF-8"
#Include general functions
source bash-common-functions.sh
#Identify the OS
func_identify_os
#Function install the landing page
func_install_landing_page() {
mkdir -p $WELCOME_DIR
# Copy files
cp -r /usr/src/cdr-stats/install/landing-page/* $WELCOME_DIR
echo ""
echo "Add Nginx configuration for Welcome page..."
cp -rf /usr/src/cdr-stats/install/nginx/global /etc/nginx/
case $DIST in
'DEBIAN')
cp /usr/src/cdr-stats/install/nginx/sites-available/cdr_stats.conf /etc/nginx/sites-available/
ln -s /etc/nginx/sites-available/cdr_stats.conf /etc/nginx/sites-enabled/cdr_stats.conf
#Remove default NGINX landing page
rm /etc/nginx/sites-enabled/default
;;
'CENTOS')
cp /usr/src/cdr-stats/install/nginx/sites-available/cdr_stats.conf /etc/nginx/conf.d/
rm /etc/nginx/conf.d/default.conf
;;
esac
cp -rf /usr/src/cdr-stats/install/nginx/global /etc/nginx/
#Restart Nginx
service nginx restart
#Update Welcome page IP
sed -i "s/LOCALHOST/$IPADDR:$HTTP_PORT/g" $WELCOME_DIR/index.html
}
func_check_dependencies() {
echo ""
echo "Checking Python dependencies..."
echo ""
#Check Django
grep_pip=`pip freeze| grep Django`
if echo $grep_pip | grep -i "Django" > /dev/null ; then
echo "OK : Django installed..."
else
echo "Error : Django not installed..."
exit 1
fi
#Check celery
grep_pip=`pip freeze| grep celery`
if echo $grep_pip | grep -i "celery" > /dev/null ; then
echo "OK : celery installed..."
else
echo "Error : celery not installed..."
exit 1
fi
echo ""
echo "Python dependencies successfully installed!"
echo ""
}
#Fuction to create the virtual env
func_setup_virtualenv() {
echo "This will install virtualenv & virtualenvwrapper"
echo "and create a new virtualenv : $CDRSTATS_ENV"
pip install virtualenv
pip install virtualenvwrapper
#Prepare settings for installation
case $DIST in
'DEBIAN')
SCRIPT_VIRTUALENVWRAPPER="/usr/local/bin/virtualenvwrapper.sh"
;;
'CENTOS')
SCRIPT_VIRTUALENVWRAPPER="/usr/bin/virtualenvwrapper.sh"
;;
esac
# Enable virtualenvwrapper
chk=`grep "virtualenvwrapper" ~/.bashrc|wc -l`
if [ $chk -lt 1 ] ; then
echo "Set Virtualenvwrapper into bash"
echo "export WORKON_HOME=/usr/share/virtualenvs" >> ~/.bashrc
echo "source $SCRIPT_VIRTUALENVWRAPPER" >> ~/.bashrc
fi
# Setup virtualenv
export WORKON_HOME=/usr/share/virtualenvs
source $SCRIPT_VIRTUALENVWRAPPER
mkvirtualenv $CDRSTATS_ENV
workon $CDRSTATS_ENV
echo "Virtualenv $CDRSTATS_ENV created and activated"
}
func_prepare_system_common(){
#Create CDRStats User
echo "Create CDRStats User/Group : $CDRSTATS_USER"
useradd $CDRSTATS_USER --user-group --system --no-create-home
case $DIST in
'DEBIAN')
apt-get update
export LANGUAGE=en_US.UTF-8
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
locale-gen en_US.UTF-8
dpkg-reconfigure locales
apt-get -y install --reinstall language-pack-en
apt-get -y install postgresql postgresql-contrib
pg_createcluster 9.1 main --start
apt-get -y install python-setuptools python-dev build-essential libevent-dev python-pip
#We need both Postgresql and Mysql for the Connectors
apt-get -y install libmysqlclient-dev mysql-client-core-5.5
apt-get -y install git-core mercurial gawk
#PostgreSQL
apt-get -y install libpq-dev
;;
'CENTOS')
if [ "$INSTALLMODE" = "FULL" ]; then
yum -y update
fi
yum -y install autoconf automake bzip2 cpio curl curl-devel curl-devel expat-devel fileutils gcc-c++ gettext-devel gnutls-devel libjpeg-devel libogg-devel libtiff-devel libtool libvorbis-devel make ncurses-devel nmap openssl openssl-devel openssl-devel perl patch unzip wget zip zlib zlib-devel policycoreutils-python sudo
yum -y install git
#Install epel repo for pip and mod_python
if [ $KERNELARCH = "x86_64" ]; then
rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
else
rpm -ivh http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
fi
# disable epel repository since by default it is enabled.
sed -i "s/enabled=1/enable=0/" /etc/yum.repos.d/epel.repo
yum -y --enablerepo=epel install python-pip mod_python python-setuptools python-tools python-devel mercurial libevent libevent-devel
;;
esac
}
#Function to prepare the system, add user, install dependencies, etc...
func_prepare_system_frontend(){
func_prepare_system_common
case $DIST in
'DEBIAN')
apt-get -y install libapache2-mod-python libapache2-mod-wsgi
#PostgreSQL
apt-get -y install postgresql
#Start PostgreSQL
/etc/init.d/postgresql start
;;
'CENTOS')
yum -y install mysql mysql-devel mysql-server
yum -y --enablerepo=epel install mod_wsgi
#start http after reboot
chkconfig --levels 235 httpd on
if echo $DB_BACKEND | grep -i "^SQLITE" > /dev/null ; then
yum -y install sqlite
elif echo $DB_BACKEND | grep -i "^POSTGRESQL" > /dev/null ; then
#Configure PostgreSQL
echo "Configure PostgreSQL pg_hba.conf"
yum -y install postgresql-server postgresql-devel
chkconfig --levels 235 postgresql on
echo "service postgresql initdb"
echo "/etc/init.d/postgresql initdb"
/etc/init.d/postgresql initdb
/etc/init.d/postgresql start
mv /var/lib/pgsql/data/pg_hba.conf /var/lib/pgsql/data/pg_hba.conf.bak
sed -e '/^local/ d' -e '/^host / d' /var/lib/pgsql/data/pg_hba.conf.bak > /var/lib/pgsql/data/pg_hba.conf
echo 'local all postgres trust' >> /var/lib/pgsql/data/pg_hba.conf
echo 'local all all md5' >> /var/lib/pgsql/data/pg_hba.conf
echo 'host all postgres 127.0.0.1/32 trust' >> /var/lib/pgsql/data/pg_hba.conf
echo 'host all all 127.0.0.1/32 md5' >> /var/lib/pgsql/data/pg_hba.conf
echo 'host all postgres ::1/128 trust' >> /var/lib/pgsql/data/pg_hba.conf
echo 'host all all ::1/128 md5' >> /var/lib/pgsql/data/pg_hba.conf
#Restart PostgreSQL
service postgresql restart
else
#Install & Start MySQL
yum -y install mysql-server mysql-devel
chkconfig --levels 235 mysqld on
#Start Mysql
/etc/init.d/mysqld start
#Configure MySQL
if [ "$INSTALLMODE" = "FULL" ]; then
/usr/bin/mysql_secure_installation
fi
until mysql -u$MYSQLUSER -p$MYSQLPASSWORD -P$MYHOSTPORT -h$MYHOST -e ";" ; do
clear
echo "Enter your database settings"
func_get_mysql_database_setting
done
fi
;;
esac
#Prepare Database
echo "Prepare Database"
if echo $DB_BACKEND | grep -i "^SQLITE" > /dev/null ; then
#Permission on database folder if we use SQLite
mkdir $INSTALL_DIR/database
chown -R $APACHE_USER:$APACHE_USER $INSTALL_DIR/database/
elif echo $DB_BACKEND | grep -i "^POSTGRESQL" > /dev/null ; then
# Create the Database
echo "We will remove existing Database"
echo "Press Enter to continue"
echo ""
echo "sudo -u postgres dropdb $DATABASENAME"
echo "Remove Existing Database if exists..."
echo "Press Enter to continue"
read TEMP
sudo -u postgres dropdb $DATABASENAME
# if [ `sudo -u postgres psql -qAt --list | egrep $DATABASENAME | wc -l` -eq 1 ]; then
# echo "sudo -u postgres dropdb $DATABASENAME"
# sudo -u postgres dropdb $DATABASENAME
# fi
echo "Create Database..."
echo "sudo -u postgres createdb $DATABASENAME"
sudo -u postgres createdb $DATABASENAME
#CREATE ROLE / USER
echo "Create Postgresql user $DB_USERNAME"
#echo "sudo -u postgres createuser --no-createdb --no-createrole --no-superuser $DB_USERNAME"
#sudo -u postgres createuser --no-createdb --no-createrole --no-superuser $DB_USERNAME
echo "sudo -u postgres psql --command=\"create user $DB_USERNAME with password 'XXXXXXXXXX';\""
sudo -u postgres psql --command="CREATE USER $DB_USERNAME with password '$DB_PASSWORD';"
echo "Grant all privileges to user..."
echo "sudo -u postgres psql --command=\"GRANT ALL PRIVILEGES on database $DATABASENAME to $DB_USERNAME;\""
sudo -u postgres psql --command="GRANT ALL PRIVILEGES on database $DATABASENAME to $DB_USERNAME;"
else
# Create the Database
echo "Remove Existing Database if exists..."
if [ -d "/var/lib/mysql/$DATABASENAME" ]; then
echo "mysql --user=$DB_USERNAME --password=$DB_PASSWORD -e 'DROP DATABASE $DATABASENAME;'"
mysql --user=$DB_USERNAME --password=$DB_PASSWORD -e "DROP DATABASE $DATABASENAME;"
fi
echo "Create Database..."
echo "mysql --user=$DB_USERNAME --password=$DB_PASSWORD -e 'CREATE DATABASE $DATABASENAME CHARACTER SET UTF8;'"
mysql --user=$DB_USERNAME --password=$DB_PASSWORD -e "CREATE DATABASE $DATABASENAME CHARACTER SET UTF8;"
fi
}
#Function to backup the data from the previous installation
func_backup_prev_install(){
if [ -d "$INSTALL_DIR" ]; then
# CDR-Stats is already installed
echo ""
echo "We detect an existing CDR-Stats Installation"
echo "if you continue the existing installation will be removed!"
echo ""
echo "Press Enter to continue or CTRL-C to exit"
read TEMP
mkdir /tmp/old-cdr-stats_$DATETIME
mv $INSTALL_DIR /tmp/old-cdr-stats_$DATETIME
echo "Files from $INSTALL_DIR has been moved to /tmp/old-cdr-stats_$DATETIME"
if [ `sudo -u postgres psql -qAt --list | egrep '^$DATABASENAME\|' | wc -l` -eq 1 ]; then
echo "Run backup with postgresql..."
sudo -u postgres pg_dump $DATABASENAME > /tmp/old-cdr-stats_$DATETIME.pgsqldump.sql
echo "PostgreSQL Dump of database $DATABASENAME added in /tmp/old-cdr-stats_$DATETIME.pgsqldump.sql"
echo "Press Enter to continue"
read TEMP
fi
fi
}
#Function to install the code source
func_install_source(){
#get CDR-Stats
echo "Install CDR-Stats..."
cd /usr/src/
rm -rf cdr-stats
mkdir /var/log/cdr-stats
git clone -b $BRANCH git://github.com/Star2Billing/cdr-stats.git
cd cdr-stats
#Install Develop / Master
if echo $BRANCH | grep -i "^develop" > /dev/null ; then
git checkout -b develop --track origin/develop
fi
# Copy files
cp -r /usr/src/cdr-stats/cdr_stats $INSTALL_DIR
}
#Function to install Python dependencies
func_install_pip_deps(){
echo "func_install_pip_deps..."
#Upgrade pip to latest (1.5)
pip install pip --upgrade
#For python 2.6 only
pip install importlib
case $DIST in
'DEBIAN')
#pip now only installs stable versions by default, so we need to use --pre option
pip install --pre pytz
;;
'CENTOS')
pip install pytz
;;
esac
echo "Install basic requirements..."
for line in $(cat /usr/src/cdr-stats/install/requirements/basic-requirements.txt | grep -v \#)
do
pip install $line
done
echo "Install Django requirements..."
for line in $(cat /usr/src/cdr-stats/install/requirements/django-requirements.txt | grep -v \#)
do
pip install $line --allow-all-external --allow-unverified django-admin-tools
done
#Check Python dependencies
func_check_dependencies
echo "**********"
echo "PIP Freeze"
echo "**********"
pip freeze
}
#Function to prepare settings_local.py
func_prepare_settings(){
#Copy settings_local.py into cdr-stats dir
cp /usr/src/cdr-stats/install/conf/settings_local.py $INSTALL_DIR
#Update Secret Key
echo "Update Secret Key..."
RANDPASSW=`</dev/urandom tr -dc A-Za-z0-9| (head -c $1 > /dev/null 2>&1 || head -c 50)`
sed -i "s/^SECRET_KEY.*/SECRET_KEY = \'$RANDPASSW\'/g" $INSTALL_DIR/settings.py
echo ""
#Disable Debug
sed -i "s/DEBUG = True/DEBUG = False/g" $INSTALL_DIR/settings_local.py
sed -i "s/TEMPLATE_DEBUG = DEBUG/TEMPLATE_DEBUG = False/g" $INSTALL_DIR/settings_local.py
if echo $DB_BACKEND | grep -i "^SQLITE" > /dev/null ; then
# Setup settings_local.py for SQLite
sed -i "s/'init_command/#'init_command/g" $INSTALL_DIR/settings_local.py
elif echo $DB_BACKEND | grep -i "^POSTGRESQL" > /dev/null ; then
#Setup settings_local.py for POSTGRESQL
sed -i "s/DATABASENAME/$DATABASENAME/" $INSTALL_DIR/settings_local.py
sed -i "s/DB_USERNAME/$DB_USERNAME/" $INSTALL_DIR/settings_local.py
sed -i "s/DB_PASSWORD/$DB_PASSWORD/" $INSTALL_DIR/settings_local.py
sed -i "s/DB_HOSTNAME/$DB_HOSTNAME/" $INSTALL_DIR/settings_local.py
sed -i "s/DB_PORT/$DB_PORT/" $INSTALL_DIR/settings_local.py
else
# Setup settings_local.py for MySQL
sed -i "s/'django.db.backends.postgresql_psycopg2'/'django.db.backends.mysql'/" $INSTALL_DIR/settings_local.py
sed -i "s/DATABASENAME/$DATABASENAME/" $INSTALL_DIR/settings_local.py
sed -i "s/DB_USERNAME/$DB_USERNAME/" $INSTALL_DIR/settings_local.py
sed -i "s/DB_PASSWORD/$DB_PASSWORD/" $INSTALL_DIR/settings_local.py
sed -i "s/DB_HOSTNAME/$DB_HOSTNAME/" $INSTALL_DIR/settings_local.py
sed -i "s/DB_PORT/$DB_PORT/" $INSTALL_DIR/settings_local.py
fi
#Setup Timezone
case $DIST in
'DEBIAN')
#Get TZ
ZONE=$(head -1 /etc/timezone)
;;
'CENTOS')
#Get TZ
. /etc/sysconfig/clock
;;
esac
#Set Timezone in settings_local.py
sed -i "s@Europe/Madrid@$ZONE@g" $INSTALL_DIR/settings_local.py
IFCONFIG=`which ifconfig 2>/dev/null||echo /sbin/ifconfig`
IPADDR=`$IFCONFIG eth0|gawk '/inet addr/{print $2}'|gawk -F: '{print $2}'`
if [ -z "$IPADDR" ]; then
clear
echo "we have not detected your IP address automatically, please enter it manually"
read IPADDR
fi
#Update Authorize local IP
sed -i "s/SERVER_IP_PORT/$IPADDR:$HTTP_PORT/g" $INSTALL_DIR/settings_local.py
sed -i "s/#'SERVER_IP',/'$IPADDR',/g" $INSTALL_DIR/settings_local.py
sed -i "s/SERVER_IP/$IPADDR/g" $INSTALL_DIR/settings_local.py
}
#Function Prepare Settings for the CDR backend
func_prepare_backend_settings(){
#Configure Backend Asterisk / Freeswitch etc...
case $INSTALL_TYPE in
'ASTERISK')
DATABASENAME='asteriskcdr'
MYSQLUSER='root'
MYSQLPASSWORD='password'
MYHOST='127.0.0.1'
MYHOSTPORT='3306'
echo ""
echo "Enter database settings for Asterisk..."
echo ""
func_get_mysql_database_setting_asteriskcdrdb
#Update Mysql schema
echo "We will now add a Primary Key to your CDR database"
echo "We advice you to first backup your database prior continuing"
echo ""
echo "Press Enter to continue or CTRL-C to exit"
read TEMP
mysql -u$MYSQLUSER -p$MYSQLPASSWORD -P$MYHOSTPORT -h$MYHOST $DATABASENAME -e "ALTER TABLE cdr ADD acctid BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST;"
#Enable CDR-Stats for Asterisk
sed -i "s/'freeswitch'/'asterisk'/g" $INSTALL_DIR/settings_local.py
#Configure CDR Import
sed -i "s/MYSQL_IMPORT_CDR_DBNAME/$DATABASENAME/g" $INSTALL_DIR/settings_local.py
sed -i "s/MYSQL_IMPORT_CDR_TABLENAME/cdr/g" $INSTALL_DIR/settings_local.py
sed -i "s/MYSQL_IMPORT_CDR_HOST/$MYHOST/g" $INSTALL_DIR/settings_local.py
sed -i "s/MYSQL_IMPORT_CDR_USER/$MYSQLUSER/g" $INSTALL_DIR/settings_local.py
sed -i "s/MYSQL_IMPORT_CDR_PASSWORD/$MYSQLPASSWORD/g" $INSTALL_DIR/settings_local.py
;;
'FREESWITCH')
echo "You will need to configure your CDR Connectors to access your CDRs"
echo "After the installation please edit the config file: $INSTALL_DIR/settings_local.py"
read TEMP
;;
esac
}
#function to configure SELinux
func_configure_selinux(){
if [ "$INSTALLMODE" = "FULL" ]; then
#Setup Firewall / SELINUX
case $DIST in
'CENTOS')
echo ""
echo "We will now add port $HTTP_PORT and port 80 to your Firewall"
echo "Press Enter to continue or CTRL-C to exit"
read TEMP
#add HTTP port
iptables -I INPUT 2 -p tcp -m state --state NEW -m tcp --dport $HTTP_PORT -j ACCEPT
iptables -I INPUT 3 -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
service iptables save
#Selinux to allow apache to access this directory
chcon -Rv --type=httpd_sys_content_t /usr/share/virtualenvs/cdr-stats/
chcon -Rv --type=httpd_sys_content_t $INSTALL_DIR/usermedia
semanage port -a -t http_port_t -p tcp $HTTP_PORT
#Allowing Apache to access Redis port
semanage port -a -t http_port_t -p tcp 6379
setsebool -P httpd_can_network_connect 1
setsebool -P httpd_can_network_connect_db 1
;;
esac
fi
}
#Function to configure Apache
func_configure_http_server(){
# prepare Apache
echo "Prepare Apache configuration..."
echo '
'$WSGI_ADDITIONAL'
Listen *:'$HTTP_PORT'
<VirtualHost *:'$HTTP_PORT'>
DocumentRoot '$INSTALL_DIR'/
ErrorLog /var/log/cdr-stats/err-apache-cdr-stats.log
LogLevel warn
Alias /static/ "'$INSTALL_DIR'/static/"
<Location "/static/">
SetHandler None
</Location>
WSGIPassAuthorization On
WSGIDaemonProcess cdr-stats user='$APACHE_USER' user='$APACHE_USER' threads=25
WSGIProcessGroup cdr-stats
WSGIScriptAlias / '$INSTALL_DIR'/django.wsgi
<Directory '$INSTALL_DIR'>
AllowOverride all
Order deny,allow
Allow from all
'$WSGIApplicationGroup'
</Directory>
</VirtualHost>
' > $APACHE_CONF_DIR/cdr-stats.conf
#correct the above file
sed -i "s/@/'/g" $APACHE_CONF_DIR/cdr-stats.conf
#Restart HTTP Server
service $APACHE_SERVICE restart
}
#Install Django CDR-stats
func_django_cdrstats_install(){
cd $INSTALL_DIR/
#Fix permission on python-egg
mkdir $INSTALL_DIR/.python-eggs
chown $APACHE_USER:$APACHE_USER $INSTALL_DIR/.python-eggs
#upload audio files
mkdir -p $INSTALL_DIR/usermedia/upload/audiofiles
chown -R $APACHE_USER:$APACHE_USER $INSTALL_DIR/usermedia
python manage.py syncdb --noinput
python manage.py migrate
echo "Create a super admin user..."
python manage.py createsuperuser
echo "Install Bower deps"
python manage.py bower_install -- --allow-root
#Collect static files from apps and other locations in a single location.
python manage.py collectstatic --noinput
#Install Celery & redis-server
echo "Install Redis-server ..."
func_install_redis_server
#Redis is needed to load countries
#Load Countries Dialcode
python manage.py load_country_dialcode
}
#Function to install Frontend
func_install_frontend(){
echo ""
echo ""
echo "We will now install CDR-Stats on your server"
echo "============================================"
echo ""
#Prepare
func_prepare_system_frontend
#Backup
func_backup_prev_install
#Install Code Source
func_install_source
#Create and enable virtualenv
func_setup_virtualenv
#Install Pip Dependencies
func_install_pip_deps
#Prepare Settings
func_prepare_settings
#Prepare Backend Settings
func_prepare_backend_settings
#Configure Logs files and logrotate
func_prepare_logger
#Install Django cdr-stats
func_django_cdrstats_install
#Configure SELinux
func_configure_selinux
#Configure Apache
func_configure_http_server
echo ""
echo "**************************************************************"
echo "Congratulations, CDR-Stats Web Application is now installed!"
echo "**************************************************************"
echo ""
echo "Please log on to CDR-Stats at "
echo "http://$IPADDR:$HTTP_PORT"
echo "the username and password are the ones you entered during this installation."
echo ""
echo "Thank you for installing CDR-Stats"
echo "Yours,"
echo "The Star2Billing Team"
echo "http://www.star2billing.com and http://www.cdr-stats.org/"
echo ""
echo ""
}
#Function to install backend
func_install_backend() {
echo ""
echo "This will install CDR-Stats Backend, Celery & Redis on your server"
echo "Press Enter to continue or CTRL-C to exit"
read TEMP
#Configure Logs files and logrotate
func_prepare_logger
#Create directory for pid file
mkdir -p /var/run/celery
#Install Celery & redis-server
echo "Install Redis-server ..."
func_install_redis_server
case $DIST in
'DEBIAN')
# Add init-scripts
cp /usr/src/cdr-stats/install/celery-init/debian/etc/default/cdr-stats-celeryd /etc/default/
cp /usr/src/cdr-stats/install/celery-init/debian/etc/init.d/cdr-stats-celeryd /etc/init.d/
# Configure init-scripts
sed -i "s/CELERYD_USER='celery'/CELERYD_USER='$CELERYD_USER'/g" /etc/default/cdr-stats-celeryd
sed -i "s/CELERYD_GROUP='celery'/CELERYD_GROUP='$CELERYD_GROUP'/g" /etc/default/cdr-stats-celeryd
chmod +x /etc/default/cdr-stats-celeryd
chmod +x /etc/init.d/cdr-stats-celeryd
/etc/init.d/cdr-stats-celeryd restart
cd /etc/init.d; update-rc.d cdr-stats-celeryd defaults 99
#Check permissions on /dev/shm to ensure that celery can start and run for openVZ.
DIR="/dev/shm"
echo "Checking the permissions for $dir"
stat $DIR
if [ `stat -c "%a" $DIR` -ge 777 ] ; then
echo "$DIR has read write permissions."
else
echo "$DIR has no read write permissions."
chmod -R 777 /dev/shm
if [ `grep -i /dev/shm /etc/fstab | wc -l` -eq 0 ]; then
echo "Adding fstab entry to set permissions /dev/shm"
echo "none /dev/shm tmpfs rw,nosuid,nodev,noexec 0 0" >> /etc/fstab
fi
fi
;;
'CENTOS')
# Add init-scripts
cp /usr/src/cdr-stats/install/celery-init/centos/etc/default/cdr-stats-celeryd /etc/default/
cp /usr/src/cdr-stats/install/celery-init/centos/etc/init.d/cdr-stats-celeryd /etc/init.d/
# Configure init-scripts
sed -i "s/CELERYD_USER='celery'/CELERYD_USER='$CELERYD_USER'/g" /etc/default/cdr-stats-celeryd
sed -i "s/CELERYD_GROUP='celery'/CELERYD_GROUP='$CELERYD_GROUP'/g" /etc/default/cdr-stats-celeryd
chmod +x /etc/init.d/cdr-stats-celeryd
/etc/init.d/cdr-stats-celeryd restart
chkconfig --add cdr-stats-celeryd
chkconfig --level 2345 cdr-stats-celeryd on
;;
esac
echo ""
echo ""
echo "**************************************************************"
echo "Congratulations, CDR-Stats Backend is now installed"
echo "**************************************************************"
echo ""
echo "Yours,"
echo "The Star2Billing Team"
echo "http://www.star2billing.com and http://www.cdr-stats.org/"
echo ""
echo ""
}
#Function to install Standalone Backend
func_install_standalone_backend(){
echo ""
echo "This will install the CDR-backend without configuring Apache, etc..."
read TEMP
func_prepare_system_common
#Install Code Source
func_install_source
#Install Pip Dependencies
func_install_pip_deps
#Prepare Settings
func_prepare_settings
#Prepare Backend Settings
func_prepare_backend_settings
#Configure Logs files and logrotate
func_prepare_logger
#Install CDR-Stats Backend
func_install_backend
}
#Install recent version of redis-server
func_install_redis_server() {
case $DIST in
'DEBIAN')
if [ "$(lsb_release -cs)" == "precise" ]; then
#Ubuntu 12.04 TLS
apt-get -y install redis-server
/etc/init.d/redis-server.dpkg-dist start
else
#Ubuntu 10.04 TLS
cd /usr/src
wget https://redis.googlecode.com/files/redis-2.6.4.tar.gz
tar -zxf redis-2.6.4.tar.gz
cd redis-2.6.4
make
make install
cp /usr/src/cdr-stats/install/redis/debian/etc/redis.conf /etc/redis.conf
cp /usr/src/cdr-stats/install/redis/debian/etc/init.d/redis-server /etc/init.d/redis-server
chmod +x /etc/init.d/redis-server
useradd redis
mkdir -p /var/lib/redis
mkdir -p /var/log/redis
chown redis.redis /var/lib/redis
chown redis.redis /var/log/redis
cd /etc/init.d/
update-rc.d -f redis-server defaults
#Start redis-server
/etc/init.d/redis-server start
fi
;;
'CENTOS')
#install redis
yum -y --enablerepo=epel install redis
chkconfig --add redis
chkconfig --level 2345 redis on
/etc/init.d/redis start
;;
esac
}
#Configure Logs files and logrotate
func_prepare_logger() {
touch /var/log/cdr-stats/cdr-stats.log
touch /var/log/cdr-stats/cdr-stats-db.log
touch /var/log/cdr-stats/err-apache-cdr-stats.log
chown -R $APACHE_USER:$APACHE_USER /var/log/cdr-stats
rm /etc/logrotate.d/cdr_stats
touch /etc/logrotate.d/cdr_stats
echo '
/var/log/cdr-stats/*.log {
daily
rotate 10
size = 50M
missingok
compress
}
' > /etc/logrotate.d/cdr_stats
logrotate /etc/logrotate.d/cdr_stats
}
#Menu Section for Script
show_menu_cdr_stats() {
clear
echo " > CDR-Stats Installation Menu"
echo "====================================="
echo " 1) Install All"
echo " 2) Install CDR-Stats Web Frontend"
echo " 3) Install CDR-Stats Backend / CDR-Stats-Celery"
echo " 0) Quit"
echo -n "(0-4) : "
read OPTION < /dev/tty
}
run_menu_cdr_stats_install() {
ExitFinish=0
while [ $ExitFinish -eq 0 ]; do
# Show menu with Installation items
show_menu_cdr_stats
case $OPTION in
1)
func_install_frontend
func_install_backend
echo done
;;
2)
func_install_frontend
;;
3)
func_install_backend
;;
0)
ExitFinish=1
;;
*)
esac
done
}