Gen6_Public_Blockchain/g6_swiss_army.sh

192 lines
5.9 KiB
Bash
Executable File

#!/bin/bash
set -e
real_path=$(realpath $0)
base_path=$(dirname ${real_path})
rust_output_binary_name="g6-solo-node"
temporal_dir=${base_path}/tmp/
deb_package_name=g6-solo-chain
deb_dir_tree_sbin=${base_path}/${deb_package_name}/usr/local/sbin
chainspec_dir=${base_path}/chainspecs/live
deb_dir_chainspec=${base_path}/${deb_package_name}/etc/g6/public-chain/chainspec
nexus_user=""
nexus_pass=""
NEXUS_URL="https://nexus.g6.network/repository/g6_os_apt_packages/"
CREDENTIALS_FILE="nexus_credentials.env"
PIPELINE_SUFFIX=${PIPELINE_SUFFIX:-""}
# Function to show usage
function usage() {
echo "Usage: $0 [OPTIONS]"
echo "Options:"
echo " -b, --build Build the g6-solo-chain binary"
echo " -c, --clean-build Clean build the g6-solo-chain binary (runs 'cargo clean' first)"
echo " -d, --deploy Deploy the Docker services"
echo " -u, --undeploy Stop and remove Docker Compose services"
echo " -p, --package VERSION Build and upload the deb package to Nexus with specified VERSION"
echo " -h, --help Show this help message"
exit 1
}
# Validate version format
function validate_version() {
local version=$1
echo "Validating version: $version"
if [[ ! $version =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "Error: Invalid version format. Use major.minor.bugfix (e.g., 1.0.1)."
exit 1
fi
}
# Read Nexus credentials
function get_nexus_credentials() {
if [[ -f "$CREDENTIALS_FILE" ]]; then
# Read the credentials file, skipping comment lines
local lines=()
while IFS= read -r line; do
[[ "$line" =~ ^#.*$ || -z "$line" ]] && continue # Skip comments and empty lines
lines+=("$line")
done <"$CREDENTIALS_FILE"
# Assign user and pass from the first two non-comment lines
nexus_user="${lines[0]}"
nexus_pass="${lines[1]}"
fi
echo "Passed first stage"
# Check environment variables if credentials are not found in the file
if [[ -z "$nexus_user" || -z "$nexus_pass" ]]; then
nexus_user="${NEXUS_USER:-}"
nexus_pass="${NEXUS_PASSWORD:-}"
fi
echo "Passed second stage"
}
# Build function
function build() {
echo "Building g6-solo-chain binary..."
echo "Using the following Docker Compose file: docker/builder/docker-compose${PIPELINE_SUFFIX}.yml"
docker compose -f docker/builder/docker-compose${PIPELINE_SUFFIX}.yml up -d
docker compose -f docker/builder/docker-compose${PIPELINE_SUFFIX}.yml exec node_builder${PIPELINE_SUFFIX} bash -c "cargo build --release"
echo "Build completed."
}
# Clean build function
function clean_build() {
echo "Performing a clean build for g6-solo-chain binary..."
echo "Using the following Docker Compose file: docker/builder/docker-compose${PIPELINE_SUFFIX}.yml"
docker compose -f docker/builder/docker-compose${PIPELINE_SUFFIX}.yml up -d
docker compose -f docker/builder/docker-compose${PIPELINE_SUFFIX}.yml exec node_builder${PIPELINE_SUFFIX} bash -c "cargo clean && cargo build --release"
echo "Clean build completed."
}
# Deploy function
function deploy() {
echo "Deploying Docker services..."
echo "Using the following Docker Compose file: docker/builder/docker-compose${PIPELINE_SUFFIX}.yml"
docker compose -f docker/builder/docker-compose${PIPELINE_SUFFIX}.yml up -d
echo "Deploy completed."
}
# Undeploy function
function undeploy() {
echo "Stopping and removing Docker services..."
echo "Using the following Docker Compose file: docker/builder/docker-compose${PIPELINE_SUFFIX}.yml"
docker compose -f docker/builder/docker-compose${PIPELINE_SUFFIX}.yml stop
docker compose -f docker/builder/docker-compose${PIPELINE_SUFFIX}.yml down
echo "Undeploy completed."
}
# Package function
function package() {
# Parse version from control file
local control_file="${base_path}/${deb_package_name}/DEBIAN/control"
if [[ ! -f "$control_file" ]]; then
echo "Error: Control file not found at $control_file."
exit 1
fi
validate_version "$VERSION_DEB_PACKAGE" # Ensure extracted version is valid
echo "Packaging g6-solo-chain version $VERSION_DEB_PACKAGE..."
mkdir -p ${deb_dir_tree_sbin}
build # Ensure the binary is built before packaging
# Copy binary
cp ${base_path}/target/release/${rust_output_binary_name} ${deb_dir_tree_sbin}/
# Copy chainspecs/
mkdir -p ${deb_dir_chainspec}
cp $chainspec_dir/* ${deb_dir_chainspec}/
# Include version in package name
echo "VERSION_DEB_PACKAGE: $VERSION_DEB_PACKAGE"
# Set version value
sed -i s/VERSION_PLACEHOLDER/$VERSION_DEB_PACKAGE/ $base_path/$deb_package_name/DEBIAN/control
# Deb package generation
dpkg-deb --build ${deb_package_name}
sed -i s/$VERSION_DEB_PACKAGE/VERSION_PLACEHOLDER/ $base_path/$deb_package_name/DEBIAN/control
mv "${deb_package_name}.deb" "${deb_package_name}_${version}.deb"
# Get Nexus credentials
get_nexus_credentials
echo "Obtained credentials: ${nexus_user}"
echo "Uploading to Nexus..."
curl -u "${nexus_user}:${nexus_pass}" -H "Content-Type: multipart/form-data" --data-binary "@./${deb_package_name}_${version}.deb" "${NEXUS_URL}"
echo "Cleaning up..."
rm -f "${deb_package_name}_${version}.deb"
rm -rf ${temporal_dir}
rm -rf ${deb_dir_chainspec}/*
echo "Package version $VERSION_DEB_PACKAGE uploaded successfully."
}
# Parse options
if [[ "$#" -eq 0 ]]; then
usage
fi
while [[ "$#" -gt 0 ]]; do
case "$1" in
-b | --build)
build
exit 0
;;
-c | --clean-build)
clean_build
exit 0
;;
-d | --deploy)
deploy
exit 0
;;
-u | --undeploy)
undeploy
exit 0
;;
-p | --package)
shift
package
exit 0
;;
-h | --help)
usage
;;
*)
echo "Unknown option: $1"
usage
;;
esac
shift
done