Add status of octoprint container
Signed-off-by: Thomas Klaehn <thomas.klaehn@perinet.io>
This commit is contained in:
@@ -14,7 +14,8 @@ import (
|
||||
type state string
|
||||
|
||||
type State struct {
|
||||
State state `json:"state"`
|
||||
State state `json:"state"`
|
||||
DockerState octoprint.DockerState `json:"octoprint_containerstate"`
|
||||
}
|
||||
|
||||
const (
|
||||
@@ -56,6 +57,7 @@ func handle_get_printerstate(w http.ResponseWriter, r *http.Request) {
|
||||
} else {
|
||||
res.State = StateOff
|
||||
}
|
||||
res.DockerState = octoprint.Status()
|
||||
tmp, err := json.Marshal(res)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
|
@@ -3,11 +3,23 @@ package octoprint
|
||||
import (
|
||||
"log"
|
||||
"powerswitch/internal/app/process"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type DockerState struct {
|
||||
ContainerID string `json:"container_id"`
|
||||
Image string `json:"image"`
|
||||
Command string `json:"command"`
|
||||
Created string `json:"created"`
|
||||
Status string `json:"status"`
|
||||
Ports string `json:"ports"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
const (
|
||||
start_cmd = "docker-compose -f /etc/printctrl/octoprint/docker-compose.yml up -d"
|
||||
stop_cmd = "docker-compose -f /etc/printctrl/octoprint/docker-compose.yml down"
|
||||
start_cmd = "docker-compose -f /etc/printctrl/octoprint/docker-compose.yml up -d"
|
||||
stop_cmd = "docker-compose -f /etc/printctrl/octoprint/docker-compose.yml down"
|
||||
status_cmd = "docker ps"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -35,3 +47,37 @@ func ReStart() error {
|
||||
}
|
||||
return Start()
|
||||
}
|
||||
|
||||
func Status() DockerState {
|
||||
var state DockerState
|
||||
state.Status = "off"
|
||||
p := process.NewProcess(status_cmd)
|
||||
p.Start()
|
||||
go func() {
|
||||
for {
|
||||
std := <-p.StdoutChannel
|
||||
if strings.Contains(std, "octoprint/octoprint") {
|
||||
res := strings.Split(std, " ")
|
||||
if len(res) != 7 {
|
||||
logger.Printf("Unkown number pf parameters (%d vs. 7)", len(res))
|
||||
} else {
|
||||
state.ContainerID = res[0]
|
||||
state.Image = res[1]
|
||||
state.Command = res[2]
|
||||
state.Created = res[3]
|
||||
state.Status = res[4]
|
||||
state.Ports = res[5]
|
||||
state.Name = res[6]
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
go func() {
|
||||
for {
|
||||
err := <-p.StderrChannel
|
||||
logger.Print(err)
|
||||
}
|
||||
}()
|
||||
p.Wait()
|
||||
return state
|
||||
}
|
||||
|
Reference in New Issue
Block a user