add poweroff and reboot functions
This commit is contained in:
9
client/power/power.go
Normal file
9
client/power/power.go
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
package power
|
||||||
|
|
||||||
|
const APIVersion string = "power/v1alpha1"
|
||||||
|
|
||||||
|
type PowerActionResponse struct {
|
||||||
|
APIVersion string `json:"apiVersion"`
|
||||||
|
Action string `json:"action"`
|
||||||
|
Success bool `json:"success"`
|
||||||
|
}
|
||||||
30
client/power/poweroff.go
Normal file
30
client/power/poweroff.go
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package power
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"os/exec"
|
||||||
|
|
||||||
|
"git.dubyatp.xyz/orphanage/client/httputil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func PowerOffResponse(success bool) PowerActionResponse {
|
||||||
|
resp := PowerActionResponse{
|
||||||
|
APIVersion: APIVersion,
|
||||||
|
Action: "poweroff",
|
||||||
|
Success: success,
|
||||||
|
}
|
||||||
|
return resp
|
||||||
|
}
|
||||||
|
|
||||||
|
func PowerOff(next http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
cmd := exec.Command("systemctl", "poweroff")
|
||||||
|
err := cmd.Run()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
httputil.WriteJSON(w, http.StatusOK, PowerOffResponse(false))
|
||||||
|
} else {
|
||||||
|
httputil.WriteJSON(w, http.StatusOK, PowerOffResponse(true))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
30
client/power/reboot.go
Normal file
30
client/power/reboot.go
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package power
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"os/exec"
|
||||||
|
|
||||||
|
"git.dubyatp.xyz/orphanage/client/httputil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func RebootResponse(success bool) PowerActionResponse {
|
||||||
|
resp := PowerActionResponse{
|
||||||
|
APIVersion: APIVersion,
|
||||||
|
Action: "reboot",
|
||||||
|
Success: success,
|
||||||
|
}
|
||||||
|
return resp
|
||||||
|
}
|
||||||
|
|
||||||
|
func Reboot(next http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
cmd := exec.Command("systemctl", "reboot")
|
||||||
|
err := cmd.Run()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
httputil.WriteJSON(w, http.StatusOK, RebootResponse(false))
|
||||||
|
} else {
|
||||||
|
httputil.WriteJSON(w, http.StatusOK, RebootResponse(true))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -5,14 +5,17 @@ import (
|
|||||||
|
|
||||||
"git.dubyatp.xyz/orphanage/client/facts"
|
"git.dubyatp.xyz/orphanage/client/facts"
|
||||||
"git.dubyatp.xyz/orphanage/client/httputil"
|
"git.dubyatp.xyz/orphanage/client/httputil"
|
||||||
|
"git.dubyatp.xyz/orphanage/client/power"
|
||||||
"git.dubyatp.xyz/orphanage/client/testfunc"
|
"git.dubyatp.xyz/orphanage/client/testfunc"
|
||||||
)
|
)
|
||||||
|
|
||||||
func AddRoutes(
|
func AddRoutes(
|
||||||
mux *http.ServeMux,
|
mux *http.ServeMux,
|
||||||
) {
|
) {
|
||||||
mux.Handle("/", http.NotFoundHandler())
|
mux.Handle("GET /", http.NotFoundHandler())
|
||||||
mux.Handle("/helloworld", httputil.HelloWorld(nil))
|
mux.Handle("GET /helloworld", httputil.HelloWorld(nil))
|
||||||
mux.Handle("/testjson", testfunc.HelloWorldJSON(nil))
|
mux.Handle("GET /testjson", testfunc.HelloWorldJSON(nil))
|
||||||
mux.Handle("/facts", facts.GetFacts(nil))
|
mux.Handle("GET /facts", facts.GetFacts(nil))
|
||||||
|
mux.Handle("POST /power/reboot", power.Reboot(nil))
|
||||||
|
mux.Handle("POST /power/poweroff", power.PowerOff(nil))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user