From 29fac94afbc0049558a2ff980c2938decadda2f6 Mon Sep 17 00:00:00 2001 From: JeffMboya Date: Tue, 3 Dec 2024 17:31:25 +0300 Subject: [PATCH] First commit Signed-off-by: JeffMboya Revert docs Signed-off-by: JeffMboya Revert docs Signed-off-by: JeffMboya Revert docs Signed-off-by: JeffMboya --- README.md | 38 ++++++++++++++++---------------- go.sum | 2 -- worker/api/endpoint.go | 15 +++++++++++++ worker/api/requests.go | 5 +++++ worker/api/responses.go | 18 +++++++++++++++ worker/api/transport.go | 49 +++++++++++++++++++++++++++++++++++++++++ worker/service.go | 33 +++++++++++++++++++++++++++ 7 files changed, 139 insertions(+), 21 deletions(-) create mode 100644 worker/api/endpoint.go create mode 100644 worker/api/requests.go create mode 100644 worker/api/responses.go create mode 100644 worker/api/transport.go create mode 100644 worker/service.go diff --git a/README.md b/README.md index 907a7cb..830a694 100644 --- a/README.md +++ b/README.md @@ -1,18 +1,18 @@ -# 🚀 Propeller +# 🚀 Propeller -**Propeller** is a cutting-edge orchestrator for **WebAssembly (Wasm)** workloads across the **Cloud-Edge continuum**. It enables seamless deployment of Wasm applications from powerful cloud servers to constrained microcontrollers, combining flexibility, security, and performance. +**Propeller** is a cutting-edge orchestrator for **WebAssembly (Wasm)** workloads across the **Cloud-Edge continuum**. It enables seamless deployment of Wasm applications from powerful cloud servers to constrained microcontrollers, combining flexibility, security, and performance. --- -## 🌟 Features +## 🌟 Features -- 🌐 **Cloud-Edge Orchestration**: Deploy Wasm workloads effortlessly across diverse environments, from robust cloud servers to lightweight microcontrollers. -- ⚡ **Fast Boot Times**: Take advantage of Wasm's near-instant startup for efficient workload execution. -- 📦 **FaaS Deployment**: Enable Function-as-a-Service (FaaS) capabilities for scalable and event-driven applications. -- 🖥️ **OCI Registry Support**: Push and pull Wasm workloads from OCI-compliant registries for streamlined workflow integration. -- 🔧 **WAMR on Zephyr RTOS**: Deploy lightweight Wasm workloads on constrained devices running Zephyr RTOS via the WebAssembly Micro Runtime (WAMR). -- 🛠️ **Powerful Service Mesh**: Integrates with **[SuperMQ](https://github.com/absmach)** for secure, efficient IoT device communication. -- 🔒 **Security at the Core**: Propeller ensures secure workload execution and communication for IoT environments. +- 🌐 **Cloud-Edge Orchestration**: Deploy Wasm workloads effortlessly across diverse environments, from robust cloud servers to lightweight microcontrollers. +- ⚡ **Fast Boot Times**: Take advantage of Wasm's near-instant startup for efficient workload execution. +- 📦 **FaaS Deployment**: Enable Function-as-a-Service (FaaS) capabilities for scalable and event-driven applications. +- 🖥️ **OCI Registry Support**: Push and pull Wasm workloads from OCI-compliant registries for streamlined workflow integration. +- 🔧 **WAMR on Zephyr RTOS**: Deploy lightweight Wasm workloads on constrained devices running Zephyr RTOS via the WebAssembly Micro Runtime (WAMR). +- 🛠️ **Powerful Service Mesh**: Integrates with **[SuperMQ](https://github.com/absmach)** for secure, efficient IoT device communication. +- 🔒 **Security at the Core**: Propeller ensures secure workload execution and communication for IoT environments. --- @@ -34,21 +34,21 @@ For setup instructions, API references, and usage examples, see the documentatio --- -## 💡 Use Cases +## 💡 Use Cases -- 🏭 **Industrial IoT**: Deploy analytics or control applications to edge devices in factories. -- 🛡️ **Secure Workloads**: Run isolated, portable workloads securely on cloud or edge devices. -- 🌎 **Smart Cities**: Power scalable IoT networks with efficient communication and dynamic workloads. -- ☁️ **Serverless Applications**: Deploy FaaS applications leveraging Propeller's Wasm orchestration capabilities. +- 🏭 **Industrial IoT**: Deploy analytics or control applications to edge devices in factories. +- 🛡️ **Secure Workloads**: Run isolated, portable workloads securely on cloud or edge devices. +- 🌎 **Smart Cities**: Power scalable IoT networks with efficient communication and dynamic workloads. +- ☁️ **Serverless Applications**: Deploy FaaS applications leveraging Propeller's Wasm orchestration capabilities. --- -## 🤝 Contributing +## 🤝 Contributing -Contributions are welcome! Please check the [CONTRIBUTING.md](#) for details on how to get started. +Contributions are welcome! Please check the [CONTRIBUTING.md](#) for details on how to get started. --- -## 📜 License +## 📜 License -Propeller is licensed under the **Apache-2.0 License**. See the [LICENSE](LICENSE) file for more details. +Propeller is licensed under the **Apache-2.0 License**. See the [LICENSE](LICENSE) file for more details. diff --git a/go.sum b/go.sum index 695655c..44344fc 100644 --- a/go.sum +++ b/go.sum @@ -6,8 +6,6 @@ github.com/absmach/magistrala v0.15.1 h1:3Bk2hlyWcV591LxPYwlvRcyCXTfuZ1g/EkNmU+o github.com/absmach/magistrala v0.15.1/go.mod h1:9pto6xuBt/IuCtZRdEha0iDQKNQ5tyNOjLXJgUiikYk= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= -github.com/caarlos0/env/v11 v11.2.2 h1:95fApNrUyueipoZN/EhA8mMxiNxrBwDa+oAZrMWl3Kg= -github.com/caarlos0/env/v11 v11.2.2/go.mod h1:JBfcdeQiBoI3Zh1QRAWfe+tpiNTmDtcCj/hHHHMx0vc= github.com/caarlos0/env/v11 v11.3.0 h1:CVTN6W6+twFC1jHKUwsw9eOTEiFpzyJOSA2AyHa8uvw= github.com/caarlos0/env/v11 v11.3.0/go.mod h1:Q5lYHeOsgY20CCV/R+b50Jwg2MnjySid7+3FUBz2BJw= github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8= diff --git a/worker/api/endpoint.go b/worker/api/endpoint.go new file mode 100644 index 0000000..a2a99f8 --- /dev/null +++ b/worker/api/endpoint.go @@ -0,0 +1,15 @@ +package api + +import ( + "context" + + "github.com/absmach/propeller/worker" + "github.com/go-kit/kit/endpoint" +) + +func MakeGetWorkerStatEndpoint(svc worker.WorkerService) endpoint.Endpoint { + return func(ctx context.Context, request interface{}) (interface{}, error) { + req := request.(WorkerRequestDTO) + return svc.GetWorkerStats(ctx, req.WorkerID) + } +} diff --git a/worker/api/requests.go b/worker/api/requests.go new file mode 100644 index 0000000..0f876bd --- /dev/null +++ b/worker/api/requests.go @@ -0,0 +1,5 @@ +package api + +type WorkerRequestDTO struct { + WorkerID string `json:"worker_id"` +} diff --git a/worker/api/responses.go b/worker/api/responses.go new file mode 100644 index 0000000..9c8546e --- /dev/null +++ b/worker/api/responses.go @@ -0,0 +1,18 @@ +package api + +import "time" + +type WorkerResponseDTO struct { + WorkerID string `json:"worker_id"` + Name string `json:"name"` + State string `json:"state"` + Function string `json:"function"` + RestartPolicy string `json:"restart_policy"` + RestartCount int `json:"restart_count"` + StartTime time.Time `json:"start_time"` + FinishTime time.Time `json:"finish_time"` + CPUUsage float64 `json:"cpu_usage"` + MemoryUsage float64 `json:"memory_usage"` + TaskCount int `json:"task_count"` + RunningTasks int `json:"running_tasks"` +} diff --git a/worker/api/transport.go b/worker/api/transport.go new file mode 100644 index 0000000..862138a --- /dev/null +++ b/worker/api/transport.go @@ -0,0 +1,49 @@ +package api + +import ( + "context" + "encoding/json" + "errors" + "net/http" + "strings" + + "github.com/absmach/propeller/worker" + + "github.com/go-chi/chi/v5" + kithttp "github.com/go-kit/kit/transport/http" +) + +func MakeHandler(svc worker.WorkerService) http.Handler { + opts := []kithttp.ServerOption{} + + mux := chi.NewRouter() + + mux.Route("/workers", func(r chi.Router) { + r.Get("/{worker_id}", kithttp.NewServer( + MakeGetWorkerStatEndpoint(svc), + decodeWorkerRequest, + encodeWorkerResponse, + opts..., + ).ServeHTTP) + }) + + return mux +} + +func decodeWorkerRequest(_ context.Context, r *http.Request) (interface{}, error) { + if !strings.Contains(r.Header.Get("Content-Type"), "application/json") { + return nil, errors.New("unsupported content type") + } + + workerID := chi.URLParam(r, "worker_id") + if workerID == "" { + return nil, errors.New("worker_id is required") + } + + return WorkerRequestDTO{WorkerID: workerID}, nil +} + +func encodeWorkerResponse(_ context.Context, w http.ResponseWriter, response interface{}) error { + w.Header().Set("Content-Type", "application/json") + return json.NewEncoder(w).Encode(response) +} diff --git a/worker/service.go b/worker/service.go new file mode 100644 index 0000000..d35a120 --- /dev/null +++ b/worker/service.go @@ -0,0 +1,33 @@ +package worker + +import ( + "context" + "time" +) + +type WorkerService interface { + GetWorkerStats(ctx context.Context, workerID string) (WorkerResponseDTO, error) +} + +type workerService struct{} + +func NewWorkerService() WorkerService { + return &workerService{} +} + +func (s *workerService) GetWorkerStats(ctx context.Context, workerID string) (WorkerResponseDTO, error) { + return WorkerResponseDTO{ + WorkerID: workerID, + Name: "WorkerName", + State: "Running", + Function: "Compute", + RestartPolicy: "Always", + RestartCount: 1, + StartTime: time.Now().Add(-2 * time.Hour), + FinishTime: time.Now().Add(1 * time.Hour), + CPUUsage: 0.75, + MemoryUsage: 0.65, + TaskCount: 10, + RunningTasks: 3, + }, nil +}