Skip to content

Commit

Permalink
Merge pull request #36 from rodneyosodo/wasm-e2e-test
Browse files Browse the repository at this point in the history
NOISSUE - Test e2e running wasm file from cli
  • Loading branch information
drasko authored Dec 19, 2024
2 parents 158d0f0 + 0841f96 commit b794084
Show file tree
Hide file tree
Showing 28 changed files with 760 additions and 1,126 deletions.
3 changes: 3 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ run:
timeout: 10m

issues:
exclude-dirs:
- examples/
max-issues-per-linter: 100
max-same-issues: 100

Expand Down Expand Up @@ -30,6 +32,7 @@ linters:
- dupl
- err113
- noctx
- cyclop

linters-settings:
gocritic:
Expand Down
21 changes: 15 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,30 @@ BUILD_DIR = build
TIME=$(shell date -u '+%Y-%m-%dT%H:%M:%SZ')
VERSION ?= $(shell git describe --abbrev=0 --tags 2>/dev/null || echo 'v0.0.0')
COMMIT ?= $(shell git rev-parse HEAD)
EXAMPLES = addition long-addition
SERVICES = manager proplet cli

define compile_service
CGO_ENABLED=$(CGO_ENABLED) GOOS=$(GOOS) GOARCH=$(GOARCH) \
go build -ldflags "-s -w \
-X 'github.com/absmach/magistrala.BuildTime=$(TIME)' \
-X 'github.com/absmach/magistrala.Version=$(VERSION)' \
-X 'github.com/absmach/magistrala.Commit=$(COMMIT)'" \
-o ${BUILD_DIR}/propellerd cmd/propellerd/main.go
-o ${BUILD_DIR}/$(1) cmd/$(1)/main.go
endef

.PHONY: build
build:
$(call compile_service)
$(SERVICES):
$(call compile_service,$(@))

install:
cp ${BUILD_DIR}/propellerd $(GOBIN)/propellerd
for file in $(BUILD_DIR)/*; do \
if [[ ! "$$file" =~ \.wasm$$ ]]; then \
cp "$$file" $(GOBIN)/propeller-`basename "$$file"`; \
fi \
done

all: build
.PHONY: all $(SERVICES)
all: $(SERVICES)

clean:
rm -rf build
Expand All @@ -36,6 +42,9 @@ start-magistrala:
stop-magistrala:
docker compose -f docker/compose.yaml down

$(EXAMPLES):
GOOS=js GOARCH=wasm tinygo build -o build/$@.wasm -target wasi examples/$@/$@.go

help:
@echo "Usage: make <target>"
@echo ""
Expand Down
8 changes: 3 additions & 5 deletions cmd/propellerd/main.go → cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (

func main() {
rootCmd := &cobra.Command{
Use: "propellerd",
Short: "Propeller Daemon",
Long: `Propeller Daemon is a daemon that manages the lifecycle of Propeller components.`,
Use: "propeller-cli",
Short: "Propeller CLI",
Long: `Propeller CLI is a command line interface for interacting with Propeller components.`,
PersistentPreRun: func(_ *cobra.Command, _ []string) {
sdkConf := sdk.Config{
ManagerURL: propellerd.DefManagerURL,
Expand All @@ -23,10 +23,8 @@ func main() {
},
}

managerCmd := propellerd.NewManagerCmd()
tasksCmd := propellerd.NewTasksCmd()

rootCmd.AddCommand(managerCmd)
rootCmd.AddCommand(tasksCmd)

if err := rootCmd.Execute(); err != nil {
Expand Down
139 changes: 139 additions & 0 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package main

import (
"context"
"fmt"
"log"
"log/slog"
"net/url"
"os"
"time"

"github.com/absmach/magistrala/pkg/jaeger"
"github.com/absmach/magistrala/pkg/prometheus"
"github.com/absmach/magistrala/pkg/server"
httpserver "github.com/absmach/magistrala/pkg/server/http"
"github.com/absmach/propeller/manager"
"github.com/absmach/propeller/manager/api"
"github.com/absmach/propeller/manager/middleware"
"github.com/absmach/propeller/pkg/mqtt"
"github.com/absmach/propeller/pkg/scheduler"
"github.com/absmach/propeller/pkg/storage"
"github.com/caarlos0/env/v11"
"github.com/google/uuid"
"go.opentelemetry.io/otel/trace"
"go.opentelemetry.io/otel/trace/noop"
"golang.org/x/sync/errgroup"
)

const (
svcName = "manager"
defHTTPPort = "7070"
envPrefixHTTP = "MANAGER_HTTP_"
)

type config struct {
LogLevel string `env:"MANAGER_LOG_LEVEL" envDefault:"info"`
InstanceID string `env:"MANAGER_INSTANCE_ID"`
MQTTAddress string `env:"MANAGER_MQTT_ADDRESS" envDefault:"tcp://localhost:1883"`
MQTTQoS uint8 `env:"MANAGER_MQTT_QOS" envDefault:"2"`
MQTTTimeout time.Duration `env:"MANAGER_MQTT_TIMEOUT" envDefault:"30s"`
ChannelID string `env:"MANAGER_CHANNEL_ID,notEmpty"`
ThingID string `env:"MANAGER_THING_ID,notEmpty"`
ThingKey string `env:"MANAGER_THING_KEY,notEmpty"`
Server server.Config
OTELURL url.URL `env:"MANAGER_OTEL_URL"`
TraceRatio float64 `env:"MANAGER_TRACE_RATIO" envDefault:"0"`
}

func main() {
ctx, cancel := context.WithCancel(context.Background())
g, ctx := errgroup.WithContext(ctx)

cfg := config{}
if err := env.Parse(&cfg); err != nil {
log.Fatalf("failed to load configuration : %s", err.Error())
}

if cfg.InstanceID == "" {
cfg.InstanceID = uuid.NewString()
}

var level slog.Level
if err := level.UnmarshalText([]byte(cfg.LogLevel)); err != nil {
log.Fatalf("failed to parse log level: %s", err.Error())
}
logHandler := slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
Level: level,
})
logger := slog.New(logHandler)
slog.SetDefault(logger)

var tp trace.TracerProvider
switch {
case cfg.OTELURL == (url.URL{}):
tp = noop.NewTracerProvider()
default:
sdktp, err := jaeger.NewProvider(ctx, svcName, cfg.OTELURL, "", cfg.TraceRatio)
if err != nil {
logger.Error("failed to initialize opentelemetry", slog.String("error", err.Error()))

return
}
defer func() {
if err := sdktp.Shutdown(ctx); err != nil {
logger.Error("error shutting down tracer provider", slog.Any("error", err))
}
}()
tp = sdktp
}
tracer := tp.Tracer(svcName)

mqttPubSub, err := mqtt.NewPubSub(cfg.MQTTAddress, cfg.MQTTQoS, svcName, cfg.ThingID, cfg.ThingKey, cfg.ChannelID, cfg.MQTTTimeout, logger)
if err != nil {
logger.Error("failed to initialize mqtt pubsub", slog.String("error", err.Error()))

return
}

svc := manager.NewService(
storage.NewInMemoryStorage(),
storage.NewInMemoryStorage(),
storage.NewInMemoryStorage(),
scheduler.NewRoundRobin(),
mqttPubSub,
cfg.ChannelID,
logger,
)
svc = middleware.Logging(logger, svc)
svc = middleware.Tracing(tracer, svc)
counter, latency := prometheus.MakeMetrics(svcName, "api")
svc = middleware.Metrics(counter, latency, svc)

if err := svc.Subscribe(ctx); err != nil {
logger.Error("failed to subscribe to manager channel", slog.String("error", err.Error()))

return
}

httpServerConfig := server.Config{Port: defHTTPPort}
if err := env.ParseWithOptions(&httpServerConfig, env.Options{Prefix: envPrefixHTTP}); err != nil {
logger.Error(fmt.Sprintf("failed to load %s HTTP server configuration : %s", svcName, err.Error()))

return
}

hs := httpserver.NewServer(ctx, cancel, svcName, httpServerConfig, api.MakeHandler(svc, logger, cfg.InstanceID), logger)

g.Go(func() error {
return hs.Start()
})

g.Go(func() error {
return server.StopSignalHandler(ctx, cancel, logger, svcName, hs)
})

if err := g.Wait(); err != nil {
logger.Error(fmt.Sprintf("%s service exited with error: %s", svcName, err))
}
}
111 changes: 0 additions & 111 deletions cmd/manager/start.go

This file was deleted.

Loading

0 comments on commit b794084

Please sign in to comment.