Fixed small typo, replaced DB connection details in Makefile by .env loading

loop_loop_execution
Paulo Truta 2021-02-17 12:10:15 +01:00
parent b04e854fdd
commit 59707b8abd
6 changed files with 60 additions and 30 deletions

View File

@ -5,11 +5,6 @@ COMMIT := $(shell git rev-parse --short HEAD)
BUILD_DATE := $(shell date -u '+%Y-%m-%d_%H:%M:%S')
BUILD_DIR = bin
DBHOST = 127.0.0.1:3306
DBNAME = docker
DBUSER = root
DBPASS = tiger
build-all:
GOOS=linux GOARCH=amd64 make build
GOOS=linux GOARCH=arm make build
@ -19,11 +14,7 @@ build:
GOOS=${GOOS} GOARCH=${GOARCH} go build \
-trimpath -ldflags "-s -w -X ${PROJECT}/internal/diagnostics.Version=${RELEASE} \
-X ${PROJECT}/internal/diagnostics.Commit=${COMMIT} \
-X ${PROJECT}/internal/diagnostics.BuildDate=${BUILD_DATE} \
-X ${PROJECT}/internal/tasks.Dbhost=${DBHOST} \
-X ${PROJECT}/internal/tasks.Dbname=${DBNAME} \
-X ${PROJECT}/internal/tasks.Dbuser=${DBUSER} \
-X ${PROJECT}/internal/tasks.Dbpass=${DBPASS}" \
-X ${PROJECT}/internal/diagnostics.BuildDate=${BUILD_DATE}" \
-o bin/sysctl-${GOOS}-${GOARCH} ${PROJECT}/cmd/sysctl
cp ./bin/sysctl-${GOOS}-${GOARCH} ./bin/sysctl

View File

@ -11,6 +11,7 @@ import (
"github.com/edgebox-iot/sysctl/internal/diagnostics"
"github.com/edgebox-iot/sysctl/internal/tasks"
"github.com/edgebox-iot/sysctl/internal/utils"
)
const defaultNotReadySleepTime time.Duration = time.Second * 60
@ -85,8 +86,8 @@ func printVersion() {
func printDbDetails() {
fmt.Printf(
"\n\nDatabase Connection Information:\nHost: %s\nuser: %s\npassword: %s\n\n",
tasks.Dbhost, tasks.Dbuser, tasks.Dbpass,
"\n\nDatabase Connection Information:\n %s\n\n",
utils.GetMySQLDbConnectionDetails(),
)
}

1
go.mod
View File

@ -4,5 +4,6 @@ go 1.15
require (
github.com/go-sql-driver/mysql v1.5.0
github.com/joho/godotenv v1.3.0
gopkg.in/yaml.v2 v2.4.0
)

View File

@ -31,8 +31,12 @@ type EdgeAppService struct {
}
const configFilename = "/edgebox-compose.yml"
const edgeAppsPath = "/home/system/components/apps/"
const wsPath = "/home/system/components/ws/"
// const edgeAppsPath = "/home/system/components/apps/"
const edgeAppsPath = "/home/jpt/Repositories/edgebox/apps/"
// const wsPath = "/home/system/components/ws/"
const wsPath = "/home/jpt/Repositories/edgebox/ws"
// GetEdgeApps : Returns a list of EdgeApp struct filled with information
func GetEdgeApps() []EdgeApp {
@ -53,7 +57,8 @@ func GetEdgeApps() []EdgeApp {
_, err := os.Stat(edgeAppsPath + f.Name() + configFilename)
if !os.IsNotExist(err) {
// File exists. Start digging!
edgeApp := EdgeApp{ID: f.Name(), Status: GetEdgeAppStatus(f.Name()), Services: GetEdgeAppServices(f.Name()), NetworkURL: f.Name() + ".edgebox.local"}
edgeAppName := "Nextcloud"
edgeApp := EdgeApp{ID: f.Name(), Name: edgeAppName, Status: GetEdgeAppStatus(f.Name()), Services: GetEdgeAppServices(f.Name()), NetworkURL: f.Name() + ".edgebox.local"}
edgeApps = append(edgeApps, edgeApp)
}

View File

@ -13,18 +13,6 @@ import (
_ "github.com/go-sql-driver/mysql" // Mysql Driver
)
// Dbhost : Database host (can be tweaked in makefile)
var Dbhost string
// Dbname : Database name (can be tweaked in makefile)
var Dbname string
// Dbuser : Database user (can be tweaked in makefile)
var Dbuser string
// Dbpass : Database password (can be tweaked in)
var Dbpass string
// Task : Struct for Task type
type Task struct {
ID int `json:"id"`
@ -47,7 +35,7 @@ type taskSetupTunnelArgs struct {
func GetNextTask() Task {
// Will try to connect to API database, which should be running locally under WS.
db, err := sql.Open("mysql", Dbuser+":"+Dbpass+"@tcp("+Dbhost+")/"+Dbname)
db, err := sql.Open("mysql", utils.GetMySQLDbConnectionDetails())
// if there is an error opening the connection, handle it
if err != nil {
@ -86,7 +74,7 @@ func GetNextTask() Task {
// ExecuteTask : Performs execution of the given task, updating the task status as it goes, and publishing the task result
func ExecuteTask(task Task) Task {
db, err := sql.Open("mysql", Dbuser+":"+Dbpass+"@tcp("+Dbhost+")/"+Dbname)
db, err := sql.Open("mysql", utils.GetMySQLDbConnectionDetails())
if err != nil {
panic(err.Error())
@ -193,7 +181,27 @@ func taskGetEdgeApps() string {
fmt.Println("Executing taskGetEdgeApps")
log.Println(edgeapps.GetEdgeApps())
edgeApps := edgeapps.GetEdgeApps()
edgeAppsJSON, _ := json.Marshal(edgeApps)
log.Println(string(edgeAppsJSON))
// db, err := sql.Open("mysql", utils.GetMySQLDbConnectionDetails())
// if err != nil {
// panic(err.Error())
// }
// defer db.Close()
// statusUpdate, err := db.Query("UPDATE options SET value = '" + + "' WHERE name = 'EDGEAPPS_LIST'")
// if err != nil {
// panic(err.Error())
// }
// for statusUpdate.Next() {
// }
// Saving information in the "options" table.
return "OK"

View File

@ -5,6 +5,8 @@ import (
"fmt"
"log"
"os/exec"
"github.com/joho/godotenv"
)
// Exec : Runs a terminal Command, catches and logs errors, returns the result.
@ -35,3 +37,25 @@ func DeleteEmptySlices(s []string) []string {
}
return r
}
// GetMySQLDbConnectionDetails : Returns the necessary string as connection info for SQL.db()
func GetMySQLDbConnectionDetails() string {
// const apiEnvFileLocation = "/home/system/components/api/edgebox.env"
const apiEnvFileLocation = "/home/jpt/Repositories/edgebox/api/edgebox.env"
var apiEnv map[string]string
apiEnv, err := godotenv.Read(apiEnvFileLocation)
if err != nil {
log.Fatal("Error loading .env file")
}
Dbhost := "127.0.0.1:" + apiEnv["HOST_MACHINE_MYSQL_PORT"]
Dbname := apiEnv["MYSQL_DATABASE"]
Dbuser := apiEnv["MYSQL_USER"]
Dbpass := apiEnv["MYSQL_PASSWORD"]
return Dbuser + ":" + Dbpass + "@tcp(" + Dbhost + ")/" + Dbname
}