Skip to content

Commit

Permalink
cli: node startup cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
iurii-ssv committed Oct 26, 2024
1 parent 3de919b commit 7ad4069
Show file tree
Hide file tree
Showing 10 changed files with 196 additions and 199 deletions.
12 changes: 11 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ RUN apt-get update && \
make=4.3-4.1 \
&& rm -rf /var/lib/apt/lists/*

# install jemalloc
WORKDIR /tmp/jemalloc-temp
RUN curl -s -L "https://github.com/jemalloc/jemalloc/releases/download/5.3.0/jemalloc-5.3.0.tar.bz2" -o jemalloc.tar.bz2 \
&& tar xjf ./jemalloc.tar.bz2
RUN cd jemalloc-5.3.0 \
&& ./configure --with-jemalloc-prefix='je_' --with-malloc-conf='background_thread:true,metadata_thp:auto' \
&& make && make install

RUN go version

WORKDIR /go/src/github.com/ssvlabs/ssv/
Expand All @@ -38,13 +46,15 @@ RUN --mount=type=cache,target=/root/.cache/go-build \
COMMIT=$(git rev-parse HEAD) && \
VERSION=$(git describe --tags $(git rev-list --tags --max-count=1) --always) && \
CGO_ENABLED=1 GOOS=linux go install \
-tags="blst_enabled" \
-tags="blst_enabled,jemalloc,allocator" \
-ldflags "-X main.Commit=$COMMIT -X main.Version=$VERSION -linkmode external -extldflags \"-static -lm\"" \
./cmd/ssvnode

#
# STEP 3: Prepare image to run the binary
#
# IMPORTANT: before upgrading to go 1.23 or higher make sure the following issue has been resolved:
# https://github.com/golang/go/issues/69978
FROM golang:1.22 AS runner

RUN apt-get update && \
Expand Down
31 changes: 12 additions & 19 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@ ifndef BUILD_PATH
export BUILD_PATH
endif

# Node command.
NODE_COMMAND=--config=${CONFIG_PATH}
NODE_COMMAND_ARGS=--config=${CONFIG_PATH}
ifneq ($(SHARE_CONFIG),)
NODE_COMMAND+= --share-config=${SHARE_CONFIG}
NODE_COMMAND_ARGS+= --share-config=${SHARE_CONFIG}
endif

# Bootnode command.
BOOTNODE_COMMAND=--config=${CONFIG_PATH}
BOOTNODE_COMMAND_ARGS_ARGS=--config=${CONFIG_PATH}

COV_CMD="-cover"
ifeq ($(COVERAGE),true)
Expand Down Expand Up @@ -111,28 +109,23 @@ build:

.PHONY: start-node
start-node:
@echo "Build ${BUILD_PATH}"
@echo "Build ${CONFIG_PATH}"
@echo "Build ${CONFIG_PATH2}"
@echo "Command ${NODE_COMMAND}"
@echo "Build binary: ${BUILD_PATH}"
@echo "Config path: ${CONFIG_PATH}"
@echo "Share config path: ${SHARE_CONFIG}"
@echo "Command provided: ${NODE_COMMAND_ARGS}"
ifdef DEBUG_PORT
@echo "Running node-${NODE_ID} in debug mode"
@dlv --continue --accept-multiclient --headless --listen=:${DEBUG_PORT} --api-version=2 exec \
${BUILD_PATH} start-node -- ${NODE_COMMAND}
${BUILD_PATH} start-node -- ${NODE_COMMAND_ARGS}
else
@echo "Running node on address: ${HOST_ADDRESS})"
@${BUILD_PATH} start-node ${NODE_COMMAND}
@echo "Running node on address: ${HOST_ADDRESS}"
@${BUILD_PATH} start-node ${NODE_COMMAND_ARGS}
endif

.PHONY: docker
docker:
@echo "node ${NODES_ID}"
@docker rm -f ssv_node && docker build -t ssv_node . && docker run -d --env-file .env --restart unless-stopped --name=ssv_node -p 13000:13000 -p 12000:12000/udp -it ssv_node make BUILD_PATH=/go/bin/ssvnode start-node && docker logs ssv_node --follow

.PHONY: docker-image
docker-image:
@echo "node ${NODES_ID}"
@sudo docker rm -f ssv_node && docker run -d --env-file .env --restart unless-stopped --name=ssv_node -p 13000:13000 -p 12000:12000/udp 'ssvlabs/ssv-node:latest' make BUILD_PATH=/go/bin/ssvnode start-node
@docker rm -f ssv_node && docker build -t ssv_node . && docker run --env-file .env --name=ssv_node -p 16000:16000 -p 13001:13001 -p 12001:12001/udp -v ./data:/data -it ssv_node make BUILD_PATH=/go/bin/ssvnode start-node && docker logs ssv_node --follow

NODES=ssv-node-1 ssv-node-2 ssv-node-3 ssv-node-4
.PHONY: docker-all
Expand All @@ -159,7 +152,7 @@ stop:
.PHONY: start-boot-node
start-boot-node:
@echo "Running start-boot-node"
${BUILD_PATH} start-boot-node ${BOOTNODE_COMMAND}
${BUILD_PATH} start-boot-node ${BOOTNODE_COMMAND_ARGS_ARGS}

MONITOR_NODES=prometheus grafana
.PHONY: docker-monitor
Expand Down
32 changes: 14 additions & 18 deletions cli/bootnode/boot_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,23 @@ import (
"fmt"
"log"

"github.com/spf13/cobra"
globalcfg "github.com/ssvlabs/ssv/cli/config"
"github.com/ssvlabs/ssv/logging"
"github.com/ssvlabs/ssv/networkconfig"
bootnode "github.com/ssvlabs/ssv/utils/boot_node"
"github.com/ssvlabs/ssv/utils/commons"

"github.com/ssvlabs/ssv/logging"

"github.com/ilyakaznacheev/cleanenv"
"github.com/spf13/cobra"
"go.uber.org/zap"

global_config "github.com/ssvlabs/ssv/cli/config"
bootnode "github.com/ssvlabs/ssv/utils/boot_node"
)

type config struct {
global_config.GlobalConfig `yaml:"global"`
Options bootnode.Options `yaml:"bootnode"`
globalcfg.Global `yaml:"global"`
Options bootnode.Options `yaml:"bootnode"`
}

var cfg config

var globalArgs global_config.Args
var globalArgs globalcfg.Args

// StartBootNodeCmd is the command to start SSV boot node
var StartBootNodeCmd = &cobra.Command{
Expand All @@ -33,11 +29,12 @@ var StartBootNodeCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
commons.SetBuildData(cmd.Parent().Short, cmd.Parent().Version)

if err := cleanenv.ReadConfig(globalArgs.ConfigPath, &cfg); err != nil {
log.Fatal(err)
err := globalcfg.Prepare(&cfg, &globalArgs)
if err != nil {
log.Fatalf("could not prepare config: %v", err)
}

err := logging.SetGlobalLogger(
err = logging.SetGlobalLogger(
cfg.LogLevel,
cfg.LogLevelFormat,
cfg.LogFormat,
Expand All @@ -47,12 +44,11 @@ var StartBootNodeCmd = &cobra.Command{
MaxBackups: cfg.LogFileBackups,
},
)

if err != nil {
log.Fatal(err)
log.Fatalf("could not create logger: %v", err)
}

logger := zap.L()
defer logging.CapturePanic(logger)

logger.Info(fmt.Sprintf("starting %v", commons.GetBuildData()))

Expand All @@ -71,5 +67,5 @@ var StartBootNodeCmd = &cobra.Command{
}

func init() {
global_config.ProcessArgs(&cfg, &globalArgs, StartBootNodeCmd)
globalcfg.ProcessArgs(&cfg, &globalArgs, StartBootNodeCmd)
}
41 changes: 35 additions & 6 deletions cli/config/config.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
package config

import (
"fmt"

"github.com/ilyakaznacheev/cleanenv"
"github.com/spf13/cobra"
)

// Args expose available global args for cli command
type Args struct {
ConfigPath string
// ConfigPath is a path to main configuration file.
ConfigPath string
// ShareConfigPath is an additional config file (path) that (if present) will overwrite
// configuration supplied from config file at ConfigPath.
ShareConfigPath string
}

// GlobalConfig expose available global config for cli command
type GlobalConfig struct {
LogLevel string `yaml:"LogLevel" env:"LOG_LEVEL" env-default:"info" env-description:"Defines logger's log level'"`
LogFormat string `yaml:"LogFormat" env:"LOG_FORMAT" env-default:"console" env-description:"Defines logger's encoding, valid values are 'json' (default) and 'console''"`
LogLevelFormat string `yaml:"LogLevelFormat" env:"LOG_LEVEL_FORMAT" env-default:"capitalColor" env-description:"Defines logger's level format, valid values are 'capitalColor' (default), 'capital' or 'lowercase''"`
// Global expose available global config for cli command
type Global struct {
LogLevel string `yaml:"LogLevel" env:"LOG_LEVEL" env-default:"info" env-description:"Defines logger's log level"`
LogFormat string `yaml:"LogFormat" env:"LOG_FORMAT" env-default:"console" env-description:"Defines logger's encoding, valid values are 'json' and 'console'(default)"`
LogLevelFormat string `yaml:"LogLevelFormat" env:"LOG_LEVEL_FORMAT" env-default:"capitalColor" env-description:"Defines logger's level format, valid values are 'capitalColor' (default), 'capital' or 'lowercase'"`
LogFilePath string `yaml:"LogFilePath" env:"LOG_FILE_PATH" env-default:"./data/debug.log" env-description:"Defines a file path to write logs into"`
LogFileSize int `yaml:"LogFileSize" env:"LOG_FILE_SIZE" env-default:"500" env-description:"Defines a file size in megabytes to rotate logs"`
LogFileBackups int `yaml:"LogFileBackups" env:"LOG_FILE_BACKUPS" env-default:"3" env-description:"Defines a number of backups to keep when rotating logs"`
Expand All @@ -33,5 +38,29 @@ func ProcessArgs(cfg interface{}, a *Args, cmd *cobra.Command) {

envHelp, _ := cleanenv.GetDescription(cfg, nil)
cmd.SetUsageTemplate(envHelp + "\n" + cmd.UsageTemplate())
}

func Prepare(cfg interface{}, a *Args) error {
if a.ConfigPath != "" {
err := cleanenv.ReadConfig(a.ConfigPath, cfg)
if err != nil {
return fmt.Errorf("could not read config: %w", err)
}
}
if a.ShareConfigPath != "" {
err := cleanenv.ReadConfig(a.ShareConfigPath, cfg)
if err != nil {
return fmt.Errorf("could not read share config: %w", err)
}
}

if a.ConfigPath == "" && a.ShareConfigPath == "" {
// When no config file is provided fall back to env vars + default annotations defined on cfg struct.
err := cleanenv.ReadEnv(cfg)
if err != nil {
return fmt.Errorf("could not set up config based on environment variables and struct defaults: %w", err)
}
}

return nil
}
Loading

0 comments on commit 7ad4069

Please sign in to comment.