Skip to content

Commit

Permalink
feat: info added
Browse files Browse the repository at this point in the history
  • Loading branch information
erayarslan committed Nov 24, 2024
1 parent 927b4e5 commit a9fa400
Show file tree
Hide file tree
Showing 16 changed files with 804 additions and 188 deletions.
72 changes: 37 additions & 35 deletions agent/agent.pb.go

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

2 changes: 1 addition & 1 deletion agent/agent.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import "common/common.proto";

service Rpc {
rpc instances (GetInstancesRequest) returns (GetInstancesReply) {};
rpc info (GetInfoRequest) returns (GetInfoReply) {};
rpc info (common.GetInfoRequest) returns (common.GetInfoReply) {};
rpc shell (stream common.ShellRequest) returns (stream common.ShellReply) {};
rpc launch (common.LaunchRequest) returns (common.LaunchReply) {};
}
Expand Down
14 changes: 7 additions & 7 deletions agent/agent_grpc.pb.go

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

14 changes: 7 additions & 7 deletions agent/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@ type client struct {
}

type Client interface {
Instances(ctx context.Context) (*GetInstancesReply, error)
Info(ctx context.Context) (*GetInfoReply, error)
Instances(ctx context.Context, request *GetInstancesRequest) (*GetInstancesReply, error)
Info(ctx context.Context, request *common.GetInfoRequest) (*common.GetInfoReply, error)
Shell(ctx context.Context) (grpc.BidiStreamingClient[common.ShellRequest, common.ShellReply], error)
Close() error
Launch(ctx context.Context, launchRequest *common.LaunchRequest) (*common.LaunchReply, error)
Launch(ctx context.Context, request *common.LaunchRequest) (*common.LaunchReply, error)
}

func (c *client) Close() error {
return c.conn.Close()
}

func (c *client) Instances(ctx context.Context) (*GetInstancesReply, error) {
return c.client.Instances(ctx, &GetInstancesRequest{})
func (c *client) Instances(ctx context.Context, request *GetInstancesRequest) (*GetInstancesReply, error) {
return c.client.Instances(ctx, request)
}

func (c *client) Info(ctx context.Context) (*GetInfoReply, error) {
return c.client.Info(ctx, &GetInfoRequest{})
func (c *client) Info(ctx context.Context, request *common.GetInfoRequest) (*common.GetInfoReply, error) {
return c.client.Info(ctx, request)
}

func (c *client) Launch(ctx context.Context, launchRequest *common.LaunchRequest) (*common.LaunchReply, error) {
Expand Down
16 changes: 4 additions & 12 deletions agent/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,12 @@ func (s *server) Instances(_ context.Context, _ *GetInstancesRequest) (*GetInsta
}, nil
}

func (s *server) Launch(_ context.Context, req *common.LaunchRequest) (*common.LaunchReply, error) {
if err := s.multipassClient.Launch(context.Background(), req.InstanceName); err != nil {
return nil, err
}

return &common.LaunchReply{}, nil
func (s *server) Launch(ctx context.Context, req *common.LaunchRequest) (*common.LaunchReply, error) {
return s.multipassClient.Launch(ctx, req)
}

func (s *server) Info(_ context.Context, _ *GetInfoRequest) (*GetInfoReply, error) {
resource := s.state.GetState().Resource

return &GetInfoReply{
Resource: resource,
}, nil
func (s *server) Info(ctx context.Context, req *common.GetInfoRequest) (*common.GetInfoReply, error) {
return s.multipassClient.Info(ctx, req)
}

type windowSize struct {
Expand Down
22 changes: 4 additions & 18 deletions agent/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ func (s *state) updateResources() {
log.Printf("error while getting virtual memory: %v", err)
return
}

cpuInfoStats, err := cpu.Info()
if err != nil {
log.Printf("error while getting cpu info: %v", err)
Expand All @@ -72,25 +71,12 @@ func (s *state) updateResources() {
}
totalCore := cpuInfoStats[0].Cores
availableCore := totalCore - int32(math.Ceil(float64(totalCore)*percents[0]/100))

partitions, err := disk.Partitions(true)
diskUsage, err := disk.Usage("/")
if err != nil {
log.Printf("error while getting disk partitions: %v", err)
log.Printf("error while getting disk usage: %v", err)
return
}

var diskUsageTotal uint64
var diskUsageFree uint64
for _, partition := range partitions {
diskUsageStat, err := disk.Usage(partition.Mountpoint)
if err != nil {
log.Printf("error while getting disk usage: %v", err)
return
}
diskUsageTotal += diskUsageStat.Total
diskUsageFree += diskUsageStat.Free
}

s.Resource = &Resource{
Cpu: &CPU{
Total: totalCore,
Expand All @@ -101,8 +87,8 @@ func (s *state) updateResources() {
Available: virtualMemoryStat.Available,
},
Disk: &Disk{
Total: diskUsageTotal,
Available: diskUsageFree,
Total: diskUsage.Total,
Available: diskUsage.Free,
},
}
}
Expand Down
Loading

0 comments on commit a9fa400

Please sign in to comment.