Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maintain integration env #84

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,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 @@ -231,7 +231,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-utilities/pkg/buildinfo"
"github.com/skycoin/skywire-utilities/pkg/cipher"
"github.com/skycoin/skywire-utilities/pkg/cmdutil"
"github.com/skycoin/skywire-utilities/pkg/logging"
"github.com/skycoin/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"
2 changes: 0 additions & 2 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: "3.7"

networks:
srv:
driver: "bridge"
Expand Down
37 changes: 12 additions & 25 deletions docker/docker_build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ trap "exit" INT
## Variables
image_tag="$1"
go_buildopts="$2"
build_arch="$3"
git_branch="$(git rev-parse --abbrev-ref HEAD)"
nv_dev_url="https://nv.skywire.dev/map"
nv_prod_url="https://nv.skycoin.com/map"
Expand All @@ -15,7 +16,7 @@ platform="--platform=linux/amd64"
registry="$REGISTRY"

# shellcheck disable=SC2153
base_image=golang:1.21-alpine
base_image=golang:1.23-alpine

if [[ "$#" != 2 ]]; then
echo "docker_build.sh <IMAGE_TAG> <GO_BUILDOPTS>"
Expand All @@ -25,6 +26,10 @@ if [[ "$go_buildopts" == "" ]]; then
go_buildopts="-mod=vendor -ldflags\"-w -s\""
fi

if [[ "$build_arch" != "" ]]; then
platform="--platform=$build_arch"
fi

if [[ "$git_branch" != "master" ]] && [[ "$git_branch" != "develop" ]]; then
git_branch="develop"
fi
Expand Down Expand Up @@ -64,6 +69,7 @@ if [[ "$image_tag" == "e2e" ]]; then
DOCKER_BUILDKIT="$bldkit" docker build -f docker/images/dmsg-discovery/Dockerfile \
--build-arg build_opts="$go_buildopts" \
--build-arg image_tag="$image_tag" \
$platform \
--build-arg base_image="skycoin/dmsg-discovery:$dockerhub_image_tag" \
-t "$registry"/dmsg-discovery:"$image_tag" .

Expand All @@ -72,6 +78,7 @@ if [[ "$image_tag" == "e2e" ]]; then
--build-arg base_image="skycoin/dmsg-server:$dockerhub_image_tag" \
--build-arg build_opts="$go_buildopts" \
--build-arg image_tag="$image_tag" \
$platform \
-t "$registry"/dmsg-server:"$image_tag" .

echo "build service discovery image"
Expand All @@ -81,13 +88,6 @@ if [[ "$image_tag" == "e2e" ]]; then
--build-arg image_tag="$image_tag" \
$platform \
-t "$registry"/service-discovery:"$image_tag" .

echo "building uptime tracker image"
DOCKER_BUILDKIT="$bldkit" docker build -f docker/images/uptime-tracker/Dockerfile \
--build-arg base_image="$base_image" \
--build-arg build_opts="$go_buildopts" \
--build-arg image_tag="$image_tag" \
-t "$registry"/uptime-tracker:"$image_tag" .

rm -rf ./tmp/skycoin-service-discovery
fi
Expand All @@ -97,41 +97,36 @@ if [[ "$image_tag" == "integration" ]]; then
rm -rf ./tmp/skycoin-service-discovery
rm -rf ./tmp/dmsg
rm -rf ./tmp/skywire
rm -rf ./tmp/skywire-ut
cp -r ../skycoin-service-discovery ./tmp
cp -r ../dmsg ./tmp
cp -r ../skywire ./tmp
cp -r ../skywire-ut ./tmp

echo ====================================================
echo "BUILDING SKYWIRE VISOR"

DOCKER_BUILDKIT="$bldkit" docker build -f docker/images/skywire-visor/DockerfileInt \
$platform \
-t "$registry"/skywire-visor:"$image_tag" .

echo ============ Base images ready ======================

echo "build dmsg discovery image"
DOCKER_BUILDKIT="$bldkit" docker build -f docker/images/dmsg-discovery/DockerfileInt \
$platform \
-t "$registry"/dmsg-discovery:"$image_tag" .

echo "build dmsg server image"
DOCKER_BUILDKIT="$bldkit" docker build -f docker/images/dmsg-server/DockerfileInt \
$platform \
-t "$registry"/dmsg-server:"$image_tag" .

echo "build service discovery image"
DOCKER_BUILDKIT="$bldkit" docker build -f docker/images/service-discovery/Dockerfile \
--build-arg base_image="$base_image" \
--build-arg build_opts="$go_buildopts" \
--build-arg image_tag="$image_tag" \
$platform \
-t "$registry"/service-discovery:"$image_tag" .

echo "building uptime tracker image"
DOCKER_BUILDKIT="$bldkit" docker build -f docker/images/uptime-tracker/Dockerfile \
--build-arg base_image="$base_image" \
--build-arg build_opts="$go_buildopts" \
--build-arg image_tag="$image_tag" \
-t "$registry"/uptime-tracker:"$image_tag" .

rm -rf ./tmp/*
fi
Expand Down Expand Up @@ -205,14 +200,6 @@ DOCKER_BUILDKIT="$bldkit" docker build -f docker/images/network-monitor/Dockerfi
$platform \
-t "$registry"/network-monitor:"$image_tag" .

echo "building liveness checker image"
DOCKER_BUILDKIT="$bldkit" docker build -f docker/images/liveness-checker/Dockerfile \
--build-arg base_image="$base_image" \
--build-arg build_opts="$go_buildopts" \
--build-arg image_tag="$image_tag" \
$platform \
-t "$registry"/liveness-checker:"$image_tag" .

echo "building config bootstrapper image"
DOCKER_BUILDKIT="$bldkit" docker build -f docker/images/config-bootstrapper/Dockerfile \
--build-arg base_image="$base_image" \
Expand Down
1 change: 0 additions & 1 deletion docker/docker_clean.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ declare -a images_arr=(
"skycoin/network-monitor:${image_tag}"
# "skycoin/node-visualizer:${image_tag}"
"skycoin/config-bootstrapper:${image_tag}"
"skycoin/liveness-checker:${image_tag}"
"skycoin/vpn-monitor:${image_tag}"
"skycoin/public-visor-monitor:${image_tag}"
"skycoin/dmsg-monitor:${image_tag}"
Expand Down
1 change: 0 additions & 1 deletion docker/docker_push.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ declare -a images_arr=(
"network-monitor"
# "node-visualizer"
"config-bootstrapper"
"liveness-checker"
"vpn-monitor"
"public-visor-monitor"
"dmsg-monitor"
Expand Down
10 changes: 5 additions & 5 deletions docker/images/address-resolver/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ARG base_image
ARG image_tag

FROM ${base_image} as builder
FROM ${base_image} AS builder

ARG build_opts

Expand All @@ -12,14 +12,14 @@ RUN go build "${build_opts}" -o /release/address-resolver \
./cmd/address-resolver


FROM alpine as prod
FROM alpine AS prod
COPY --from=builder /release/address-resolver /release/address-resolver
ENTRYPOINT ["/release/address-resolver"]

FROM prod as test
FROM prod AS test

# OS image
FROM alpine as e2e
FROM alpine AS e2e

COPY ./docker/common/install-prequisites.sh /release/install-prequisites.sh
RUN sh -c /release/install-prequisites.sh cert-only \
Expand All @@ -28,6 +28,6 @@ RUN sh -c /release/install-prequisites.sh cert-only \
COPY --from=builder /release/address-resolver /release/address-resolver
ENTRYPOINT ["/release/address-resolver"]

FROM e2e as integration
FROM e2e AS integration

FROM ${image_tag}
Loading
Loading