use log function instead of fmt

This commit is contained in:
yuli
2024-08-21 23:49:41 +03:00
parent 9bb99d6bce
commit 9a0dd13b9e
2 changed files with 29 additions and 33 deletions

18
run.sh
View File

@@ -7,20 +7,23 @@ if [ ! -f /databunker/data/databunker.db ]; then
if [ "$1" == "demo" ]; then if [ "$1" == "demo" ]; then
OPTION="-demoinit" OPTION="-demoinit"
fi fi
echo "-------------INIT------------" echo "-------------------------------------------------"
echo "run.sh: init database, encryption, and access keys"
#/bin/busybox mkdir -p /tmp #/bin/busybox mkdir -p /tmp
RESULT=`/databunker/bin/databunker $OPTION -db /databunker/data/databunker.db -conf /databunker/conf/databunker.yaml > /tmp/init.txt` RESULT=`/databunker/bin/databunker $OPTION -db /databunker/data/databunker.db -conf /databunker/conf/databunker.yaml > /tmp/init.txt 2>&1`
if [ ! -f /databunker/data/databunker.db ]; then if [ ! -f /databunker/data/databunker.db ]; then
echo "Failed to init databunker database. Probably permission issue for /databunker/data directory." echo "Failed to init databunker database. Probably permission issue for /databunker/data directory."
/bin/busybox sleep 60 /bin/busybox sleep 60
exit exit
fi fi
DATABUNKER_ROOTTOKEN=`/bin/busybox awk '/API Root token:/ {print $4}' /tmp/init.txt` /bin/busybox cat /tmp/init.txt
DATABUNKER_MASTERKEY2=`/bin/busybox awk '/Master key:/ {print $3}' /tmp/init.txt` DATABUNKER_ROOTTOKEN=`/bin/busybox awk '/API Root token:/ {print $6}' /tmp/init.txt`
DATABUNKER_MASTERKEY2=`/bin/busybox awk '/Master key:/ {print $5}' /tmp/init.txt`
echo "DATABUNKER_ROOTTOKEN $DATABUNKER_ROOTTOKEN" echo "DATABUNKER_ROOTTOKEN $DATABUNKER_ROOTTOKEN"
echo "DATABUNKER_MASTERKEY $DATABUNKER_MASTERKEY2" echo "DATABUNKER_MASTERKEY $DATABUNKER_MASTERKEY2"
/bin/busybox rm -rf /tmp/init.txt /bin/busybox rm -rf /tmp/init.txt
if [ -z "$DATABUNKER_MASTERKEY" ]; then if [ -z "$DATABUNKER_MASTERKEY" ]; then
echo "export DATABUNKER_MASTERKEY=$DATABUNKER_MASTERKEY2"
export DATABUNKER_MASTERKEY=$DATABUNKER_MASTERKEY2 export DATABUNKER_MASTERKEY=$DATABUNKER_MASTERKEY2
fi fi
fi fi
@@ -29,9 +32,6 @@ if [ -z "$DATABUNKER_MASTERKEY" ]; then
/bin/busybox sleep 60 /bin/busybox sleep 60
exit exit
fi fi
#echo "-------------ENV-------------" echo "-------------------------------------------------"
#/bin/busybox env echo "run.sh: shart databunker service"
#echo "-------------FIND------------"
#/bin/busybox find /databunker
echo "-------------RUN-------------"
/databunker/bin/databunker -start -db /databunker/data/databunker.db -conf /databunker/conf/databunker.yaml /databunker/bin/databunker -start -db /databunker/data/databunker.db -conf /databunker/conf/databunker.yaml

View File

@@ -11,7 +11,6 @@ import (
"encoding/hex" "encoding/hex"
"errors" "errors"
"flag" "flag"
"fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"log" "log"
@@ -341,7 +340,7 @@ func readConfFile(cfg *Config, filepath *string) error {
confFile = *filepath confFile = *filepath
} }
} }
fmt.Printf("Databunker configuration file: %s\n", confFile) log.Printf("Databunker configuration file: %s\n", confFile)
f, err := os.Open(confFile) f, err := os.Open(confFile)
if err != nil { if err != nil {
return err return err
@@ -496,30 +495,30 @@ func reqMiddleware(handler http.Handler) http.Handler {
} }
func setupDB(dbPtr *string, masterKeyPtr *string, customRootToken string) (*dbcon, string, error) { func setupDB(dbPtr *string, masterKeyPtr *string, customRootToken string) (*dbcon, string, error) {
fmt.Printf("\nDatabunker init\n\n") log.Println("Databunker init")
var masterKey []byte var masterKey []byte
var err error var err error
if variableProvided("DATABUNKER_MASTERKEY", masterKeyPtr) == true { if variableProvided("DATABUNKER_MASTERKEY", masterKeyPtr) == true {
masterKey, err = masterkeyGet(masterKeyPtr) masterKey, err = masterkeyGet(masterKeyPtr)
if err != nil { if err != nil {
fmt.Printf("Failed to parse master key: %s", err) log.Printf("Failed to parse master key: %s\n", err)
os.Exit(0) os.Exit(0)
} }
fmt.Printf("Master key: ****\n\n") log.Println("Master key: ****")
} else { } else {
masterKey, err = generateMasterKey() masterKey, err = generateMasterKey()
if err != nil { if err != nil {
fmt.Printf("Failed to generate master key: %s", err) log.Printf("Failed to generate master key: %s", err)
os.Exit(0) os.Exit(0)
} }
fmt.Printf("Master key: %x\n\n", masterKey) log.Printf("Master key: %x\n", masterKey)
} }
hash := md5.Sum(masterKey) hash := md5.Sum(masterKey)
fmt.Printf("Init database\n\n") log.Println("Init database")
store, err := storage.InitDB(dbPtr) store, err := storage.InitDB(dbPtr)
for numAttempts := 60; err != nil && numAttempts > 0; numAttempts-- { for numAttempts := 60; err != nil && numAttempts > 0; numAttempts-- {
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
fmt.Printf("Trying to init db: %d\n", 61-numAttempts) log.Printf("Trying to init db: %d\n", 61-numAttempts)
store, err = storage.InitDB(dbPtr) store, err = storage.InitDB(dbPtr)
} }
if err != nil { if err != nil {
@@ -531,10 +530,10 @@ func setupDB(dbPtr *string, masterKeyPtr *string, customRootToken string) (*dbco
rootToken, err := db.createRootXtoken(customRootToken) rootToken, err := db.createRootXtoken(customRootToken)
if err != nil { if err != nil {
//log.Panic("error %s", err.Error()) //log.Panic("error %s", err.Error())
fmt.Printf("Failed to init root token: %s", err.Error()) log.Printf("Failed to init root token: %s", err.Error())
os.Exit(0) os.Exit(0)
} }
log.Println("Creating default entities: core-send-email-on-login and core-send-sms-on-login") log.Println("Creating default legal bases records")
db.createLegalBasis("core-send-email-on-login", "", "login", "Send email on login", db.createLegalBasis("core-send-email-on-login", "", "login", "Send email on login",
"Confirm to allow sending access code using 3rd party email gateway", "consent", "Confirm to allow sending access code using 3rd party email gateway", "consent",
"This consent is required to give you our service.", "active", true, true) "This consent is required to give you our service.", "active", true, true)
@@ -542,9 +541,9 @@ func setupDB(dbPtr *string, masterKeyPtr *string, customRootToken string) (*dbco
"Confirm to allow sending access code using 3rd party SMS gateway", "consent", "Confirm to allow sending access code using 3rd party SMS gateway", "consent",
"This consent is required to give you our service.", "active", true, true) "This consent is required to give you our service.", "active", true, true)
if len(customRootToken) > 0 && customRootToken != "DEMO" { if len(customRootToken) > 0 && customRootToken != "DEMO" {
fmt.Printf("\nAPI Root token: ****\n\n") log.Println("API Root token: ****")
} else { } else {
fmt.Printf("\nAPI Root token: %s\n\n", rootToken) log.Printf("API Root token: %s\n", rootToken)
} }
return db, rootToken, err return db, rootToken, err
} }
@@ -611,7 +610,7 @@ func main() {
flag.Parse() flag.Parse()
if *versionPtr { if *versionPtr {
fmt.Printf("Databunker version: %s\n", version) log.Printf("Databunker version: %s\n", version)
os.Exit(0) os.Exit(0)
} }
@@ -631,7 +630,7 @@ func main() {
} }
if *initPtr || *demoPtr { if *initPtr || *demoPtr {
if storage.DBExists(dbPtr) == true { if storage.DBExists(dbPtr) == true {
fmt.Printf("\nDatabase is alredy initialized.\n\n") log.Println("Database is alredy initialized.")
} else { } else {
db, _, _ := setupDB(dbPtr, masterKeyPtr, customRootToken) db, _, _ := setupDB(dbPtr, masterKeyPtr, customRootToken)
db.store.CloseDB() db.store.CloseDB()
@@ -641,25 +640,22 @@ func main() {
dbExists := storage.DBExists(dbPtr) dbExists := storage.DBExists(dbPtr)
for numAttempts := 60; dbExists == false && numAttempts > 0; numAttempts-- { for numAttempts := 60; dbExists == false && numAttempts > 0; numAttempts-- {
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
fmt.Printf("Trying to open db: %d\n", 61-numAttempts) log.Printf("Trying to open db [%d]\n", 61-numAttempts)
dbExists = storage.DBExists(dbPtr) dbExists = storage.DBExists(dbPtr)
} }
if dbExists == false { if dbExists == false {
fmt.Printf("\nDatabase is not initialized.\n\n") log.Println("Database is not initialized")
fmt.Println(`Run "databunker -init" for the first time to generate keys and init database.`) log.Println(`Run "databunker -init" for the first time to generate keys and init database.`)
fmt.Println("")
os.Exit(0) os.Exit(0)
} }
if masterKeyPtr == nil && *startPtr == false { if masterKeyPtr == nil && *startPtr == false {
fmt.Println("") log.Println(`Run "databunker -start" will load DATABUNKER_MASTERKEY environment variable.`)
fmt.Println(`Run "databunker -start" will load DATABUNKER_MASTERKEY environment variable.`) log.Println(`For testing "databunker -masterkey MASTER_KEY_VALUE" can be used. Not recommended for production.`)
fmt.Println(`For testing "databunker -masterkey MASTER_KEY_VALUE" can be used. Not recommended for production.`)
fmt.Println("")
os.Exit(0) os.Exit(0)
} }
err := loadUserSchema(cfg, confPtr) err := loadUserSchema(cfg, confPtr)
if err != nil { if err != nil {
fmt.Printf("Failed to load user schema: %s\n", err) log.Printf("Failed to load user schema: %s\n", err)
os.Exit(0) os.Exit(0)
} }
masterKey, masterKeyErr := masterkeyGet(masterKeyPtr) masterKey, masterKeyErr := masterkeyGet(masterKeyPtr)