Some checks failed
CI Docker Mining Proof / Linux agent hashrate proof (push) Has been cancelled
Dashboard ambient layer, comrade presence, Mission Deck and War Room, Emberwake supply chain, spread/docs publishing, fleet policy and modules API, CI docker mining, and refreshed USB pack.
60 lines
1.7 KiB
Go
60 lines
1.7 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
)
|
|
|
|
// FleetAgentPolicy is runtime mining/policy pushed to agents without re-forge.
|
|
type FleetAgentPolicy struct {
|
|
MiningMode string `json:"mining_mode,omitempty"`
|
|
ScheduleStart string `json:"schedule_start,omitempty"`
|
|
ScheduleEnd string `json:"schedule_end,omitempty"`
|
|
MaxCPUUsagePct int `json:"max_cpu_usage_pct,omitempty"`
|
|
PoolHost string `json:"pool_host,omitempty"`
|
|
PoolPort int `json:"pool_port,omitempty"`
|
|
PoolTLS *bool `json:"pool_tls,omitempty"`
|
|
PoolPass string `json:"pool_pass,omitempty"`
|
|
}
|
|
|
|
func (p FleetAgentPolicy) IsEmpty() bool {
|
|
var zero FleetAgentPolicy
|
|
return p == zero
|
|
}
|
|
|
|
func normalizeFleetAgentPolicy(p FleetAgentPolicy) FleetAgentPolicy {
|
|
p.MiningMode = strings.TrimSpace(strings.ToLower(p.MiningMode))
|
|
p.ScheduleStart = strings.TrimSpace(p.ScheduleStart)
|
|
p.ScheduleEnd = strings.TrimSpace(p.ScheduleEnd)
|
|
p.PoolHost = strings.TrimSpace(p.PoolHost)
|
|
p.PoolPass = strings.TrimSpace(p.PoolPass)
|
|
if p.MaxCPUUsagePct < 0 {
|
|
p.MaxCPUUsagePct = 0
|
|
}
|
|
if p.MaxCPUUsagePct > 100 {
|
|
p.MaxCPUUsagePct = 100
|
|
}
|
|
return p
|
|
}
|
|
|
|
func marshalFleetPolicyPayload(p FleetAgentPolicy) json.RawMessage {
|
|
return mustMarshal(normalizeFleetAgentPolicy(p))
|
|
}
|
|
|
|
// marshalPolicyUpdatePayload attaches push_id so dashboards can correlate agent acks.
|
|
func marshalPolicyUpdatePayload(pushID string, p FleetAgentPolicy) json.RawMessage {
|
|
norm := normalizeFleetAgentPolicy(p)
|
|
if pushID == "" {
|
|
return mustMarshal(norm)
|
|
}
|
|
body, _ := json.Marshal(norm)
|
|
var flat map[string]interface{}
|
|
_ = json.Unmarshal(body, &flat)
|
|
if flat == nil {
|
|
flat = map[string]interface{}{}
|
|
}
|
|
flat["push_id"] = pushID
|
|
out, _ := json.Marshal(flat)
|
|
return out
|
|
}
|