-
Notifications
You must be signed in to change notification settings - Fork 359
Add scheduled power on / off via Wake-on-LAN and ATX #1537
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
0xfacade
wants to merge
6
commits into
jetkvm:dev
Choose a base branch
from
0xfacade:feat/power-scheduler
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3485dc6
feat(scheduler): add power schedule model
9624dba
test(scheduler): cover crontab generation and validation
a3adbbf
feat(scheduler): run power schedules on the device
686ea19
feat(scheduler): expose power schedules over JSON-RPC
77846f9
feat(i18n): add power scheduler strings
20fa46b
feat(ui/scheduler): add power scheduler settings page
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| // Package powersched describes recurring power actions for the attached host. | ||
| // | ||
| // It holds only the schedule model and its translation to a crontab, with no | ||
| // dependency on the device runtime, so the rules can be unit tested on any | ||
| // platform. Executing a schedule lives in the main kvm package. | ||
| package powersched | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "net" | ||
| "sort" | ||
| "strings" | ||
| "time" | ||
| _ "time/tzdata" | ||
| ) | ||
|
|
||
| // Schedule methods. | ||
| const ( | ||
| MethodWOL = "wol" | ||
| MethodATX = "atx" | ||
| ) | ||
|
|
||
| // Schedule actions. | ||
| const ( | ||
| ActionOn = "on" | ||
| ActionOff = "off" | ||
| ActionOffForce = "off-force" | ||
| ) | ||
|
|
||
| // MaxSchedules limits how many schedules a device may store, mirroring the | ||
| // keyboard macro limits so a misbehaving client can't grow the config forever. | ||
| const MaxSchedules = 25 | ||
|
|
||
| // Schedule describes a recurring power action on the attached host. | ||
| // | ||
| // The schedule is stored as a weekday set plus a wall-clock time in an IANA | ||
| // timezone rather than a raw crontab: the UI exposes a weekday/time picker, and | ||
| // keeping the structured form lets both ends render the schedule consistently. | ||
| type Schedule struct { | ||
| ID string `json:"id"` | ||
| Name string `json:"name"` | ||
| Enabled bool `json:"enabled"` | ||
| Method string `json:"method"` // "wol" | "atx" | ||
| Action string `json:"action"` // "on" | "off" | "off-force" | ||
| Weekdays []int `json:"weekdays"` // 0=Sunday .. 6=Saturday | ||
| Hour int `json:"hour"` // 0-23 | ||
| Minute int `json:"minute"` // 0-59 | ||
| Timezone string `json:"timezone"` // IANA name, e.g. "Europe/Berlin" | ||
|
|
||
| // Wake-on-LAN only. The MAC is copied onto the schedule rather than | ||
| // referencing an entry in WakeOnLanDevices, so removing a saved device | ||
| // can't leave a schedule pointing at nothing. | ||
| MacAddress string `json:"macAddress,omitempty"` | ||
| BroadcastIP string `json:"broadcastIP,omitempty"` | ||
| } | ||
|
|
||
| // AllowedActions returns the actions that are valid for a given method. | ||
| func AllowedActions(method string) []string { | ||
| switch method { | ||
| case MethodWOL: | ||
| // A magic packet can only ever turn a host on. | ||
| return []string{ActionOn} | ||
| case MethodATX: | ||
| return []string{ActionOn, ActionOff, ActionOffForce} | ||
| default: | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| // Validate checks the schedule and normalises its weekday list. It returns an | ||
| // error describing the first problem found. | ||
| func (s *Schedule) Validate() error { | ||
| if strings.TrimSpace(s.Name) == "" { | ||
| return fmt.Errorf("schedule name cannot be empty") | ||
| } | ||
|
|
||
| actions := AllowedActions(s.Method) | ||
| if actions == nil { | ||
| return fmt.Errorf("invalid method: %s", s.Method) | ||
| } | ||
|
|
||
| valid := false | ||
| for _, a := range actions { | ||
| if s.Action == a { | ||
| valid = true | ||
| break | ||
| } | ||
| } | ||
| if !valid { | ||
| return fmt.Errorf("action %q is not valid for method %q", s.Action, s.Method) | ||
| } | ||
|
|
||
| if s.Method == MethodWOL { | ||
| if _, err := net.ParseMAC(s.MacAddress); err != nil { | ||
| return fmt.Errorf("invalid MAC address %q: %w", s.MacAddress, err) | ||
| } | ||
| if s.BroadcastIP != "" { | ||
| if ip := net.ParseIP(s.BroadcastIP); ip == nil || ip.To4() == nil { | ||
| return fmt.Errorf("invalid broadcast IP address: %s", s.BroadcastIP) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if s.Hour < 0 || s.Hour > 23 { | ||
| return fmt.Errorf("hour must be between 0 and 23, got %d", s.Hour) | ||
| } | ||
| if s.Minute < 0 || s.Minute > 59 { | ||
| return fmt.Errorf("minute must be between 0 and 59, got %d", s.Minute) | ||
| } | ||
|
|
||
| if len(s.Weekdays) == 0 { | ||
| return fmt.Errorf("at least one weekday must be selected") | ||
| } | ||
| seen := make(map[int]bool, len(s.Weekdays)) | ||
| days := make([]int, 0, len(s.Weekdays)) | ||
| for _, d := range s.Weekdays { | ||
| if d < 0 || d > 6 { | ||
| return fmt.Errorf("weekday must be between 0 and 6, got %d", d) | ||
| } | ||
| if seen[d] { | ||
| continue | ||
| } | ||
| seen[d] = true | ||
| days = append(days, d) | ||
| } | ||
| sort.Ints(days) | ||
| s.Weekdays = days | ||
|
|
||
| if s.Timezone != "" { | ||
| if _, err := time.LoadLocation(s.Timezone); err != nil { | ||
| return fmt.Errorf("invalid timezone %q: %w", s.Timezone, err) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // CronTab renders the schedule as a 6-field crontab, matching the | ||
| // with-seconds format the jiggler already uses. | ||
| func (s *Schedule) CronTab() string { | ||
| days := make([]string, 0, len(s.Weekdays)) | ||
| for _, d := range s.Weekdays { | ||
| days = append(days, fmt.Sprintf("%d", d)) | ||
| } | ||
|
|
||
| tab := fmt.Sprintf("0 %d %d * * %s", s.Minute, s.Hour, strings.Join(days, ",")) | ||
| if s.Timezone != "" && s.Timezone != "UTC" { | ||
| tab = fmt.Sprintf("TZ=%s %s", s.Timezone, tab) | ||
| } | ||
| return tab | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose here it would make sense to use a shorter reference so that the diff becomes smaller.