<commit_before>#!/bin/bash

#for debug use bash -x ./kvm_spawner.sh

DIR=`dirname $0`

footer(){
    echo "=============== ================="
    echo "===============  $1 ================="
    echo "=============== ================="
}
checker(){
    if [ $? -ne 0 ]; then
    footer "Error!$1" ; exit 1 ; fi
}

generate_next_nodename_funx(){
    nodes_all_count=0
    read nodes_all <<< `virsh list --all |grep -v "broker" | grep "${SYSTEMS_PREFIX}" | awk ' {print $2}'`
    read nodes_all_count <<< `virsh list --all |grep -v "broker" | grep "${SYSTEMS_PREFIX}" | wc -l `

    echo -e "For deployment \"${SYSTEMS_PREFIX}\" you have VMs: $nodes_all.\n Count: $nodes_all_count"
    #gen next node name
    let "nodes_all_count++"
    echo "next=$nodes_all_count"
    VMNAME=${SYSTEMS_PREFIX}-node-$nodes_all_count
}


prepare_vm(){
mkdir -p "${BASE_DIR}/systems"

if [ "$1" == "broker" ] || [ "$1" == "node" ]; then
	echo "Prepare system for role=$1"
	echo "role=$1 VMNAME=$2"
	HDD="${BASE_DIR}/systems/$2.qcow2"
	sed -e "s#HDD_STUB#${HDD}#g" ${1}_template.xml > ${2}.xml
	sed -i "s#NAME_STUB#${2}#g" ${2}.xml
	#copy hdd image
	if [ -f ${HDD} ]
	    then
	    echo "Error!HDD file ($HDD) already exist!Rewrite or skip?"
		read -p "[R/S]" -n 1 -r
		echo    # just move to a new line
		    if [[ $REPLY =~ ^[Rr]$ ]]
	    	    then
	    		# do dangerous stuff
	    		cp -f ${COPY_FROM_IMG} ${HDD}
		    else 
			footer "Use OLD hdd image.Its bad idea,but okay :("
		    fi
	    else
    		cp ${COPY_FROM_IMG} ${HDD}
		checker "When try copy img!"
	    fi
	virsh define ${2}.xml
	checker "When try define systems!"
	footer "Finish define kvm system with ROLE=${1} and fqdn=$TEMPHOSTNAME.$CLOUDNAME"
else
        footer "Wrong choose,Neo..."
	exit 1
fi
}


####Role chooser####
source ../role/next_role.sh

echo "Choose next VM-system role:"<commit_msg>	modified:   kvm_spawner/kvm_spawner.sh<commit_after>#!/bin/bash

#for debug use bash -x ./kvm_spawner.sh

DIR=`dirname $0`

footer(){
    echo "=============== ================="
    echo "===============  $1 ================="
    echo "=============== ================="
}
checker(){
    if [ $? -ne 0 ]; then
    footer "Error!$1" ; exit 1 ; fi
}

generate_next_nodename_funx(){
    nodes_all_count=0
    read nodes_all <<< `virsh list --all |grep -v "broker" | grep "${SYSTEMS_PREFIX}" | awk ' {print $2}'`
    read nodes_all_count <<< `virsh list --all |grep -v "broker" | grep "${SYSTEMS_PREFIX}" | wc -l `

    echo -e "For deployment \"${SYSTEMS_PREFIX}\" you have VMs: $nodes_all.\n Count: $nodes_all_count"
    #gen next node name
    let "nodes_all_count++"
    echo "next=$nodes_all_count"
    VMNAME=${SYSTEMS_PREFIX}-node-$nodes_all_count
}


prepare_vm(){
mkdir -p "${BASE_DIR}/systems"

if [ "$1" == "broker" ] || [ "$1" == "node" ]; then
	echo "Prepare system for role=$1"
	echo "role=$1 VMNAME=$2"
	HDD="${BASE_DIR}/systems/$2.qcow2"
	sed -e "s#HDD_STUB#${HDD}#g" ${1}_template.xml > ${2}.xml
	sed -i "s#NAME_STUB#${2}#g" ${2}.xml
	#copy hdd image
	if [ -f ${HDD} ]
	    then
	    echo "Error!HDD file ($HDD) already exist!Rewrite or skip?"
		read -p "[R/S]" -n 1 -r
		echo    # just move to a new line
		    if [[ $REPLY =~ ^[Rr]$ ]]
	    	    then
	    		# do dangerous stuff
	    		cp -f ${COPY_FROM_IMG} ${HDD}
		    else 
			footer "Use OLD hdd image.Its bad idea,but okay :("
		    fi
	    else
    		cp ${COPY_FROM_IMG} ${HDD}
		checker "When try copy img!"
	    fi
	virsh define ${2}.xml
	checker "When try define systems!"
	source ../role/next_role.sh
	footer "Finish define kvm system with ROLE=${1} and fqdn=$TEMPHOSTNAME.$CLOUDNAME"
else
        footer "Wrong choose,Neo..."
	exit 1
fi
}


####Role chooser####
source ../role/next_role.sh

echo "Choose next VM-system role:"
{'fraction_non_alphanumeric': 0.21464713715046604, 'fraction_numerical': 0.011717709720372836, 'mean_word_length': 3.1779755283648496, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '633365', 'n_tokens_mistral': 1533, 'n_tokens_neox': 1425, 'n_words': 385}