CERN

From short to long

Haha, this always seems to happen; a “short bash-script” suddenly becomes quite long. I’m currently in the process of making one that will generate the large parts of DHCP, BIND and RADIUS configuration files.

This is how it looks thus far;

#!/bin/bash
 
# Variables
ip_start="10.162"
#ib_mac="00:00:00:00:00:00"
ib_mac="99:99:99:99:99:99"
 
# Switches
rename_cn=1
rename_fep=0
print_stuff=1
generate_config=0
 
# Remove unwanted files
rm dhcpd-10.162.* 2> /dev/null
 
die (){
        echo >&2 "$@"
        exit 1
}
 
increment (){
	stripped_hostname=`echo $new_hostname|perl -wple 's/(\-).?(charm|bmc|ib)$//ig'`
 
	if [ -z "$last_hostname" ]; then
        	# This is the first hostname we're processing
        	last_hostname="$stripped_hostname"
        fi
 
        if ! echo "$stripped_hostname"|grep -qiE "^$last_hostname$"; then
                increment_ip
        fi
 
        last_hostname=$stripped_hostname
}
 
 
increment_ip (){
	if [[ $fourth_octet -eq 255 ]]; then
                # Increase third octet, reset fourth
                let "third_octet += 1"
                fourth_octet=0
        else
		# Just increase fourth
                let "fourth_octet += 1"
        fi
}
 
generate_config (){
	# Called when we want to generate configuration
 
	# $1 = hostname
	# $2 = mac
	# $3 = ip
	# $4 = infinihost
 
	generate_dhcp $1 $2 $3 $4
	#generate_bind $1 $2 $3
	#generate_radius $1 $2 $3
}
 
generate_dhcp (){
	# Generate the DHCP-config
 
	if [ -z $4 ]; then
		# We don't want infinihosts in the DHCP-config
 
		echo "host $1 { hardware ethernet $2; fixed-address $3; }" >> $dhcp_conf_filename
	fi
 
}
 
generate_bind (){
	# Generate the BIND-config
 
	echo ""
 
}
 
generate_radius (){
	# Generate the RADIUS-config
 
	echo ""
}
 
run_code (){
 
	# Variables
	static_third_octet=$2
	third_octet=$static_third_octet
	fourth_octet=$3
	ib_offset="$4"
	mgmt_offset="$5"
	cn_counter=0
	cngpu_counter=0
	fep_counter=0
	fephost_without_number="LOLKEK"
	dhcp_conf_filename="dhcpd-10.162.$static_third_octet.0.conf"
	last_hostname=""
 
	while read hostinfo; do
		old_hostname=`echo $hostinfo | cut -d'#' -f1 | perl -wple 's/[ \t]*//ig'`
	 	mac=`echo $hostinfo | cut -d'#' -f2 | perl -wple 's/[ \t]*//ig'`
 
		# make new cn*-hosts & IPs
		if echo "$old_hostname"|grep -qi "^cn"; then
			if [ $rename_cn -eq 1 ]; then
				if echo "$old_hostname"|grep -qi "^cngpu"; then
     		                	#cngpu*
					hostnumber=`printf "%03d" $cngpu_counter`
					new_hostname="cngpu$hostnumber"
 
					if echo "$old_hostname"|grep -qiE "(charm|bmc)$"; then
						let "cngpu_counter += 1"
					fi
	       	         	else
       		                 	#cn*
					hostnumber=`printf "%03d" $cn_counter`
					new_hostname="cn$hostnumber"
 
					if echo "$old_hostname"|grep -qiE "(charm|bmc)$"; then
						let "cn_counter += 1"
					fi
	       	         	fi
			else
				new_hostname="$old_hostname"
			fi
 
			increment
 
			infinihost=0
			if cat "hostnames.txt"|grep -qiE "$old_hostnameib$"; then
				#node has infiniband
				infinihost=1
				ib_hostname=""$new_hostname"-ib"
			fi
 
			if echo "$old_hostname"|grep -qiE "(charm|bmc)$"; then
 
				if [ $rename_cn -eq 1 ]; then
					if echo "$old_hostname"|grep -qi "charm$"; then
						mgmt_hostname="$new_hostname-charm"
					else
						mgmt_hostname="$new_hostname-bmc"
					fi
				else
					mgmt_hostname="$old_hostname"
				fi
 
				mgmt_ip="$ip_start.$(($third_octet $mgmt_offset)).$fourth_octet"
 
	       	         	if [ $print_stuff -eq 1 ]; then
					echo "$old_hostname -> $mgmt_hostname ($mac, $mgmt_ip)"
				fi
 
				if [ $generate_config -eq 1 ]; then
					generate_config $mgmt_hostname $mac $mgmt_ip
				fi
	                else
				host_ip="$ip_start.$third_octet.$fourth_octet"
				ib_ip="$ip_start.$(($third_octet $ib_offset)).$fourth_octet"
 
				if [ $print_stuff -eq 1 ]; then
					echo "$old_hostname -> $new_hostname ($mac, $host_ip)"
 
					if [ $infinihost -eq 1 ]; then
	                         	echo ""$old_hostname"-ib -> $ib_hostname ($ib_mac, $ib_ip)"
	       	                 	fi
				fi
 
				if [ $generate_config -eq 1 ]; then
					generate_config $new_hostname $mac $host_ip
 
					if [ $infinihost -eq 1 ]; then
						generate_config $ib_hostname $ib_mac $ib_ip $infinihost
					fi
				fi
	                fi
 
		# make new IPs for fep*-hosts
		elif echo "$old_hostname"|grep -qi "^fep"; then
 
			if [ $rename_fep -eq 1 ]; then
				if ! echo "$old_hostname"|grep -qi "$fephost_without_number"; then
					fephost_without_number=`echo $old_hostname|sed -e 's/[0-9].*//'`
					fep_counter=0
				fi
 
				hostnumber=`printf "%03d" $fep_counter`
				new_hostname="$fephost_without_number$hostnumber"
			else
				new_hostname="$old_hostname"
			fi
 
			increment
 
			infinihost=0
			if cat "hostnames.txt"|grep -qiE "$old_hostnameib$"; then
                        	#node has infiniband
                        	infinihost=1
				ib_hostname=""$new_hostname"-ib"
			fi
 
			if echo "$old_hostname"|grep -qiE "(charm|bmc)$"; then
				if [ $rename_fep -eq 1 ]; then
		                        if echo "$old_hostname"|grep -qi "charm$"; then
		                                mgmt_hostname="$new_hostname-charm"
	        	                else
	                	                mgmt_hostname="$new_hostname-bmc"
	                        	fi
					let "fep_counter += 1"
				else
					mgmt_hostname="$old_hostname"
				fi
 
				mgmt_ip="$ip_start.$(($third_octet $mgmt_offset)).$fourth_octet"
 
				if [ $print_stuff -eq 1 ]; then
					echo "$old_hostname -> $mgmt_hostname ($mac, $mgmt_ip)"
				fi
 
				if [ $generate_config -eq 1 ]; then
	                                generate_config $mgmt_hostname $mac $mgmt_ip
	                        fi
	                else
				host_ip="$ip_start.$third_octet.$fourth_octet"
	                        ib_ip="$ip_start.$(($third_octet $ib_offset)).$fourth_octet"
 
				if [ $print_stuff -eq 1 ]; then
	                                echo "$old_hostname -> $new_hostname ($mac, $host_ip)"
 
	                                if [ $infinihost -eq 1 ]; then
	                                        echo ""$old_hostname"-ib -> $ib_hostname ($ib_mac, $ib_ip)"
	                                fi
	                        fi
 
	                        if [ $generate_config -eq 1 ]; then
	                                generate_config $new_hostname $mac $host_ip
 
	                                if [ $infinihost -eq 1 ]; then
	                                        generate_config $ib_hostname $ib_mac $ib_ip $infinihost
	                                fi
	                        fi
	                fi
		else
			# Everything else
 
			new_hostname="$old_hostname"
 
			increment
 
			infinihost=0
	                if cat "hostnames.txt"|grep -qiE ""$new_hostname".?ib$"; then
	                        #node has infiniband
	                        infinihost=1
	                        ib_hostname=""$new_hostname"-ib"
	                fi
 
			if echo "$old_hostname"|grep -qiE "(charm|bmc)$"; then
	                        mgmt_hostname="$old_hostname"
 
	                        mgmt_ip="$ip_start.$(($third_octet $mgmt_offset)).$fourth_octet"
 
        	                if [ $print_stuff -eq 1 ]; then
	                                echo "$old_hostname -> $mgmt_hostname ($mac, $mgmt_ip)"
	                        fi
 
	                        if [ $generate_config -eq 1 ]; then
	                                generate_config $mgmt_hostname $mac $mgmt_ip
	                        fi
	                else
	                        host_ip="$ip_start.$third_octet.$fourth_octet"
	                        ib_ip="$ip_start.$(($third_octet $ib_offset)).$fourth_octet"
 
	                        if [ $print_stuff -eq 1 ]; then
	                                echo "$old_hostname -> $new_hostname ($mac, $host_ip)"
 
	                                if [ $infinihost -eq 1 ]; then
	                                        echo ""$old_hostname"-ib -> $ib_hostname ($ib_mac, $ib_ip)"
	                                fi
	                        fi
 
	                        if [ $generate_config -eq 1 ]; then
	                                generate_config $new_hostname $mac $host_ip
 
	                                if [ $infinihost -eq 1 ]; then
	                                        generate_config $ib_hostname $ib_mac $ib_ip $infinihost
	                                fi
	                        fi
	                fi
		fi
	done < $1 
}
 
# run_code
# $1 = filename
# $2 = third octet
# $3 = fourth octet
# $4 = ib_offset
# $5 = mgmt_offset
 
# invalid-hosts.txt
 
run_code valid-hosts-prod.txt 16 11 "+ 16" "+ 128"
run_code valid-hosts-dev.txt 24 11 "+ 16" "+ 128"
run_code valid-hosts-vm.txt 52 11 "+ 0" "+ 128"