From bd84875d040286f294bbfe7f48e48f56cb755d6a Mon Sep 17 00:00:00 2001 From: Chalith Desaman Date: Tue, 6 Sep 2022 09:16:28 +0530 Subject: [PATCH] Introduce starting port number env variable (#6) --- docker/build.sh | 3 ++ docker/push.sh | 3 ++ docker/scripts/cluster.sh | 12 ++++--- linux/hpdevkit.sh | 69 +++++++++++++++++++++++++++++++-------- upload.sh | 56 +++++++++++++++++++++++++++++++ windows/hpdevkit.ps1 | 15 ++++++++- 6 files changed, 138 insertions(+), 20 deletions(-) create mode 100755 docker/build.sh create mode 100755 docker/push.sh create mode 100755 upload.sh diff --git a/docker/build.sh b/docker/build.sh new file mode 100755 index 0000000..216cec1 --- /dev/null +++ b/docker/build.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +docker build -t evernodedev/hpdevkit . \ No newline at end of file diff --git a/docker/push.sh b/docker/push.sh new file mode 100755 index 0000000..ed21b49 --- /dev/null +++ b/docker/push.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +docker push evernodedev/hpdevkit \ No newline at end of file diff --git a/docker/scripts/cluster.sh b/docker/scripts/cluster.sh index c4737e6..f3d521d 100644 --- a/docker/scripts/cluster.sh +++ b/docker/scripts/cluster.sh @@ -10,6 +10,8 @@ volume_mount=$VOLUME_MOUNT bundle_mount=$BUNDLE_MOUNT hotpocket_image=$HOTPOCKET_IMAGE config_overrides_file=$CONFIG_OVERRIDES_FILE +user_port_begin=$HP_USER_PORT_BEGIN +peer_port_begin=$HP_PEER_PORT_BEGIN if [ "$command" = "create" ] || [ "$command" = "bindmesh" ] || [ "$command" = "destroy" ] || \ [ "$command" = "start" ] || [ "$command" = "stop" ] || \ @@ -72,8 +74,8 @@ function create_instance { # Create contract instance directory. docker run --rm --mount type=volume,src=$volume,dst=$volume_mount --rm $hotpocket_image new $volume_mount/node$node - let peer_port=22860+$node - let user_port=8080+$node + let peer_port=$(($peer_port_begin + $node - 1)) + let user_port=$(($user_port_begin + $node - 1)) # Create container for hotpocket instance. local container_name="${container_prefix}_$node" @@ -119,7 +121,7 @@ function joinarr { # Update all instances hotpocket configs so they connect to each other as a cluster. function bind_mesh { local instance_count=$(get_container_count) - + # Collect pubkeys and peers of all nodes. local all_pubkeys local all_peers @@ -133,8 +135,8 @@ function bind_mesh { [ $i -eq 1 ] && contract_id=$(jq ".contract.id" $cfg_file) # Assign user and peer ports in incrementing order. - let peer_port=22860+$i - let user_port=8080+$i + let peer_port=$(($peer_port_begin + $i - 1)) + let user_port=$(($user_port_begin + $i - 1)) jq ".contract.id=$contract_id | .contract.roundtime=2000 | .mesh.port=$peer_port | .user.port=$user_port" $cfg_file > $cfg_file.tmp \ && mv $cfg_file.tmp $cfg_file diff --git a/linux/hpdevkit.sh b/linux/hpdevkit.sh index 71036f2..a03cdc1 100755 --- a/linux/hpdevkit.sh +++ b/linux/hpdevkit.sh @@ -7,6 +7,8 @@ clusterSize=$([ -z $HP_CLUSTER_SIZE ] && echo 3 || echo "$HP_CLUSTER_SIZE") defaultNode=$([ -z $HP_DEFAULT_NODE ] && echo 1 || echo "$HP_DEFAULT_NODE") devkitImage=$([ -z $HP_DEVKIT_IMAGE ] && echo "evernodedev/hpdevkit" || echo "$HP_DEVKIT_IMAGE") instanceImage=$([ -z $HP_INSTANCE_IMAGE ] && echo "evernodedev/hotpocket:latest-ubt.20.04-njs.16" || echo "$HP_INSTANCE_IMAGE") +hpUserPortBegin=$([ -z $HP_USER_PORT_BEGIN ] && echo 8081 || echo "$HP_USER_PORT_BEGIN") +hpPeerPortBegin=$([ -z $HP_PEER_PORT_BEGIN ] && echo 22861 || echo "$HP_PEER_PORT_BEGIN") volumeMount=/$globalPrefix\_vol volume=$globalPrefix\_$cluster\_vol @@ -18,7 +20,7 @@ codegenContainerName=$globalPrefix\_codegen configOverridesFile="hp.cfg.override" codegenOutputDir="/codegen-output" -cloudStorage="https://stevernode.blob.core.windows.net/evernode-beta" +cloudStorage="https://stevernode.blob.core.windows.net/evernode-dev-bb7ec110-f72e-430e-b297-9210468a4cbb" bashScriptUrl="$cloudStorage/$globalPrefix-linux/$globalPrefix.sh" hpdevkitDataDir="/etc/$globalPrefix" versionTimestampFile="$hpdevkitDataDir/linuxlauncherscript.timestamp" @@ -55,6 +57,7 @@ function devKitContainer() { command+=" -e CLUSTER=$cluster -e CLUSTER_SIZE=$clusterSize -e DEFAULT_NODE=$defaultNode -e VOLUME=$volume -e NETWORK=$network" command+=" -e CONTAINER_PREFIX=$containerPrefix -e VOLUME_MOUNT=$volumeMount -e BUNDLE_MOUNT=$bundleMount -e HOTPOCKET_IMAGE=$instanceImage" command+=" -e CONFIG_OVERRIDES_FILE=$configOverridesFile -e CODEGEN_OUTPUT=$codegenOutputDir" + command+=" -e HP_USER_PORT_BEGIN=$hpUserPortBegin -e HP_PEER_PORT_BEGIN=$hpPeerPortBegin" command+=" $devkitImage" @@ -156,6 +159,17 @@ function codeGenerator() { } +function removeLauncher() { + rm $scriptBinPath +} + +function createLauncher() { + # Copying the current script file to the bin directory + ! curl -fsSL $bashScriptUrl --output $scriptBinPath &>/dev/null && echo "Error in creating launcher." && return 1 + ! chmod +x $scriptBinPath &>/dev/null && echo "Error in changing permission for the launcher." && return 1 + return 0 +} + function updateDevKit() { local latestVersionTimestamp=$(online_version_timestamp $bashScriptUrl) [ -z "$latestVersionTimestamp" ] && echo "Online launcher not found." && exit 1 @@ -169,10 +183,10 @@ function updateDevKit() { echo "HotPocket devkit is already upto date." else echo "Found a new version of HotPocket devkit." - ! rm $scriptBinPath && echo " Removing previous launcher failed" - ! curl -fsSL $bashScriptUrl --output $scriptBinPath 2>&1 && echo "Error in downloading the new launcher." - ! chmod +x $scriptBinPath &>/dev/null && echo "Error in changing permission for the launcher." + ! removeLauncher && echo "Removing the launcher failed." + ! createLauncher && echo "Launcher creation failed." echo $latestVersionTimestamp >$versionTimestampFile + echo "HotPocket devkit update completed !!" fi fi @@ -180,23 +194,35 @@ function updateDevKit() { docker pull $devkitImage &>/dev/null docker pull $instanceImage &>/dev/null - echo "Update Completed." + # Clear if there's already deployed cluster since they are outdated now. + if docker inspect $deploymentContainerName &>/dev/null; then + echo "Cleaning the deployed contracts..." + teardownDeploymentCluster + fi + + echo "Update Completed !!" echo "NOTE: You need to re-deploy your contracts to make the new changes effective." } +function checkExistance() { + command -v hpdevkit &>/dev/null && [ -f $scriptBinPath ] && [ -d $hpdevkitDataDir ] && + echo -e "hpdevkit is already installed on your host. \nUse the 'hpdevkit' start your local HotPocket testing." && + exit 1 +} + function online_version_timestamp() { # Send HTTP HEAD request and get last modified timestamp of the installer package or setup.sh. curl --silent --head $1 | grep 'Last-Modified:' | sed 's/[^ ]* //' } function install() { + checkExistance + if [[ ! -d $hpdevkitDataDir ]]; then ! mkdir $hpdevkitDataDir && echo "Data path creation error." && exit 1 fi - # Copying the current script file to the bin directory - ! curl -fsSL $bashScriptUrl --output $scriptBinPath 2>/dev/null && echo "Binary copying to '/usr/bin' failed." && exit 1 - ! chmod +x $scriptBinPath 2>&1 && echo "Error in changing permission for the launcher." && exit 1 + ! createLauncher && echo "Launcher creation failed." # Creating timestamp file local latestVersionTimestamp=$(online_version_timestamp $bashScriptUrl) @@ -204,14 +230,29 @@ function install() { } function uninstall() { - if [[ -d $hpdevkitDataDir ]]; then - rm $hpdevkitDataDir/* 2>/dev/null - rm -d $hpdevkitDataDir + # Remove deployment cluster if exist. + if docker inspect $deploymentContainerName &>/dev/null; then + echo "Cleaning the deployed contracts..." + teardownDeploymentCluster fi - if [[ -f "/usr/bin/hpdevkit" ]]; then - rm "$scriptBinPath" + # Remove docker images if exist. + if docker image inspect $devkitImage &>/dev/null; then + echo "Removing devkit docker image..." + docker image rm $devkitImage &>/dev/null fi + if docker image inspect $instanceImage &>/dev/null; then + echo "Removing instance docker image..." + docker image rm $instanceImage &>/dev/null + fi + + echo "Removing binaries..." + if [[ -d $hpdevkitDataDir ]]; then + rm -r $hpdevkitDataDir + fi + + echo "Removing the launcher..." + removeLauncher } function is_user_root() { @@ -271,4 +312,4 @@ else echo -e $helpMessage fi -exit 0 \ No newline at end of file +exit 0 diff --git a/upload.sh b/upload.sh new file mode 100755 index 0000000..0d5d6d3 --- /dev/null +++ b/upload.sh @@ -0,0 +1,56 @@ +#!/bin/bash +# Upload specified file to Azure blob storage. +# Based on https://gist.github.com/rtyler/30e51dc72bed23718388c43f9c11da76 + +AZURE_STORAGE_ACCOUNT=$1 +AZURE_CONTAINER_NAME=$2 +AZURE_ACCESS_KEY=$3 +FILEPATH=$4 + +[ ! -f $FILEPATH ] && echo "$FILEPATH not found." && exit 1 + +FILENAME=$(basename $FILEPATH) + +authorization="SharedKey" + +HTTP_METHOD="PUT" +request_date=$(TZ=GMT date "+%a, %d %h %Y %H:%M:%S %Z") +storage_service_version="2015-02-21" + +# HTTP Request headers +x_ms_date_h="x-ms-date:$request_date" +x_ms_version_h="x-ms-version:$storage_service_version" +x_ms_blob_type_h="x-ms-blob-type:BlockBlob" + +FILE_LENGTH=$(wc --bytes < ${FILEPATH}) +FILE_TYPE=$(file --mime-type -b ${FILEPATH}) + +# Build the signature string +canonicalized_headers="${x_ms_blob_type_h}\n${x_ms_date_h}\n${x_ms_version_h}" +canonicalized_resource="/${AZURE_STORAGE_ACCOUNT}/${AZURE_CONTAINER_NAME}/${FILENAME}" +string_to_sign="${HTTP_METHOD}\n\n\n${FILE_LENGTH}\n\n${FILE_TYPE}\n\n\n\n\n\n\n${canonicalized_headers}\n${canonicalized_resource}" + +# Decode the Base64 encoded access key, convert to Hex. +decoded_hex_key="$(echo -n $AZURE_ACCESS_KEY | base64 -d -w0 | xxd -p -c256)" + +# Create the HMAC signature for the Authorization header +signature=$(printf "$string_to_sign" | openssl dgst -sha256 -mac HMAC -macopt "hexkey:$decoded_hex_key" -binary | base64 -w0) + +authorization_header="Authorization: $authorization $AZURE_STORAGE_ACCOUNT:$signature" +OUTPUT_FILE="https://${AZURE_STORAGE_ACCOUNT}.blob.core.windows.net/${AZURE_CONTAINER_NAME}/${FILENAME}" + +echo "Uploading..." +curl -X ${HTTP_METHOD} \ + -T ${FILEPATH} \ + -H "$x_ms_date_h" \ + -H "$x_ms_version_h" \ + -H "$x_ms_blob_type_h" \ + -H "$authorization_header" \ + -H "Content-Type: ${FILE_TYPE}" \ + ${OUTPUT_FILE} + +if [ $? -eq 0 ]; then + echo "Uploaded to:" ${OUTPUT_FILE} + exit 0; +fi; +exit 1 \ No newline at end of file diff --git a/windows/hpdevkit.ps1 b/windows/hpdevkit.ps1 index b3f1292..40e0f99 100644 --- a/windows/hpdevkit.ps1 +++ b/windows/hpdevkit.ps1 @@ -6,6 +6,8 @@ $ClusterSize = if ($env:HP_CLUSTER_SIZE) { $env:HP_CLUSTER_SIZE } else { 3 }; $DefaultNode = if ($env:HP_DEFAULT_NODE) { $env:HP_DEFAULT_NODE } else { 1 }; $DevKitImage = if ($env:HP_DEVKIT_IMAGE) { $env:HP_DEVKIT_IMAGE } else { "evernodedev/hpdevkit" }; $InstanceImage = if ($env:HP_INSTANCE_IMAGE) { $env:HP_INSTANCE_IMAGE } else { "evernodedev/hotpocket:latest-ubt.20.04-njs.16" }; +$HpUserPortBegin = if ($env:HP_USER_PORT_BEGIN) { $env:HP_USER_PORT_BEGIN } else { 8081 }; +$HpPeerPortBegin = if ($env:HP_PEER_PORT_BEGIN) { $env:HP_PEER_PORT_BEGIN } else { 22861 }; $VolumeMount = "/$($GlobalPrefix)_vol" $Volume = "$($GlobalPrefix)_$($Cluster)_vol" @@ -16,7 +18,7 @@ $DeploymentContainerName = "$($GlobalPrefix)_$($Cluster)_deploymgr" $CodegenContainerName = "$($GlobalPrefix)_codegen" $ConfigOverridesFile = "hp.cfg.override" $CodegenOutputDir = "/codegen-output" -$CloudStorage = "https://stevernode.blob.core.windows.net/evernode-beta" +$CloudStorage = "https://stevernode.blob.core.windows.net/evernode-dev-bb7ec110-f72e-430e-b297-9210468a4cbb" $HPDevKitExeUrl = "$($CloudStorage)/hpdevkit-windows/hpdevkit.exe"; $HPDevKitBackup = "\hpdevkit.exe.bak"; $ExePath = (Get-Process -Id $pid).Path @@ -53,6 +55,7 @@ function DevKitContainer([string]$Mode, [string]$Name, [switch]$Detached, [switc $Command += " -e CLUSTER=$($Cluster) -e CLUSTER_SIZE=$($ClusterSize) -e DEFAULT_NODE=$($DefaultNode) -e VOLUME=$($Volume) -e NETWORK=$($Network)" $Command += " -e CONTAINER_PREFIX=$($ContainerPrefix) -e VOLUME_MOUNT=$($VolumeMount) -e BUNDLE_MOUNT=$($BundleMount) -e HOTPOCKET_IMAGE=$($InstanceImage)" $Command += " -e CONFIG_OVERRIDES_FILE=$($ConfigOverridesFile) -e CODEGEN_OUTPUT=$($CodegenOutputDir)" + $Command += " -e HP_USER_PORT_BEGIN=$($HpUserPortBegin) -e HP_PEER_PORT_BEGIN=$($HpPeerPortBegin)" $Command += " $($DevKitImage)" if ($Cmd) { @@ -180,6 +183,16 @@ Function UpdateHPDevKit() { ## Pull the updated Docker instance image from Docker Hub. Write-Host "Pulling the latest $InstanceImage Image." docker pull $InstanceImage + + ## Clear if there's already deployed cluster since they are outdated now. + $Null = docker inspect $DeploymentContainerName *>&1 + if ($?) { + Write-Host "Cleaning the deployed contracts." + TeardownDeploymentCluster + } + + Write-Host "Update Completed !!" + Write-Host "NOTE: You need to re-deploy your contracts to make the new changes effective." } if ($ExePath.Contains('hpdevkit.exe')) {