#!/bin/bash # Script to assign roles to cluster nodes based on a definition in cluster.txt : # # - If no hostname is provided, all nodes will be attempted # # - if a nodename is provided, either by hostname or ip address, only # that node will be attempted # # - if the special nodename "heads" is given, all head nodes will be # attempted # # - if the special nodename "workers" is given, all work nodes will be # attempted # # - A node may be excluded by setting its role to something other than # "head" or "work" in cluster.txt. For example "done" might be # useful for nodes that have been completed set -e if [[ -z "$1" ]] { echo "Usage : $0 environment (hostname)" exit 1 } setglobal ENVIRONMENT = $1 setglobal EXACTHOST = $2 if [[ ! -f "environments/$ENVIRONMENT.json" ]] { echo "Error: Couldn't find '$ENVIRONMENT.json'. Did you forget to pass the environment as first param?" exit 1 } declare -A FQDNS while read HOST MACADDR IPADDR ILOIPADDR DOMAIN ROLE { if [[ -z "$EXACTHOST" || "$EXACTHOST" = all || "$EXACTHOST" = "$HOST" || "$EXACTHOST" = "$IPADDR" || "$EXACTHOST" = "heads" && "$ROLE" = "head" || "$EXACTHOST" = "workers" && "$ROLE" = "work" ]] { setglobal IDX = $[echo $IPADDR | tr '.' '-] if [[ "$ROLE" = "bootstrap" ]] { continue } if [[ "$ROLE" = head ]] { setglobal HEADS = ""$HEADS $IPADDR"" compat array-assign FQDNS '"$IDX"' ""$(HOST).$(DOMAIN)"" } elif [[ "$ROLE" = work ]] { setglobal WORKERS = ""$WORKERS $IPADDR"" compat array-assign FQDNS '"$IDX"' ""$(HOST).$(DOMAIN)"" } } } < cluster.txt echo "heads : $HEADS" echo "workers : $WORKERS" setglobal PASSWD = $[knife data bag show configs $ENVIRONMENT | grep "cobbler-root-password:" | awk ' {print $2}] # All nodes now use an unbundled initialisation - chef is installed from # a .deb (allowing disconnected working) and then the node is # bootstrapped with no role to start, and then it is made admin, and # then the role is assigned, finally chef-client is run for HEAD in [$HEADS] { setglobal MATCH = $HEAD echo "About to bootstrap head node $HEAD..." ./chefit.sh $HEAD $ENVIRONMENT echo $PASSWD | sudo knife bootstrap -E $ENVIRONMENT $HEAD -x ubuntu -P $PASSWD --sudo setglobal IDX = $[echo $HEAD | tr '.' '-] setglobal FQDN = $(FQDNS["$IDX"]) knife actor map knife group add actor admins $FQDN knife node run_list add $FQDN 'role[BCPC-Headnode]' setglobal SSHCMD = ""./nodessh.sh $ENVIRONMENT $HEAD"" $SSHCMD "/home/ubuntu/finish-head.sh" sudo } # As of R5, we seem to need to use the two-step bootstrap for work nodes too for WORKER in [$WORKERS] { setglobal MATCH = $WORKER echo "About to bootstrap worker worker $WORKER..." ./chefit.sh $WORKER $ENVIRONMENT echo $PASSWD | sudo knife bootstrap -E $ENVIRONMENT $WORKER -x ubuntu -P $PASSWD --sudo setglobal IDX = $[echo $WORKER | tr '.' '-] setglobal FQDN = $(FQDNS["$IDX"]) knife actor map knife group add actor admins $FQDN knife node run_list add $FQDN 'role[BCPC-Worknode]' setglobal SSHCMD = ""./nodessh.sh $ENVIRONMENT $WORKER"" $SSHCMD "/home/ubuntu/finish-worker.sh" sudo } if [[ -z "$MATCH" ]] { echo "Warning: No nodes found" }