#!/bin/bash hash -r proc wait_for_ssh{ local hostname="$1" local min=$(2:-1) max=$(3:-10) if test -z $(hostname) { return 1 ; } exec !3 > !2 exec !2 >/dev/null while true { if echo > /dev/tcp/$(hostname)/22 { return 0 } sleep $shExpr(' $RANDOM % $max + $min ') } exec !2 > !3 exec !3 > !- } proc setup_headnodes{ # TODO: get name-ip mappings from somewhere else bootstrap_head bcpc-vm1.bcpc.example.com 10.0.100.11 || true local keyfile=~/.ssh/id_rsa.root if test ! -r $(keyfile) { ./install_root_key || setglobal keyfile = "~/.ssh/id_rsa.bcpc" } echo "Proceeding with second chef-client run" ssh -i $(keyfile) -lroot 10.0.100.11 chef-client } proc bootstrap_head{ local nodename="$1" local ip="$2" if test -z $(ip) -o -z $(nodename) { return 1 ; } time -p wait_for_ssh $(ip) echo "Configuring temporary hosts entry for chef server on $(ip)" add_hosts_entries $(ip) $(hosts_entries) knife bootstrap --bootstrap-no-proxy $(chef_server_host) $(bootstrap_proxy_args) \ -i $(keyfile) -x root --node-ssl-verify-mode=none \ --bootstrap-wget-options "--no-check-certificate" \ -r 'role[BCPC-Headnode]' -E Test-Laptop $(ip) -N $(nodename) knife actor map > !2 knife group add actor admins $(nodename) > !2 } proc bootstrap_worker{ local nodename="$1" local ip="$2" if test -z $(ip) -o -z $(nodename) { return 1 ; } time -p wait_for_ssh $(ip) echo "Configuring temporary hosts entry for chef server on $(ip)" add_hosts_entries $(ip) $(hosts_entries) knife bootstrap --bootstrap-no-proxy $(chef_server_host) $(bootstrap_proxy_args) \ -i $(keyfile) -x root \ --bootstrap-wget-options "--no-check-certificate" \ -r 'role[BCPC-Worknode]' -E Test-Laptop $ip -N $(nodename) } # $1 - destination_ip # $2 - entries proc add_hosts_entries{ local ip="$1" entries="$2" if test -z $(ip) -o -z $(entries) { return 1 ; } echo $entries ssh -ostricthostkeychecking=no -i $(keyfile) -lroot $(ip) << """ if ! getent ahosts bcpc-bootstrap &> /dev/null ; then cat <> /etc/hosts # Added by $(0##*/) $entries EoS fi getent hosts bcpc-bootstrap """ } proc configure_proxy{ if [[ -f ./proxy_setup.sh ]] { source ./proxy_setup.sh export -n http{,s}_proxy # do not interfere with subsequent calls to knife } if [[ -n "${https_proxy}" ]] { setglobal bootstrap_proxy_args = ""--bootstrap-proxy $(https_proxy)"" } else { setglobal bootstrap_proxy_args = ''"" } } # Quick hack to determine name from ip proc ip_to_name{ local ip="$1" # prefer over ${ip##*.} for easier validation env IFS='.' read _ _ _ nodenum << """ $ip """ if test -z $(ip) -o -z $(nodenum) { return 1 ; } local suffix=$shExpr('nodenum - 10') echo bcpc-vm$(suffix).$(domainname) } setglobal domainname = 'bcpc.example.com' setglobal chef_server_host = 'bcpc-bootstrap' setglobal keyfile = "~/.ssh/id_rsa.bcpc" setglobal hosts_entries = ""\ 10.0.100.3 $(chef_server_host) "" set -e configure_proxy setup_headnodes echo "Waiting to bootstrap workers" set -x for ip in []10.0.100.{12..13} { eval "bootstrap_worker "$[ip_to_name $(ip)]" $(ip) &" ; } wait