WIP: implemented Ping, implemented Set and Shutdown in agent, and added umask
This commit is contained in:
parent
cfdfcb6ed5
commit
b22529be7b
8
.idea/.gitignore
generated
vendored
Normal file
8
.idea/.gitignore
generated
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
9
.idea/go-agent.iml
generated
Normal file
9
.idea/go-agent.iml
generated
Normal file
@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="WEB_MODULE" version="4">
|
||||
<component name="Go" enabled="true" />
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/go-agent.iml" filepath="$PROJECT_DIR$/.idea/go-agent.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
@ -19,6 +19,7 @@ limitations under the License.
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"google.golang.org/protobuf/types/known/emptypb"
|
||||
"net"
|
||||
|
||||
pb "gitea.suyono.dev/suyono/go-agent/proto"
|
||||
@ -51,6 +52,10 @@ func NewCacheConn(socketPath string) (CacheConn, error) {
|
||||
}
|
||||
|
||||
result.client = pb.NewAgentClient(result.conn)
|
||||
if _, err = result.client.Ping(context.Background(), new(emptypb.Empty)); err != nil {
|
||||
return result, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
@ -71,4 +76,26 @@ func (c CacheConn) Get(ctx context.Context, key string) (string, error) {
|
||||
return rpcResult.Value, nil
|
||||
}
|
||||
|
||||
//TODO: implement Set and Shutdown
|
||||
func (c CacheConn) Set(ctx context.Context, key string, value string) error {
|
||||
rpcStatus, err := c.client.Set(ctx, &pb.CacheSetRequest{Key: key, Value: value})
|
||||
if err != nil {
|
||||
return fmt.Errorf("set to cache: %w", err)
|
||||
}
|
||||
|
||||
if rpcStatus.Status != "OK" {
|
||||
return fmt.Errorf("set to cache: status %s with message %s", rpcStatus.Status, rpcStatus.Message)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c CacheConn) Shutdown(ctx context.Context) error {
|
||||
_, err := c.client.Shutdown(ctx, new(emptypb.Empty))
|
||||
if err != nil {
|
||||
return fmt.Errorf("shutdown: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c CacheConn) Close() error {
|
||||
return c.conn.Close()
|
||||
}
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.36.6
|
||||
// protoc v6.31.1
|
||||
// source: gitea.suyono.dev/suyono/go-agent/proto/agent.proto
|
||||
// protoc v5.29.3
|
||||
// source: agent.proto
|
||||
|
||||
package proto
|
||||
|
||||
@ -31,7 +31,7 @@ type CacheGetRequest struct {
|
||||
|
||||
func (x *CacheGetRequest) Reset() {
|
||||
*x = CacheGetRequest{}
|
||||
mi := &file_gitea_suyono_dev_suyono_go_agent_proto_agent_proto_msgTypes[0]
|
||||
mi := &file_agent_proto_msgTypes[0]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
@ -43,7 +43,7 @@ func (x *CacheGetRequest) String() string {
|
||||
func (*CacheGetRequest) ProtoMessage() {}
|
||||
|
||||
func (x *CacheGetRequest) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_gitea_suyono_dev_suyono_go_agent_proto_agent_proto_msgTypes[0]
|
||||
mi := &file_agent_proto_msgTypes[0]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
@ -56,7 +56,7 @@ func (x *CacheGetRequest) ProtoReflect() protoreflect.Message {
|
||||
|
||||
// Deprecated: Use CacheGetRequest.ProtoReflect.Descriptor instead.
|
||||
func (*CacheGetRequest) Descriptor() ([]byte, []int) {
|
||||
return file_gitea_suyono_dev_suyono_go_agent_proto_agent_proto_rawDescGZIP(), []int{0}
|
||||
return file_agent_proto_rawDescGZIP(), []int{0}
|
||||
}
|
||||
|
||||
func (x *CacheGetRequest) GetKey() string {
|
||||
@ -77,7 +77,7 @@ type CacheValue struct {
|
||||
|
||||
func (x *CacheValue) Reset() {
|
||||
*x = CacheValue{}
|
||||
mi := &file_gitea_suyono_dev_suyono_go_agent_proto_agent_proto_msgTypes[1]
|
||||
mi := &file_agent_proto_msgTypes[1]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
@ -89,7 +89,7 @@ func (x *CacheValue) String() string {
|
||||
func (*CacheValue) ProtoMessage() {}
|
||||
|
||||
func (x *CacheValue) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_gitea_suyono_dev_suyono_go_agent_proto_agent_proto_msgTypes[1]
|
||||
mi := &file_agent_proto_msgTypes[1]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
@ -102,7 +102,7 @@ func (x *CacheValue) ProtoReflect() protoreflect.Message {
|
||||
|
||||
// Deprecated: Use CacheValue.ProtoReflect.Descriptor instead.
|
||||
func (*CacheValue) Descriptor() ([]byte, []int) {
|
||||
return file_gitea_suyono_dev_suyono_go_agent_proto_agent_proto_rawDescGZIP(), []int{1}
|
||||
return file_agent_proto_rawDescGZIP(), []int{1}
|
||||
}
|
||||
|
||||
func (x *CacheValue) GetStatus() string {
|
||||
@ -136,7 +136,7 @@ type CacheSetRequest struct {
|
||||
|
||||
func (x *CacheSetRequest) Reset() {
|
||||
*x = CacheSetRequest{}
|
||||
mi := &file_gitea_suyono_dev_suyono_go_agent_proto_agent_proto_msgTypes[2]
|
||||
mi := &file_agent_proto_msgTypes[2]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
@ -148,7 +148,7 @@ func (x *CacheSetRequest) String() string {
|
||||
func (*CacheSetRequest) ProtoMessage() {}
|
||||
|
||||
func (x *CacheSetRequest) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_gitea_suyono_dev_suyono_go_agent_proto_agent_proto_msgTypes[2]
|
||||
mi := &file_agent_proto_msgTypes[2]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
@ -161,7 +161,7 @@ func (x *CacheSetRequest) ProtoReflect() protoreflect.Message {
|
||||
|
||||
// Deprecated: Use CacheSetRequest.ProtoReflect.Descriptor instead.
|
||||
func (*CacheSetRequest) Descriptor() ([]byte, []int) {
|
||||
return file_gitea_suyono_dev_suyono_go_agent_proto_agent_proto_rawDescGZIP(), []int{2}
|
||||
return file_agent_proto_rawDescGZIP(), []int{2}
|
||||
}
|
||||
|
||||
func (x *CacheSetRequest) GetKey() string {
|
||||
@ -188,7 +188,7 @@ type SetStatus struct {
|
||||
|
||||
func (x *SetStatus) Reset() {
|
||||
*x = SetStatus{}
|
||||
mi := &file_gitea_suyono_dev_suyono_go_agent_proto_agent_proto_msgTypes[3]
|
||||
mi := &file_agent_proto_msgTypes[3]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
@ -200,7 +200,7 @@ func (x *SetStatus) String() string {
|
||||
func (*SetStatus) ProtoMessage() {}
|
||||
|
||||
func (x *SetStatus) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_gitea_suyono_dev_suyono_go_agent_proto_agent_proto_msgTypes[3]
|
||||
mi := &file_agent_proto_msgTypes[3]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
@ -213,7 +213,7 @@ func (x *SetStatus) ProtoReflect() protoreflect.Message {
|
||||
|
||||
// Deprecated: Use SetStatus.ProtoReflect.Descriptor instead.
|
||||
func (*SetStatus) Descriptor() ([]byte, []int) {
|
||||
return file_gitea_suyono_dev_suyono_go_agent_proto_agent_proto_rawDescGZIP(), []int{3}
|
||||
return file_agent_proto_rawDescGZIP(), []int{3}
|
||||
}
|
||||
|
||||
func (x *SetStatus) GetStatus() string {
|
||||
@ -230,11 +230,11 @@ func (x *SetStatus) GetMessage() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
var File_gitea_suyono_dev_suyono_go_agent_proto_agent_proto protoreflect.FileDescriptor
|
||||
var File_agent_proto protoreflect.FileDescriptor
|
||||
|
||||
const file_gitea_suyono_dev_suyono_go_agent_proto_agent_proto_rawDesc = "" +
|
||||
const file_agent_proto_rawDesc = "" +
|
||||
"\n" +
|
||||
"2gitea.suyono.dev/suyono/go-agent/proto/agent.proto\x12\x05proto\x1a\x1bgoogle/protobuf/empty.proto\"#\n" +
|
||||
"\vagent.proto\x12\x05proto\x1a\x1bgoogle/protobuf/empty.proto\"#\n" +
|
||||
"\x0fCacheGetRequest\x12\x10\n" +
|
||||
"\x03key\x18\x01 \x01(\tR\x03key\"T\n" +
|
||||
"\n" +
|
||||
@ -247,66 +247,69 @@ const file_gitea_suyono_dev_suyono_go_agent_proto_agent_proto_rawDesc = "" +
|
||||
"\x05value\x18\x02 \x01(\tR\x05value\"=\n" +
|
||||
"\tSetStatus\x12\x16\n" +
|
||||
"\x06status\x18\x01 \x01(\tR\x06status\x12\x18\n" +
|
||||
"\amessage\x18\x02 \x01(\tR\amessage2\xa6\x01\n" +
|
||||
"\amessage\x18\x02 \x01(\tR\amessage2\xde\x01\n" +
|
||||
"\x05Agent\x120\n" +
|
||||
"\x03Get\x12\x16.proto.CacheGetRequest\x1a\x11.proto.CacheValue\x12/\n" +
|
||||
"\x03Set\x12\x16.proto.CacheSetRequest\x1a\x10.proto.SetStatus\x12:\n" +
|
||||
"\bShutdown\x12\x16.google.protobuf.Empty\x1a\x16.google.protobuf.EmptyB.Z,gitea.suyono.dev/suyono/go-agent/proto;protob\x06proto3"
|
||||
"\bShutdown\x12\x16.google.protobuf.Empty\x1a\x16.google.protobuf.Empty\x126\n" +
|
||||
"\x04Ping\x12\x16.google.protobuf.Empty\x1a\x16.google.protobuf.EmptyB.Z,gitea.suyono.dev/suyono/go-agent/proto;protob\x06proto3"
|
||||
|
||||
var (
|
||||
file_gitea_suyono_dev_suyono_go_agent_proto_agent_proto_rawDescOnce sync.Once
|
||||
file_gitea_suyono_dev_suyono_go_agent_proto_agent_proto_rawDescData []byte
|
||||
file_agent_proto_rawDescOnce sync.Once
|
||||
file_agent_proto_rawDescData []byte
|
||||
)
|
||||
|
||||
func file_gitea_suyono_dev_suyono_go_agent_proto_agent_proto_rawDescGZIP() []byte {
|
||||
file_gitea_suyono_dev_suyono_go_agent_proto_agent_proto_rawDescOnce.Do(func() {
|
||||
file_gitea_suyono_dev_suyono_go_agent_proto_agent_proto_rawDescData = protoimpl.X.CompressGZIP(unsafe.Slice(unsafe.StringData(file_gitea_suyono_dev_suyono_go_agent_proto_agent_proto_rawDesc), len(file_gitea_suyono_dev_suyono_go_agent_proto_agent_proto_rawDesc)))
|
||||
func file_agent_proto_rawDescGZIP() []byte {
|
||||
file_agent_proto_rawDescOnce.Do(func() {
|
||||
file_agent_proto_rawDescData = protoimpl.X.CompressGZIP(unsafe.Slice(unsafe.StringData(file_agent_proto_rawDesc), len(file_agent_proto_rawDesc)))
|
||||
})
|
||||
return file_gitea_suyono_dev_suyono_go_agent_proto_agent_proto_rawDescData
|
||||
return file_agent_proto_rawDescData
|
||||
}
|
||||
|
||||
var file_gitea_suyono_dev_suyono_go_agent_proto_agent_proto_msgTypes = make([]protoimpl.MessageInfo, 4)
|
||||
var file_gitea_suyono_dev_suyono_go_agent_proto_agent_proto_goTypes = []any{
|
||||
var file_agent_proto_msgTypes = make([]protoimpl.MessageInfo, 4)
|
||||
var file_agent_proto_goTypes = []any{
|
||||
(*CacheGetRequest)(nil), // 0: proto.CacheGetRequest
|
||||
(*CacheValue)(nil), // 1: proto.CacheValue
|
||||
(*CacheSetRequest)(nil), // 2: proto.CacheSetRequest
|
||||
(*SetStatus)(nil), // 3: proto.SetStatus
|
||||
(*emptypb.Empty)(nil), // 4: google.protobuf.Empty
|
||||
}
|
||||
var file_gitea_suyono_dev_suyono_go_agent_proto_agent_proto_depIdxs = []int32{
|
||||
var file_agent_proto_depIdxs = []int32{
|
||||
0, // 0: proto.Agent.Get:input_type -> proto.CacheGetRequest
|
||||
2, // 1: proto.Agent.Set:input_type -> proto.CacheSetRequest
|
||||
4, // 2: proto.Agent.Shutdown:input_type -> google.protobuf.Empty
|
||||
1, // 3: proto.Agent.Get:output_type -> proto.CacheValue
|
||||
3, // 4: proto.Agent.Set:output_type -> proto.SetStatus
|
||||
4, // 5: proto.Agent.Shutdown:output_type -> google.protobuf.Empty
|
||||
3, // [3:6] is the sub-list for method output_type
|
||||
0, // [0:3] is the sub-list for method input_type
|
||||
4, // 3: proto.Agent.Ping:input_type -> google.protobuf.Empty
|
||||
1, // 4: proto.Agent.Get:output_type -> proto.CacheValue
|
||||
3, // 5: proto.Agent.Set:output_type -> proto.SetStatus
|
||||
4, // 6: proto.Agent.Shutdown:output_type -> google.protobuf.Empty
|
||||
4, // 7: proto.Agent.Ping:output_type -> google.protobuf.Empty
|
||||
4, // [4:8] is the sub-list for method output_type
|
||||
0, // [0:4] is the sub-list for method input_type
|
||||
0, // [0:0] is the sub-list for extension type_name
|
||||
0, // [0:0] is the sub-list for extension extendee
|
||||
0, // [0:0] is the sub-list for field type_name
|
||||
}
|
||||
|
||||
func init() { file_gitea_suyono_dev_suyono_go_agent_proto_agent_proto_init() }
|
||||
func file_gitea_suyono_dev_suyono_go_agent_proto_agent_proto_init() {
|
||||
if File_gitea_suyono_dev_suyono_go_agent_proto_agent_proto != nil {
|
||||
func init() { file_agent_proto_init() }
|
||||
func file_agent_proto_init() {
|
||||
if File_agent_proto != nil {
|
||||
return
|
||||
}
|
||||
type x struct{}
|
||||
out := protoimpl.TypeBuilder{
|
||||
File: protoimpl.DescBuilder{
|
||||
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
|
||||
RawDescriptor: unsafe.Slice(unsafe.StringData(file_gitea_suyono_dev_suyono_go_agent_proto_agent_proto_rawDesc), len(file_gitea_suyono_dev_suyono_go_agent_proto_agent_proto_rawDesc)),
|
||||
RawDescriptor: unsafe.Slice(unsafe.StringData(file_agent_proto_rawDesc), len(file_agent_proto_rawDesc)),
|
||||
NumEnums: 0,
|
||||
NumMessages: 4,
|
||||
NumExtensions: 0,
|
||||
NumServices: 1,
|
||||
},
|
||||
GoTypes: file_gitea_suyono_dev_suyono_go_agent_proto_agent_proto_goTypes,
|
||||
DependencyIndexes: file_gitea_suyono_dev_suyono_go_agent_proto_agent_proto_depIdxs,
|
||||
MessageInfos: file_gitea_suyono_dev_suyono_go_agent_proto_agent_proto_msgTypes,
|
||||
GoTypes: file_agent_proto_goTypes,
|
||||
DependencyIndexes: file_agent_proto_depIdxs,
|
||||
MessageInfos: file_agent_proto_msgTypes,
|
||||
}.Build()
|
||||
File_gitea_suyono_dev_suyono_go_agent_proto_agent_proto = out.File
|
||||
file_gitea_suyono_dev_suyono_go_agent_proto_agent_proto_goTypes = nil
|
||||
file_gitea_suyono_dev_suyono_go_agent_proto_agent_proto_depIdxs = nil
|
||||
File_agent_proto = out.File
|
||||
file_agent_proto_goTypes = nil
|
||||
file_agent_proto_depIdxs = nil
|
||||
}
|
||||
|
||||
@ -30,5 +30,6 @@ service Agent {
|
||||
rpc Get(CacheGetRequest) returns (CacheValue);
|
||||
rpc Set(CacheSetRequest) returns (SetStatus);
|
||||
rpc Shutdown(google.protobuf.Empty) returns (google.protobuf.Empty);
|
||||
rpc Ping(google.protobuf.Empty) returns (google.protobuf.Empty);
|
||||
}
|
||||
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
|
||||
// versions:
|
||||
// - protoc-gen-go-grpc v1.5.1
|
||||
// - protoc v6.31.1
|
||||
// source: gitea.suyono.dev/suyono/go-agent/proto/agent.proto
|
||||
// - protoc v5.29.3
|
||||
// source: agent.proto
|
||||
|
||||
package proto
|
||||
|
||||
@ -23,6 +23,7 @@ const (
|
||||
Agent_Get_FullMethodName = "/proto.Agent/Get"
|
||||
Agent_Set_FullMethodName = "/proto.Agent/Set"
|
||||
Agent_Shutdown_FullMethodName = "/proto.Agent/Shutdown"
|
||||
Agent_Ping_FullMethodName = "/proto.Agent/Ping"
|
||||
)
|
||||
|
||||
// AgentClient is the client API for Agent service.
|
||||
@ -32,6 +33,7 @@ type AgentClient interface {
|
||||
Get(ctx context.Context, in *CacheGetRequest, opts ...grpc.CallOption) (*CacheValue, error)
|
||||
Set(ctx context.Context, in *CacheSetRequest, opts ...grpc.CallOption) (*SetStatus, error)
|
||||
Shutdown(ctx context.Context, in *emptypb.Empty, opts ...grpc.CallOption) (*emptypb.Empty, error)
|
||||
Ping(ctx context.Context, in *emptypb.Empty, opts ...grpc.CallOption) (*emptypb.Empty, error)
|
||||
}
|
||||
|
||||
type agentClient struct {
|
||||
@ -72,6 +74,16 @@ func (c *agentClient) Shutdown(ctx context.Context, in *emptypb.Empty, opts ...g
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *agentClient) Ping(ctx context.Context, in *emptypb.Empty, opts ...grpc.CallOption) (*emptypb.Empty, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(emptypb.Empty)
|
||||
err := c.cc.Invoke(ctx, Agent_Ping_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// AgentServer is the server API for Agent service.
|
||||
// All implementations must embed UnimplementedAgentServer
|
||||
// for forward compatibility.
|
||||
@ -79,6 +91,7 @@ type AgentServer interface {
|
||||
Get(context.Context, *CacheGetRequest) (*CacheValue, error)
|
||||
Set(context.Context, *CacheSetRequest) (*SetStatus, error)
|
||||
Shutdown(context.Context, *emptypb.Empty) (*emptypb.Empty, error)
|
||||
Ping(context.Context, *emptypb.Empty) (*emptypb.Empty, error)
|
||||
mustEmbedUnimplementedAgentServer()
|
||||
}
|
||||
|
||||
@ -98,6 +111,9 @@ func (UnimplementedAgentServer) Set(context.Context, *CacheSetRequest) (*SetStat
|
||||
func (UnimplementedAgentServer) Shutdown(context.Context, *emptypb.Empty) (*emptypb.Empty, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method Shutdown not implemented")
|
||||
}
|
||||
func (UnimplementedAgentServer) Ping(context.Context, *emptypb.Empty) (*emptypb.Empty, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method Ping not implemented")
|
||||
}
|
||||
func (UnimplementedAgentServer) mustEmbedUnimplementedAgentServer() {}
|
||||
func (UnimplementedAgentServer) testEmbeddedByValue() {}
|
||||
|
||||
@ -173,6 +189,24 @@ func _Agent_Shutdown_Handler(srv interface{}, ctx context.Context, dec func(inte
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _Agent_Ping_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(emptypb.Empty)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(AgentServer).Ping(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: Agent_Ping_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(AgentServer).Ping(ctx, req.(*emptypb.Empty))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
// Agent_ServiceDesc is the grpc.ServiceDesc for Agent service.
|
||||
// It's only intended for direct use with grpc.RegisterService,
|
||||
// and not to be introspected or modified (even as a copy)
|
||||
@ -192,7 +226,11 @@ var Agent_ServiceDesc = grpc.ServiceDesc{
|
||||
MethodName: "Shutdown",
|
||||
Handler: _Agent_Shutdown_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "Ping",
|
||||
Handler: _Agent_Ping_Handler,
|
||||
},
|
||||
},
|
||||
Streams: []grpc.StreamDesc{},
|
||||
Metadata: "gitea.suyono.dev/suyono/go-agent/proto/agent.proto",
|
||||
Metadata: "agent.proto",
|
||||
}
|
||||
|
||||
@ -19,9 +19,12 @@ limitations under the License.
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"golang.org/x/sys/unix"
|
||||
"net"
|
||||
"os"
|
||||
"os/exec"
|
||||
"sync/atomic"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
pb "gitea.suyono.dev/suyono/go-agent/proto"
|
||||
@ -32,8 +35,12 @@ import (
|
||||
|
||||
type Option interface {
|
||||
SocketPath() string
|
||||
Expiry() string
|
||||
StopGraceTime() time.Duration
|
||||
Background() bool
|
||||
ExecArgs() []string
|
||||
ExecEnv() []string
|
||||
ExecLogPath() string
|
||||
Umask() int
|
||||
}
|
||||
|
||||
type Storage interface {
|
||||
@ -42,6 +49,18 @@ type Storage interface {
|
||||
Shutdown() error
|
||||
}
|
||||
|
||||
type BgProcFlag int
|
||||
|
||||
const (
|
||||
child BgProcFlag = iota
|
||||
parent
|
||||
)
|
||||
|
||||
const (
|
||||
goAgentDaemonEnvFlag = "GO_AGENT_DAEMON"
|
||||
goAgentDaemonLogPath = "GO_AGENT_DAEMON_LOG"
|
||||
)
|
||||
|
||||
type server struct {
|
||||
pb.UnimplementedAgentServer
|
||||
stor Storage
|
||||
@ -77,6 +96,48 @@ func (s *server) Shutdown(context.Context, *emptypb.Empty) (*emptypb.Empty, erro
|
||||
return &emptypb.Empty{}, nil
|
||||
}
|
||||
|
||||
func GetDaemonLogPath() (string, error) {
|
||||
// The child process of the main program should call this function and set up the logging infrastructure
|
||||
envVal, ok := os.LookupEnv(goAgentDaemonLogPath)
|
||||
if !ok {
|
||||
return "", fmt.Errorf("programming issue: parent process did not set log path")
|
||||
}
|
||||
return envVal, nil
|
||||
}
|
||||
|
||||
func ExecBackground(opt Option) (BgProcFlag, error) {
|
||||
var (
|
||||
cmd *exec.Cmd
|
||||
ok bool
|
||||
envVal string
|
||||
)
|
||||
|
||||
args := opt.ExecArgs()
|
||||
envVal, ok = os.LookupEnv(goAgentDaemonEnvFlag)
|
||||
if !opt.Background() || len(args) == 0 || (envVal == "1" && ok) {
|
||||
return child, nil
|
||||
}
|
||||
|
||||
if len(args) > 1 {
|
||||
cmd = exec.Command(args[0], args[1:]...)
|
||||
} else {
|
||||
cmd = exec.Command(args[0])
|
||||
}
|
||||
cmd.Env = append(os.Environ(), fmt.Sprintf("%s=1", goAgentDaemonEnvFlag))
|
||||
if opt.ExecLogPath() != "" {
|
||||
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", goAgentDaemonLogPath, opt.ExecLogPath()))
|
||||
}
|
||||
cmd.Env = append(cmd.Env, opt.ExecEnv()...)
|
||||
cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true}
|
||||
cmd.Stdin, cmd.Stdout, cmd.Stderr = nil, nil, nil
|
||||
|
||||
if err := cmd.Start(); err != nil {
|
||||
return parent, err
|
||||
}
|
||||
|
||||
return parent, nil
|
||||
}
|
||||
|
||||
func Serve(ctx context.Context, opt Option) error {
|
||||
var (
|
||||
l net.Listener
|
||||
@ -85,18 +146,30 @@ func Serve(ctx context.Context, opt Option) error {
|
||||
ok bool
|
||||
sw *atomic.Bool
|
||||
cancel context.CancelFunc
|
||||
bgFlag BgProcFlag
|
||||
)
|
||||
socketPath := opt.SocketPath()
|
||||
os.Remove(socketPath)
|
||||
|
||||
if l, err = net.Listen("unix", socketPath); err != nil {
|
||||
return fmt.Errorf("failed to listen on unix socket: %w", err)
|
||||
if bgFlag, err = ExecBackground(opt); err != nil {
|
||||
return fmt.Errorf("could not start background process: %w", err)
|
||||
}
|
||||
if bgFlag == parent {
|
||||
return nil
|
||||
}
|
||||
|
||||
_ = os.Remove(opt.SocketPath())
|
||||
|
||||
oldUmask := unix.Umask(opt.Umask())
|
||||
if l, err = net.Listen("unix", opt.SocketPath()); err != nil {
|
||||
return fmt.Errorf("failed to listen on unix socket: %w", err)
|
||||
}
|
||||
unix.Umask(oldUmask)
|
||||
|
||||
grpcServer := grpc.NewServer()
|
||||
pb.RegisterAgentServer(grpcServer, &server{stor: storage.NewInMap()})
|
||||
errChan := make(chan error, 1)
|
||||
ctx, cancel = context.WithCancel(ctx)
|
||||
pb.RegisterAgentServer(grpcServer, &server{
|
||||
stor: storage.NewInMap(cancel),
|
||||
})
|
||||
errChan := make(chan error, 1)
|
||||
sw = new(atomic.Bool)
|
||||
sw.Store(false)
|
||||
|
||||
@ -137,3 +210,7 @@ fsl:
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *server) Ping(ctx context.Context, req *emptypb.Empty) (*emptypb.Empty, error) {
|
||||
return &emptypb.Empty{}, nil
|
||||
}
|
||||
|
||||
@ -16,17 +16,22 @@ See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import "sync"
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type InMap struct {
|
||||
mtx *sync.Mutex
|
||||
stor map[string]string
|
||||
mtx *sync.Mutex
|
||||
stor map[string]string
|
||||
cancel context.CancelFunc
|
||||
}
|
||||
|
||||
func NewInMap() *InMap {
|
||||
func NewInMap(ctxCancel context.CancelFunc) *InMap {
|
||||
return &InMap{
|
||||
mtx: new(sync.Mutex),
|
||||
stor: make(map[string]string),
|
||||
mtx: new(sync.Mutex),
|
||||
stor: make(map[string]string),
|
||||
cancel: ctxCancel,
|
||||
}
|
||||
}
|
||||
|
||||
@ -56,5 +61,6 @@ func (i *InMap) Get(key string) (string, error) {
|
||||
}
|
||||
|
||||
func (i *InMap) Shutdown() error {
|
||||
i.cancel()
|
||||
return nil
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user