Improved the startup for a new environment

This commit is contained in:
Thorsten Sommer 2015-06-21 20:20:27 +02:00
parent 16ac349049
commit e1465c2fd0
7 changed files with 148 additions and 85 deletions

View File

@ -2,6 +2,7 @@ package Configuration
import (
"encoding/json"
"fmt"
"github.com/SommerEngineering/Ocean/Log"
"github.com/SommerEngineering/Ocean/Log/Meta"
"os"
@ -20,18 +21,19 @@ func readConfiguration() {
// Access to the working directory?
currentDir, dirError := os.Getwd()
if dirError != nil {
panic(`Was not able to read the working directory: ` + dirError.Error())
return
fmt.Printf("[Error] Was not able to read the working directory. %s\n", dirError.Error())
os.Exit(0)
}
// Access to the configuration file?
currentPath := filepath.Join(currentDir, filename)
if _, errFile := os.Stat(currentPath); errFile != nil {
if os.IsNotExist(errFile) {
panic(`It was not possible to find the necessary configuration file 'configuration.json' at the application directory.`)
fmt.Printf("[Error] Cannot read the project name file 'configuration.json': File not found! Please read https://github.com/SommerEngineering/Ocean\n")
} else {
panic(`There was an error while open the configuration: ` + errFile.Error())
fmt.Printf("[Error] Cannot read the project name file 'configuration.json': %s. Please read https://github.com/SommerEngineering/Ocean\n", errFile.Error())
}
os.Exit(0)
}
// Open the file:
@ -39,8 +41,8 @@ func readConfiguration() {
defer file.Close()
if fileError != nil {
panic(`The configuration file is not accessible: ` + fileError.Error())
return
fmt.Printf("[Error] The configuration file 'configuration.json' is not accessible: %s. Please read https://github.com/SommerEngineering/Ocean\n", fileError.Error())
os.Exit(0)
}
// Try to decode / parse the file:
@ -48,7 +50,8 @@ func readConfiguration() {
decError := decoder.Decode(&configuration)
if decError != nil {
panic(`Decoding of the configuration file was not possible: ` + decError.Error())
fmt.Printf("[Error] Decoding of the configuration file 'configuration.json' was not possible: %s. Please read https://github.com/SommerEngineering/Ocean\n", decError.Error())
os.Exit(0)
}
Log.LogShort(senderName, Meta.CategorySYSTEM, Meta.LevelINFO, Meta.MessageNameINIT, `Init of configuration is done.`)

View File

@ -0,0 +1,51 @@
package ConfigurationDB
import (
"fmt"
"github.com/SommerEngineering/Ocean/Configuration/Meta"
"github.com/SommerEngineering/Ocean/Log"
LM "github.com/SommerEngineering/Ocean/Log/Meta"
"gopkg.in/mgo.v2"
"os"
)
// Try to connect to the database.
func connectDatabase(config Meta.Configuration) {
defer func() {
if err := recover(); err != nil {
fmt.Printf("[Error] Was not able to connect to the configuration database: %s. Please read https://github.com/SommerEngineering/Ocean.\n", err)
os.Exit(0)
}
}()
// Connect to MongoDB:
if newSession, errDial := mgo.Dial(config.ConfigDBHostname); errDial != nil {
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityUnknown, LM.ImpactUnknown, LM.MessageNameDATABASE, `It was not possible to connect to the MongoDB host `+config.ConfigDBHostname, errDial.Error())
fmt.Printf("[Error] Was not able to connect to the configuration database: %s. Please read https://github.com/SommerEngineering/Ocean.\n", errDial.Error())
os.Exit(0)
} else {
session = newSession
}
// Use the correct database:
db = session.DB(config.ConfigDBDatabase)
// Login:
if errLogin := db.Login(config.ConfigDBConfigurationCollectionUsername, config.ConfigDBConfigurationCollectionPassword); errLogin != nil {
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelSECURITY, LM.SeverityUnknown, LM.ImpactUnknown, LM.MessageNameDATABASE, `It was not possible to login the user `+config.ConfigDBConfigurationCollectionUsername, errLogin.Error())
fmt.Printf("[Error] Was not able to connect to the configuration database: %s. Please read https://github.com/SommerEngineering/Ocean.\n", errLogin.Error())
os.Exit(0)
}
// In case of write operations, wait for the majority of servers to be done:
session.SetSafe(&mgo.Safe{WMode: "majority"})
// Set the consistency mode to read from any secondary server and write to the primary.
session.SetMode(mgo.Eventual, true)
// Get the collection:
collection = db.C(config.ConfigDBConfigurationCollection)
// Take care about the index:
collection.EnsureIndexKey(`Name`)
}

View File

@ -4,41 +4,14 @@ import (
"github.com/SommerEngineering/Ocean/Configuration"
"github.com/SommerEngineering/Ocean/Log"
LM "github.com/SommerEngineering/Ocean/Log/Meta"
"gopkg.in/mgo.v2"
)
// The init function for this package.
func init() {
config := Configuration.Read()
// Connect to MongoDB:
if newSession, errDial := mgo.Dial(config.ConfigDBHostname); errDial != nil {
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityUnknown, LM.ImpactUnknown, LM.MessageNameDATABASE, `It was not possible to connect to the MongoDB host `+config.ConfigDBHostname, errDial.Error())
return
} else {
session = newSession
}
// Use the correct database:
db = session.DB(config.ConfigDBDatabase)
// Login:
if errLogin := db.Login(config.ConfigDBConfigurationCollectionUsername, config.ConfigDBConfigurationCollectionPassword); errLogin != nil {
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelSECURITY, LM.SeverityUnknown, LM.ImpactUnknown, LM.MessageNameDATABASE, `It was not possible to login the user `+config.ConfigDBConfigurationCollectionUsername, errLogin.Error())
return
}
// In case of write operations, wait for the majority of servers to be done:
session.SetSafe(&mgo.Safe{WMode: "majority"})
// Set the consistency mode to read from any secondary server and write to the primary.
session.SetMode(mgo.Eventual, true)
// Get the collection:
collection = db.C(config.ConfigDBConfigurationCollection)
// Take care about the index:
collection.EnsureIndexKey(`Name`)
// Connect to the database:
connectDatabase(config)
// Check the system configuration:
checkConfiguration()

View File

@ -0,0 +1,64 @@
package CustomerDB
import (
"fmt"
"github.com/SommerEngineering/Ocean/Log"
LM "github.com/SommerEngineering/Ocean/Log/Meta"
"gopkg.in/mgo.v2"
"os"
)
// Try to connect to the database.
func connectDatabase(host, username, password, database string) {
defer func() {
if err := recover(); err != nil {
fmt.Printf("[Error] Was not able to connect to the customer database: %s. Please read https://github.com/SommerEngineering/Ocean.\n", err)
os.Exit(0)
}
}()
// Connect to MongoDB:
if newSession, errDial := mgo.Dial(host); errDial != nil {
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityUnknown, LM.ImpactUnknown, LM.MessageNameDATABASE, `It was not possible to connect to the MongoDB host `+host, errDial.Error())
fmt.Printf("[Error] Was not able to connect to the customer database: %s. Please read https://github.com/SommerEngineering/Ocean.\n", errDial.Error())
os.Exit(0)
} else {
mainSession = newSession
}
// Use the correct database:
db := mainSession.DB(database)
if db == nil {
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityCritical, LM.ImpactCritical, LM.MessageNameDATABASE, `Was not able to get the customer database.`)
fmt.Printf("[Error] Was not able to connect to the customer database. Please read https://github.com/SommerEngineering/Ocean.\n")
os.Exit(0)
}
// Login:
if errLogin := db.Login(username, password); errLogin != nil {
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelSECURITY, LM.SeverityUnknown, LM.ImpactUnknown, LM.MessageNameDATABASE, `It was not possible to login the user `+databaseUsername, errLogin.Error())
fmt.Printf("[Error] Was not able to connect to the customer database: %s. Please read https://github.com/SommerEngineering/Ocean.\n", errLogin.Error())
os.Exit(0)
}
// In case of write operations, wait for the majority of servers to be done:
mainSession.SetSafe(&mgo.Safe{WMode: "majority"})
// Set the consistency mode to read from any secondary server and write to the primary.
// Copied sessions can overwrite this setting of necessary.
mainSession.SetMode(mgo.Eventual, true)
// Get the GridFS:
gridFS := db.GridFS(`fs`)
if gridFS == nil {
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityCritical, LM.ImpactCritical, LM.MessageNameDATABASE, `Was not able to get the GridFS from the database.`)
fmt.Printf("[Error] Was not able to connect to the customer database. Please read https://github.com/SommerEngineering/Ocean.\n")
os.Exit(0)
}
// Ensure the indexes for the GridFS:
filesCollection := gridFS.Files
filesCollection.EnsureIndexKey(`uploadDate`)
filesCollection.EnsureIndexKey(`filename`)
filesCollection.EnsureIndexKey(`filename`, `uploadDate`)
}

View File

@ -4,7 +4,6 @@ import (
"github.com/SommerEngineering/Ocean/ConfigurationDB"
"github.com/SommerEngineering/Ocean/Log"
LM "github.com/SommerEngineering/Ocean/Log/Meta"
"gopkg.in/mgo.v2"
)
// The init function for this package.
@ -17,46 +16,7 @@ func init() {
databaseUsername = ConfigurationDB.Read(`CustomerDBUsername`)
databasePassword = ConfigurationDB.Read(`CustomerDBPassword`)
// Connect to MongoDB:
if newSession, errDial := mgo.Dial(databaseHost); errDial != nil {
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityUnknown, LM.ImpactUnknown, LM.MessageNameDATABASE, `It was not possible to connect to the MongoDB host `+databaseHost, errDial.Error())
return
} else {
mainSession = newSession
}
// Use the correct database:
db := mainSession.DB(databaseDB)
if db == nil {
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityCritical, LM.ImpactCritical, LM.MessageNameDATABASE, `Was not able to get the customer database.`)
return
}
// Login:
if errLogin := db.Login(databaseUsername, databasePassword); errLogin != nil {
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelSECURITY, LM.SeverityUnknown, LM.ImpactUnknown, LM.MessageNameDATABASE, `It was not possible to login the user `+databaseUsername, errLogin.Error())
return
}
// In case of write operations, wait for the majority of servers to be done:
mainSession.SetSafe(&mgo.Safe{WMode: "majority"})
// Set the consistency mode to read from any secondary server and write to the primary.
// Copied sessions can overwrite this setting of necessary.
mainSession.SetMode(mgo.Eventual, true)
// Get the GridFS:
gridFS := db.GridFS(`fs`)
if gridFS == nil {
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityCritical, LM.ImpactCritical, LM.MessageNameDATABASE, `Was not able to get the GridFS from the database.`)
return
}
// Ensure the indexes for the GridFS:
filesCollection := gridFS.Files
filesCollection.EnsureIndexKey(`uploadDate`)
filesCollection.EnsureIndexKey(`filename`)
filesCollection.EnsureIndexKey(`filename`, `uploadDate`)
// Try to connect to the database:
connectDatabase(databaseHost, databaseUsername, databasePassword, databaseDB)
Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameDATABASE, `Customer database is now ready.`)
}

View File

@ -7,6 +7,7 @@ import (
LM "github.com/SommerEngineering/Ocean/Log/Meta"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"os"
"strconv"
"strings"
"time"
@ -14,6 +15,12 @@ import (
// Init the database for the logging.
func initDatabase() {
defer func() {
if err := recover(); err != nil {
fmt.Printf("[Error] Was not able to connect to the logging database: %s. Please read https://github.com/SommerEngineering/Ocean.\n", err)
os.Exit(0)
}
}()
Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameINIT, `Checking and init the logging database collection.`)
defer Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameINIT, `Checking and init the logging database collection done.`)
@ -49,7 +56,8 @@ func initDatabase() {
// Connect to MongoDB:
if newSession, errDial := mgo.Dial(databaseHost); errDial != nil {
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityUnknown, LM.ImpactUnknown, LM.MessageNameDATABASE, `It was not possible to connect to the MongoDB host `+databaseHost, errDial.Error())
return
fmt.Printf("[Error] Was not able to connect to the logging database: %s. Please read https://github.com/SommerEngineering/Ocean.\n", errDial.Error())
os.Exit(0)
} else {
logDBSession = newSession
}
@ -60,7 +68,8 @@ func initDatabase() {
// Login:
if errLogin := logDB.Login(databaseUsername, databasePassword); errLogin != nil {
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelSECURITY, LM.SeverityUnknown, LM.ImpactUnknown, LM.MessageNameDATABASE, `It was not possible to login the user `+databaseUsername, errLogin.Error())
return
fmt.Printf("[Error] Was not able to connect to the logging database: %s. Please read https://github.com/SommerEngineering/Ocean.\n", errLogin.Error())
os.Exit(0)
}
// Get the collection:
@ -215,5 +224,4 @@ func initDatabase() {
indexTimeUTCMessageDescription := mgo.Index{}
indexProjectTimeUTCMessageDescription.Key = []string{`TimeUTC`, `MessageDescription`}
logDBCollection.EnsureIndex(indexTimeUTCMessageDescription)
}

View File

@ -1,6 +1,7 @@
package Log
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
@ -12,23 +13,26 @@ func readProjectName() {
// Try to get access to the working directory:
if currentDir, dirError := os.Getwd(); dirError != nil {
// Case: Error! Stop the server.
panic(`Cannot read the current working directory and therefore cannot read the project name!`)
fmt.Printf("[Error] Was not able to read the working directory. %s\n", dirError.Error())
os.Exit(0)
} else {
// Try to get access to the file:
filename := filepath.Join(currentDir, `project.name`)
if _, errFile := os.Stat(filename); errFile != nil {
// Cases: Error.
if os.IsNotExist(errFile) {
panic(`Cannot read the project name file 'project.name': File not found!`)
fmt.Printf("[Error] Cannot read the project name file 'project.name': File not found! Please read https://github.com/SommerEngineering/Ocean\n")
} else {
panic(`Cannot read the project name file 'project.name': ` + errFile.Error())
fmt.Printf("[Error] Cannot read the project name file 'project.name': %s. Please read https://github.com/SommerEngineering/Ocean\n", errFile.Error())
}
os.Exit(0)
}
// Try to read the file:
if projectNameBytes, errRead := ioutil.ReadFile(filename); errRead != nil {
// Case: Error.
panic(`Cannot read the project name file 'project.name': ` + errRead.Error())
fmt.Printf("[Error] Cannot read the project name file 'project.name': %s. Please read https://github.com/SommerEngineering/Ocean\n", errRead.Error())
os.Exit(0)
} else {
// Store the project name:
projectName = string(projectNameBytes)