From 04607e429d40aba906eb0b0a80227cf048dace6a Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Thu, 24 Sep 2015 19:15:59 +0300 Subject: [PATCH] created cassandra initialization scripts --- data/docker/devel/start.sh | 11 +++ data/storage/cassandra/create_cdrs_tables.cql | 98 +++++++++++++++++++ .../cassandra/create_db_with_users.cql | 6 ++ data/storage/cassandra/setup_cgr_db.sh | 27 +++++ engine/storage_cassandra.go | 34 ++++++- 5 files changed, 174 insertions(+), 2 deletions(-) create mode 100644 data/storage/cassandra/create_cdrs_tables.cql create mode 100644 data/storage/cassandra/create_db_with_users.cql create mode 100755 data/storage/cassandra/setup_cgr_db.sh diff --git a/data/docker/devel/start.sh b/data/docker/devel/start.sh index 9c5cd82b7..6f8be6a4e 100755 --- a/data/docker/devel/start.sh +++ b/data/docker/devel/start.sh @@ -2,6 +2,8 @@ sed -i 's/127.0.0.1/0.0.0.0/g' /etc/redis/redis.conf /etc/mysql/my.cnf echo 'host all all 0.0.0.0/32 md5'>>/etc/postgresql/9.4/main/pg_hba.conf sed -i 's/ulimit/#ulimit/g' /etc/init.d/cassandra +sed -i 's/AllowAllAuthenticator/PasswordAuthenticator/g' /etc/cassandra/cassandra.yaml +sed -i 's/AllowAllAuthorizer/CassandraAuthorizer/g' /etc/cassandra/cassandra.yaml /etc/init.d/mysql start /etc/init.d/postgresql start @@ -13,9 +15,18 @@ ln -s /root/code/src/github.com/cgrates/cgrates/data /usr/share/cgrates # create link to cgrates dir ln -s /root/code/src/github.com/cgrates/cgrates /root/cgr +#setup mysql cd /usr/share/cgrates/storage/mysql && ./setup_cgr_db.sh root CGRateS.org + +# setup postgres cd /usr/share/cgrates/storage/postgres && ./setup_cgr_db.sh +# setup cassandra +(sleep 20 && \ + cqlsh -u cassandra -p cassandra -e "alter user cassandra with password 'CGRateS.org';" && \ + cd /usr/share/cgrates/storage/cassandra && ./setup_cgr_db.sh cassandra CGRateS.org && \ + cd /root/cgr)& + #env vars export GOROOT=/root/go; export GOPATH=/root/code; export PATH=$GOROOT/bin:$GOPATH/bin:$PATH export GO15VENDOREXPERIMENT=1 diff --git a/data/storage/cassandra/create_cdrs_tables.cql b/data/storage/cassandra/create_cdrs_tables.cql new file mode 100644 index 000000000..e3dfb7a79 --- /dev/null +++ b/data/storage/cassandra/create_cdrs_tables.cql @@ -0,0 +1,98 @@ + +-- +-- Table structure for table cdrs_primary +-- + +drop table if exists cgrates.cdrs_primary; +create table cgrates.cdrs_primary ( + id uuid, + cgrid ascii, + tor text, + accid text, + cdrhost text, + cdrsource text, + reqtype text, + direction text, + tenant text, + category text, + account text, + subject text, + destination text, + setup_time timestamp, + pdd decimal, + answer_time timestamp, + usage decimal, + supplier text, + disconnect_cause text, + created_at timestamp, + deleted_at timestamp, + PRIMARY KEY (id), +); + +-- +-- Table structure for table cdrs_extra +-- + +DROP TABLE IF EXISTS cgrates.cdrs_extra; +CREATE TABLE cgrates.cdrs_extra ( + id uuid, + cgrid ascii, + extra_fields text, + created_at timestamp, + deleted_at timestamp, + PRIMARY KEY (id), +); + +-- +-- Table structure for table cost_details +-- + +DROP TABLE IF EXISTS cgrates.cost_details; +CREATE TABLE cgrates.cost_details ( + id uuid, + cgrid ascii, + runid text, + tor text, + direction text, + tenant text, + category text, + account text, + subject text, + destination text, + cost decimal, + timespans text, + cost_source text, + created_at timestamp, + updated_at timestamp, + deleted_at timestamp, + PRIMARY KEY (id), +); + +-- +-- Table structure for table rated_cdrs +-- +DROP TABLE IF EXISTS cgrates.rated_cdrs; +CREATE TABLE cgrates.rated_cdrs ( + id uuid, + cgrid ascii, + runid text, + reqtype text, + direction text, + tenant text, + category text, + account text, + subject text, + destination text, + setup_time timestamp, + pdd decimal, + answer_time timestamp, + usage decimal, + supplier text, + disconnect_cause text, + cost decimal, + extra_info text, + created_at timestamp, + updated_at timestamp, + deleted_at timestamp, + PRIMARY KEY (id), +); diff --git a/data/storage/cassandra/create_db_with_users.cql b/data/storage/cassandra/create_db_with_users.cql new file mode 100644 index 000000000..2386f3794 --- /dev/null +++ b/data/storage/cassandra/create_db_with_users.cql @@ -0,0 +1,6 @@ +DROP KEYSPACE IF EXISTS cgrates; + +create keyspace cgrates with replication = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 }; + +create user cgrates with password 'CGRateS.org'; +grant all on keyspace cgrates to cgrates; diff --git a/data/storage/cassandra/setup_cgr_db.sh b/data/storage/cassandra/setup_cgr_db.sh new file mode 100755 index 000000000..267e89b2f --- /dev/null +++ b/data/storage/cassandra/setup_cgr_db.sh @@ -0,0 +1,27 @@ +#! /usr/bin/env sh + + +if test $# -lt 2; then + echo "" + echo "setup_cgr_db.sh []" + echo "" + exit 0 +fi + +user=$1 +pass=$2 +host=$3 +if [ -z "$3" ]; then + host="localhost" +fi +cqlsh -u $user -p $pass $host -f create_db_with_users.cql +cu=$? +cqlsh -u $user -p $pass $host -f create_cdrs_tables.cql +cdrt=$? + +if [ $cu = 0 ] && [ $cdrt = 0 ]; then + echo "" + echo "\t+++ CGR-DB successfully set-up! +++" + echo "" + exit 0 +fi diff --git a/engine/storage_cassandra.go b/engine/storage_cassandra.go index f3fb76ae5..5e1111177 100644 --- a/engine/storage_cassandra.go +++ b/engine/storage_cassandra.go @@ -40,8 +40,38 @@ func (cs *CassandraStorage) Flush(ignore string) (err error) { return cs.db.Query(fmt.Sprintf("delete * from %s", cs.keyspace)).Exec() } -func (cs *CassandraStorage) SetRatedCdr(*StoredCdr) error { return nil } -func (cs *CassandraStorage) LogCallCost(cgrid, source, runid string, cc *CallCost) error { return nil } +func (cs *CassandraStorage) SetRatedCdr(*StoredCdr) error { return nil } +func (cs *CassandraStorage) LogCallCost(cgrid, source, runid string, cc *CallCost) error { + /*if cc == nil { + return nil + } + tss, err := json.Marshal(cc.Timespans) + if err != nil { + Logger.Err(fmt.Sprintf("Error marshalling timespans to json: %v", err)) + return err + } + _, err = self.Db.Exec(fmt.Sprintf("INSERT INTO %s (cgrid,runid,tor,direction,tenant,category,account,subject,destination,cost,timespans,cost_source,created_at) VALUES ('%s','%s','%s','%s','%s','%s','%s','%s','%s',%f,'%s','%s','%s') ON DUPLICATE KEY UPDATE tor=values(tor),direction=values(direction),tenant=values(tenant),category=values(category),account=values(account),subject=values(subject),destination=values(destination),cost=values(cost),timespans=values(timespans),cost_source=values(cost_source),updated_at='%s'", + utils.TBL_COST_DETAILS, + cgrid, + runid, + cc.TOR, + cc.Direction, + cc.Tenant, + cc.Category, + cc.Account, + cc.Subject, + cc.Destination, + cc.Cost, + tss, + source, + time.Now().Format(time.RFC3339), + time.Now().Format(time.RFC3339))) + if err != nil { + Logger.Err(fmt.Sprintf("failed to execute insert statement: %v", err)) + return err + }*/ + return nil +} func (cs *CassandraStorage) GetCallCostLog(cgrid, source, runid string) (*CallCost, error) { return nil, nil }