ubicloud/kubernetes/csi/lib/ubi_csi/service_helper.rb
2025-09-11 11:46:17 +02:00

37 lines
1 KiB
Ruby

# frozen_string_literal: true
require "open3"
require "securerandom"
module Csi
module ServiceHelper
def log_with_id(req_id, message)
@logger.info("[req_id=#{req_id}] #{message}")
end
def log_request_response(req, type)
raise GRPC::InvalidArgument.new("Request cannot be nil", GRPC::Core::StatusCodes::INVALID_ARGUMENT) unless req
req_id = SecureRandom.uuid
log_with_id(req_id, "#{type} request: #{req.inspect}")
resp = yield(req_id)
log_with_id(req_id, "#{type} response: #{resp.inspect}")
resp
end
def log_run_cmd(req_id, cmd)
log_with_id(req_id, "Running command: #{cmd.join(" ")}")
yield
end
def run_cmd(*cmd, req_id:, **kwargs)
log_run_cmd(req_id, cmd) do
Open3.capture2e(*cmd, **kwargs)
end
end
def log_and_raise(req_id, exception)
log_with_id(req_id, "#{exception.class}: #{exception.message}\n#{exception.backtrace.join("\n")}")
raise GRPC::Internal.new(exception.message)
end
end
end