Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NOISSUE - Simplify manager to vm provision only #353

Merged
merged 14 commits into from
Jan 20, 2025
68 changes: 68 additions & 0 deletions cli/manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright (c) Ultraviolet
// SPDX-License-Identifier: Apache-2.0
package cli

import (
"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/ultravioletrs/cocos/manager"
"google.golang.org/protobuf/types/known/emptypb"
)

func (c *CLI) NewCreateVMCmd() *cobra.Command {
return &cobra.Command{
Use: "create-vm",
Short: "Create a new virtual machine",
Example: `create-vm`,
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
if err := c.InitializeManagerClient(cmd); err == nil {
defer c.Close()
}

Check warning on line 21 in cli/manager.go

View check run for this annotation

Codecov / codecov/patch

cli/manager.go#L12-L21

Added lines #L12 - L21 were not covered by tests

if c.connectErr != nil {
printError(cmd, "Failed to connect to manager: %v ❌ ", c.connectErr)
return
}

Check warning on line 26 in cli/manager.go

View check run for this annotation

Codecov / codecov/patch

cli/manager.go#L23-L26

Added lines #L23 - L26 were not covered by tests

cmd.Println("🔗 Creating a new virtual machine")

res, err := c.managerClient.CreateVm(cmd.Context(), &emptypb.Empty{})
if err != nil {
printError(cmd, "Error creating virtual machine: %v ❌ ", err)
return
}

Check warning on line 34 in cli/manager.go

View check run for this annotation

Codecov / codecov/patch

cli/manager.go#L28-L34

Added lines #L28 - L34 were not covered by tests

cmd.Println(color.New(color.FgGreen).Sprintf("✅ Virtual machine created successfully with id %s and port %s", res.SvmId, res.ForwardedPort))

Check warning on line 36 in cli/manager.go

View check run for this annotation

Codecov / codecov/patch

cli/manager.go#L36

Added line #L36 was not covered by tests
},
}
}

func (c *CLI) NewRemoveVMCmd() *cobra.Command {
return &cobra.Command{
Use: "remove-vm",
Short: "Remove a virtual machine",
Example: `remove-vm <svm_id>`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if err := c.InitializeManagerClient(cmd); err == nil {
defer c.Close()
}

Check warning on line 50 in cli/manager.go

View check run for this annotation

Codecov / codecov/patch

cli/manager.go#L41-L50

Added lines #L41 - L50 were not covered by tests

if c.connectErr != nil {
printError(cmd, "Failed to connect to manager: %v ❌ ", c.connectErr)
return
}

Check warning on line 55 in cli/manager.go

View check run for this annotation

Codecov / codecov/patch

cli/manager.go#L52-L55

Added lines #L52 - L55 were not covered by tests

cmd.Println("🔗 Removing virtual machine")

_, err := c.managerClient.RemoveVm(cmd.Context(), &manager.RemoveReq{SvmId: args[0]})
if err != nil {
printError(cmd, "Error removing virtual machine: %v ❌ ", err)
return
}

Check warning on line 63 in cli/manager.go

View check run for this annotation

Codecov / codecov/patch

cli/manager.go#L57-L63

Added lines #L57 - L63 were not covered by tests

cmd.Println(color.New(color.FgGreen).Sprintf("✅ Virtual machine removed successfully"))

Check warning on line 65 in cli/manager.go

View check run for this annotation

Codecov / codecov/patch

cli/manager.go#L65

Added line #L65 was not covered by tests
},
}
}
35 changes: 27 additions & 8 deletions cli/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,33 @@
"context"

"github.com/spf13/cobra"
"github.com/ultravioletrs/cocos/manager"
"github.com/ultravioletrs/cocos/pkg/clients/grpc"
"github.com/ultravioletrs/cocos/pkg/clients/grpc/agent"
managergrpc "github.com/ultravioletrs/cocos/pkg/clients/grpc/manager"
"github.com/ultravioletrs/cocos/pkg/sdk"
)

var Verbose bool

type CLI struct {
agentSDK sdk.SDK
config grpc.AgentClientConfig
client grpc.Client
connectErr error
agentSDK sdk.SDK
agentConfig grpc.AgentClientConfig
managerConfig grpc.ManagerClientConfig
client grpc.Client
managerClient manager.ManagerServiceClient
connectErr error
}

func New(config grpc.AgentClientConfig) *CLI {
func New(agentConfig grpc.AgentClientConfig, managerConfig grpc.ManagerClientConfig) *CLI {

Check warning on line 27 in cli/sdk.go

View check run for this annotation

Codecov / codecov/patch

cli/sdk.go#L27

Added line #L27 was not covered by tests
return &CLI{
config: config,
agentConfig: agentConfig,
managerConfig: managerConfig,

Check warning on line 30 in cli/sdk.go

View check run for this annotation

Codecov / codecov/patch

cli/sdk.go#L29-L30

Added lines #L29 - L30 were not covered by tests
}
}

func (c *CLI) InitializeSDK(cmd *cobra.Command) error {
agentGRPCClient, agentClient, err := agent.NewAgentClient(context.Background(), c.config)
func (c *CLI) InitializeAgentSDK(cmd *cobra.Command) error {
agentGRPCClient, agentClient, err := agent.NewAgentClient(context.Background(), c.agentConfig)

Check warning on line 35 in cli/sdk.go

View check run for this annotation

Codecov / codecov/patch

cli/sdk.go#L34-L35

Added lines #L34 - L35 were not covered by tests
if err != nil {
c.connectErr = err
return err
Expand All @@ -39,6 +44,20 @@
return nil
}

func (c *CLI) InitializeManagerClient(cmd *cobra.Command) error {
managerGRPCClient, managerClient, err := managergrpc.NewManagerClient(c.managerConfig)
if err != nil {
c.connectErr = err
return err
}

Check warning on line 52 in cli/sdk.go

View check run for this annotation

Codecov / codecov/patch

cli/sdk.go#L47-L52

Added lines #L47 - L52 were not covered by tests

cmd.Println("🔗 Connected to manager using ", managerGRPCClient.Secure())
c.client = managerGRPCClient

c.managerClient = managerClient
return nil

Check warning on line 58 in cli/sdk.go

View check run for this annotation

Codecov / codecov/patch

cli/sdk.go#L54-L58

Added lines #L54 - L58 were not covered by tests
}

func (c *CLI) Close() {
c.client.Close()
}
24 changes: 17 additions & 7 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ import (
)

const (
svcName = "cli"
envPrefixAgentGRPC = "AGENT_GRPC_"
completion = "completion"
filePermision = 0o755
cocosDirectory = ".cocos"
svcName = "cli"
envPrefixAgentGRPC = "AGENT_GRPC_"
envPrefixManagerGRPC = "MANAGER_GRPC_"
completion = "completion"
filePermision = 0o755
cocosDirectory = ".cocos"
)

type config struct {
Expand Down Expand Up @@ -98,9 +99,16 @@ func main() {
return
}

cliSVC := cli.New(agentGRPCConfig)
managerGRPCConfig := grpc.ManagerClientConfig{}
if err := env.ParseWithOptions(&managerGRPCConfig, env.Options{Prefix: envPrefixManagerGRPC}); err != nil {
message := color.New(color.FgRed).Sprintf("failed to load %s gRPC client configuration : %s", svcName, err)
rootCmd.Println(message)
return
}

cliSVC := cli.New(agentGRPCConfig, managerGRPCConfig)

if err := cliSVC.InitializeSDK(rootCmd); err == nil {
if err := cliSVC.InitializeAgentSDK(rootCmd); err == nil {
defer cliSVC.Close()
}

Expand All @@ -119,6 +127,8 @@ func main() {
rootCmd.AddCommand(attestationPolicyCmd)
rootCmd.AddCommand(keysCmd)
rootCmd.AddCommand(cliSVC.NewCABundleCmd(directoryCachePath))
rootCmd.AddCommand(cliSVC.NewCreateVMCmd())
rootCmd.AddCommand(cliSVC.NewRemoveVMCmd())

// Attestation commands
attestationCmd.AddCommand(cliSVC.NewGetAttestationCmd())
Expand Down
62 changes: 15 additions & 47 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,24 @@ import (
"log/slog"
"net/url"
"os"
"os/signal"
"strings"
"syscall"

mglog "github.com/absmach/magistrala/logger"
"github.com/absmach/magistrala/pkg/jaeger"
"github.com/absmach/magistrala/pkg/prometheus"
"github.com/absmach/magistrala/pkg/uuid"
"github.com/caarlos0/env/v11"
"github.com/ultravioletrs/cocos/internal/server"
grpcserver "github.com/ultravioletrs/cocos/internal/server/grpc"
"github.com/ultravioletrs/cocos/manager"
"github.com/ultravioletrs/cocos/manager/api"
managerapi "github.com/ultravioletrs/cocos/manager/api/grpc"
"github.com/ultravioletrs/cocos/manager/events"
managergrpc "github.com/ultravioletrs/cocos/manager/api/grpc"
"github.com/ultravioletrs/cocos/manager/qemu"
"github.com/ultravioletrs/cocos/manager/tracing"
pkggrpc "github.com/ultravioletrs/cocos/pkg/clients/grpc"
managergrpc "github.com/ultravioletrs/cocos/pkg/clients/grpc/manager"
"go.opentelemetry.io/otel/trace"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)

const (
Expand Down Expand Up @@ -92,73 +91,42 @@ func main() {
args := qemuCfg.ConstructQemuArgs()
logger.Info(strings.Join(args, " "))

managerGRPCConfig := pkggrpc.CVMClientConfig{}
managerGRPCConfig := server.ServerConfig{}
if err := env.ParseWithOptions(&managerGRPCConfig, env.Options{Prefix: envPrefixGRPC}); err != nil {
logger.Error(fmt.Sprintf("failed to load %s gRPC client configuration : %s", svcName, err))
exitCode = 1
return
}

managerGRPCClient, managerClient, err := managergrpc.NewManagerClient(managerGRPCConfig)
svc, err := newService(logger, tracer, qemuCfg, cfg.AttestationPolicyBinary, cfg.EosVersion)
if err != nil {
logger.Error(err.Error())
exitCode = 1
return
}
defer managerGRPCClient.Close()

pc, err := managerClient.Process(ctx)
if err != nil {
logger.Error(err.Error())
exitCode = 1
return
}

eventsChan := make(chan *manager.ClientStreamMessage, clientBufferSize)
svc, err := newService(logger, tracer, qemuCfg, eventsChan, cfg.AttestationPolicyBinary, cfg.EosVersion)
if err != nil {
logger.Error(err.Error())
exitCode = 1
return
registerManagerServiceServer := func(srv *grpc.Server) {
reflection.Register(srv)
manager.RegisterManagerServiceServer(srv, managergrpc.NewServer(svc))
}

eventsSvc, err := events.New(logger, svc.ReportBrokenConnection, eventsChan)
if err != nil {
logger.Error(err.Error())
exitCode = 1
return
}

go eventsSvc.Listen(ctx)

mc := managerapi.NewClient(pc, svc, eventsChan, logger)
gs := grpcserver.New(ctx, cancel, svcName, managerGRPCConfig, registerManagerServiceServer, logger, nil, nil)

g.Go(func() error {
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
defer signal.Stop(ch)

select {
case <-ch:
logger.Info("Received signal, shutting down...")
cancel()
return nil
case <-ctx.Done():
return ctx.Err()
}
return gs.Start()
})

g.Go(func() error {
return mc.Process(ctx, cancel)
return server.StopHandler(ctx, cancel, logger, svcName, gs)
})

if err := g.Wait(); err != nil {
logger.Error(fmt.Sprintf("%s service terminated: %s", svcName, err))
}
}

func newService(logger *slog.Logger, tracer trace.Tracer, qemuCfg qemu.Config, eventsChan chan *manager.ClientStreamMessage, attestationPolicyPath string, eosVersion string) (manager.Service, error) {
svc, err := manager.New(qemuCfg, attestationPolicyPath, logger, eventsChan, qemu.NewVM, eosVersion)
func newService(logger *slog.Logger, tracer trace.Tracer, qemuCfg qemu.Config, attestationPolicyPath string, eosVersion string) (manager.Service, error) {
svc, err := manager.New(qemuCfg, attestationPolicyPath, logger, qemu.NewVM, eosVersion)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.23.0
require (
github.com/absmach/magistrala v0.15.1
github.com/caarlos0/env/v11 v11.2.2
github.com/cenkalti/backoff/v4 v4.3.0
github.com/fatih/color v1.18.0
github.com/go-kit/kit v0.13.0
github.com/gofrs/uuid v4.4.0+incompatible
Expand All @@ -25,6 +24,7 @@ require (

require (
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/distribution/reference v0.6.0 // indirect
github.com/docker/go-connections v0.5.0 // indirect
Expand Down Expand Up @@ -59,7 +59,7 @@ require (
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/go-configfs-tsm v0.2.2 // indirect
github.com/google/logger v1.1.1
github.com/google/uuid v1.6.0 // indirect
github.com/google/uuid v1.6.0
github.com/grpc-ecosystem/grpc-gateway/v2 v2.23.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/mdlayher/socket v0.4.1 // indirect
Expand Down
Loading
Loading