in sharedlibraries/guestactions/guestactions.go [138:174]
func (g *GuestActions) messageHandler(ctx context.Context, req *anypb.Any, cloudProperties *metadataserver.CloudProperties) (*anypb.Any, error) {
var results []*gpb.CommandResult
gar, err := parseRequest(ctx, req)
if err != nil {
log.CtxLogger(ctx).Debugw("failed to parse request.", "err", err)
return nil, err
}
log.CtxLogger(ctx).Debugw("received GuestActionRequest to handle", "gar", prototext.Format(gar))
for _, command := range gar.GetCommands() {
log.CtxLogger(ctx).Debugw("processing command.", "command", prototext.Format(command))
pr := command.ProtoReflect()
fd := pr.WhichOneof(pr.Descriptor().Oneofs().ByName("command_type"))
result := &gpb.CommandResult{}
switch {
case fd == nil:
errMsg := fmt.Sprintf("received unknown command: %s", prototext.Format(command))
results = append(results, errorResult(errMsg))
return anyResponse(ctx, guestActionResponse(ctx, results, errMsg)), errors.New(errMsg)
case fd.Name() == shellCommand:
result = handleShellCommand(ctx, command, commandlineexecutor.ExecuteCommand)
results = append(results, result)
case fd.Name() == agentCommand:
result = g.handleAgentCommand(ctx, command, cloudProperties)
results = append(results, result)
default:
errMsg := fmt.Sprintf("received unknown command: %s", prototext.Format(command))
results = append(results, errorResult(errMsg))
return anyResponse(ctx, guestActionResponse(ctx, results, errMsg)), errors.New(errMsg)
}
// Exit early if we get an error
if result.GetExitCode() != int32(0) {
errMsg := fmt.Sprintf("received nonzero exit code with output: %s", result.GetStdout())
return anyResponse(ctx, guestActionResponse(ctx, results, errMsg)), errors.New(errMsg)
}
}
return anyResponse(ctx, guestActionResponse(ctx, results, "")), nil
}