Skip to content

Commit

Permalink
add pr#84 changes - part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
mrpalide committed Dec 22, 2024
1 parent 850a2a6 commit 93f1aa4
Show file tree
Hide file tree
Showing 5 changed files with 634 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ check-help: ## Cursory check of the help menus
## : ## _ [E2E tests suite]

e2e-build: set-forwarding ## E2E. Build dockers and containers for e2e-tests
./docker/docker_build.sh e2e ${BUILD_OPTS_DEPLOY}
./docker/docker_build.sh e2e ${BUILD_OPTS_DEPLOY} $(BUILD_ARCH)

e2e-run: ## E2E. Start e2e environment
bash -c "DOCKER_TAG=e2e docker compose up -d"
Expand Down Expand Up @@ -225,7 +225,7 @@ reset-forwarding:
## : ## _ [Interactive integration tests]

integration-env-build: set-forwarding #build
./docker/docker_build.sh integration ${BUILD_OPTS_DEPLOY}
./docker/docker_build.sh integration ${BUILD_OPTS_DEPLOY} $(BUILD_ARCH)
bash -c "DOCKER_TAG=integration docker compose up -d"

integration-env-start: set-forwarding #start
Expand Down
16 changes: 16 additions & 0 deletions cmd/dmsg-monitor/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# DMSG Monitor

## API endpoints

### GET `/health`
Gets the health info of the service. e.g.
```
{
"build_info": {
"version": "v1.0.1-267-ge1617c5b",
"commit": "e1617c5b0121182cfd2b610dc518e4753e56440e",
"date": "2022-10-25T11:01:52Z"
},
"started_at": "2022-10-25T11:10:45.152629597Z"
}
```
123 changes: 123 additions & 0 deletions cmd/dmsg-monitor/commands/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// Package commands cmd/dmsg-monitor/commands/root.go
package commands

import (
"context"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"time"

"github.com/skycoin/skywire/pkg/skywire-utilities/pkg/buildinfo"
"github.com/skycoin/skywire/pkg/skywire-utilities/pkg/cipher"
"github.com/skycoin/skywire/pkg/skywire-utilities/pkg/cmdutil"
"github.com/skycoin/skywire/pkg/skywire-utilities/pkg/logging"
"github.com/skycoin/skywire/pkg/skywire-utilities/pkg/tcpproxy"
"github.com/spf13/cobra"

"github.com/skycoin/skywire-services/pkg/dmsg-monitor/api"
)

var (
confPath string
dmsgURL string
utURL string
addr string
tag string
logLvl string
sleepDeregistration time.Duration
batchSize int
)

func init() {
RootCmd.Flags().StringVarP(&addr, "addr", "a", ":9080", "address to bind to.\033[0m")
RootCmd.Flags().DurationVarP(&sleepDeregistration, "sleep-deregistration", "s", 60, "Sleep time for derigstration process in minutes\033[0m")
RootCmd.Flags().IntVarP(&batchSize, "batchsize", "b", 20, "Batch size of deregistration\033[0m")
RootCmd.Flags().StringVarP(&confPath, "config", "c", "dmsg-monitor.json", "config file location.\033[0m")
RootCmd.Flags().StringVarP(&dmsgURL, "dmsg-url", "d", "", "url to dmsg data.\033[0m")
RootCmd.Flags().StringVarP(&utURL, "ut-url", "u", "", "url to uptime tracker visor data.\033[0m")
RootCmd.Flags().StringVar(&tag, "tag", "dmsg_monitor", "logging tag\033[0m")
RootCmd.Flags().StringVarP(&logLvl, "loglvl", "l", "info", "set log level one of: info, error, warn, debug, trace, panic")
}

// RootCmd contains the root command
var RootCmd = &cobra.Command{
Use: func() string {
return strings.Split(filepath.Base(strings.ReplaceAll(strings.ReplaceAll(fmt.Sprintf("%v", os.Args), "[", ""), "]", "")), " ")[0]
}(),
Short: "DMSG monitor of DMSG discovery entries.",
Long: `
┌┬┐┌┬┐┌─┐┌─┐ ┌┬┐┌─┐┌┐┌┬┌┬┐┌─┐┬─┐
│││││└─┐│ ┬───││││ │││││ │ │ │├┬┘
─┴┘┴ ┴└─┘└─┘ ┴ ┴└─┘┘└┘┴ ┴ └─┘┴└─
`,
SilenceErrors: true,
SilenceUsage: true,
DisableSuggestions: true,
DisableFlagsInUseLine: true,
Version: buildinfo.Version(),
Run: func(_ *cobra.Command, _ []string) {
if _, err := buildinfo.Get().WriteTo(os.Stdout); err != nil {
log.Printf("Failed to output build info: %v", err)
}

mLogger := logging.NewMasterLogger()
lvl, err := logging.LevelFromString(logLvl)
if err != nil {
mLogger.Fatal("Invalid log level")
}
logging.SetLevel(lvl)

conf := api.InitConfig(confPath, mLogger)

if dmsgURL == "" {
dmsgURL = conf.Dmsg.Discovery
}
if utURL == "" {
utURL = conf.UptimeTracker.Addr + "/uptimes"
}

var srvURLs api.ServicesURLs
srvURLs.DMSG = dmsgURL
srvURLs.UT = utURL

logger := mLogger.PackageLogger(tag)

logger.WithField("addr", addr).Info("Serving DMSG-Monitor API...")

monitorSign, _ := cipher.SignPayload([]byte(conf.PK.Hex()), conf.SK) //nolint

var monitorConfig api.DMSGMonitorConfig
monitorConfig.PK = conf.PK
monitorConfig.Sign = monitorSign
monitorConfig.BatchSize = batchSize

dmsgMonitorAPI := api.New(logger, srvURLs, monitorConfig)

ctx, cancel := cmdutil.SignalContext(context.Background(), logger)
defer cancel()

go dmsgMonitorAPI.InitDeregistrationLoop(ctx, conf, sleepDeregistration)

go func() {
if err := tcpproxy.ListenAndServe(addr, dmsgMonitorAPI); err != nil {
logger.Errorf("serve: %v", err)
cancel()
}
}()

<-ctx.Done()
if err := dmsgMonitorAPI.Visor.Close(); err != nil {
logger.WithError(err).Error("Visor closed with error.")
}
},
}

// Execute executes root CLI command.
func Execute() {
if err := RootCmd.Execute(); err != nil {
log.Fatal("Failed to execute command: ", err)
}
}
43 changes: 43 additions & 0 deletions cmd/dmsg-monitor/dmsg-monitor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Package main cmd/dmsg-monitor/dmsg-monitor.go
package main

import (
cc "github.com/ivanpirog/coloredcobra"
"github.com/spf13/cobra"

"github.com/skycoin/skywire-services/cmd/dmsg-monitor/commands"
)

func init() {
var helpflag bool
commands.RootCmd.SetUsageTemplate(help)
commands.RootCmd.PersistentFlags().BoolVarP(&helpflag, "help", "h", false, "help for dmsgpty-cli")
commands.RootCmd.SetHelpCommand(&cobra.Command{Hidden: true})
commands.RootCmd.PersistentFlags().MarkHidden("help") //nolint
}

func main() {
cc.Init(&cc.Config{
RootCmd: commands.RootCmd,
Headings: cc.HiBlue + cc.Bold,
Commands: cc.HiBlue + cc.Bold,
CmdShortDescr: cc.HiBlue,
Example: cc.HiBlue + cc.Italic,
ExecName: cc.HiBlue + cc.Bold,
Flags: cc.HiBlue + cc.Bold,
FlagsDescr: cc.HiBlue,
NoExtraNewlines: true,
NoBottomNewline: true,
})
commands.Execute()
}

const help = "Usage:\r\n" +
" {{.UseLine}}{{if .HasAvailableSubCommands}}{{end}} {{if gt (len .Aliases) 0}}\r\n\r\n" +
"{{.NameAndAliases}}{{end}}{{if .HasAvailableSubCommands}}\r\n\r\n" +
"Available Commands:{{range .Commands}}{{if (or .IsAvailableCommand)}}\r\n " +
"{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}\r\n\r\n" +
"Flags:\r\n" +
"{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}\r\n\r\n" +
"Global Flags:\r\n" +
"{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}\r\n\r\n"
Loading

0 comments on commit 93f1aa4

Please sign in to comment.