28
pkg/portal/command.go
Normal file
28
pkg/portal/command.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package portal
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os/exec"
|
||||
"strings"
|
||||
|
||||
"github.com/apex/log"
|
||||
|
||||
"github.com/speedrunsh/speedrun/proto/portal"
|
||||
)
|
||||
|
||||
func (s *Server) RunCommand(ctx context.Context, in *portal.CommandRequest) (*portal.CommandResponse, error) {
|
||||
fields := log.Fields{
|
||||
"context": "command",
|
||||
}
|
||||
log := log.WithFields(fields)
|
||||
|
||||
log.Debugf("Received command: %s %s", in.GetName(), in.GetArgs())
|
||||
cmd := exec.Command(in.GetName(), in.GetArgs()...)
|
||||
stdout, err := cmd.CombinedOutput()
|
||||
|
||||
if err != nil {
|
||||
log.Error(err.Error())
|
||||
return nil, err
|
||||
}
|
||||
return &portal.CommandResponse{Message: strings.TrimSpace(string(stdout))}, nil
|
||||
}
|
||||
7
pkg/portal/server.go
Normal file
7
pkg/portal/server.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package portal
|
||||
|
||||
import "github.com/speedrunsh/speedrun/proto/portal"
|
||||
|
||||
type Server struct {
|
||||
portal.DRPCPortalUnimplementedServer
|
||||
}
|
||||
158
pkg/portal/service.go
Normal file
158
pkg/portal/service.go
Normal file
@@ -0,0 +1,158 @@
|
||||
package portal
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/apex/log"
|
||||
"github.com/coreos/go-systemd/v22/dbus"
|
||||
"github.com/speedrunsh/speedrun/proto/portal"
|
||||
)
|
||||
|
||||
func (s *Server) ServiceRestart(ctx context.Context, service *portal.ServiceRequest) (*portal.ServiceResponse, error) {
|
||||
fields := log.Fields{
|
||||
"context": "service",
|
||||
"command": "restart",
|
||||
"name": service.GetName(),
|
||||
}
|
||||
log := log.WithFields(fields)
|
||||
log.Debug("Received service restart request")
|
||||
|
||||
conn, err := dbus.NewWithContext(ctx)
|
||||
if err != nil {
|
||||
log.Error(err.Error())
|
||||
return nil, err
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
responseChan := make(chan string, 1)
|
||||
serviceName := fmt.Sprintf("%s.service", service.GetName())
|
||||
_, err = conn.RestartUnitContext(ctx, serviceName, "replace", responseChan)
|
||||
if err != nil {
|
||||
log.Error(err.Error())
|
||||
return nil, err
|
||||
}
|
||||
|
||||
res := <-responseChan
|
||||
log.Debugf("Service restart result: %v", res)
|
||||
return &portal.ServiceResponse{State: portal.State_CHANGED, Message: strings.Title(res)}, nil
|
||||
}
|
||||
|
||||
func (s *Server) ServiceStop(ctx context.Context, service *portal.ServiceRequest) (*portal.ServiceResponse, error) {
|
||||
fields := log.Fields{
|
||||
"context": "service",
|
||||
"command": "stop",
|
||||
"name": service.GetName(),
|
||||
}
|
||||
log := log.WithFields(fields)
|
||||
log.Debug("Received service stop request")
|
||||
|
||||
conn, err := dbus.NewWithContext(ctx)
|
||||
if err != nil {
|
||||
log.Error(err.Error())
|
||||
return nil, err
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
responseChan := make(chan string, 1)
|
||||
serviceName := fmt.Sprintf("%s.service", service.GetName())
|
||||
list, err := conn.ListUnitsByNamesContext(ctx, []string{serviceName})
|
||||
if err != nil {
|
||||
log.Error(err.Error())
|
||||
return nil, err
|
||||
}
|
||||
log.Debugf("Fetched service list by name: %v", list)
|
||||
if list[0].ActiveState == "inactive" {
|
||||
return &portal.ServiceResponse{State: portal.State_UNCHANGED, Message: "Service already stopped"}, nil
|
||||
}
|
||||
|
||||
_, err = conn.StopUnitContext(ctx, serviceName, "replace", responseChan)
|
||||
if err != nil {
|
||||
log.Error(err.Error())
|
||||
return nil, err
|
||||
}
|
||||
|
||||
res := <-responseChan
|
||||
log.Debugf("Service stop result: %v", res)
|
||||
return &portal.ServiceResponse{State: portal.State_CHANGED, Message: strings.Title(res)}, nil
|
||||
|
||||
}
|
||||
|
||||
func (s *Server) ServiceStart(ctx context.Context, service *portal.ServiceRequest) (*portal.ServiceResponse, error) {
|
||||
fields := log.Fields{
|
||||
"context": "service",
|
||||
"command": "start",
|
||||
"name": service.GetName(),
|
||||
}
|
||||
log := log.WithFields(fields)
|
||||
log.Debug("Received service start request")
|
||||
|
||||
conn, err := dbus.NewWithContext(ctx)
|
||||
if err != nil {
|
||||
log.Error(err.Error())
|
||||
return nil, err
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
responseChan := make(chan string, 1)
|
||||
serviceName := fmt.Sprintf("%s.service", service.GetName())
|
||||
list, err := conn.ListUnitsByNamesContext(ctx, []string{serviceName})
|
||||
if err != nil {
|
||||
log.Error(err.Error())
|
||||
return nil, err
|
||||
}
|
||||
log.Debugf("Fetched service list by name: %v", list)
|
||||
if list[0].ActiveState == "active" {
|
||||
return &portal.ServiceResponse{State: portal.State_UNCHANGED, Message: "Service already running"}, nil
|
||||
}
|
||||
|
||||
_, err = conn.StartUnitContext(ctx, serviceName, "replace", responseChan)
|
||||
if err != nil {
|
||||
log.Error(err.Error())
|
||||
return nil, err
|
||||
}
|
||||
|
||||
res := <-responseChan
|
||||
log.Debugf("Service start result: %v", res)
|
||||
return &portal.ServiceResponse{State: portal.State_CHANGED, Message: strings.Title(res)}, nil
|
||||
|
||||
}
|
||||
|
||||
func (s *Server) ServiceStatus(ctx context.Context, service *portal.ServiceRequest) (*portal.ServiceStatusResponse, error) {
|
||||
fields := log.Fields{
|
||||
"context": "service",
|
||||
"command": "status",
|
||||
"name": service.GetName(),
|
||||
}
|
||||
log := log.WithFields(fields)
|
||||
log.Debug("Received service status request")
|
||||
|
||||
conn, err := dbus.NewWithContext(ctx)
|
||||
if err != nil {
|
||||
log.Error(err.Error())
|
||||
return nil, err
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
serviceName := fmt.Sprintf("%s.service", service.GetName())
|
||||
res, err := conn.ListUnitsByNamesContext(ctx, []string{serviceName})
|
||||
if err != nil {
|
||||
log.Error(err.Error())
|
||||
return nil, err
|
||||
}
|
||||
log.Debugf("Fetched service list by name: %v", res[0])
|
||||
|
||||
if res[0].LoadState == "not-found" {
|
||||
log.Error("service not found")
|
||||
return nil, fmt.Errorf("service not found")
|
||||
}
|
||||
|
||||
return &portal.ServiceStatusResponse{
|
||||
State: portal.State_UNCHANGED,
|
||||
Activestate: res[0].ActiveState,
|
||||
Loadstate: res[0].LoadState,
|
||||
Substate: res[0].SubState,
|
||||
}, nil
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user