From 4d2617f55f5db77d75e69a668db67930c0e55b89 Mon Sep 17 00:00:00 2001 From: Omar Abdulaziz Date: Tue, 3 Jun 2025 17:07:35 +0300 Subject: [PATCH] add myceliumrx module in zoslight --- cmds/modules/myceliumrx/main.go | 81 +++++++++++++++++++ cmds/zos/main.go | 2 + etc/zinit/myceliumrx.yaml | 3 + qemu/overlay.normal/bin/myceliumrx | 1 + qemu/overlay.normal/etc/zinit/internet.yaml | 1 - qemu/overlay.normal/etc/zinit/myceliumrx.yaml | 1 + qemu/overlay.normal/etc/zinit/qsfsd.yaml | 1 - 7 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 cmds/modules/myceliumrx/main.go create mode 100644 etc/zinit/myceliumrx.yaml create mode 120000 qemu/overlay.normal/bin/myceliumrx delete mode 120000 qemu/overlay.normal/etc/zinit/internet.yaml create mode 120000 qemu/overlay.normal/etc/zinit/myceliumrx.yaml delete mode 120000 qemu/overlay.normal/etc/zinit/qsfsd.yaml diff --git a/cmds/modules/myceliumrx/main.go b/cmds/modules/myceliumrx/main.go new file mode 100644 index 00000000..42b29aff --- /dev/null +++ b/cmds/modules/myceliumrx/main.go @@ -0,0 +1,81 @@ +package myceliumrx + +import ( + "crypto/ed25519" + "fmt" + + "github.com/pkg/errors" + substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go" + "github.com/threefoldtech/tfgrid-sdk-go/messenger" + "github.com/threefoldtech/zosbase/pkg/api" + "github.com/threefoldtech/zosbase/pkg/api/jsonrpc" + "github.com/threefoldtech/zosbase/pkg/environment" + "github.com/threefoldtech/zosbase/pkg/stubs" + "github.com/urfave/cli/v2" + + "github.com/threefoldtech/zbus" +) + +// Module is entry point for module +var Module cli.Command = cli.Command{ + Name: "myceliumrx", + Usage: "handles mycelium messages", + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "broker", + Usage: "connection string to the message `BROKER`", + Value: "unix:///var/run/redis.sock", + }, + }, + Action: action, +} + +func action(cli *cli.Context) error { + var broker string = cli.String("broker") + client, err := zbus.NewRedisClient(broker) + if err != nil { + return errors.Wrap(err, "failed to connect to zbus broker") + } + + a, err := api.NewAPI(client, broker, "light") + if err != nil { + return errors.Wrap(err, "failed to create api") + } + + idStub := stubs.NewIdentityManagerStub(client) + sk := ed25519.PrivateKey(idStub.PrivateKey(cli.Context)) + id, err := substrate.NewIdentityFromEd25519Key(sk) + if err != nil { + return err + } + + man, err := environment.GetSubstrate() + if err != nil { + return fmt.Errorf("failed to get substrate manager: %w", err) + } + ctx := cli.Context + + msgr, err := messenger.NewMessenger( + messenger.WithEnableTwinIdentity(false), + messenger.WithSubstrateManager(man), + messenger.WithIdentity(id), + ) + + if err != nil { + return fmt.Errorf("failed to create substrate manager: %w", err) + } + defer msgr.Close() + + server := messenger.NewJSONRPCServer(msgr) + + hdrs := jsonrpc.NewRpcHandler(a) + jsonrpc.RegisterHandlers(server, hdrs) + + if err := server.Start(ctx); err != nil { + return fmt.Errorf("failed to start server: %w", err) + } + + // block forever + <-ctx.Done() + return nil +} diff --git a/cmds/zos/main.go b/cmds/zos/main.go index 721149f9..4fa26dac 100644 --- a/cmds/zos/main.go +++ b/cmds/zos/main.go @@ -11,6 +11,7 @@ import ( "github.com/threefoldtech/zos4/cmds/modules/contd" "github.com/threefoldtech/zos4/cmds/modules/flistd" "github.com/threefoldtech/zos4/cmds/modules/gateway" + "github.com/threefoldtech/zos4/cmds/modules/myceliumrx" "github.com/threefoldtech/zos4/cmds/modules/netlightd" "github.com/threefoldtech/zos4/cmds/modules/noded" "github.com/threefoldtech/zos4/cmds/modules/powerd" @@ -56,6 +57,7 @@ func main() { &gateway.Module, &powerd.Module, &apigateway.Module, + &myceliumrx.Module, }, Before: func(c *cli.Context) error { if c.Bool("debug") { diff --git a/etc/zinit/myceliumrx.yaml b/etc/zinit/myceliumrx.yaml new file mode 100644 index 00000000..514f495a --- /dev/null +++ b/etc/zinit/myceliumrx.yaml @@ -0,0 +1,3 @@ +exec: myceliumrx --broker unix:///var/run/redis.sock +after: + - networkd \ No newline at end of file diff --git a/qemu/overlay.normal/bin/myceliumrx b/qemu/overlay.normal/bin/myceliumrx new file mode 120000 index 00000000..25eae5c7 --- /dev/null +++ b/qemu/overlay.normal/bin/myceliumrx @@ -0,0 +1 @@ +../../../bin/zos \ No newline at end of file diff --git a/qemu/overlay.normal/etc/zinit/internet.yaml b/qemu/overlay.normal/etc/zinit/internet.yaml deleted file mode 120000 index d485ff54..00000000 --- a/qemu/overlay.normal/etc/zinit/internet.yaml +++ /dev/null @@ -1 +0,0 @@ -../../../../bootstrap/etc/zinit/internet.yaml \ No newline at end of file diff --git a/qemu/overlay.normal/etc/zinit/myceliumrx.yaml b/qemu/overlay.normal/etc/zinit/myceliumrx.yaml new file mode 120000 index 00000000..e2b2bb23 --- /dev/null +++ b/qemu/overlay.normal/etc/zinit/myceliumrx.yaml @@ -0,0 +1 @@ +../../../../etc/zinit/myceliumrx.yaml \ No newline at end of file diff --git a/qemu/overlay.normal/etc/zinit/qsfsd.yaml b/qemu/overlay.normal/etc/zinit/qsfsd.yaml deleted file mode 120000 index b9640530..00000000 --- a/qemu/overlay.normal/etc/zinit/qsfsd.yaml +++ /dev/null @@ -1 +0,0 @@ -../../../../etc/zinit/qsfsd.yaml \ No newline at end of file