diff --git a/docker/scripts/cluster.sh b/docker/scripts/cluster.sh index 9153aa9..cee7269 100644 --- a/docker/scripts/cluster.sh +++ b/docker/scripts/cluster.sh @@ -1,16 +1,14 @@ #!/bin/bash -global_prefix="hpdevkit" -imgname="evernodedev/hotpocket:latest-ubt.20.04-njs.16" -volmount="/devkitvol" - command=$1 -cluster="${HP_CLUSTER:-default}" -volume="${global_prefix}_${cluster}_vol" -network="${global_prefix}_${cluster}_net" -container_prefix="${global_prefix}_${cluster}_con" -bundle_mount="${volmount}/contract_bundle" -echo "Hot Pocket development toolkit" +cluster=$CLUSTER +cluster_size=$CLUSTER_SIZE +volume=$VOLUME +network=$NETWORK +container_prefix=$CONTAINER_PREFIX +volume_mount=$VOLUME_MOUNT +bundle_mount=$BUNDLE_MOUNT +hotpocket_image=$HOTPOCKET_IMAGE if [ "$command" = "create" ] || [ "$command" = "destroy" ] || [ "$command" = "start" ] || [ "$command" = "stop" ] || [ "$command" = "logs" ] || [ "$command" = "sync" ] ; then @@ -30,7 +28,7 @@ function validate_node_num_arg { } function contract_dir_mount_path { - echo "$volmount/node$1" + echo "$volume_mount/node$1" } function exists { @@ -69,17 +67,19 @@ function get_container_count { function create_cluster { ensure_cluser_not_exists + + echo "Creating '$cluster' cluster of size $1" docker volume create $volume docker network create $network for ((i=1; i<=$1; i++)); do # Create contract instance directory. - docker run --rm --mount type=volume,src=$volume,dst=$volmount --rm $imgname new $volmount/node$i + docker run --rm --mount type=volume,src=$volume,dst=$volume_mount --rm $hotpocket_image new $volume_mount/node$i # Create container for hot pocket instance. local container_name="${container_prefix}_$i" - docker container create --name $container_name --privileged --mount type=volume,src=$volume,dst=$volmount $imgname run $(contract_dir_mount_path $i) + docker container create --name $container_name --privileged --mount type=volume,src=$volume,dst=$volume_mount $hotpocket_image run $(contract_dir_mount_path $i) done } @@ -133,8 +133,8 @@ function sync_contract_bundle { } if [ $command = "create" ]; then - ! validate_node_num_arg $2 && echo "Usage: create " && exit 1 - create_cluster $2 + ! validate_node_num_arg $cluster_size && echo "Invalid cluster size." && exit 1 + create_cluster $cluster_size elif [ $command = "destroy" ]; then destroy_cluster elif [ $command = "start" ]; then diff --git a/windows/hpdevkit.ps1 b/windows/hpdevkit.ps1 index 4677bbc..b10ef04 100644 --- a/windows/hpdevkit.ps1 +++ b/windows/hpdevkit.ps1 @@ -1,32 +1,84 @@ $GlobalPrefix = "hpdevkit" -$HotPocketImage = "evernodedev/hotpocket:latest-ubt.20.04-njs.16" $DevKitImage = "hpdevkit" $VolumeMount = "/devkitvol" +$HotPocketImage = "evernodedev/hotpocket:latest-ubt.20.04-njs.16" $Cluster = if ($env:HP_CLUSTER) { $env:HP_CLUSTER } else { "default" }; -$Volume="$($GlobalPrefix)_$($Cluster)_vol" -$Network="$($GlobalPrefix)_$($Cluster)_net" -$ContainerPrefix="$($GlobalPrefix)_$($Cluster)_con" -$BundleMountPath="$($VolumeMount)/contract_bundle" +$ClusterSize = if ($env:HP_CLUSTER_SIZE) { $env:HP_CLUSTER_SIZE } else { 1 }; +$Volume = "$($GlobalPrefix)_$($Cluster)_vol" +$Network = "$($GlobalPrefix)_$($Cluster)_net" +$ContainerPrefix = "$($GlobalPrefix)_$($Cluster)_con" +$BundleMount = "$($VolumeMount)/contract_bundle" +$DevKitContainerName = "$($GlobalPrefix)_launcher" -Function DeployContractFiles([string]$path) { - $ContainerName = "hpdevkit_cptemp" +function DevKitContainer([string]$Mode, [switch]$Detached, [switch]$AutoRemove, [switch]$MountSock, [switch]$MountVolume, [string]$Cmd) { + $Command = "docker $($Mode) --name $($DevKitContainerName) -it" + if ($Detached) { + $Command += " -d" + } + if ($AutoRemove) { + $Command += " --rm" + } + if ($MountSock) { + $Command += " --mount type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock" + } + if ($MountVolume) { + $Command += " --mount type=volume,src=$($Volume),dst=$($VolumeMount)" + } - # If copying a directory, delete target bundle directory. If not create empty target bundle directory to copy a file. - $PrepareCommand = "" - if ((Get-Item $path) -is [System.IO.DirectoryInfo]) { - $PrepareCommand = "rm -rf $($BundleMountPath)" + # Env variables. + $Command += " -e CLUSTER=$($Cluster) -e CLUSTER_SIZE=$($ClusterSize) -e VOLUME=$($Volume) -e NETWORK=$($Network)" + $Command += " -e CONTAINER_PREFIX=$($ContainerPrefix) -e VOLUME_MOUNT=$($VolumeMount) -e BUNDLE_MOUNT=$($BundleMount) -e HOTPOCKET_IMAGE=$($HotPocketImage)" + + $Command += " $($DevKitImage)" + if ($Cmd) { + $Command += " $($Cmd)" } - else { - $PrepareCommand = "mkdir -p $($BundleMountPath) && rm -rf $($BundleMountPath)/* $($BundleMountPath)/.??*" - } - $Null = docker rm $ContainerName *>&1 - docker run -d -it --name $ContainerName --mount type=volume,src=$Volume,dst=$VolumeMount --mount type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock $DevKitImage - docker exec -it $ContainerName /bin/bash -c $PrepareCommand - docker cp $path "$($ContainerName):$($BundleMountPath)" - docker exec -it $ContainerName cluster sync - docker stop $ContainerName - docker rm $ContainerName + + Invoke-Expression $Command } -DeployContractFiles $args[0] \ No newline at end of file +function RemoveDevKitContainer() { + $Null = docker stop $DevKitContainerName *>&1 + $Null = docker rm $DevKitContainerName *>&1 +} + +function ExecuteInDevKitContainer([string]$Cmd) { + docker exec -it $DevKitContainerName /bin/bash -c $Cmd +} + +Function DeployContractFiles([string]$Path) { + + RemoveDevKitContainer + DevKitContainer -Mode "run" -Detached -MountSock -MountVolume + + # If copying a directory, delete target bundle directory. If not create empty target bundle directory to copy a file. + $PrepareBundleDir = "" + if ((Get-Item $Path) -is [System.IO.DirectoryInfo]) { + $PrepareBundleDir = "rm -rf $($BundleMount)" + } + 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" + + RemoveDevKitContainer +} + +$Command = $args[0] +Write-Host "Hot Pocket devkit" +Write-Host "command: $Command" + +if ($Command -eq "deploy") { + DeployContractFiles -Path $Path +} +elseif ($Command -eq "cluster") { + DevKitContainer -Mode "run" -AutoRemove -MountSock -Cmd $args +} +else { + Write-Host "Invalid command. Expected: deploy | cluster" +} \ No newline at end of file