forked from phc/cluster-dashboard
feat: added basic scaffolding for host executor
This commit is contained in:
@@ -1,16 +1,15 @@
|
||||
package executor
|
||||
|
||||
import "time"
|
||||
|
||||
// Service is a service that handles executing commands on the main host and does a first processing of the raw data it gets from the system
|
||||
type Service interface {
|
||||
SlurmQueue() []string
|
||||
SlurmJobs() []string
|
||||
SlurmQueue() ([]string, error)
|
||||
SlurmJobs() ([]string, error)
|
||||
|
||||
NodeUptime(hostname string) time.Time
|
||||
NodeUptime(hostname string) (string, error)
|
||||
|
||||
Temperature(hostname string) float64
|
||||
MemoryUsage(hostname string) int64
|
||||
StorageUsage(hostname string) int64
|
||||
NetworkUploadDownload(hostname string) (int64, int64)
|
||||
Temperature(hostname string) (string, error)
|
||||
MemoryUsage(hostname string) (string, error)
|
||||
StorageUsage(hostname string) (string, error)
|
||||
NetworkUpload(hostname string) (string, error)
|
||||
NetworkDownload(hostname string) (string, error)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
package executor
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type HostRunner struct{}
|
||||
|
||||
func (HostRunner) SlurmQueue() ([]string, error) {
|
||||
panic("todo")
|
||||
}
|
||||
|
||||
func (HostRunner) SlurmJobs() ([]string, error) {
|
||||
panic("todo")
|
||||
}
|
||||
|
||||
func (HostRunner) NodeUptime(hostname string) (string, error) {
|
||||
panic("todo")
|
||||
}
|
||||
|
||||
func (HostRunner) Temperature(hostname string) (string, error) {
|
||||
panic("todo")
|
||||
}
|
||||
|
||||
func (HostRunner) MemoryUsage(hostname string) (string, error) {
|
||||
panic("todo")
|
||||
}
|
||||
|
||||
func (HostRunner) StorageUsage(hostname string) (string, error) {
|
||||
panic("todo")
|
||||
}
|
||||
|
||||
func (HostRunner) NetworkUpload(hostname string) (string, error) {
|
||||
panic("todo")
|
||||
}
|
||||
|
||||
func (HostRunner) NetworkDownload(hostname string) (string, error) {
|
||||
panic("todo")
|
||||
}
|
||||
|
||||
//
|
||||
// Internals
|
||||
//
|
||||
|
||||
// run a shell command and returns its output as string
|
||||
func run(command string) (string, error) {
|
||||
buf := &bytes.Buffer{}
|
||||
|
||||
// ctx := context.Background()
|
||||
// ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
|
||||
// defer cancel()
|
||||
// cmd := exec.CommandContext(ctx, "sh", "-c", command)
|
||||
|
||||
cmd := exec.Command("sh", "-c", command)
|
||||
cmd.Stdout = buf
|
||||
|
||||
// BUG: Al momento se questo comando ci mette troppo tutto si blocca, quindi sarebbe meglio
|
||||
// usare un context per dare un timeout al comando
|
||||
if err := cmd.Run(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return buf.String(), nil
|
||||
}
|
||||
|
||||
// run a shell command on a given remote via ssh and returns its output as string, does a bit of escaping
|
||||
func runOnRemote(destination, command string) (string, error) {
|
||||
return run(fmt.Sprintf(`ssh %s '%s'`, destination, strings.ReplaceAll(command, `'`, `\'`)))
|
||||
}
|
||||
+26
-23
@@ -1,43 +1,46 @@
|
||||
package executor
|
||||
|
||||
import "time"
|
||||
|
||||
var _ Service = &Mock{}
|
||||
|
||||
type Mock struct {
|
||||
SlurmQueueFunc func() []string
|
||||
SlurmJobsFunc func() []string
|
||||
NodeUptimeFunc func(hostname string) time.Time
|
||||
TemperatureFunc func(hostname string) float64
|
||||
MemoryUsageFunc func(hostname string) int64
|
||||
StorageUsageFunc func(hostname string) int64
|
||||
NetworkUploadDownloadFunc func(hostname string) (int64, int64)
|
||||
SlurmQueueFunc func() ([]string, error)
|
||||
SlurmJobsFunc func() ([]string, error)
|
||||
NodeUptimeFunc func(hostname string) (string, error)
|
||||
TemperatureFunc func(hostname string) (string, error)
|
||||
MemoryUsageFunc func(hostname string) (string, error)
|
||||
StorageUsageFunc func(hostname string) (string, error)
|
||||
NetworkUploadFunc func(hostname string) (string, error)
|
||||
NetworkDownloadFunc func(hostname string) (string, error)
|
||||
}
|
||||
|
||||
func (ex *Mock) SlurmQueue() []string {
|
||||
return ex.SlurmQueueFunc()
|
||||
func (m Mock) SlurmQueue() ([]string, error) {
|
||||
return m.SlurmQueueFunc()
|
||||
}
|
||||
|
||||
func (ex *Mock) SlurmJobs() []string {
|
||||
return ex.SlurmJobsFunc()
|
||||
func (m Mock) SlurmJobs() ([]string, error) {
|
||||
return m.SlurmJobsFunc()
|
||||
}
|
||||
|
||||
func (ex *Mock) NodeUptime(hostname string) time.Time {
|
||||
return ex.NodeUptimeFunc(hostname)
|
||||
func (m Mock) NodeUptime(hostname string) (string, error) {
|
||||
return m.NodeUptimeFunc(hostname)
|
||||
}
|
||||
|
||||
func (ex *Mock) Temperature(hostname string) float64 {
|
||||
return ex.TemperatureFunc(hostname)
|
||||
func (m Mock) Temperature(hostname string) (string, error) {
|
||||
return m.TemperatureFunc(hostname)
|
||||
}
|
||||
|
||||
func (ex *Mock) MemoryUsage(hostname string) int64 {
|
||||
return ex.MemoryUsageFunc(hostname)
|
||||
func (m Mock) MemoryUsage(hostname string) (string, error) {
|
||||
return m.MemoryUsageFunc(hostname)
|
||||
}
|
||||
|
||||
func (ex *Mock) StorageUsage(hostname string) int64 {
|
||||
return ex.StorageUsageFunc(hostname)
|
||||
func (m Mock) StorageUsage(hostname string) (string, error) {
|
||||
return m.StorageUsageFunc(hostname)
|
||||
}
|
||||
|
||||
func (ex *Mock) NetworkUploadDownload(hostname string) (int64, int64) {
|
||||
return ex.NetworkUploadDownloadFunc(hostname)
|
||||
func (m Mock) NetworkUpload(hostname string) (string, error) {
|
||||
return m.NetworkUploadFunc(hostname)
|
||||
}
|
||||
|
||||
func (m Mock) NetworkDownload(hostname string) (string, error) {
|
||||
return m.NetworkDownloadFunc(hostname)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user