initial commit
This commit is contained in:
		
							
								
								
									
										35
									
								
								services/discovery.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								services/discovery.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,35 @@
 | 
			
		||||
package services
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"os"
 | 
			
		||||
 | 
			
		||||
	SelfAPI "cloud.o-forge.io/core/oc-catalog/selfapi"
 | 
			
		||||
	"github.com/beego/beego/v2/core/logs"
 | 
			
		||||
	beego "github.com/beego/beego/v2/server/web"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var DC_NAME string
 | 
			
		||||
 | 
			
		||||
func GetSelfAPI(host string) *SelfAPI.APIClient {
 | 
			
		||||
	return SelfAPI.NewAPIClient(&SelfAPI.Configuration{BasePath: "http://" + host + "/v1"})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func Discoveryinit() {
 | 
			
		||||
 | 
			
		||||
	dcNameOS := os.Getenv("DOCKER_DCNAME")
 | 
			
		||||
	if len(dcNameOS) != 0 {
 | 
			
		||||
		DC_NAME = dcNameOS
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	//FIXME: Beego doesn't retrieve the dcname
 | 
			
		||||
	beegoDC, err := beego.AppConfig.String("DCNAME")
 | 
			
		||||
	if err == nil && len(beegoDC) != 0 {
 | 
			
		||||
		DC_NAME = beegoDC
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	DC_NAME = "DC_DEFAULT"
 | 
			
		||||
	logs.Warning("Default DC name is used")
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										28
									
								
								services/init.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								services/init.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,28 @@
 | 
			
		||||
package services
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"os"
 | 
			
		||||
 | 
			
		||||
	"github.com/beego/beego/v2/core/logs"
 | 
			
		||||
	beego "github.com/beego/beego/v2/server/web"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func Init() {
 | 
			
		||||
	Discoveryinit() //First init DC name
 | 
			
		||||
 | 
			
		||||
	var DBpoint string
 | 
			
		||||
	var err error
 | 
			
		||||
 | 
			
		||||
	DBpoint = os.Getenv("DOCKER_DBPOINT")
 | 
			
		||||
	if len(DBpoint) == 0 {
 | 
			
		||||
		DBpoint, err = beego.AppConfig.String("DBPOINT")
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			logs.Critical("DBPOINT URI error: %v", err)
 | 
			
		||||
			panic(err)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	Mongoinit(DC_NAME + "-" + DBpoint)
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										157
									
								
								services/mongo.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										157
									
								
								services/mongo.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,157 @@
 | 
			
		||||
package services
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"os"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"github.com/beego/beego/v2/core/logs"
 | 
			
		||||
	beego "github.com/beego/beego/v2/server/web"
 | 
			
		||||
 | 
			
		||||
	"go.mongodb.org/mongo-driver/mongo"
 | 
			
		||||
	"go.mongodb.org/mongo-driver/mongo/options"
 | 
			
		||||
	"go.mongodb.org/mongo-driver/x/bsonx"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type MongoCollectionNames struct {
 | 
			
		||||
	DATA       string
 | 
			
		||||
	COMPUTING  string
 | 
			
		||||
	STORAGE    string
 | 
			
		||||
	DATACENTER string
 | 
			
		||||
	WORKSPACE  string
 | 
			
		||||
	SCHEDULE   string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var (
 | 
			
		||||
	mngoClient *mongo.Client
 | 
			
		||||
	mngoDB     *mongo.Database
 | 
			
		||||
	MngoCtx    context.Context
 | 
			
		||||
 | 
			
		||||
	MngoNamesCollection = MongoCollectionNames{
 | 
			
		||||
		DATA:       "datas",
 | 
			
		||||
		COMPUTING:  "computings",
 | 
			
		||||
		STORAGE:    "storages",
 | 
			
		||||
		DATACENTER: "datacenters",
 | 
			
		||||
		WORKSPACE:  "WORKSPACE",
 | 
			
		||||
		SCHEDULE:   "SCHEDULE",
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	MngoCollData       *mongo.Collection
 | 
			
		||||
	MngoCollComputing  *mongo.Collection
 | 
			
		||||
	MngoCollStorage    *mongo.Collection
 | 
			
		||||
	MngoCollDatacenter *mongo.Collection
 | 
			
		||||
	MngoCollWorkspace  *mongo.Collection
 | 
			
		||||
	MngoCollSchedule   *mongo.Collection
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// func GetMongoDBclient() *mongo.Client {
 | 
			
		||||
// 	return mongoDBclient
 | 
			
		||||
// }
 | 
			
		||||
 | 
			
		||||
func MongoDisconnect() {
 | 
			
		||||
	if err := mngoClient.Disconnect(MngoCtx); err != nil {
 | 
			
		||||
		panic(err)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func Mongoinit(DBname string) {
 | 
			
		||||
 | 
			
		||||
	var baseConfig string
 | 
			
		||||
 | 
			
		||||
	if len(os.Getenv("DOCKER_ENVIRONMENT")) == 0 {
 | 
			
		||||
		baseConfig = "mongodb"
 | 
			
		||||
	} else {
 | 
			
		||||
		baseConfig = "mongodb_docker"
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	mongoURI, err := beego.AppConfig.String(baseConfig + "::url")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		logs.Critical("MongoDB URI error: %v", err)
 | 
			
		||||
		panic(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	logs.Info("Connecting to %v", mongoURI)
 | 
			
		||||
 | 
			
		||||
	clientOptions := options.Client().ApplyURI(mongoURI)
 | 
			
		||||
	// mngoClient, err = mongo.NewClient(options.Client().ApplyURI(mongoURI))
 | 
			
		||||
	// if err = mngoClient.Connect(MngoCtx); err != nil {
 | 
			
		||||
	// 	logs.Critical("Mongodb NewClient %v: %v", mongoURI, err)
 | 
			
		||||
	// 	panic(err)
 | 
			
		||||
	// }
 | 
			
		||||
	MngoCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
 | 
			
		||||
 | 
			
		||||
	defer cancel()
 | 
			
		||||
 | 
			
		||||
	// Ping the primary
 | 
			
		||||
	if mngoClient, err = mongo.Connect(MngoCtx, clientOptions); err != nil {
 | 
			
		||||
		logs.Critical("Mongodb Connect %v: %v", mongoURI, err)
 | 
			
		||||
		panic(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if err = mngoClient.Ping(MngoCtx, nil); err != nil {
 | 
			
		||||
		logs.Critical("Mongodb Ping %v: %v", mongoURI, err)
 | 
			
		||||
		panic(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	mngoDB = mngoClient.Database(DBname)
 | 
			
		||||
 | 
			
		||||
	MngoCollData = mngoDB.Collection(MngoNamesCollection.DATA)
 | 
			
		||||
	MngoCollComputing = mngoDB.Collection(MngoNamesCollection.COMPUTING)
 | 
			
		||||
	MngoCollStorage = mngoDB.Collection(MngoNamesCollection.STORAGE)
 | 
			
		||||
	MngoCollDatacenter = mngoDB.Collection(MngoNamesCollection.DATACENTER)
 | 
			
		||||
	MngoCollWorkspace = mngoDB.Collection(MngoNamesCollection.WORKSPACE)
 | 
			
		||||
	MngoCollSchedule = mngoDB.Collection(MngoNamesCollection.SCHEDULE)
 | 
			
		||||
 | 
			
		||||
	if _, err = MngoCollData.Indexes().CreateMany(MngoCtx, []mongo.IndexModel{
 | 
			
		||||
		{
 | 
			
		||||
			Keys: bsonx.Doc{
 | 
			
		||||
				{Key: "description", Value: bsonx.String("text")},
 | 
			
		||||
				{Key: "example", Value: bsonx.String("text")},
 | 
			
		||||
			},
 | 
			
		||||
		},
 | 
			
		||||
	}); err != nil && err.(mongo.CommandError).Code != 85 {
 | 
			
		||||
		logs.Critical(err)
 | 
			
		||||
		panic(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if _, err = MngoCollComputing.Indexes().CreateMany(MngoCtx, []mongo.IndexModel{
 | 
			
		||||
		{
 | 
			
		||||
			Keys: bsonx.Doc{
 | 
			
		||||
				{Key: "description", Value: bsonx.String("text")},
 | 
			
		||||
				{Key: "owner", Value: bsonx.String("text")},
 | 
			
		||||
				{Key: "license", Value: bsonx.String("text")},
 | 
			
		||||
			},
 | 
			
		||||
		},
 | 
			
		||||
	}); err != nil && err.(mongo.CommandError).Code != 85 {
 | 
			
		||||
		logs.Critical(err)
 | 
			
		||||
		panic(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if _, err = MngoCollStorage.Indexes().CreateMany(MngoCtx, []mongo.IndexModel{
 | 
			
		||||
		{
 | 
			
		||||
			Keys: bsonx.Doc{
 | 
			
		||||
				{Key: "name", Value: bsonx.String("text")},
 | 
			
		||||
				{Key: "description", Value: bsonx.String("text")},
 | 
			
		||||
			},
 | 
			
		||||
		},
 | 
			
		||||
	}); err != nil && err.(mongo.CommandError).Code != 85 {
 | 
			
		||||
		logs.Critical(err)
 | 
			
		||||
		panic(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if _, err = MngoCollDatacenter.Indexes().CreateMany(MngoCtx, []mongo.IndexModel{
 | 
			
		||||
		{
 | 
			
		||||
			Keys: bsonx.Doc{
 | 
			
		||||
				{Key: "name", Value: bsonx.String("text")},
 | 
			
		||||
				{Key: "description", Value: bsonx.String("text")},
 | 
			
		||||
				{Key: "owner", Value: bsonx.String("text")},
 | 
			
		||||
			},
 | 
			
		||||
		},
 | 
			
		||||
	}); err != nil && err.(mongo.CommandError).Code != 85 {
 | 
			
		||||
		logs.Critical(err)
 | 
			
		||||
		panic(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	logs.Info("Database is READY")
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user