Added storage module, base structs, getDevices() func, taskGetStorageDevices (ticks every 30)

pull/13/head
Paulo Truta 2021-06-06 12:29:46 +00:00
parent c9f97d42bf
commit fd002987fa
2 changed files with 103 additions and 0 deletions

View File

@ -0,0 +1,70 @@
package storage
import (
"bufio"
"strings"
"github.com/edgebox-iot/edgeboxctl/internal/utils"
)
// Device : Struct representing a storage device in the system
type Device struct {
ID string `json:"id"`
Name string `json:"name"`
Size string `json:"size"`
MainDevice bool `json:"main_device"`
MAJ string `json:"maj"`
MIN string `json:"min"`
RM string `json:"rm"`
RO string `json:"ro"`
Status DeviceStatus `json:"status"`
}
// DeviceStatus : Struct representing possible storage device statuses (code + description)
type DeviceStatus struct {
ID int `json:"id"`
Description string `json:"description"`
}
// GetDevices : Returns a list of all available sotrage devices in structs filled with information
func GetDevices() []Device {
var devices []Device
cmdArgs := []string{"--raw", "--nodeps", "--noheadings"}
cmdOutput := utils.Exec("lsblk", cmdArgs)
cmdOutputReader := strings.NewReader(cmdOutput)
scanner := bufio.NewScanner(cmdOutputReader)
scanner.Split(bufio.ScanLines)
mainDevice := true
for scanner.Scan() {
// 1 Device is represented here. Extract words in order for filling a Device struct
// Example deviceRawInfo: "mmcblk0 179:0 0 29.7G 0 disk"
deviceRawInfo := strings.Fields(scanner.Text())
majMin := strings.SplitN(deviceRawInfo[1], ":", 2)
device := Device{
ID: deviceRawInfo[0],
Name: deviceRawInfo[0],
Size: deviceRawInfo[3],
MainDevice: mainDevice,
MAJ: majMin[0],
MIN: majMin[1],
RO: deviceRawInfo[4],
RM: deviceRawInfo[2],
Status: DeviceStatus{ID: 1, Description: "healthy"},
}
// Once the first device is found, set to false.
if mainDevice == true {
mainDevice = false
}
devices = append(devices, device)
}
return devices
}

View File

@ -10,6 +10,7 @@ import (
"github.com/edgebox-iot/edgeboxctl/internal/diagnostics" "github.com/edgebox-iot/edgeboxctl/internal/diagnostics"
"github.com/edgebox-iot/edgeboxctl/internal/edgeapps" "github.com/edgebox-iot/edgeboxctl/internal/edgeapps"
"github.com/edgebox-iot/edgeboxctl/internal/storage"
"github.com/edgebox-iot/edgeboxctl/internal/system" "github.com/edgebox-iot/edgeboxctl/internal/system"
"github.com/edgebox-iot/edgeboxctl/internal/utils" "github.com/edgebox-iot/edgeboxctl/internal/utils"
_ "github.com/go-sql-driver/mysql" // Mysql Driver _ "github.com/go-sql-driver/mysql" // Mysql Driver
@ -253,12 +254,14 @@ func ExecuteSchedules(tick int) {
uptime := taskGetSystemUptime() uptime := taskGetSystemUptime()
log.Println("Uptime is " + uptime + " seconds (" + system.GetUptimeFormatted() + ")") log.Println("Uptime is " + uptime + " seconds (" + system.GetUptimeFormatted() + ")")
log.Println(taskGetStorageDevices())
log.Println(taskGetEdgeApps()) log.Println(taskGetEdgeApps())
} }
if tick%30 == 0 { if tick%30 == 0 {
// Executing every 30 ticks // Executing every 30 ticks
log.Println(taskGetStorageDevices())
log.Println(taskGetEdgeApps()) log.Println(taskGetEdgeApps())
} }
@ -435,3 +438,33 @@ func taskGetSystemUptime() string {
return uptime return uptime
} }
func taskGetStorageDevices() string {
fmt.Println("Executing taskGetStorageDevices")
devices := storage.GetDevices()
devicesJSON, _ := json.Marshal(devices)
db, err := sql.Open("sqlite3", utils.GetSQLiteDbConnectionDetails())
if err != nil {
log.Fatal(err.Error())
}
statement, err := db.Prepare("REPLACE into option (name, value, created, updated) VALUES (?, ?, ?, ?);") // Prepare SQL Statement
if err != nil {
log.Fatal(err.Error())
}
formatedDatetime := utils.GetSQLiteFormattedDateTime(time.Now())
_, err = statement.Exec("STORAGE_DEVICES_LIST", devicesJSON, formatedDatetime, formatedDatetime) // Execute SQL Statement
if err != nil {
log.Fatal(err.Error())
}
db.Close()
return string(devicesJSON)
}