예제 #1
0
파일: cgr-loader.go 프로젝트: bhepp/cgrates
func main() {
	flag.Parse()
	if *version {
		fmt.Println("CGRateS " + utils.VERSION)
		return
	}
	var errRatingDb, errAccDb, errStorDb, err error
	var ratingDb engine.RatingStorage
	var accountDb engine.AccountingStorage
	var storDb engine.LoadStorage
	var rater, cdrstats, users *rpc.Client
	var loader engine.LoadReader
	if *migrateRC8 != "" {
		var db_nb int
		db_nb, err = strconv.Atoi(*datadb_name)
		if err != nil {
			log.Print("Redis db name must be an integer!")
			return
		}
		host := *datadb_host
		if *datadb_port != "" {
			host += ":" + *datadb_port
		}
		migratorRC8acc, err := NewMigratorRC8(host, db_nb, *datadb_pass, *dbdata_encoding)
		if err != nil {
			log.Print(err.Error())
			return
		}
		if strings.Contains(*migrateRC8, "acc") || strings.Contains(*migrateRC8, "*all") {
			if err := migratorRC8acc.migrateAccounts(); err != nil {
				log.Print(err.Error())
			}
		}

		db_nb, err = strconv.Atoi(*tpdb_name)
		if err != nil {
			log.Print("Redis db name must be an integer!")
			return
		}
		host = *tpdb_host
		if *tpdb_port != "" {
			host += ":" + *tpdb_port
		}
		migratorRC8rat, err := NewMigratorRC8(host, db_nb, *tpdb_pass, *dbdata_encoding)
		if err != nil {
			log.Print(err.Error())
			return
		}
		if strings.Contains(*migrateRC8, "atr") || strings.Contains(*migrateRC8, "*all") {
			if err := migratorRC8rat.migrateActionTriggers(); err != nil {
				log.Print(err.Error())
			}
		}
		if strings.Contains(*migrateRC8, "act") || strings.Contains(*migrateRC8, "*all") {
			if err := migratorRC8rat.migrateActions(); err != nil {
				log.Print(err.Error())
			}
		}
		if strings.Contains(*migrateRC8, "dcs") || strings.Contains(*migrateRC8, "*all") {
			if err := migratorRC8rat.migrateDerivedChargers(); err != nil {
				log.Print(err.Error())
			}
		}
		if strings.Contains(*migrateRC8, "apl") || strings.Contains(*migrateRC8, "*all") {
			if err := migratorRC8rat.migrateActionPlans(); err != nil {
				log.Print(err.Error())
			}
		}
		if strings.Contains(*migrateRC8, "shg") || strings.Contains(*migrateRC8, "*all") {
			if err := migratorRC8rat.migrateSharedGroups(); err != nil {
				log.Print(err.Error())
			}
		}
		log.Print("Done!")
		return
	}
	// Init necessary db connections, only if not already
	if !*dryRun { // make sure we do not need db connections on dry run, also not importing into any stordb
		if *fromStorDb {
			ratingDb, errRatingDb = engine.ConfigureRatingStorage(*tpdb_type, *tpdb_host, *tpdb_port, *tpdb_name,
				*tpdb_user, *tpdb_pass, *dbdata_encoding)
			accountDb, errAccDb = engine.ConfigureAccountingStorage(*datadb_type, *datadb_host, *datadb_port, *datadb_name, *datadb_user, *datadb_pass, *dbdata_encoding)
			storDb, errStorDb = engine.ConfigureLoadStorage(*stor_db_type, *stor_db_host, *stor_db_port, *stor_db_name, *stor_db_user, *stor_db_pass, *dbdata_encoding,
				cgrConfig.StorDBMaxOpenConns, cgrConfig.StorDBMaxIdleConns, cgrConfig.StorDBCDRSIndexes)
		} else if *toStorDb { // Import from csv files to storDb
			storDb, errStorDb = engine.ConfigureLoadStorage(*stor_db_type, *stor_db_host, *stor_db_port, *stor_db_name, *stor_db_user, *stor_db_pass, *dbdata_encoding,
				cgrConfig.StorDBMaxOpenConns, cgrConfig.StorDBMaxIdleConns, cgrConfig.StorDBCDRSIndexes)
		} else { // Default load from csv files to dataDb
			ratingDb, errRatingDb = engine.ConfigureRatingStorage(*tpdb_type, *tpdb_host, *tpdb_port, *tpdb_name,
				*tpdb_user, *tpdb_pass, *dbdata_encoding)
			accountDb, errAccDb = engine.ConfigureAccountingStorage(*datadb_type, *datadb_host, *datadb_port, *datadb_name, *datadb_user, *datadb_pass, *dbdata_encoding)
		}
		// Defer databases opened to be closed when we are done
		for _, db := range []engine.Storage{ratingDb, accountDb, storDb} {
			if db != nil {
				defer db.Close()
			}
		}
		// Stop on db errors
		for _, err = range []error{errRatingDb, errAccDb, errStorDb} {
			if err != nil {
				log.Fatalf("Could not open database connection: %v", err)
			}
		}
		if *toStorDb { // Import files from a directory into storDb
			if *tpid == "" {
				log.Fatal("TPid required, please define it via *-tpid* command argument.")
			}
			csvImporter := engine.TPCSVImporter{
				TPid:     *tpid,
				StorDb:   storDb,
				DirPath:  *dataPath,
				Sep:      ',',
				Verbose:  *verbose,
				ImportId: *runId,
			}
			if errImport := csvImporter.Run(); errImport != nil {
				log.Fatal(errImport)
			}
			return
		}
	}
	if *fromStorDb { // Load Tariff Plan from storDb into dataDb
		loader = storDb
	} else { // Default load from csv files to dataDb
		/*for fn, v := range engine.FileValidators {
			err := engine.ValidateCSVData(path.Join(*dataPath, fn), v.Rule)
			if err != nil {
				log.Fatal(err, "\n\t", v.Message)
			}
		}*/
		loader = engine.NewFileCSVStorage(',',
			path.Join(*dataPath, utils.DESTINATIONS_CSV),
			path.Join(*dataPath, utils.TIMINGS_CSV),
			path.Join(*dataPath, utils.RATES_CSV),
			path.Join(*dataPath, utils.DESTINATION_RATES_CSV),
			path.Join(*dataPath, utils.RATING_PLANS_CSV),
			path.Join(*dataPath, utils.RATING_PROFILES_CSV),
			path.Join(*dataPath, utils.SHARED_GROUPS_CSV),
			path.Join(*dataPath, utils.LCRS_CSV),
			path.Join(*dataPath, utils.ACTIONS_CSV),
			path.Join(*dataPath, utils.ACTION_PLANS_CSV),
			path.Join(*dataPath, utils.ACTION_TRIGGERS_CSV),
			path.Join(*dataPath, utils.ACCOUNT_ACTIONS_CSV),
			path.Join(*dataPath, utils.DERIVED_CHARGERS_CSV),
			path.Join(*dataPath, utils.CDR_STATS_CSV),
			path.Join(*dataPath, utils.USERS_CSV),
			path.Join(*dataPath, utils.ALIASES_CSV),
		)
	}
	tpReader := engine.NewTpReader(ratingDb, accountDb, loader, *tpid, *timezone, *loadHistorySize)
	err = tpReader.LoadAll()
	if err != nil {
		log.Fatal(err)
	}
	if *stats {
		tpReader.ShowStatistics()
	}
	if *validate {
		if !tpReader.IsValid() {
			return
		}
	}
	if *dryRun { // We were just asked to parse the data, not saving it
		return
	}
	if *historyServer != "" { // Init scribeAgent so we can store the differences
		if scribeAgent, err := history.NewProxyScribe(*historyServer, 3, 3); err != nil {
			log.Fatalf("Could not connect to history server, error: %s. Make sure you have properly configured it via -history_server flag.", err.Error())
			return
		} else {
			engine.SetHistoryScribe(scribeAgent)
			//defer scribeAgent.Client.Close()
		}
	} else {
		log.Print("WARNING: Rates history archiving is disabled!")
	}
	if *raterAddress != "" { // Init connection to rater so we can reload it's data
		rater, err = rpc.Dial("tcp", *raterAddress)
		if err != nil {
			log.Fatalf("Could not connect to rater: %s", err.Error())
			return
		}
	} else {
		log.Print("WARNING: Rates automatic cache reloading is disabled!")
	}
	if *cdrstatsAddress != "" { // Init connection to rater so we can reload it's data
		if *cdrstatsAddress == *raterAddress {
			cdrstats = rater
		} else {
			cdrstats, err = rpc.Dial("tcp", *cdrstatsAddress)
			if err != nil {
				log.Fatalf("Could not connect to CDRStats API: %s", err.Error())
				return
			}
		}
	} else {
		log.Print("WARNING: CDRStats automatic data reload is disabled!")
	}
	if *usersAddress != "" { // Init connection to rater so we can reload it's data
		if *usersAddress == *raterAddress {
			users = rater
		} else {
			users, err = rpc.Dial("tcp", *usersAddress)
			if err != nil {
				log.Fatalf("Could not connect to Users API: %s", err.Error())
				return
			}
		}
	} else {
		log.Print("WARNING: Users automatic data reload is disabled!")
	}

	// write maps to database
	if err := tpReader.WriteToDatabase(*flush, *verbose); err != nil {
		log.Fatal("Could not write to database: ", err)
	}
	if len(*historyServer) != 0 && *verbose {
		log.Print("Wrote history.")
	}
	var dstIds, rplIds, rpfIds, actIds, shgIds, alsIds, lcrIds, dcsIds []string
	if rater != nil {
		dstIds, _ = tpReader.GetLoadedIds(utils.DESTINATION_PREFIX)
		rplIds, _ = tpReader.GetLoadedIds(utils.RATING_PLAN_PREFIX)
		rpfIds, _ = tpReader.GetLoadedIds(utils.RATING_PROFILE_PREFIX)
		actIds, _ = tpReader.GetLoadedIds(utils.ACTION_PREFIX)
		shgIds, _ = tpReader.GetLoadedIds(utils.SHARED_GROUP_PREFIX)
		alsIds, _ = tpReader.GetLoadedIds(utils.ALIASES_PREFIX)
		lcrIds, _ = tpReader.GetLoadedIds(utils.LCR_PREFIX)
		dcsIds, _ = tpReader.GetLoadedIds(utils.DERIVEDCHARGERS_PREFIX)
	}
	actTmgIds, _ := tpReader.GetLoadedIds(utils.ACTION_PLAN_PREFIX)
	var statsQueueIds []string
	if cdrstats != nil {
		statsQueueIds, _ = tpReader.GetLoadedIds(utils.CDR_STATS_PREFIX)
	}
	var userIds []string
	if users != nil {
		userIds, _ = tpReader.GetLoadedIds(utils.USERS_PREFIX)
	}
	// release the reader with it's structures
	tpReader.Init()

	// Reload scheduler and cache
	if rater != nil {
		reply := ""

		// Reload cache first since actions could be calling info from within
		if *verbose {
			log.Print("Reloading cache")
		}
		if *flush {
			dstIds, rplIds, rpfIds, lcrIds = nil, nil, nil, nil // Should reload all these on flush
		}
		if err = rater.Call("ApierV1.ReloadCache", utils.ApiReloadCache{
			DestinationIds:   dstIds,
			RatingPlanIds:    rplIds,
			RatingProfileIds: rpfIds,
			ActionIds:        actIds,
			SharedGroupIds:   shgIds,
			Aliases:          alsIds,
			LCRIds:           lcrIds,
			DerivedChargers:  dcsIds,
		}, &reply); err != nil {
			log.Printf("WARNING: Got error on cache reload: %s\n", err.Error())
		}

		if len(actTmgIds) != 0 {
			if *verbose {
				log.Print("Reloading scheduler")
			}
			if err = rater.Call("ApierV1.ReloadScheduler", "", &reply); err != nil {
				log.Printf("WARNING: Got error on scheduler reload: %s\n", err.Error())
			}
		}

	}
	if cdrstats != nil {
		if *flush {
			statsQueueIds = []string{} // Force reload all
		}
		if len(statsQueueIds) != 0 {
			if *verbose {
				log.Print("Reloading CDRStats data")
			}
			var reply string
			if err := cdrstats.Call("CDRStatsV1.ReloadQueues", utils.AttrCDRStatsReloadQueues{StatsQueueIds: statsQueueIds}, &reply); err != nil {
				log.Printf("WARNING: Failed reloading stat queues, error: %s\n", err.Error())
			}
		}
	}

	if users != nil {
		if len(userIds) > 0 {
			if *verbose {
				log.Print("Reloading Users data")
			}
			var reply string
			if err := cdrstats.Call("UsersV1.ReloadUsers", "", &reply); err != nil {
				log.Printf("WARNING: Failed reloading users data, error: %s\n", err.Error())
			}

		}
	}
}
예제 #2
0
파일: apier.go 프로젝트: bhepp/cgrates
func (self *ApierV2) LoadTariffPlanFromFolder(attrs utils.AttrLoadTpFromFolder, reply *engine.LoadInstance) error {
	if len(attrs.FolderPath) == 0 {
		return fmt.Errorf("%s:%s", utils.ErrMandatoryIeMissing.Error(), "FolderPath")
	}
	if fi, err := os.Stat(attrs.FolderPath); err != nil {
		if strings.HasSuffix(err.Error(), "no such file or directory") {
			return utils.ErrInvalidPath
		}
		return utils.NewErrServerError(err)
	} else if !fi.IsDir() {
		return utils.ErrInvalidPath
	}
	loader := engine.NewTpReader(self.RatingDb, self.AccountDb, engine.NewFileCSVStorage(utils.CSV_SEP,
		path.Join(attrs.FolderPath, utils.DESTINATIONS_CSV),
		path.Join(attrs.FolderPath, utils.TIMINGS_CSV),
		path.Join(attrs.FolderPath, utils.RATES_CSV),
		path.Join(attrs.FolderPath, utils.DESTINATION_RATES_CSV),
		path.Join(attrs.FolderPath, utils.RATING_PLANS_CSV),
		path.Join(attrs.FolderPath, utils.RATING_PROFILES_CSV),
		path.Join(attrs.FolderPath, utils.SHARED_GROUPS_CSV),
		path.Join(attrs.FolderPath, utils.LCRS_CSV),
		path.Join(attrs.FolderPath, utils.ACTIONS_CSV),
		path.Join(attrs.FolderPath, utils.ACTION_PLANS_CSV),
		path.Join(attrs.FolderPath, utils.ACTION_TRIGGERS_CSV),
		path.Join(attrs.FolderPath, utils.ACCOUNT_ACTIONS_CSV),
		path.Join(attrs.FolderPath, utils.DERIVED_CHARGERS_CSV),
		path.Join(attrs.FolderPath, utils.CDR_STATS_CSV),
		path.Join(attrs.FolderPath, utils.USERS_CSV),
		path.Join(attrs.FolderPath, utils.ALIASES_CSV),
	), "", self.Config.DefaultTimezone, self.Config.LoadHistorySize)
	if err := loader.LoadAll(); err != nil {
		return utils.NewErrServerError(err)
	}
	if attrs.DryRun {
		*reply = engine.LoadInstance{LoadId: utils.DRYRUN}
		return nil // Mission complete, no errors
	}

	if attrs.Validate {
		if !loader.IsValid() {
			return errors.New("invalid data")
		}
	}

	if err := loader.WriteToDatabase(attrs.FlushDb, false); err != nil {
		return utils.NewErrServerError(err)
	}
	// Make sure the items are in the cache
	dstIds, _ := loader.GetLoadedIds(utils.DESTINATION_PREFIX)
	dstKeys := make([]string, len(dstIds))
	for idx, dId := range dstIds {
		dstKeys[idx] = utils.DESTINATION_PREFIX + dId // Cache expects them as redis keys
	}
	rplIds, _ := loader.GetLoadedIds(utils.RATING_PLAN_PREFIX)
	rpKeys := make([]string, len(rplIds))
	for idx, rpId := range rplIds {
		rpKeys[idx] = utils.RATING_PLAN_PREFIX + rpId
	}
	rpfIds, _ := loader.GetLoadedIds(utils.RATING_PROFILE_PREFIX)
	rpfKeys := make([]string, len(rpfIds))
	for idx, rpfId := range rpfIds {
		rpfKeys[idx] = utils.RATING_PROFILE_PREFIX + rpfId
	}
	actIds, _ := loader.GetLoadedIds(utils.ACTION_PREFIX)
	actKeys := make([]string, len(actIds))
	for idx, actId := range actIds {
		actKeys[idx] = utils.ACTION_PREFIX + actId
	}
	aplIds, _ := loader.GetLoadedIds(utils.ACTION_PLAN_PREFIX)
	aplKeys := make([]string, len(aplIds))
	for idx, aplId := range aplIds {
		aplKeys[idx] = utils.ACTION_PLAN_PREFIX + aplId
	}
	shgIds, _ := loader.GetLoadedIds(utils.SHARED_GROUP_PREFIX)
	shgKeys := make([]string, len(shgIds))
	for idx, shgId := range shgIds {
		shgKeys[idx] = utils.SHARED_GROUP_PREFIX + shgId
	}
	aliases, _ := loader.GetLoadedIds(utils.ALIASES_PREFIX)
	alsKeys := make([]string, len(aliases))
	for idx, alias := range aliases {
		alsKeys[idx] = utils.ALIASES_PREFIX + alias
	}
	lcrIds, _ := loader.GetLoadedIds(utils.LCR_PREFIX)
	lcrKeys := make([]string, len(lcrIds))
	for idx, lcrId := range lcrIds {
		lcrKeys[idx] = utils.LCR_PREFIX + lcrId
	}
	dcs, _ := loader.GetLoadedIds(utils.DERIVEDCHARGERS_PREFIX)
	dcsKeys := make([]string, len(dcs))
	for idx, dc := range dcs {
		dcsKeys[idx] = utils.DERIVEDCHARGERS_PREFIX + dc
	}
	aps, _ := loader.GetLoadedIds(utils.ACTION_PLAN_PREFIX)
	utils.Logger.Info("ApierV1.LoadTariffPlanFromFolder, reloading cache.")

	cstKeys, _ := loader.GetLoadedIds(utils.CDR_STATS_PREFIX)
	userKeys, _ := loader.GetLoadedIds(utils.USERS_PREFIX)
	li := loader.GetLoadInstance()

	// release the tp data
	loader.Init()

	if err := self.RatingDb.CacheRatingPrefixValues(map[string][]string{
		utils.DESTINATION_PREFIX:     dstKeys,
		utils.RATING_PLAN_PREFIX:     rpKeys,
		utils.RATING_PROFILE_PREFIX:  rpfKeys,
		utils.LCR_PREFIX:             lcrKeys,
		utils.DERIVEDCHARGERS_PREFIX: dcsKeys,
		utils.ACTION_PREFIX:          actKeys,
		utils.ACTION_PLAN_PREFIX:     aplKeys,
		utils.SHARED_GROUP_PREFIX:    shgKeys,
	}); err != nil {
		return err
	}
	if err := self.AccountDb.CacheAccountingPrefixValues(map[string][]string{
		utils.ALIASES_PREFIX: alsKeys,
	}); err != nil {
		return err
	}
	if len(aps) != 0 && self.Sched != nil {
		utils.Logger.Info("ApierV1.LoadTariffPlanFromFolder, reloading scheduler.")
		self.Sched.Reload(true)
	}
	if len(cstKeys) != 0 && self.CdrStatsSrv != nil {
		if err := self.CdrStatsSrv.ReloadQueues(cstKeys, nil); err != nil {
			return err
		}
	}

	if len(userKeys) != 0 && self.Users != nil {
		var r string
		if err := self.Users.ReloadUsers("", &r); err != nil {
			return err
		}
	}
	*reply = *li
	return nil
}
예제 #3
0
파일: apier.go 프로젝트: cgrates/cgrates
func (self *ApierV1) LoadTariffPlanFromFolder(attrs utils.AttrLoadTpFromFolder, reply *string) error {
	if len(attrs.FolderPath) == 0 {
		return fmt.Errorf("%s:%s", utils.ErrMandatoryIeMissing.Error(), "FolderPath")
	}
	if fi, err := os.Stat(attrs.FolderPath); err != nil {
		if strings.HasSuffix(err.Error(), "no such file or directory") {
			return utils.ErrInvalidPath
		}
		return utils.NewErrServerError(err)
	} else if !fi.IsDir() {
		return utils.ErrInvalidPath
	}
	loader := engine.NewTpReader(self.RatingDb, self.AccountDb, engine.NewFileCSVStorage(utils.CSV_SEP,
		path.Join(attrs.FolderPath, utils.DESTINATIONS_CSV),
		path.Join(attrs.FolderPath, utils.TIMINGS_CSV),
		path.Join(attrs.FolderPath, utils.RATES_CSV),
		path.Join(attrs.FolderPath, utils.DESTINATION_RATES_CSV),
		path.Join(attrs.FolderPath, utils.RATING_PLANS_CSV),
		path.Join(attrs.FolderPath, utils.RATING_PROFILES_CSV),
		path.Join(attrs.FolderPath, utils.SHARED_GROUPS_CSV),
		path.Join(attrs.FolderPath, utils.LCRS_CSV),
		path.Join(attrs.FolderPath, utils.ACTIONS_CSV),
		path.Join(attrs.FolderPath, utils.ACTION_PLANS_CSV),
		path.Join(attrs.FolderPath, utils.ACTION_TRIGGERS_CSV),
		path.Join(attrs.FolderPath, utils.ACCOUNT_ACTIONS_CSV),
		path.Join(attrs.FolderPath, utils.DERIVED_CHARGERS_CSV),
		path.Join(attrs.FolderPath, utils.CDR_STATS_CSV),
		path.Join(attrs.FolderPath, utils.USERS_CSV),
		path.Join(attrs.FolderPath, utils.ALIASES_CSV),
		path.Join(attrs.FolderPath, utils.ResourceLimitsCsv),
	), "", self.Config.DefaultTimezone)
	if err := loader.LoadAll(); err != nil {
		return utils.NewErrServerError(err)
	}
	if attrs.DryRun {
		*reply = OK
		return nil // Mission complete, no errors
	}

	if attrs.Validate {
		if !loader.IsValid() {
			*reply = OK
			return errors.New("invalid data")
		}
	}

	if err := loader.WriteToDatabase(attrs.FlushDb, false, false); err != nil {
		return utils.NewErrServerError(err)
	}
	utils.Logger.Info("ApierV1.LoadTariffPlanFromFolder, reloading cache.")
	for _, prfx := range []string{utils.DESTINATION_PREFIX,
		utils.RATING_PLAN_PREFIX,
		utils.RATING_PROFILE_PREFIX,
		utils.ACTION_PREFIX,
		utils.ACTION_PLAN_PREFIX,
		utils.SHARED_GROUP_PREFIX,
		utils.DERIVEDCHARGERS_PREFIX,
		utils.LCR_PREFIX,
		utils.ALIASES_PREFIX,
		utils.ResourceLimitsPrefix} {
		loadedIDs, _ := loader.GetLoadedIds(prfx)
		if err := self.RatingDb.CacheDataFromDB(prfx, loadedIDs, true); err != nil {
			return utils.NewErrServerError(err)
		}
	}
	aps, _ := loader.GetLoadedIds(utils.ACTION_PLAN_PREFIX)
	cstKeys, _ := loader.GetLoadedIds(utils.CDR_STATS_PREFIX)
	userKeys, _ := loader.GetLoadedIds(utils.USERS_PREFIX)

	// relase the tp data
	loader.Init()

	if len(aps) != 0 {
		sched := self.ServManager.GetScheduler()
		if sched != nil {
			sched.Reload()
		}
	}
	if len(cstKeys) != 0 && self.CdrStatsSrv != nil {
		var out int
		if err := self.CdrStatsSrv.Call("CDRStatsV1.ReloadQueues", cstKeys, &out); err != nil {
			return err
		}
	}
	if len(userKeys) != 0 && self.Users != nil {
		var r string
		if err := self.Users.Call("UsersV1.ReloadUsers", "", &r); err != nil {
			return err
		}
	}
	*reply = utils.OK
	return nil
}