Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tpoxa committed Apr 27, 2023
0 parents commit bdbb7bd
Show file tree
Hide file tree
Showing 68 changed files with 7,491 additions and 0 deletions.
26 changes: 26 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*


node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
.npmrc
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Maksym Trofimenko

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
## Module
All-in-one library to run and build Tiny Systems modules.
103 changes: 103 additions & 0 deletions cli/build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package cli

import (
"fmt"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/spf13/viper"
tinymodule "github.com/tiny-systems/module/pkg/api/module-go"
"github.com/tiny-systems/module/pkg/utils"
"github.com/tiny-systems/module/registry"
"github.com/tiny-systems/module/tools/build"
"github.com/tiny-systems/module/tools/readme"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"os"
)

var (
devKey string
pathToMain string
)

var buildCmd = &cobra.Command{
Use: "build",
Short: "build module",
Long: `Run from module's root folder (go.mod && README.md files should exist there) If your main's package path differ from ./cmd please specify path parameter'`,
Run: func(cmd *cobra.Command, args []string) {
ctx := cmd.Context()

cwd, err := os.Getwd()
if err != nil {
log.Fatal().Err(err).Msgf("unable to get current path: %v", err)
}

info, err := readme.GetReadme(cwd)
if err != nil {
log.Fatal().Err(err).Msgf("unable to get README.md by path %s: %v", cwd, err)
}

var opts []grpc.DialOption

if viper.GetBool("insecure") || grpcServerInsecureConnect {
opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
}

conn, err := grpc.Dial(grpcConnStr, opts...)
if err != nil {
log.Fatal().Err(err).Msg("unable to connect to platform gRPC server")
}
defer conn.Close()

platformClient := tinymodule.NewPlatformServiceClient(conn)

componentsApi := make([]*tinymodule.Component, 0)
for _, c := range registry.Get() {
cmpApi, err := utils.GetComponentApi(c)
if err != nil {
log.Error().Err(err).Msg("component to api")
continue
}
componentsApi = append(componentsApi, cmpApi)
}

resp, err := platformClient.PublishModule(ctx, &tinymodule.PublishModuleRequest{
Name: name,
Info: info,
Version: version,
DeveloperKey: devKey,
Components: componentsApi,
})
if err != nil {
log.Fatal().Err(err).Msg("unable to publish module")
}
if resp.Module == nil {
log.Fatal().Err(err).Msg("invalid server response")
}

buildOpts := build.Options{
Repo: resp.Options.Repo,
Tag: resp.Options.Tag,
VersionID: resp.Module.ID,
}
if err := build.Build(ctx, cwd, pathToMain, buildOpts); err != nil {
log.Fatal().Err(err).Msg("unable to build")
}
image := fmt.Sprintf("%s:%s", resp.Options.Repo, resp.Options.Tag)

if err = build.Push(ctx, image, resp.Options.Username, resp.Options.Password); err != nil {
log.Fatal().Err(err).Str("image", image).Msg("unable to push")
}

_, err = platformClient.UpdateModuleVersion(ctx, &tinymodule.UpdateModuleVersionRequest{
ID: resp.Module.ID,
Repo: resp.Options.Repo,
Tag: resp.Options.Tag,
})
if err != nil {
log.Fatal().Err(err).Str("image", image).Msg("unable to update server")
}

log.Info().Str("image", image).Msg("pushed")
},
}
38 changes: 38 additions & 0 deletions cli/cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package cli

import "github.com/spf13/cobra"

var (
serverKey string
grpcServerInsecureConnect bool
grpcConnStr string
)

func RegisterCommands(root *cobra.Command) {
runCmd.Flags().StringVarP(&serverKey, "key", "k", "", "Server key")
applyCommonBuildFlags(runCmd)

applyConnFlags(runCmd)
root.AddCommand(runCmd)
//
root.AddCommand(infoCmd)
//
buildCmd.Flags().StringVarP(&pathToMain, "path", "p", "./cmd", "path to main package regarding to the root")
applyCommonBuildFlags(buildCmd)
//
buildCmd.Flags().StringVarP(&devKey, "devkey", "d", "", "developer key")
buildCmd.MarkFlagRequired("devkey")

applyConnFlags(buildCmd)
root.AddCommand(buildCmd)
}

func applyConnFlags(cmd *cobra.Command) {
cmd.Flags().BoolVarP(&grpcServerInsecureConnect, "insecure", "i", false, "gRPC connect with insecure credentials")
cmd.Flags().StringVarP(&grpcConnStr, "conn", "c", "localhost:50189", "gRPC server connection string")
}

func applyCommonBuildFlags(cmd *cobra.Command) {
cmd.Flags().StringVarP(&version, "version", "v", "0.0.0", "version")
cmd.Flags().StringVarP(&name, "name", "n", "main", "module name")
}
20 changes: 20 additions & 0 deletions cli/info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package cli

import (
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/tiny-systems/module/registry"
)

var infoCmd = &cobra.Command{
Use: "info",
Short: "get components info",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
components := registry.Get()
log.Info().Int("components", len(components)).Msg("registered")
for _, c := range registry.Get() {
log.Info().Msgf("%s - %s\n", c.GetInfo().Name, c.GetInfo().Description)
}
},
}
125 changes: 125 additions & 0 deletions cli/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package cli

import (
"context"
"github.com/nats-io/nats.go"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/tiny-systems/module"
tinyserver "github.com/tiny-systems/module/pkg/api/module-go"
m "github.com/tiny-systems/module/pkg/module"
"github.com/tiny-systems/module/pkg/service-discovery/client"
"github.com/tiny-systems/module/pkg/service-discovery/discovery"
"github.com/tiny-systems/module/registry"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"sync"
)

// override by ldflags
var (
version string
name string
versionID string // ldflags
)

var runCmd = &cobra.Command{
Use: "run",
Short: "run module",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {

log.Info().Str("versionID", versionID).Msg("starting...")
// run all modules
ctx, cancel := context.WithCancel(cmd.Context())
defer cancel()
if serverKey == "" {
serverKey = viper.GetString("server_key")
}

if serverKey == "" {
log.Fatal().Msg("no server key defined")
}

natsConnStr := viper.GetString("nats_conn_str")
if natsConnStr == "" {
natsConnStr = nats.DefaultURL
}
nc, err := nats.Connect(natsConnStr)
if err != nil {
log.Fatal().Err(err).Msg("unable to connect to NATS")
}

discovery, err := client.NewClient(nc, discovery.DefaultLivecycle)
if err != nil {
log.Fatal().Err(err).Msg("unable to connect to NATS")
}
defer nc.Close()

var opts []grpc.DialOption

if viper.GetBool("insecure") || grpcServerInsecureConnect {
opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
}

conn, err := grpc.Dial(grpcConnStr, opts...)
if err != nil {
log.Fatal().Err(err).Msg("unable to connect to platform gRPC server")
}
defer conn.Close()

platformClient := tinyserver.NewPlatformServiceClient(conn)
manifestResp, err := platformClient.GetManifest(ctx, &tinyserver.GetManifestRequest{
ServerKey: serverKey,
ModuleVersionID: versionID,
Version: version,
})
if err != nil {
log.Fatal().Err(err).Msg("manifest error")
}

errChan := make(chan error)
defer close(errChan)
go func() {
for err := range errChan {
log.Error().Err(err).Msg("")
}
}()

serv := module.New(manifestResp.RunnerConfig, errChan)

serv.SetLogger(log.Logger)
serv.SetDiscovery(discovery)
serv.SetNats(nc)

wg := &sync.WaitGroup{}
wg.Add(1)

go func() {
defer wg.Done()
if err := serv.Run(ctx); err != nil {
log.Error().Err(err).Msg("unable to run server")
}
}()

info := m.Info{
Version: version,
Name: name,
VersionID: versionID,
}

for _, cmp := range registry.Get() {
if err = serv.InstallComponent(ctx, info, cmp); err != nil {
log.Error().Err(err).Str("component", cmp.GetInfo().Name).Msg("unable to install component")
}
}

for _, instance := range manifestResp.Instances {
serv.RunInstance(instance)
}

wg.Wait()
log.Info().Msg("all done")
},
}
Loading

0 comments on commit bdbb7bd

Please sign in to comment.