Skip to content

Commit

Permalink
feat: basic launch added
Browse files Browse the repository at this point in the history
  • Loading branch information
erayarslan committed Nov 24, 2024
1 parent ba38230 commit 927b4e5
Show file tree
Hide file tree
Showing 16 changed files with 507 additions and 122 deletions.
242 changes: 159 additions & 83 deletions agent/agent.pb.go

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions agent/agent.proto
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ service Rpc {
rpc instances (GetInstancesRequest) returns (GetInstancesReply) {};
rpc info (GetInfoRequest) returns (GetInfoReply) {};
rpc shell (stream common.ShellRequest) returns (stream common.ShellReply) {};
rpc launch (common.LaunchRequest) returns (common.LaunchReply) {};
}

message CPU {
Expand All @@ -21,9 +22,15 @@ message Memory {
uint64 available = 2;
}

message Disk {
uint64 total = 1;
uint64 available = 2;
}

message Resource {
CPU cpu = 1;
Memory memory = 2;
Disk disk = 3;
}

message GetInfoRequest {
Expand Down
38 changes: 38 additions & 0 deletions agent/agent_grpc.pb.go

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

5 changes: 5 additions & 0 deletions agent/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Client interface {
Info(ctx context.Context) (*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)
}

func (c *client) Close() error {
Expand All @@ -33,6 +34,10 @@ func (c *client) Info(ctx context.Context) (*GetInfoReply, error) {
return c.client.Info(ctx, &GetInfoRequest{})
}

func (c *client) Launch(ctx context.Context, launchRequest *common.LaunchRequest) (*common.LaunchReply, error) {
return c.client.Launch(ctx, launchRequest)
}

func (c *client) Shell(ctx context.Context) (grpc.BidiStreamingClient[common.ShellRequest, common.ShellReply], error) {
return c.client.Shell(ctx)
}
Expand Down
8 changes: 8 additions & 0 deletions agent/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ 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) Info(_ context.Context, _ *GetInfoRequest) (*GetInfoReply, error) {
resource := s.state.GetState().Resource

Expand Down
26 changes: 25 additions & 1 deletion agent/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"sync"
"time"

"github.com/shirou/gopsutil/v4/disk"

"github.com/erayarslan/multiverse/multipass"
"github.com/shirou/gopsutil/v4/cpu"
"github.com/shirou/gopsutil/v4/mem"
Expand Down Expand Up @@ -57,6 +59,7 @@ 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 @@ -67,10 +70,27 @@ func (s *state) updateResources() {
log.Printf("error while getting cpu percent: %v", err)
return
}

totalCore := cpuInfoStats[0].Cores
availableCore := totalCore - int32(math.Ceil(float64(totalCore)*percents[0]/100))

partitions, err := disk.Partitions(true)
if err != nil {
log.Printf("error while getting disk partitions: %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 @@ -80,6 +100,10 @@ func (s *state) updateResources() {
Total: virtualMemoryStat.Total,
Available: virtualMemoryStat.Available,
},
Disk: &Disk{
Total: diskUsageTotal,
Available: diskUsageFree,
},
}
}

Expand Down
30 changes: 19 additions & 11 deletions api/api.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/api.proto
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ service Rpc {
rpc instances (GetInstancesRequest) returns (GetInstancesReply) {};
rpc nodes (GetNodesRequest) returns (GetNodesReply) {};
rpc shell (stream common.ShellRequest) returns (stream common.ShellReply) {};
rpc launch (common.LaunchRequest) returns (common.LaunchReply) {};
}

message Node {
Expand Down
38 changes: 38 additions & 0 deletions api/api_grpc.pb.go

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

5 changes: 5 additions & 0 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Client interface {
Instances(ctx context.Context) (*GetInstancesReply, error)
Nodes(ctx context.Context) (*GetNodesReply, error)
Shell(ctx context.Context, instanceName string) error
Launch(ctx context.Context, launchRequest *common.LaunchRequest) (*common.LaunchReply, error)
Close() error
}

Expand All @@ -42,6 +43,10 @@ func (c *client) Nodes(ctx context.Context) (*GetNodesReply, error) {
return c.client.Nodes(ctx, &GetNodesRequest{})
}

func (c *client) Launch(ctx context.Context, launchRequest *common.LaunchRequest) (*common.LaunchReply, error) {
return c.client.Launch(ctx, launchRequest)
}

type shellRequestWriter struct {
stream grpc.BidiStreamingClient[common.ShellRequest, common.ShellReply]
closed chan struct{}
Expand Down
13 changes: 13 additions & 0 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ func (s *server) Nodes(_ context.Context, _ *GetNodesRequest) (*GetNodesReply, e
return getNodesReply, nil
}

func (s *server) Launch(ctx context.Context, req *common.LaunchRequest) (*common.LaunchReply, error) {
// todo: detect best worker due to resource
var agentClient agent.Client
s.clusterServer.IterateWorkers(func(workerInfo *cluster.WorkerInfo) bool {
agentClient = workerInfo.AgentClient
return false
})
if agentClient == nil {
return nil, fmt.Errorf("agent client not found")
}
return agentClient.Launch(ctx, req)
}

func (s *server) Shell(stream grpc.BidiStreamingServer[common.ShellRequest, common.ShellReply]) error {
md, ok := metadata.FromIncomingContext(stream.Context())
if !ok {
Expand Down
Loading

0 comments on commit 927b4e5

Please sign in to comment.