Improved vm scripts. (#129)

* Added node-or-cluster mode.
* Added json based cluster config.
* Restructured new, update, reconfig modes.
* Added screen logging mode.
This commit is contained in:
Ravin Perera
2020-10-03 21:38:43 +05:30
committed by GitHub
parent 1245da52b2
commit 54d6bf5bf6
6 changed files with 277 additions and 227 deletions

View File

@@ -1,5 +1,3 @@
cfg
vmpass.txt
vmlist*.txt
vmresgroup.txt
vmconfig*
hpfiles

View File

@@ -1,110 +1,222 @@
#!/bin/bash
# HotPocket VM cluster setup script.
# Hot Pocket VM cluster management script.
# Usage examples:
# ./cluster.sh new
# ./cluster.sh update
# ./cluster.sh run 1
# ./cluster.sh start 1
# ./cluster.sh start
# VM login username and password must exist in vmpass.txt as first two lines.
vmuser=$(head -1 vmpass.txt | tail -1)
vmpass=$(head -2 vmpass.txt | tail -1)
# List of vm domain names of the cluster must exist in vmlist.txt
# (This list will be treated as the node numbers 1,2.3... from topmost address to the bottom)
readarray -t vmaddrs < vmlist.txt
# jq command is used for json manipulation.
if ! command -v jq &> /dev/null
then
echo "jq command not found. Install with 'sudo apt-get install -y jq'"
exit 1
fi
conf=vmconfig.json
if [ ! -f $conf ]; then
# Create default config file.
echo '{"vmuser":"root","vmpass":"","vms":[],"contracts":[{"name":"contract","config":{}}]}' | jq . > $conf
fi
vmuser=$(jq -r '.vmuser' $conf)
if [ "$vmuser" = "" ]; then
echo "vmuser not specified."
exit 1
elif [ "$CONTRACT" = "" ]; then
CONTRACT=contract # Default contract name (can be set with 'export CONTRACT=<name>'').
fi
if [ "$vmuser" = "root" ]; then
basedir=/$vmuser
else
basedir=/home/$vmuser
fi
contconfig=$(jq -r ".contracts[] | select(.name == \"${CONTRACT}\") | .config" $conf)
if [ "$contconfig" = "" ]; then
# Apply default config.
contconfig="{'pubport': 8080, peerport: 22860, 'roundtime': 2000, 'loglevel': 'dbg', loggers:['console','file']}"
fi
vmpass=$(jq -r '.vmpass' $conf)
readarray -t vmaddrs <<< $(jq -r '.vms[]' $conf)
contdir=$basedir/$CONTRACT
vmcount=${#vmaddrs[@]}
mode=$1
hpcore=$(realpath ../..)
let nodeid=$2-1
if [ "$mode" = "new" ] || [ "$mode" = "update" ] || [ "$mode" = "reconfig" ] || \
[ "$mode" = "vminfo" ] || [ "$mode" = "vmresize" ] || [ "$mode" = "vmstop" ] || [ "$mode" = "vmstart" ] || \
[ "$mode" = "run" ] || [ "$mode" = "check" ] || [ "$mode" = "monitor" ] || [ "$mode" = "kill" ] || [ "$mode" = "reboot" ] || \
[ "$mode" = "ssh" ] || [ "$mode" = "dns" ] || [ "$mode" = "ssl" ] || [ "$mode" = "lcl" ]; then
echo "mode: $mode"
if [ "$mode" = "info" ] || [ "$mode" = "new" ] || [ "$mode" = "update" ] || [ "$mode" = "reconfig" ] || \
[ "$mode" = "start" ] || [ "$mode" = "stop" ] || [ "$mode" = "check" ] || [ "$mode" = "log" ] || [ "$mode" = "kill" ] || \
[ "$mode" = "ssh" ] || [ "$mode" = "reboot" ] || [ "$mode" = "dns" ] || [ "$mode" = "ssl" ] || [ "$mode" = "lcl" ]; then
echo "mode: $mode ($contdir)"
else
echo "Invalid command. [ new | update | reconfig | vmresize <size> | vmstop | vmstart" \
" | run <N> | check <N> | monitor <N> | kill <N> | reboot <N> | ssh <N> <custom command>" \
echo "Invalid command. [ info | new | update | reconfig" \
" | start [N] | stop [N] | check [N] | log <N> | kill [N] | reboot <N> | ssh <N>or<command>" \
" | dns <N> <zerossl file> | ssl <N> | lcl ] expected."
exit 1
fi
# Command modes:
# info - Displays information about current cluster configuration status.
# new - Install hot pocket dependencies and hot pocket with example contracts to each vm.
# update - Deploy updated hot pocket and example binaries into each vm.
# reconfig - Reconfigures the entire cluster using already uploaded HP binaries.
# run - Run hot pocket of specified vm node.
# check - Check hot pocket running status of specified vm node.
# monitor - Monitor streaming hot pocket console output (if running) of specified vm node.
# kill - Kill hot pocket (if running) of specified vm node.
# start - Run hot pocket on specified vm node or entire cluster.
# stop - Gracefully stop hot pocket (if running) on specified vm node or entire cluster.
# check - Get hot pocket running process ids on specified vm node or entire cluster.
# log - Stream hot pocket console output log (if running) on specified vm node.
# kill - Force kill hot pocket (if running) on specified vm node or entire cluster.
# reboot - Reboot specified vm node.
# ssh - Open up an ssh terminal for the specified vm node.
# dns - Uploads given zerossl domain verification file to vm and starts http server for DNS check.
# ssl - Uploads matching zerossl certificate bundle from ~/Downloads/ to the contract.
# lcl - Displays the lcls of all nodes.
if [ $mode = "run" ]; then
let nodeid=$2-1
vmaddr=${vmaddrs[$nodeid]}
sshpass -p $vmpass ssh $vmuser@$vmaddr 'nohup sudo ~/hpfiles/bin/hpcore run ~/contract'
sshpass -p $vmpass ssh $vmuser@$vmaddr 'tail -f nohup.out'
if [ $mode = "info" ]; then
echo "${vmaddrs[*]}" | tr ' ' '\n'
echo $contconfig
exit 0
fi
if [ $mode = "start" ]; then
# Use the screen command so that the execution does not stop when ssh session ends.
command="mkdir -p $contdir/screen && screen -c $contdir/hp.screenrc -m -d bash $contdir/start.sh"
if [ $nodeid = -1 ]; then
for (( i=0; i<$vmcount; i++ ))
do
vmaddr=${vmaddrs[i]}
let nodeid=$i+1
sshpass -p $vmpass ssh $vmuser@$vmaddr $command &
done
wait
else
vmaddr=${vmaddrs[$nodeid]}
sshpass -p $vmpass ssh $vmuser@$vmaddr $command
fi
exit 0
fi
if [ $mode = "stop" ]; then
command="$contdir/stop.sh"
if [ $nodeid = -1 ]; then
for (( i=0; i<$vmcount; i++ ))
do
vmaddr=${vmaddrs[i]}
let nodeid=$i+1
sshpass -p $vmpass ssh $vmuser@$vmaddr $command &
done
wait
else
vmaddr=${vmaddrs[$nodeid]}
sshpass -p $vmpass ssh $vmuser@$vmaddr $command
fi
exit 0
fi
if [ $mode = "check" ]; then
let nodeid=$2-1
vmaddr=${vmaddrs[$nodeid]}
sshpass -p $vmpass ssh $vmuser@$vmaddr 'echo hpcore pid:$(pidof hpcore) hpfs pid:$(pidof hpfs) websocketd pid:$(pidof websocketd) websocat pid:$(pidof websocat)'
command="$contdir/check.sh"
if [ $nodeid = -1 ]; then
for (( i=0; i<$vmcount; i++ ))
do
vmaddr=${vmaddrs[i]}
let nodeid=$i+1
echo "node"$nodeid":" $(sshpass -p $vmpass ssh $vmuser@$vmaddr $command) &
done
wait
else
vmaddr=${vmaddrs[$nodeid]}
sshpass -p $vmpass ssh $vmuser@$vmaddr $command
fi
exit 0
fi
if [ $mode = "monitor" ]; then
let nodeid=$2-1
if [ $mode = "log" ]; then
if [ $nodeid = -1 ]; then
echo "Please specify node no.."
exit 1
fi
vmaddr=${vmaddrs[$nodeid]}
sshpass -p $vmpass ssh $vmuser@$vmaddr 'tail -f nohup.out'
sshpass -p $vmpass ssh -t $vmuser@$vmaddr screen -r -S hp_$(basename $contdir)
exit 0
fi
if [ $mode = "kill" ]; then
let nodeid=$2-1
vmaddr=${vmaddrs[$nodeid]}
sshpass -p $vmpass ssh $vmuser@$vmaddr 'sudo kill $(pidof hpcore) > /dev/null 2>&1'
sshpass -p $vmpass ssh $vmuser@$vmaddr 'sudo kill $(pidof hpfs) > /dev/null 2>&1'
sshpass -p $vmpass ssh $vmuser@$vmaddr 'sudo kill $(pidof websocketd) > /dev/null 2>&1'
sshpass -p $vmpass ssh $vmuser@$vmaddr 'sudo kill $(pidof websocat) > /dev/null 2>&1'
command="$contdir/kill.sh"
if [ $nodeid = -1 ]; then
for (( i=0; i<$vmcount; i++ ))
do
vmaddr=${vmaddrs[i]}
let nodeid=$i+1
echo "node"$nodeid":" $(sshpass -p $vmpass ssh $vmuser@$vmaddr $command) &
done
wait
else
vmaddr=${vmaddrs[$nodeid]}
sshpass -p $vmpass ssh $vmuser@$vmaddr $command
fi
exit 0
fi
if [ $mode = "reboot" ]; then
let nodeid=$2-1
if [ $nodeid = -1 ]; then
echo "Please specify node no."
exit 1
fi
vmaddr=${vmaddrs[$nodeid]}
sshpass -p $vmpass ssh $vmuser@$vmaddr 'sudo reboot'
exit 0
fi
if [ $mode = "ssh" ]; then
let nodeid=$2-1
vmaddr=${vmaddrs[$nodeid]}
sshpass -p $vmpass ssh $vmuser@$vmaddr $3
exit 0
if [ $nodeid = -1 ]; then
if [ -n "$2" ]; then
# Interprit second arg as a command to execute on all nodes.
command=${*:2}
echo "Executing '$command' on all nodes..."
for (( i=0; i<$vmcount; i++ ))
do
vmaddr=${vmaddrs[i]}
let nodeid=$i+1
echo "node"$nodeid":" $(sshpass -p $vmpass ssh $vmuser@$vmaddr $command) &
done
wait
exit 0
else
echo "Please specify node no. or command to execute on all nodes."
exit 1
fi
else
vmaddr=${vmaddrs[$nodeid]}
sshpass -p $vmpass ssh -t $vmuser@$vmaddr "cd $contdir ; bash"
exit 0
fi
fi
if [ $mode = "dns" ]; then
if [ $nodeid = -1 ]; then
echo "Please specify node no."
exit 1
fi
if [[ $3 = "" ]]; then
echo "Please provide zerossl domain verification txt file path."
exit 1
fi
let nodeid=$2-1
vmaddr=${vmaddrs[$nodeid]}
sshpass -p $vmpass ssh $vmuser@$vmaddr 'mkdir -p ~/web80/.well-known/pki-validation'
sshpass -p $vmpass scp $3 $vmuser@$vmaddr:~/web80/.well-known/pki-validation/
sshpass -p $vmpass ssh $vmuser@$vmaddr -t 'cd ~/web80 && sudo python -m SimpleHTTPServer 80'
sshpass -p $vmpass ssh $vmuser@$vmaddr 'mkdir -p $basedir/web80/.well-known/pki-validation'
sshpass -p $vmpass scp $3 $vmuser@$vmaddr:$basedir/web80/.well-known/pki-validation/
sshpass -p $vmpass ssh $vmuser@$vmaddr -t 'cd $basedir/web80 && sudo python -m SimpleHTTPServer 80'
exit 0
fi
if [ $mode = "ssl" ]; then
let nodeid=$2-1
if [ $nodeid = -1 ]; then
echo "Please specify node no."
exit 1
fi
vmaddr=${vmaddrs[$nodeid]}
unzip -d ~/Downloads/$vmaddr/ ~/Downloads/$vmaddr.zip || exit 1;
@@ -115,98 +227,78 @@ if [ $mode = "ssl" ]; then
popd > /dev/null 2>&1
echo "Sending tls certs to the contract..."
sshpass -p $vmpass scp ~/Downloads/$vmaddr/certs/* $vmuser@$vmaddr:~/hpfiles/ssl/
sshpass -p $vmpass ssh $vmuser@$vmaddr 'cp -rf ~/hpfiles/ssl/* ~/contract/cfg/'
sshpass -p $vmpass scp ~/Downloads/$vmaddr/certs/* $vmuser@$vmaddr:$basedir/hpfiles/ssl/
sshpass -p $vmpass ssh $vmuser@$vmaddr cp -rf $basedir/hpfiles/ssl/* $contdir/cfg/
rm -r ~/Downloads/$vmaddr
echo "Done"
exit 0
fi
# Run vm cli script if any vm cluster commands have been specified.
if [ $mode = "vminfo" ] || [ $mode = "vmresize" ] || [ $mode = "vmstop" ] || [ $mode = "vmstart" ]; then
for (( i=0; i<$vmcount; i++ ))
do
vmaddr=${vmaddrs[i]}
# vm name is the first part of the DNS address (eg: hp1.australiaeast.cloudapp.azure.com)
vmname=${vmaddr%%.*}
# Strip out "vm" prefix from the command.
vmcommand=${mode##*vm}
/bin/bash ./vmcli.sh $vmname $vmcommand $2 &
done
wait
exit 0
fi
if [ $mode = "lcl" ]; then
for (( i=0; i<$vmcount; i++ ))
do
vmaddr=${vmaddrs[i]}
let nodeid=$i+1
echo "node"$nodeid":" $(sshpass -p $vmpass ssh $vmuser@$vmaddr 'ls -v contract/hist | tail -1') &
echo "node$nodeid:" $(sshpass -p $vmpass ssh $vmuser@$vmaddr ls -v $contdir/hist | tail -1) &
done
wait
exit 0
fi
# Run setup of entire cluster.
if [ $mode = "new" ] || [ $mode = "update" ]; then
# All code below this will only execute in 'new', 'update' or 'reconfig' mode.
# Run setup/configuration of entire cluster.
# Copy required files to hpfiles dir.
# Copy required files to remote node hpfiles dir.
if [ $mode = "new" ] || [ $mode = "update" ]; then
mkdir -p hpfiles/{bin,ssl,nodejs_contract}
strip $hpcore/build/hpcore
strip $hpcore/build/appbill
cp $hpcore/build/hpcore hpfiles/bin/
cp $hpcore/examples/nodejs_contract/{package.json,echo_contract.js,hp-contract-lib.js} hpfiles/nodejs_contract/
if [ $mode = "new" ]; then
cp ../bin/{libfuse3.so.3,libblake3.so,fusermount3,websocketd,websocat,hpfs} hpfiles/bin/
cp ./setup-hp.sh hpfiles/
fi
for (( i=0; i<$vmcount; i++ ))
do
vmaddr=${vmaddrs[i]}
let n=$i+1
/bin/bash ./setup-vm.sh $mode $n $vmuser $vmpass $vmaddr $hpcore &
done
wait
rm -r hpfiles
cp $hpcore/examples/nodejs_contract/{package.json,echo_contract.js,hp-contract-lib.js} \
hpfiles/nodejs_contract/
fi
if [ $mode = "new" ]; then
cp ../bin/{libfuse3.so.3,libblake3.so,fusermount3,websocketd,websocat,hpfs} hpfiles/bin/
cp ./setup-hp.sh hpfiles/
fi
if [ $mode = "new" ] || [ $mode = "reconfig" ]; then
mkdir ./cfg > /dev/null 2>&1
fi
# Run vm setup for all nodes.
for (( i=0; i<$vmcount; i++ ))
do
vmaddr=${vmaddrs[i]}
let n=$i+1
# Setup vm. (This will download hp.cfg in 'new' or 'reconfig' modes)
/bin/bash ./setup-vm.sh $mode $n $vmuser $vmpass $vmaddr $basedir $contdir &
done
wait
rm -r hpfiles > /dev/null 2>&1
if [ $mode = "update" ]; then
exit 0
fi
# All code below this will only execute in 'new' or 'reconfig' mode.
# Update all nodes hp.cfg files to be part of the same UNL cluster.
if [ $mode = "reconfig" ]; then
mkdir ./cfg > /dev/null 2>&1
for (( i=0; i<$vmcount; i++ ))
do
# Run hp setup script on the VM and download the generated hp.cfg
vmaddr=${vmaddrs[i]}
let nodeid=$i+1
{ sshpass -p $vmpass ssh $vmuser@$vmaddr '~/hpfiles/setup-hp.sh' && sshpass -p $vmpass scp $vmuser@$vmaddr:~/contract/cfg/hp.cfg ./cfg/node$nodeid.json; } &
done
wait
fi
# Update downloaded hp.cfg files from all nodes to be part of the same UNL cluster.
# Locally update values of download hp.cfg files.
peerport=$(echo $contconfig | jq -r ".peerport")
for (( i=0; i<$vmcount; i++ ))
do
vmaddr=${vmaddrs[i]}
let n=$i+1
# Collect each node's pub key and peer address.
pubkeys[i]=$(node -p "require('./cfg/node$n.json').pubkeyhex")
peers[i]="$vmaddr:22860"
pubkeys[i]=$(jq -r ".pubkeyhex" ./cfg/node$n.cfg)
peers[i]="$vmaddr:$peerport"
done
# Function to generate JSON array string while skiping a given index.
@@ -222,7 +314,7 @@ function joinarr {
let prevlast=$vmcount-2
if [ "$i" != "$skip" ]
then
str="$str'${arr[i]}'"
str="$str\"${arr[i]}\""
if [ $j -lt $prevlast ]
then
@@ -236,25 +328,35 @@ function joinarr {
echo $str
}
# Loop through all nodes hp.cfg and inject peer and unl lists (skip self node).
# Loop through all nodes hp.cfg.
for (( j=0; j<$vmcount; j++ ))
do
let n=$j+1
# Prepare peer and unl lists (skip self node).
mypeers=$(joinarr peers $j)
myunl=$(joinarr pubkeys $j)
node -p "JSON.stringify({...require('./cfg/node$n.json'), \
binary:'/usr/bin/node', \
binargs:'/root/hpfiles/nodejs_contract/echo_contract.js', \
peers:${mypeers}, \
unl:${myunl}, \
roundtime: 1000, \
loglevel: 'dbg', \
loggers:['console', 'file'] \
}, null, 2)" > ./cfg/node$n.cfg
# Upload local hp.cfg file back to remote vm.
vmaddr=${vmaddrs[j]}
sshpass -p $vmpass scp ./cfg/node$n.cfg $vmuser@$vmaddr:~/contract/cfg/hp.cfg
# Merge json contents to produce final contract config.
echo "$(cat ./cfg/node$n.cfg)" \
'{"binary":"/usr/bin/node"}' \
'{"binargs":"'$basedir'/hpfiles/nodejs_contract/echo_contract.js"}' \
'{"peers":'${mypeers}'}' \
'{"unl":'${myunl}'}' \
$contconfig \
| jq --slurp 'reduce .[] as $item ({}; . * $item)' > ./cfg/node$n-merged.cfg
done
rm -r ./cfg
for (( j=0; j<$vmcount; j++ ))
do
# Upload local hp.cfg file back to remote vm.
let n=$j+1
vmaddr=${vmaddrs[j]}
echo "Uploading configured hp.cfg..."
sshpass -p $vmpass scp ./cfg/node$n-merged.cfg $vmuser@$vmaddr:$contdir/cfg/hp.cfg &
done
wait
rm -r ./cfg
echo "Done."

View File

@@ -1,5 +1,9 @@
#!/bin/bash
mode=$1
basedir=$2
contdir=$3 # Contract directory
if [[ ! -f /swapfile ]]
then
echo "Adding 5GB swap space..."
@@ -24,23 +28,58 @@ if [ -x "$(command -v fusermount3)" ]; then
else
echo "Installing FUSE and other shared libraries..."
sudo apt-get -y install libgomp1
sudo cp ~/hpfiles/bin/{libfuse3.so.3,libblake3.so} /usr/local/lib/
sudo cp $basedir/hpfiles/bin/{libfuse3.so.3,libblake3.so} /usr/local/lib/
sudo ldconfig
sudo cp ~/hpfiles/bin/fusermount3 /usr/local/bin/
sudo cp $basedir/hpfiles/bin/fusermount3 /usr/local/bin/
fi
sudo rm -r ~/contract > /dev/null 2>&1
# Remove existing contract dir.
sudo rm -r $contdir > /dev/null 2>&1
echo "Creating new contract directory..."
~/hpfiles/bin/hpcore new ~/contract
$basedir/hpfiles/bin/hpcore new $contdir
if [ -f ~/hpfiles/ssl/tlscert.pem ]; then
if [ -f $basedir/hpfiles/ssl/tlscert.pem ]; then
echo "Copying ssl certs to contract directory..."
cp -rf ~/hpfiles/ssl/* ~/contract/cfg/
cp -rf $basedir/hpfiles/ssl/* $contdir/cfg/
else
echo "Generating default ssl certs..."
pushd ~/contract/cfg > /dev/null 2>&1
pushd $contdir/cfg > /dev/null 2>&1
openssl req -newkey rsa:2048 -new -nodes -x509 -days 3650 -keyout tlskey.pem -out tlscert.pem \
-subj "/C=AU/ST=ST/L=L/O=O/OU=OU/CN=localhost/emailAddress=hp@example" > /dev/null 2>&1
popd > /dev/null 2>&1
fi
if [ $mode = "new" ] || [ $mode = "reconfig" ]; then
# npm install to support nodejs contract
pushd $basedir/hpfiles/nodejs_contract > /dev/null 2>&1
npm install
popd > /dev/null 2>&1
# Create getpid script (gets process ids belonging to this contract dir)
echo "ps -fp \$(pidof \$*) | grep $contdir | awk '{print \$2}' | tr '\n' ' '" > $contdir/getpid.sh
sudo chmod +x $contdir/getpid.sh
# Create start.sh script
echo "$basedir/hpfiles/bin/hpcore run $contdir" > $contdir/start.sh
sudo chmod +x $contdir/start.sh
# Create stop.sh script (sending SIGINT to hpcore)
echo "kill -2 \$($contdir/getpid.sh hpcore)" > $contdir/stop.sh
sudo chmod +x $contdir/stop.sh
# Create check.sh script (print pids belonging to this contract dir)
echo "echo hpcore pid:\$($contdir/getpid.sh hpcore) hpfs pid:\$($contdir/getpid.sh hpfs) websocketd pid:\$($contdir/getpid.sh websocketd) websocat pid:\$($contdir/getpid.sh websocat)" > $contdir/check.sh
sudo chmod +x $contdir/check.sh
# Create kill.sh script
echo "sudo kill \$($contdir/getpid.sh hpcore hpfs websocketd websocat)" > $contdir/kill.sh
sudo chmod +x $contdir/kill.sh
# Configure .screenrc
pushd $contdir > /dev/null 2>&1
echo "chdir $contdir" >> hp.screenrc
echo "sessionname hp_$(basename $contdir)" >> hp.screenrc
echo "bindkey \"^C\" echo 'Blocked. Ctrl+A,D to detach.'" >> hp.screenrc
popd > /dev/null 2>&1
fi

View File

@@ -5,19 +5,20 @@ nodeid=$2
vmuser=$3
vmpass=$4
vmaddr=$5
hpcore=$6
basedir=$6
contdir=$7 # Contract directory
echo $nodeid. $vmaddr
echo "Uploading hp files..."
sshpass -p $vmpass scp -rp hpfiles $vmuser@$vmaddr:~/
echo "Upload finished."
if [ $mode = "new" ] || [ $mode = "update" ]; then
echo "Uploading hp files to $basedir..."
sshpass -p $vmpass scp -rp hpfiles $vmuser@$vmaddr:$basedir/
echo "Upload finished."
fi
if [ $mode = "new" ]; then
if [ $mode = "new" ] || [ $mode = "reconfig" ]; then
# Run hp setup script on the VM and download the generated hp.cfg
sshpass -p $vmpass ssh $vmuser@$vmaddr '~/hpfiles/setup-hp.sh && cd ~/hpfiles/nodejs_contract && npm install'
sshpass -p $vmpass ssh $vmuser@$vmaddr 'echo sudo ~/hpfiles/bin/hpcore run ~/contract > ~/run.sh && sudo chmod +x ~/run.sh'
sshpass -p $vmpass ssh $vmuser@$vmaddr 'echo sudo kill $(pidof hpfs) $(pidof websocketd) $(pidof websocat) > ~/kill.sh && sudo chmod +x ~/kill.sh'
mkdir ./cfg > /dev/null 2>&1
sshpass -p $vmpass scp $vmuser@$vmaddr:~/contract/cfg/hp.cfg ./cfg/node$nodeid.json
echo "Configuring HP..."
sshpass -p $vmpass ssh $vmuser@$vmaddr $basedir/hpfiles/setup-hp.sh $mode $basedir $contdir
sshpass -p $vmpass scp $vmuser@$vmaddr:$contdir/cfg/hp.cfg ./cfg/node$nodeid.cfg
fi

View File

@@ -1,45 +0,0 @@
#!/bin/bash
# Usage:
# ./vmcli.sh <vmname> <command> <optional params>
vmname=$1
mode=$2
resgroup=$(cat vmresgroup.txt)
set -e # exit on error
if [ $mode = "info" ]; then
az vm show --show-details -g $resgroup --name $vmname \
--query "{name:name, size: hardwareProfile.vmSize, location:location, status:powerState}" -o json
elif [ $mode = "resize" ]; then
az vm resize -g $resgroup --name $vmname --size Standard_$3
elif [ $mode = "stop" ]; then
# Gracefully shutdown and then deallocate.
az vm stop -g $resgroup --name $vmname > /dev/null 2>&1
az vm deallocate -g $resgroup --name $vmname
elif [ $mode = "start" ]; then
az vm start -g $resgroup --name $vmname
elif [ $mode = "delete" ]; then
echo Deleting vm $vmname...
az resource delete -g ${resgroup} -n $vmname --resource-type "Microsoft.Compute/virtualMachines"
# Get list of resources with specific tag
reslist=`az resource list --tag $vmname`
rescount=`echo $reslist | jq length`
# Delete resources
for((i=0; i<$rescount; i++)); do
# Get $i th resource name and type
resname=`echo $reslist | jq .[$i].name | tr -d '"'`
restype=`echo $reslist | jq .[$i].type | tr -d '"'`
echo Deleting $resname...
az resource delete -g ${resgroup} -n ${resname} --resource-type ${restype}
done
fi

View File

@@ -1,45 +0,0 @@
#!/bin/bash
# Usage example: ./vmcreate.sh hp1 australiaeast
# Azure vm creation script
# az login
# az account list
# az account set --subscription ''
# az account list-locations
vmname=$1
loc=$2
vmsize=Standard_B1s
vmpass=$(cat vmpass.txt)
resgroup=$(cat vmresgroup.txt)
set -e # exit on error
az vm create --name $vmname --resource-group $resgroup --size $vmsize \
--admin-username geveo --admin-password $vmpass --image UbuntuLTS --location $loc --generate-ssh-keys \
--public-ip-address-dns-name $vmname --tags $vmname
# HP peer port
az vm open-port --resource-group $resgroup --name $vmname --port 22860 --priority 900
# HP user port
az vm open-port --resource-group $resgroup --name $vmname --port 8080 --priority 901
# For DNS verification web server
az vm open-port --resource-group $resgroup --name $vmname --port 80 --priority 902
vmdns=$vmname.$loc.cloudapp.azure.com
echo $vmdns >> vmlist.txt
echo $vmdns " created and added to vmlist.txt"
# Stop the VM and downgrade disk storage to Standard SSD.
echo Downgrading OS disk...
az vm stop -g $resgroup --name $vmname
az vm deallocate -g $resgroup --name $vmname
diskid=$(az vm show -n $vmname -g $resgroup --query storageProfile.osDisk.managedDisk.id -o tsv)
az disk update --sku StandardSSD_LRS --ids $diskid
az vm start -g $resgroup --name $vmname
ssh-keyscan -H $vmdns >> ~/.ssh/known_hosts
echo Done.