From 4205b51374baf84ecf263e59f7e632d08db336c0 Mon Sep 17 00:00:00 2001 From: ravinsp <33562092+ravinsp@users.noreply.github.com> Date: Sat, 11 Jun 2022 11:53:33 +0530 Subject: [PATCH] Added contract bundle sync. --- README.md | 28 +++++++++++++++++++ {src => docker}/Dockerfile | 5 ++-- {src => docker}/scripts/cluster.sh | 45 ++++++++++++++++++++++++++---- windows/.gitignore | 1 + windows/hpdevkit.ps1 | 32 +++++++++++++++++++++ 5 files changed, 103 insertions(+), 8 deletions(-) rename {src => docker}/Dockerfile (75%) rename {src => docker}/scripts/cluster.sh (73%) create mode 100644 windows/.gitignore create mode 100644 windows/hpdevkit.ps1 diff --git a/README.md b/README.md index 3714495..6868461 100644 --- a/README.md +++ b/README.md @@ -2,3 +2,31 @@ Developer toolkit for Hot Pocket smart contract development. This toolkit makes use of Docker to provide a cross-platform development tools for developers. We use Docker containers to run Hot Pocket and smart contracts in a Linux environment. We also use Docker containers distributing developer tools so developers can use the tools on any platform as long as they install Docker. + +## Docker +Contains the docker image source files for cross-platform dev tools. + +### Local build +``` +cd docker +docker build -t hpdevkit . +``` + +### Run +``` +docker run -it --rm --mount type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock hpdevkit cluster create 2 +``` + +## Windows +Contains windows launcher scripts. Written using powershell and combiled to exe using [ps2exe](https://github.com/MScholtes/PS2EXE). + +## Prerequisites +```powershell +Install-Module ps2exe +``` + +### Generate exe +```powershell +cd windows +Invoke-ps2exe .\hpdevkit.ps1 hpdevkit.exe +``` diff --git a/src/Dockerfile b/docker/Dockerfile similarity index 75% rename from src/Dockerfile rename to docker/Dockerfile index 29733fe..0588b43 100644 --- a/src/Dockerfile +++ b/docker/Dockerfile @@ -18,5 +18,6 @@ FROM ubuntu:focal as runner COPY --from=builder /build/docker-extracted/usr/bin/docker /usr/bin/ COPY --from=builder /build/scripts/cluster.sh /usr/bin/cluster -# docker build -t hpdevtoolkit . -# docker run -it --rm --mount type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock hpdevtoolkit cluster create my 2 \ No newline at end of file +# docker build -t hpdevkit . +# docker run -it --rm --mount type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock hpdevkit cluster create 2 +# docker run -it --rm --mount type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock --mount type=bind,src=D:\evernode\hp-devkit\src,dst=/dep hpdevkit cluster deploy /dep \ No newline at end of file diff --git a/src/scripts/cluster.sh b/docker/scripts/cluster.sh similarity index 73% rename from src/scripts/cluster.sh rename to docker/scripts/cluster.sh index b57f3d7..9153aa9 100644 --- a/src/scripts/cluster.sh +++ b/docker/scripts/cluster.sh @@ -8,14 +8,16 @@ 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" -if [ "$command" = "create" ] || [ "$command" = "destroy" ] || [ "$command" = "start" ] || [ "$command" = "stop" ] || [ "$command" = "logs" ] ; then +if [ "$command" = "create" ] || [ "$command" = "destroy" ] || [ "$command" = "start" ] || [ "$command" = "stop" ] || + [ "$command" = "logs" ] || [ "$command" = "sync" ] ; then echo "command: $command" else echo "Invalid command." - echo "Expected: create | destroy | start | stop | logs" + echo "Expected: create | destroy | start | stop | logs | sync" exit 1 fi @@ -27,6 +29,10 @@ function validate_node_num_arg { return 0 } +function contract_dir_mount_path { + echo "$volmount/node$1" +} + function exists { local output=$(docker $1 ls | grep $2) if [ -z "$output" ] ; then @@ -44,12 +50,25 @@ function cluster_exists { fi } +function ensure_cluser_exists { + ! cluster_exists && echo "Cluster '$cluster' does not exist." && exit 1 +} + +function ensure_cluser_not_exists { + cluster_exists && echo "Cluster '$cluster' already exists." && exit 1 +} + +function ensure_cluster_not_running { + local running_containers=$(docker ps -a --filter "status=running" | grep $container_prefix | wc -l) + [ "$running_containers" != "0" ] && echo "Cluster '$cluster' needs to be stopped." && exit 1 +} + function get_container_count { docker ps -a | grep $container_prefix | wc -l } function create_cluster { - cluster_exists && echo "Cluster '$cluster' already exists." && exit 1 + ensure_cluser_not_exists docker volume create $volume docker network create $network @@ -60,12 +79,12 @@ function create_cluster { # 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 $volmount/node$i + docker container create --name $container_name --privileged --mount type=volume,src=$volume,dst=$volmount $imgname run $(contract_dir_mount_path $i) done } function destroy_cluster { - ! cluster_exists && echo "Cluster '$cluster' does not exist." && exit 1 + ensure_cluser_exists echo "action: stop" change_cluster_status "stop" @@ -85,7 +104,7 @@ function destroy_cluster { } function change_cluster_status { - ! cluster_exists && echo "Cluster '$cluster' does not exist." && exit 1 + ensure_cluser_exists local container_count=$(get_container_count) for ((i=1; i<=$container_count; i++)); @@ -101,6 +120,18 @@ function attach_logs { docker logs -f --tail=5 $container_name } +function sync_contract_bundle { + ensure_cluster_not_running + local container_count=$(get_container_count) + for ((i=1; i<=$container_count; i++)); + do + contract_dir=$(contract_dir_mount_path $i) + rm -rf $contract_dir/ledger_fs/* $contract_dir/contract_fs/* + mkdir -p $contract_dir/contract_fs/seed + cp -r $bundle_mount $contract_dir/contract_fs/seed/state + done +} + if [ $command = "create" ]; then ! validate_node_num_arg $2 && echo "Usage: create " && exit 1 create_cluster $2 @@ -113,4 +144,6 @@ elif [ $command = "stop" ]; then elif [ $command = "logs" ]; then ! validate_node_num_arg $2 && echo "Usage: logs " && exit 1 attach_logs $2 +elif [ $command = "sync" ]; then + sync_contract_bundle fi \ No newline at end of file diff --git a/windows/.gitignore b/windows/.gitignore new file mode 100644 index 0000000..adb36c8 --- /dev/null +++ b/windows/.gitignore @@ -0,0 +1 @@ +*.exe \ No newline at end of file diff --git a/windows/hpdevkit.ps1 b/windows/hpdevkit.ps1 new file mode 100644 index 0000000..4677bbc --- /dev/null +++ b/windows/hpdevkit.ps1 @@ -0,0 +1,32 @@ +$GlobalPrefix = "hpdevkit" +$HotPocketImage = "evernodedev/hotpocket:latest-ubt.20.04-njs.16" +$DevKitImage = "hpdevkit" +$VolumeMount = "/devkitvol" + +$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" + +Function DeployContractFiles([string]$path) { + $ContainerName = "hpdevkit_cptemp" + + # 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)" + } + 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 +} + +DeployContractFiles $args[0] \ No newline at end of file