<commit_before>#!/bin/bash

# XXX Requirements
# You have to install dsh, expect

# Usage : ./managment_config.sh <option>
#	update_mgmt_sh : update the configuration file management.sh
# 	config_dsh : enable root login, configure dsh machine.list and groups
#	clone_dreamer : download code from $REPO_URL
#   update_dreamer : update remote repo from $REPO_URL
#	setup_nodes : install software required and update configuration files on all nodes
#	setup : download code from $REPO_URL, install software required and update configuration files on all nodes
#	update_cfg_nodes : Update configuration files on all nodes
#	config : configure all nodes
#	clean : clean all nodes
#	all : complete setup and config
#	change_sh_addresses : update remote.cfg on the machines

USER="root"

#XXX Address used for the download of management.sh
MGT_SH_ADDR=https://www.dropbox.com/s/f5lse4stkxrwb6j/management.sh

update_mgmt_sh(){
	if ! [ -n "$MGT_SH_ADDR" ]; then
		echo "MGT_SH_ADDR Not Setted"
		exit 1
	fi
	rm management.sh
	echo "Updating management.sh from $MGT_SH_ADDR..."
	wget $MGT_SH_ADDR
}

config_dsh(){

echo "Please enter testbed username: "
read OFELIA_USER
echo "Please enter testbed $OFELIA_USER pass: "
read -s OFELIA_PASS
echo
echo "Please enter testbed root pass: "
read -s ROOT_PASS
echo
while true; do
    read -p "Generate ssh key (y/n)?" yn 
    case $yn in
        [Yy]* ) ssh-keygen; break;;
        [Nn]* ) break;;
        * ) echo "(y/n)";;
    esac
done

#Enable ssh root access
echo "Enable ssh root access..."

TARGET_KEY="PermitRootLogin"
REPLACEMENT_VALUE="yes"

for i in ${NODE_LIST[@]}; do
	./send_root_cmd $i $OFELIA_USER $OFELIA_PASS $ROOT_PASS "sed -i \"s/\($TARGET_KEY * *\).*/\1$REPLACEMENT_VALUE/\" /etc/ssh/sshd_config"
	./send_root_cmd $i $OFELIA_USER $OFELIA_PASS $ROOT_PASS "/etc/init.d/ssh restart"
 	./send_root_cmd $i $OFELIA_USER $OFELIA_PASS $ROOT_PASS "echo $(cat /root/.ssh/id_rsa.pub) >/root/.ssh/authorized_keys"
done
echo ""
echo "Configure DSH..."

#DSH configuration file
TARGET_KEY="remoteshell"
REPLACEMENT_VALUE="ssh"

sed -i "s/\($TARGET_KEY *= *\).*/\1$REPLACEMENT_VALUE/" /etc/dsh/dsh.conf

#Configures machines.list
echo "Configure machines.list..."


rm /etc/dsh/machines.list

for i in ${NODE_LIST[@]}; do
	echo $i
	echo $USER@$i >> /etc/dsh/machines.list
done

#Creates groups files in /etc/dsh/group
echo "Creating groups..."
for i in ${DSH_GROUPS[@]}; do
	echo > /etc/dsh/group/$i
	echo $i
	eval group=\${${i}[*]}
	for host in ${group[@]};do
		echo $USER@$host
		echo $USER@$host >> /etc/dsh/group/$i
	done	
done

}

WORK_DIR=/root/

#GITHUB
REPO_DIR=Dreamer-Setup-Scripts
REPO_URL=https://github.com/netgroup/Dreamer-Setup-Scripts

#XXX Operation with this repo could not work (bitbucket requests password)
#BITBUCKET
#REPO_DIR=dreamer-setup-scripts
#REPO_URL=https://pierventre@bitbucket.org/ssalsano/dreamer-setup-scripts.git

TESTBED_SH_ADDR="https://www.dropbox.com/s/smsyctn1qj72kpk/testbed.sh"
LMERULES_SH_ADDR="https://www.dropbox.com/s/vp30krz8vamjoxn/lmerules.sh"


clone_dreamer(){
#git clone on all hosts in NODE_LIST
dsh -M -g all -c rm -r -f $REPO_DIR
dsh -M -g all -c git clone $REPO_URL
}

update_dreamer(){
#git pull origin master on all hosts in NODE_LIST
dsh -M -g all -c "cd ./$REPO_DIR && git pull origin master"
}


for_all_group() {
# Executes $1 command on the deployed machines
for i in ${DSH_GROUPS[@]}; do
        if [ "$i" = "OSHI" ]; then
				echo $i
                dsh -M -g $i -c "cd ./$REPO_DIR/oshi/ && ./$1" 
        elif [ "$i" = "ROUTER" ];then
                echo $i
				dsh -M -g $i -c "cd ./$REPO_DIR/ip_router/ && ./$1"  
        elif [ "$i" = "EUH" ] || [ "$i" = "CTRL" ];then
				echo $i
                dsh -M -g $i -c "cd ./$REPO_DIR/othernodes/ && ./$1" 
		else    
            	#Not Handled
				echo "$i Not Handled"
				exit 1 			 
        fi
done

}

change_sh_addresses(){
# Change remote.cfg in accordance to $TESTBED_SH_ADDR and $LMERULES_SH_ADDR
for i in ${DSH_GROUPS[@]}; do
        if [ "$i" = "OSHI" ]; then
				echo $i
				if ! [ -n "$TESTBED_SH_ADDR" ] || ! [ -n "$LMERULES_SH_ADDR" ]; then
					echo "Addresses Not Setted For OSHI"
					exit 1
				fi
				TARGET_KEY="DREAMERCONFIGSERVER"
				REPLACEMENT_VALUE="$TESTBED_SH_ADDR"
				TARGET_KEY2="DREAMERCONFIGSERVER2"
				REPLACEMENT_VALUE2="$LMERULES_SH_ADDR"
				dsh -M -g $i -c "cd ./$REPO_DIR/oshi/ && sed -i \"s@\($TARGET_KEY *= *\).*@\1$REPLACEMENT_VALUE@\" ./remote.cfg  && sed -i \"s@\($TARGET_KEY2 *= *\).*@\1$REPLACEMENT_VALUE2@\" ./remote.cfg"
        elif [ "$i" = "ROUTER" ];then
                echo $i
				if ! [ -n "$TESTBED_SH_ADDR" ]; then
					echo "Addresses Not Setted For ROUTER"
					exit 1
				fi
				TARGET_KEY="DREAMERCONFIGSERVER"
				REPLACEMENT_VALUE="$TESTBED_SH_ADDR"
				dsh -M -g $i -c "cd ./$REPO_DIR/ip_router/ && sed -i \"s@\($TARGET_KEY *= *\).*@\1$REPLACEMENT_VALUE@\" ./remote.cfg"
        elif [ "$i" = "EUH" ] || [ "$i" = "CTRL" ];then
				echo $i
				if ! [ -n "$TESTBED_SH_ADDR" ]; then
					echo "Addresses Not Setted For EUH and CTRL"
					exit 1
				fi
				TARGET_KEY="DREAMERCONFIGSERVER"
				REPLACEMENT_VALUE="$TESTBED_SH_ADDR"
				dsh -M -g $i -c "cd ./$REPO_DIR/ip_router/ && sed -i \"s@\($TARGET_KEY *= *\).*@\1$REPLACEMENT_VALUE@\" ./remote.cfg"
		else    
            	#Not Handled
				echo "$i Not Handled"
				exit 1 			 
        fi
done

}

setup_nodes(){
#setup.sh
for_all_group setup.sh
}

update_cfg_nodes(){
#Update configuration files on all nodes
for_all_group update_cfg.sh
}

config(){
#Config all nodes 
for_all_group config.sh
}

clean(){
#Clean all nodes
for_all_group clean.sh
}

setup(){
#TODO:ripristinare
#clone_dreamer
setup_nodes
update_cfg_nodes
}

all(){
config_dsh
setup
config
}

USAGE="\n
# 	Usage : ./managment_config.sh <option>\n
#	update_mgmt_sh : update the configuration file management.sh\n
# 	config_dsh : enable root login, configure dsh machine.list and groups\n
#	clone_dreamer : download code from $REPO_URL\n
#   update_dreamer : update remote repo from $REPO_URL\n
#	setup_nodes : install software required and update configuration files on all nodes\n
#	setup : download code from $REPO_URL, install software required and update configuration files on all nodes\n
#	update_cfg_nodes : Update configuration files on all nodes\n
#	config : configure all nodes\n
#	clean : clean all nodes\n
#	all : complete setup and config\n
#	change_sh_addresses : update remote.cfg on the machines\n"

while getopts ":c: " opt; do
    case $opt in
            c) command=$OPTARG
			   shift;;      
			:) echo "Option -$OPTARG requires an argument."
			   echo -e $USAGE
      		   exit 1;;
    esac
	shift
done

if [ -z "$command" ]; then
		echo -e $USAGE
		exit 1
fi


if [ -f management.sh ];
	then
		# If cfg file is present in current folder, use it
		echo "---> File found in current folder. Using local configuration file management.sh"
		source management.sh
elif [ "$command" != "update_mgmt_sh" ];
	then
		echo "Error File Not Found..."
		exit -1
fi

$command
<commit_msg>Bugfix<commit_after>#!/bin/bash

# XXX Requirements
# You have to install dsh, expect

# Usage : ./managment_config.sh <option>
#	update_mgmt_sh : update the configuration file management.sh
# 	config_dsh : enable root login, configure dsh machine.list and groups
#	clone_dreamer : download code from $REPO_URL
#   update_dreamer : update remote repo from $REPO_URL
#	setup_nodes : install software required and update configuration files on all nodes
#	setup : download code from $REPO_URL, install software required and update configuration files on all nodes
#	update_cfg_nodes : Update configuration files on all nodes
#	config : configure all nodes
#	clean : clean all nodes
#	all : complete setup and config
#	change_sh_addresses : update remote.cfg on the machines

USER="root"

#XXX Address used for the download of management.sh
MGT_SH_ADDR=https://www.dropbox.com/s/f5lse4stkxrwb6j/management.sh

update_mgmt_sh(){
	if ! [ -n "$MGT_SH_ADDR" ]; then
		echo "MGT_SH_ADDR Not Setted"
		exit 1
	fi
	rm management.sh
	echo "Updating management.sh from $MGT_SH_ADDR..."
	wget $MGT_SH_ADDR
}

config_dsh(){

echo "Please enter testbed username: "
read OFELIA_USER
echo "Please enter testbed $OFELIA_USER pass: "
read -s OFELIA_PASS
echo
echo "Please enter testbed root pass: "
read -s ROOT_PASS
echo
while true; do
    read -p "Generate ssh key (y/n)?" yn 
    case $yn in
        [Yy]* ) ssh-keygen; break;;
        [Nn]* ) break;;
        * ) echo "(y/n)";;
    esac
done

#Enable ssh root access
echo "Enable ssh root access..."

TARGET_KEY="PermitRootLogin"
REPLACEMENT_VALUE="yes"

for i in ${NODE_LIST[@]}; do
	./send_root_cmd $i $OFELIA_USER $OFELIA_PASS $ROOT_PASS "sed -i \"s/\($TARGET_KEY * *\).*/\1$REPLACEMENT_VALUE/\" /etc/ssh/sshd_config"
	./send_root_cmd $i $OFELIA_USER $OFELIA_PASS $ROOT_PASS "/etc/init.d/ssh restart"
 	./send_root_cmd $i $OFELIA_USER $OFELIA_PASS $ROOT_PASS "echo $(cat /root/.ssh/id_rsa.pub) >/root/.ssh/authorized_keys"
done
echo ""
echo "Configure DSH..."

#DSH configuration file
TARGET_KEY="remoteshell"
REPLACEMENT_VALUE="ssh"

sed -i "s/\($TARGET_KEY *= *\).*/\1$REPLACEMENT_VALUE/" /etc/dsh/dsh.conf

#Configures machines.list
echo "Configure machines.list..."


rm /etc/dsh/machines.list

for i in ${NODE_LIST[@]}; do
	echo $i
	echo $USER@$i >> /etc/dsh/machines.list
done

#Creates groups files in /etc/dsh/group
echo "Creating groups..."
for i in ${DSH_GROUPS[@]}; do
	echo > /etc/dsh/group/$i
	echo $i
	eval group=\${${i}[*]}
	for host in ${group[@]};do
		echo $USER@$host
		echo $USER@$host >> /etc/dsh/group/$i
	done	
done

}

WORK_DIR=/root/

#GITHUB
REPO_DIR=Dreamer-Setup-Scripts
REPO_URL=https://github.com/netgroup/Dreamer-Setup-Scripts

#XXX Operation with this repo could not work (bitbucket requests password)
#BITBUCKET
#REPO_DIR=dreamer-setup-scripts
#REPO_URL=https://pierventre@bitbucket.org/ssalsano/dreamer-setup-scripts.git

TESTBED_SH_ADDR="https://www.dropbox.com/s/smsyctn1qj72kpk/testbed.sh"
LMERULES_SH_ADDR="https://www.dropbox.com/s/vp30krz8vamjoxn/lmerules.sh"


clone_dreamer(){
#git clone on all hosts in NODE_LIST
dsh -M -g all -c rm -r -f $REPO_DIR
dsh -M -g all -c git clone $REPO_URL
}

update_dreamer(){
#git pull origin master on all hosts in NODE_LIST
dsh -M -g all -c "cd ./$REPO_DIR && git pull origin master"
}


for_all_group() {
# Executes $1 command on the deployed machines
for i in ${DSH_GROUPS[@]}; do
        if [ "$i" = "OSHI" ]; then
				echo $i
                dsh -M -g $i -c "cd ./$REPO_DIR/oshi/ && ./$1" 
        elif [ "$i" = "ROUTER" ];then
                echo $i
				dsh -M -g $i -c "cd ./$REPO_DIR/ip_router/ && ./$1"  
        elif [ "$i" = "EUH" ] || [ "$i" = "CTRL" ];then
				echo $i
                dsh -M -g $i -c "cd ./$REPO_DIR/othernodes/ && ./$1" 
		else    
            	#Not Handled
				echo "$i Not Handled"
				exit 1 			 
        fi
done

}

change_sh_addresses(){
# Change remote.cfg in accordance to $TESTBED_SH_ADDR and $LMERULES_SH_ADDR
for i in ${DSH_GROUPS[@]}; do
        if [ "$i" = "OSHI" ]; then
				echo $i
				if ! [ -n "$TESTBED_SH_ADDR" ] || ! [ -n "$LMERULES_SH_ADDR" ]; then
					echo "Addresses Not Setted For OSHI"
					exit 1
				fi
				TARGET_KEY="DREAMERCONFIGSERVER"
				REPLACEMENT_VALUE="$TESTBED_SH_ADDR"
				TARGET_KEY2="DREAMERCONFIGSERVER2"
				REPLACEMENT_VALUE2="$LMERULES_SH_ADDR"
				dsh -M -g $i -c "cd ./$REPO_DIR/oshi/ && sed -i \"s@\($TARGET_KEY *= *\).*@\1$REPLACEMENT_VALUE@\" ./remote.cfg  && sed -i \"s@\($TARGET_KEY2 *= *\).*@\1$REPLACEMENT_VALUE2@\" ./remote.cfg"
        elif [ "$i" = "ROUTER" ];then
                echo $i
				if ! [ -n "$TESTBED_SH_ADDR" ]; then
					echo "Addresses Not Setted For ROUTER"
					exit 1
				fi
				TARGET_KEY="DREAMERCONFIGSERVER"
				REPLACEMENT_VALUE="$TESTBED_SH_ADDR"
				dsh -M -g $i -c "cd ./$REPO_DIR/ip_router/ && sed -i \"s@\($TARGET_KEY *= *\).*@\1$REPLACEMENT_VALUE@\" ./remote.cfg"
        elif [ "$i" = "EUH" ] || [ "$i" = "CTRL" ];then
				echo $i
				if ! [ -n "$TESTBED_SH_ADDR" ]; then
					echo "Addresses Not Setted For EUH and CTRL"
					exit 1
				fi
				TARGET_KEY="DREAMERCONFIGSERVER"
				REPLACEMENT_VALUE="$TESTBED_SH_ADDR"
				dsh -M -g $i -c "cd ./$REPO_DIR/othernodes/ && sed -i \"s@\($TARGET_KEY *= *\).*@\1$REPLACEMENT_VALUE@\" ./remote.cfg"
		else    
            	#Not Handled
				echo "$i Not Handled"
				exit 1 			 
        fi
done

}

setup_nodes(){
#setup.sh
for_all_group setup.sh
}

update_cfg_nodes(){
#Update configuration files on all nodes
for_all_group update_cfg.sh
}

config(){
#Config all nodes 
for_all_group config.sh
}

clean(){
#Clean all nodes
for_all_group clean.sh
}

setup(){
#TODO:ripristinare
#clone_dreamer
setup_nodes
update_cfg_nodes
}

all(){
config_dsh
setup
config
}

USAGE="\n
# 	Usage : ./managment_config.sh <option>\n
#	update_mgmt_sh : update the configuration file management.sh\n
# 	config_dsh : enable root login, configure dsh machine.list and groups\n
#	clone_dreamer : download code from $REPO_URL\n
#   update_dreamer : update remote repo from $REPO_URL\n
#	setup_nodes : install software required and update configuration files on all nodes\n
#	setup : download code from $REPO_URL, install software required and update configuration files on all nodes\n
#	update_cfg_nodes : Update configuration files on all nodes\n
#	config : configure all nodes\n
#	clean : clean all nodes\n
#	all : complete setup and config\n
#	change_sh_addresses : update remote.cfg on the machines\n"

while getopts ":c: " opt; do
    case $opt in
            c) command=$OPTARG
			   shift;;      
			:) echo "Option -$OPTARG requires an argument."
			   echo -e $USAGE
      		   exit 1;;
    esac
	shift
done

if [ -z "$command" ]; then
		echo -e $USAGE
		exit 1
fi


if [ -f management.sh ];
	then
		# If cfg file is present in current folder, use it
		echo "---> File found in current folder. Using local configuration file management.sh"
		source management.sh
elif [ "$command" != "update_mgmt_sh" ];
	then
		echo "Error File Not Found..."
		exit -1
fi

$command

{'fraction_non_alphanumeric': 0.13395017793594305, 'fraction_numerical': 0.0046975088967971534, 'mean_word_length': 3.220787023130069, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 21, 'https://': 10, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2611868', 'n_tokens_mistral': 5638, 'n_tokens_neox': 5262, 'n_words': 1476}