Skip to content

Commit

Permalink
- adding scripts for the SC calls executor
Browse files Browse the repository at this point in the history
  • Loading branch information
iulianpascalau committed Jan 17, 2025
1 parent 05bfbf5 commit a30c830
Show file tree
Hide file tree
Showing 6 changed files with 549 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ cache
artifacts

/cmd/cmd

# default config directory & files that will be used by the app
cmd/scCallsExecutor/config/*.toml
File renamed without changes.
251 changes: 251 additions & 0 deletions scripts/scCallsExecutor/config/functions.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,251 @@
#!/bin/bash
set -e

function init {
if ! [ -f "$LOCAL_OVERRIDES" ]; then
echo "#!/bin/bash
set -e
CUSTOM_HOME=\"/home/ubuntu\"
CUSTOM_USER=\"ubuntu\"
GITHUBTOKEN=\"\"
MONITOR_EXTRA_FLAGS=\"\"
# Allow user to override the current version of the sc calls executor
OVERRIDE_VER=\"\"
" > $LOCAL_OVERRIDES

echo -e
echo -e "${GREEN}Created ./config/local.cfg - now will be a good time to add your scripts variables there...${NC}"
echo -e
else
echo -e
echo -e "${CYAN}Will not override the existing file ./config/local.cfg${NC}"
echo -e
fi

copy_config_file "$WORKDIR/cmd/scCallsExecutor/config/config.toml.example" "$WORKDIR/cmd/scCallsExecutor/config/config.toml"
echo -e
}

function copy_config_file {
if ! [ -f "$2" ]; then
cp $1 $2

echo -e "${GREEN}Copied $1 to $2...${NC}"
else
echo -e "${CYAN}Will not override the existing file $2${NC}"
fi
echo -e
}

function check_variables {
# Check if CUSTOM_HOME exists
if ! [ -d "$CUSTOM_HOME" ]; then echo -e "${RED}Please configure your variables first ! (config/local.cfg (based on variables.cfg) --> CUSTOM_HOME & CUSTOM_USER)${NC}"; exit; fi
}

function prerequisites {
# Prerequisites function
echo -e
echo -e "${GREEN}Updating system & installing some dependencies...${NC}"
echo -e
DISTRO=$(cat /etc/*-release | grep -w "ID")

if [[ $DISTRO == *"linuxmint"* ]] || [[ $DISTRO == *"ubuntu"* ]] || [[ $DISTRO == *"debian"* ]] || [[ $DISTRO == *"elementary"* ]]; then
sudo DEBIAN_FRONTEND=noninteractive apt-get update -qq && sudo DEBIAN_FRONTEND=noninteractive apt-get -qy -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" dist-upgrade > /dev/null
sudo DEBIAN_FRONTEND=noninteractive apt-get -qy -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" install build-essential > /dev/null && sudo DEBIAN_FRONTEND=noninteractive apt-get -qy -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" install git rsync curl zip unzip jq gcc wget > /dev/null
echo "export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin" >> ~/.profile
echo "export GOPATH=$HOME/go" >> ~/.profile
else
echo "Unsupported DISTRO..."
exit
fi

# Check if $CUSTOM_HOME exists and if not create it
if ! [ -d "$CUSTOM_HOME" ]; then mkdir -p $CUSTOM_HOME; fi

# Limit journalctl size
echo -e
echo -e "${GREEN}Limiting journalctl node units logs size...${NC}"
echo -e
echo 'SystemMaxUse=4000M' | sudo tee -a /etc/systemd/journald.conf && echo 'SystemMaxFileSize=800M' | sudo tee -a /etc/systemd/journald.conf
sudo systemctl restart systemd-journald

echo -e "${GREEN}Task completed.${NC}"
echo -e
}

function paths {
# Export environment variables
export GOPATH=$HOME/go
export PATH=$GOPATH/bin:$PATH
export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin
}

function go_lang {
# Identify machine architecture
ARCH=$(dpkg --print-architecture)

# Check if golang is installed on system
if ! [ -x "$(command -v go)" ]; then
# Get the latest version of GO for amd64 & installing it
echo -e
echo -e "${RED}GO is not installed on your system${NC}"
echo -e
echo -e "${GREEN}The best working version of Go is:${CYAN}${GO_LATEST_TESTED}${NC}"
echo -e "${GREEN}Installing it now...${NC}"
echo -e
wget https://dl.google.com/go/${GO_LATEST_TESTED}.linux-${ARCH}.tar.gz
sudo tar -C /usr/local -xzf ${GO_LATEST_TESTED}.linux-${ARCH}.tar.gz
rm ${GO_LATEST_TESTED}.linux-${ARCH}.tar.gz

else
# Check the installed version number
GOVERSION=$(go version | awk '{print $3}')
if [[ "$GOVERSION" < "${GO_LATEST_TESTED}" ]]; then
echo -e
echo -e "${RED}GO version is ${CYAN}$GOVERSION${RED} and the best working version is ${CYAN}${GO_LATEST_TESTED}${RED}... ${NC}"
# Detect go install method
GO_INSTALL_METHOD=$(which go)

if [[ "$GO_INSTALL_METHOD" == "/usr/local/go/bin/go" ]]; then
# Installed by scripts. Go ahead and upgrade.
echo -e
echo -e "${GREEN}Your GO binary will pe upgraded to the minimum required version...${NC}"
sudo rm -rf /usr/local/go
wget -4 https://dl.google.com/go/${GO_LATEST_TESTED}.linux-${ARCH}.tar.gz
sudo tar -C /usr/local -xzf ${GO_LATEST_TESTED}.linux-${ARCH}.tar.gz
rm ${GO_LATEST_TESTED}.linux-${ARCH}.tar.gz

else
echo -e
echo -e "${RED}GO was not installed using the elrond scripts. Operation cannot continue...${NC}"
exit
fi

else
echo -e
echo -e "${GREEN}GO is already installed: ${CYAN}$GOVERSION${NC}${GREEN}...skipping install${NC}"
echo -e
fi
fi
}

function check_api_limit {
# Check API rate-limit
if [ ${GITHUB_API_LIMIT} -eq ${GITHUB_API_USED} ] && [[ -z "$GITHUBTOKEN" ]]; then
echo -e
echo -e "${RED}GitHub API Requests Limit for this IP has been reached !${NC}"
echo -e "${RED}The counter will reset at ${CYAN}${HUMAN_TIME}${RED}.${NC}"
echo -e
echo -e "${RED}We suggest using a GitHub Token to avoid this issue in the future.${NC}"
exit
else
echo -e
echo -e "${GREEN}Your GitHub API Requests is at ${CYAN}$GITHUB_API_USED${GREEN} out of a total of ${CYAN}$GITHUB_API_LIMIT${GREEN} (applied per hour)${NC}"
echo -e
fi
}

function build_binary {
# Build the SC calls executor from latest tag
echo -e
echo -e "${GREEN}Building your Bridge SC calls executor binary...${NC}"
echo -e

# If OVERRIDE_VER is set SHOWVER should take it into account
[[ ! -z "$OVERRIDE_VER" ]] && SHOWVER=$(echo ${OVERRIDE_VER#${TMPVAR}})
[[ ! -z "$OVERRIDE_VER" ]] && VERSION=$(echo $OVERRIDE_VER)

# Building the node from the local directory
cd $WORKDIR
echo -e "${GREEN}Compiling binary with version ${CYAN}$VERSION${GREEN}...${NC}"
echo -e

cd cmd/scCallsExecutor
# alter/create the local.go file that is .gitignored
appVersion=$SHOWVER-0-$(git describe --tags --long | tail -c 11)
echo "package main
func init() {
appVersion = \"$appVersion\"
}" > local.go

git fetch
git checkout $VERSION
if [ "$VERSION" == "$SHOWVER" ]; then
# we are on a branch, we can do a git pull
git pull
fi

go build
echo -e "${GREEN}Done !${NC}"
}

function show_menu {
echo -e
echo -e
echo -e "${CYAN}MultiversX Bridge SC calls executor scripts options:${NC}"
echo -e
echo -e "${GREEN} 1) ${CYAN}init${GREEN} - init the scripts by creating the config/local.cfg file${NC}"
echo -e "${GREEN} 2) ${CYAN}install${GREEN} - Regular install process${NC}"
echo -e "${GREEN} 3) ${CYAN}upgrade${GREEN} - Run the upgrade process${NC}"
echo -e "${GREEN} 4) ${CYAN}start${GREEN} - Start the service{NC}"
echo -e "${GREEN} 5) ${CYAN}stop${GREEN} - Stops the service{NC}"
echo -e "${GREEN} 6) ${CYAN}cleanup${GREEN} - Remove everything from the host${NC}"
echo -e "${GREEN} 7) ${CYAN}get_logs${GREEN} - Get the logs produced by the service${NC}"
echo -e "${GREEN} 8) ${CYAN}quit${GREEN} - Exit this menu${NC}"
echo -e
echo -e
}

function systemd {
# Add systemd service & syslogd logging
echo -e
echo -e "${GREEN}Installing systemd unit for the Bridge SC calls executor...${NC}"
echo -e

# Create the service file for our node
echo "[Unit]
Description=MultiversX Bridge SC calls executor
After=network-online.target
[Service]
User=$CUSTOM_USER
WorkingDirectory=$WORKDIR/cmd/scCallsExecutor
ExecStart=$WORKDIR/cmd/scCallsExecutor/scCallsExecutor -log-level *:DEBUG $EXTRA_FLAGS
StandardOutput=journal
StandardError=journal
Restart=always
RestartSec=3
LimitNOFILE=4096
[Install]
WantedBy=multi-user.target" > mx-bridge-sc-calls-executor.service

# Move files to appropriate locations
if [ -f "/etc/systemd/system/mx-bridge-sc-calls-executor.service" ]; then
echo -e
echo -e "${GREEN}Refreshing mx-bridge-sc-calls-executor service (stop-disable-remove)...${NC}"
echo -e
sudo systemctl stop mx-bridge-sc-calls-executor
sleep 1
sudo systemctl disable mx-bridge-sc-calls-executor.service
sleep 1
sudo rm -f /etc/systemd/system/mx-bridge-sc-calls-executor.service
sleep 1
fi
sudo mv mx-bridge-sc-calls-executor.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable mx-bridge-sc-calls-executor.service
}

function cleanup_files {

# Cleanup Logs
echo -e
echo -e "${RED}Erasing previously produced data (logs)...${NC}"
sudo rm -rf $WORKDIR/logs
# recreate erased folders
mkdir -p $WORKDIR/{logs}
}
114 changes: 114 additions & 0 deletions scripts/scCallsExecutor/config/menu_functions.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#!/bin/bash
set -e

source $SCRIPTPATH/config/variables.cfg
source $SCRIPTPATH/config/functions.cfg
# Load local overrides, .gitignored
LOCAL_OVERRIDES="$SCRIPTPATH/config/local.cfg"
if [ -f "$LOCAL_OVERRIDES" ]; then
source "$SCRIPTPATH/config/local.cfg"
fi

function install {
check_variables
prerequisites
paths
go_lang
check_api_limit
build_binary
systemd
echo -e
echo -e "${GREEN}--> mx-bridge-sc-calls-executor installed. Don't forget to add the multiversx.pem key file in ../cmd/scCallsExecutor/keys and to configure the ../cmd/scCallsExecutor/config/config.toml file. Then call the start function...${NC}"
echo -e
}

function upgrade {
echo -e
read -p "Do you want to go on with the upgrade (Default No) ? (Yy/Nn)" yn
echo -e

case $yn in
[Yy]* )
sudo systemctl stop mx-bridge-sc-calls-executor
check_variables
check_api_limit
build_binary
systemd
echo -e
echo -e "${CYAN}--> mx-bridge-sc-calls-executor upgraded. Don't forget to start it...${NC}"
echo -e
;;

[Nn]* )
echo -e "${GREEN}Fine ! Skipping upgrade on this machine...${NC}"
;;

* )
echo -e "${GREEN}I'll take that as a no then... moving on...${NC}"
;;
esac
}

function start {
sudo systemctl daemon-reload
sudo systemctl start mx-bridge-sc-calls-executor
}

function stop {
sudo systemctl stop mx-bridge-sc-calls-executor
}

function cleanup {
paths
echo -e
read -p "Do you want to delete installed nodes (Default No) ? (Yy/Nn)" yn
echo -e
case $yn in
[Yy]* )
echo -e "${RED}OK ! Cleaning everything !${NC}"

echo -e
echo -e "${GREEN}Stopping MultiversX Bridge SC calls executor binary on host ${CYAN}$HOST${GREEN}...${NC}"
echo -e
if [ -e /etc/systemd/system/mx-bridge-sc-calls-executor.service ]; then sudo systemctl stop mx-bridge-sc-calls-executor; fi
echo -e "${GREEN}Erasing unit file and directory for MultiversX the Bridge SC calls executor...${NC}"
echo -e
if [ -e /etc/systemd/system/mx-bridge-sc-calls-executor.service ]; then sudo rm /etc/systemd/system/mx-bridge-sc-calls-executor.service; fi
if [ -d $CUSTOM_HOME/mx-bridge-sc-calls-executor ]; then sudo rm -rf $CUSTOM_HOME/mx-bridge-sc-calls-executor; fi

# Reload systemd after deleting node units
sudo systemctl daemon-reload

echo -e "${GREEN}Removing paths from .profile on host ${CYAN}$HOST${GREEN}...${NC}"
echo -e
sed -i 'N;$!P;$!D;$d' ~/.profile

if [ -d "$GOPATH/pkg" ]; then sudo rm -rf $GOPATH/pkg; fi
;;

[Nn]* )
echo -e "${GREEN}Fine ! Skipping cleanup on this machine...${NC}"
;;

* )
echo -e "${GREEN}I'll take that as a no then... moving on...${NC}"
;;
esac
}

function get_logs {
# Get journalctl logs for the service
LOGSTIME=$(date "+%Y%m%d-%H%M")
# Make sure the log path exists
mkdir -p $CUSTOM_HOME/mx-bridge-sc-calls-executor

echo -e
echo -e "${GREEN}Getting logs for MultiversX Bridge SC calls executor...${NC}"
echo -e
sudo journalctl --unit mx-bridge-sc-calls-executor >> $CUSTOM_HOME/mx-bridge-sc-calls-executor-logs/mx-bridge-sc-calls-executor-logs.log

# Compress the logs and erase files
cd $CUSTOM_HOME/mx-bridge-sc-calls-executor-logs/ && tar -zcvf logs-$LOGSTIME.tar.gz *.log && rm *.log
echo -e
echo -e "${GREEN}---> Logs have been stored in the ${CYAN}~/mx-bridge-sc-calls-executor-logs${GREEN} directory...${NC}"
}
Loading

0 comments on commit a30c830

Please sign in to comment.