Skip to content

Commit

Permalink
Merge pull request tiiuae#23 from mbssrc/add-freeze-state
Browse files Browse the repository at this point in the history
Add FreezerState to UnitStatus
  • Loading branch information
mbssrc authored Oct 23, 2024
2 parents cd2b991 + 95ee2bb commit 8734e9b
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 89 deletions.
8 changes: 6 additions & 2 deletions api/protoc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ gen_protoc() {
"$1"/"$2"
}

for api in admin systemd wifi hwid locale; do
gen_protoc api/$api $api.proto
if [ $# -eq 0 ]; then
set -- admin hwid locale systemd wifi
fi

for protob in "$@"; do
gen_protoc api/"$protob" "$protob".proto
done
143 changes: 77 additions & 66 deletions api/systemd/systemd.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions api/systemd/systemd.proto
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ message UnitStatus {
string ActiveState = 4;
string SubState = 5;
string Path = 6;
string FreezerState = 7;
}

message UnitStatusResponse {
Expand Down
2 changes: 1 addition & 1 deletion api/systemd/systemd_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions devshell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@
help = "update git pre-commit hooks";
}
{
help = "Generate go files from protobuffers";
help = "Generate go files from protobuffers. Examples: '$ protogen systemd'";
name = "protogen";
command = "./api/protoc.sh";
command = "./api/protoc.sh $@";
}
{
help = "Like cURL, but for gRPC: Command-line tool for interacting with gRPC servers";
Expand Down
13 changes: 7 additions & 6 deletions internal/pkgs/serviceclient/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@ func GetRemoteStatus(cfg *types.EndpointConfig, unitName string) (*types.UnitSta
}

response := &types.UnitStatus{
Name: resp.UnitStatus.Name,
Description: resp.UnitStatus.Description,
LoadState: resp.UnitStatus.LoadState,
ActiveState: resp.UnitStatus.ActiveState,
SubState: resp.UnitStatus.SubState,
Path: string(resp.UnitStatus.Path),
Name: resp.UnitStatus.Name,
Description: resp.UnitStatus.Description,
LoadState: resp.UnitStatus.LoadState,
ActiveState: resp.UnitStatus.ActiveState,
SubState: resp.UnitStatus.SubState,
Path: string(resp.UnitStatus.Path),
FreezerState: resp.UnitStatus.FreezerState,
}

return response, nil
Expand Down
23 changes: 23 additions & 0 deletions internal/pkgs/servicemanager/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,29 @@ func (c *SystemdController) GetUnitProperties(ctx context.Context, unitName stri
return props, nil
}

func (c *SystemdController) GetUnitPropertyString(ctx context.Context, unitName string, propertyName string) (string, error) {

// Input validation
if ctx == nil {
return "", fmt.Errorf("context cannot be nil")
}
if unitName == "" {
return "", fmt.Errorf("incorrect input, must be unit name")
}
if propertyName == "" {
return "", fmt.Errorf("incorrect input, must be property name")
}

// Get unit properties
prop, err := c.conn.GetUnitPropertyContext(ctx, unitName, propertyName)
if err != nil {
return "", err
}

propString := strings.Trim(prop.Value.String(), "\"")
return propString, nil
}

func (c *SystemdController) StartApplication(ctx context.Context, serviceName string, serviceArgs []string) (string, error) {

cmdFailure := "Command failed."
Expand Down
19 changes: 13 additions & 6 deletions internal/pkgs/servicemanager/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,22 @@ func (s *SystemdControlServer) GetUnitStatus(ctx context.Context, req *systemd_a
return nil, grpc_status.Error(grpc_codes.NotFound, errStr)
}

freezerState, err := s.Controller.GetUnitPropertyString(context.Background(), req.UnitName, "FreezerState")
if err != nil {
log.Infof("[GetUnitStatus] Error fetching freezer state: %v\n", err)
freezerState = "error"
}

resp := &systemd_api.UnitStatusResponse{
CmdStatus: "Command successful",
UnitStatus: &systemd_api.UnitStatus{
Name: unitStatus[0].Name,
Description: unitStatus[0].Description,
LoadState: unitStatus[0].LoadState,
ActiveState: unitStatus[0].ActiveState,
SubState: unitStatus[0].SubState,
Path: string(unitStatus[0].Path),
Name: unitStatus[0].Name,
Description: unitStatus[0].Description,
LoadState: unitStatus[0].LoadState,
ActiveState: unitStatus[0].ActiveState,
SubState: unitStatus[0].SubState,
Path: string(unitStatus[0].Path),
FreezerState: freezerState,
},
}

Expand Down
13 changes: 7 additions & 6 deletions internal/pkgs/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ const (
)

type UnitStatus struct {
Name string
Description string
LoadState string
ActiveState string
SubState string
Path string
Name string
Description string
LoadState string
ActiveState string
SubState string
Path string
FreezerState string
}

type TransportConfig struct {
Expand Down

0 comments on commit 8734e9b

Please sign in to comment.