Added contract bundle sync.

This commit is contained in:
ravinsp
2022-06-11 11:53:33 +05:30
parent e1f0e9c01b
commit 4205b51374
5 changed files with 103 additions and 8 deletions

View File

@@ -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
```

View File

@@ -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
# 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

View File

@@ -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 <node count>" && 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 <node id>" && exit 1
attach_logs $2
elif [ $command = "sync" ]; then
sync_contract_bundle
fi

1
windows/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
*.exe

32
windows/hpdevkit.ps1 Normal file
View File

@@ -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]