diff --git a/docker/scripts/cluster.sh b/docker/scripts/cluster.sh index cee7269..464f711 100644 --- a/docker/scripts/cluster.sh +++ b/docker/scripts/cluster.sh @@ -12,9 +12,9 @@ hotpocket_image=$HOTPOCKET_IMAGE if [ "$command" = "create" ] || [ "$command" = "destroy" ] || [ "$command" = "start" ] || [ "$command" = "stop" ] || [ "$command" = "logs" ] || [ "$command" = "sync" ] ; then - echo "command: $command" + echo "sub-command: $command" else - echo "Invalid command." + echo "Invalid sub-command." echo "Expected: create | destroy | start | stop | logs | sync" exit 1 fi @@ -86,12 +86,8 @@ function create_cluster { function destroy_cluster { ensure_cluser_exists - echo "action: stop" - change_cluster_status "stop" - # Delete top-most matching container N times. # (This is just in case there are gaps in container numbering due to any tampering by user) - echo "action: delete" local container_count=$(get_container_count) for ((i=1; i<=$container_count; i++)); do @@ -106,11 +102,16 @@ function destroy_cluster { function change_cluster_status { ensure_cluser_exists + local action=$1 + local node=$2 local container_count=$(get_container_count) - for ((i=1; i<=$container_count; i++)); + for ((i=1; i<=$container_count; i++)); do - local container_name="${container_prefix}_$i" - docker $1 $container_name + # If valid node no. has been specified, target that node. Otherwise target all nodes. + if ! [ "$node" -eq "$node" ] 2> /dev/null || [ $node -le 0 ] || [ $i -eq $node ] ; then + local container_name="${container_prefix}_$i" + docker $action $container_name + fi done } @@ -138,9 +139,9 @@ if [ $command = "create" ]; then elif [ $command = "destroy" ]; then destroy_cluster elif [ $command = "start" ]; then - change_cluster_status start + change_cluster_status start $2 elif [ $command = "stop" ]; then - change_cluster_status stop + change_cluster_status stop $2 elif [ $command = "logs" ]; then ! validate_node_num_arg $2 && echo "Usage: logs " && exit 1 attach_logs $2 diff --git a/windows/hpdevkit.ps1 b/windows/hpdevkit.ps1 index b10ef04..f65721a 100644 --- a/windows/hpdevkit.ps1 +++ b/windows/hpdevkit.ps1 @@ -5,14 +5,19 @@ $HotPocketImage = "evernodedev/hotpocket:latest-ubt.20.04-njs.16" $Cluster = if ($env:HP_CLUSTER) { $env:HP_CLUSTER } else { "default" }; $ClusterSize = if ($env:HP_CLUSTER_SIZE) { $env:HP_CLUSTER_SIZE } else { 1 }; +$DefaultNode = if ($env:HP_DEFAULT_NODE) { $env:HP_DEFAULT_NODE } else { 1 }; $Volume = "$($GlobalPrefix)_$($Cluster)_vol" $Network = "$($GlobalPrefix)_$($Cluster)_net" $ContainerPrefix = "$($GlobalPrefix)_$($Cluster)_con" $BundleMount = "$($VolumeMount)/contract_bundle" -$DevKitContainerName = "$($GlobalPrefix)_launcher" +$DeploymentContainerName = "$($GlobalPrefix)_$($Cluster)_deploymanager" -function DevKitContainer([string]$Mode, [switch]$Detached, [switch]$AutoRemove, [switch]$MountSock, [switch]$MountVolume, [string]$Cmd) { - $Command = "docker $($Mode) --name $($DevKitContainerName) -it" +function DevKitContainer([string]$Mode, [string]$Name, [switch]$Detached, [switch]$AutoRemove, [switch]$MountSock, [switch]$MountVolume, [string]$Cmd) { + + $Command = "docker $($Mode) -it" + if ($Name) { + $Command += " --name $($Name)" + } if ($Detached) { $Command += " -d" } @@ -32,25 +37,38 @@ function DevKitContainer([string]$Mode, [switch]$Detached, [switch]$AutoRemove, $Command += " $($DevKitImage)" if ($Cmd) { - $Command += " $($Cmd)" + $Command += " /bin/bash -c '$($Cmd)'" } Invoke-Expression $Command } -function RemoveDevKitContainer() { - $Null = docker stop $DevKitContainerName *>&1 - $Null = docker rm $DevKitContainerName *>&1 +function ExecuteInDeploymentContainer([string]$Cmd) { + docker exec -it $DeploymentContainerName /bin/bash -c $Cmd } -function ExecuteInDevKitContainer([string]$Cmd) { - docker exec -it $DevKitContainerName /bin/bash -c $Cmd +function InitializeDeploymentCluster() { + $Null = docker inspect $DeploymentContainerName *>&1 + if (! ($?)) { + Write-Host "Initializing deployment cluster" + + # Stop cluster if running. Create cluster if not exists. + DevKitContainer -Mode "run" -AutoRemove -MountSock -Cmd "cluster stop ; cluster create" + + # Spin up management container. + DevKitContainer -Mode "run" -Name $DeploymentContainerName -Detached -MountSock -MountVolume + } } -Function DeployContractFiles([string]$Path) { +function TeardownDeploymentCluster() { + $Null = docker stop $DeploymentContainerName *>&1 + $Null = docker rm $DeploymentContainerName *>&1 + DevKitContainer -Mode "run" -AutoRemove -MountSock -Cmd "cluster stop ; cluster destroy" +} - RemoveDevKitContainer - DevKitContainer -Mode "run" -Detached -MountSock -MountVolume +Function Deploy([string]$Path) { + + InitializeDeploymentCluster # If copying a directory, delete target bundle directory. If not create empty target bundle directory to copy a file. $PrepareBundleDir = "" @@ -60,25 +78,44 @@ Function DeployContractFiles([string]$Path) { else { $PrepareBundleDir = "mkdir -p $($BundleMount) && rm -rf $($BundleMount)/* $($BundleMount)/.??*" } - ExecuteInDevKitContainer -Cmd $PrepareBundleDir - docker cp $Path "$($DevKitContainerName):$($BundleMount)" - - # Sync contract bundle to all instance directories in the cluster. - ExecuteInDevKitContainer -Cmd "cluster sync" + ExecuteInDeploymentContainer -Cmd $PrepareBundleDir + docker cp $Path "$($DeploymentContainerName):$($BundleMount)" - RemoveDevKitContainer + # Sync contract bundle to all instance directories in the cluster. + ExecuteInDeploymentContainer -Cmd "cluster stop ; cluster sync ; cluster start" + + if ($DefaultNode -gt 0) { + Write-Host "Streaming logs of node $($DefaultNode):" + ExecuteInDeploymentContainer -Cmd "cluster logs $($DefaultNode)" + } } +Write-Host "Hot Pocket devkit launcher" + $Command = $args[0] -Write-Host "Hot Pocket devkit" -Write-Host "command: $Command" +if ($Command) { -if ($Command -eq "deploy") { - DeployContractFiles -Path $Path -} -elseif ($Command -eq "cluster") { - DevKitContainer -Mode "run" -AutoRemove -MountSock -Cmd $args + Write-Host "command: $($Command) (cluster: $($Cluster))" + + if ($Command -eq "deploy") { + $Path = $args[1] + if ($Path) { + Deploy -Path $Path + } + else { + Write-Host "Please specify directory or file path to deploy." + } + } + elseif ($Command -eq "clean") { + TeardownDeploymentCluster + } + elseif ($Command -eq "logs" -OR $Command -eq "start" -OR $Command -eq "stop") { + DevKitContainer -Mode "run" -AutoRemove -MountSock -Cmd "cluster $($args)" + } + else { + Write-Host "Invalid command. Expected: deploy | clean | logs" + } } else { - Write-Host "Invalid command. Expected: deploy | cluster" + Write-Host "Please specify command." } \ No newline at end of file