Christoph Zangerle
Christoph Zangerle

Reputation: 61

bash script stuck at the end of a for loop

I'm trying to deploy $DM_COUNT docker-machines through a script, and write new network configuration to the nodes. The script works fine until the end of the loop and then gets stuck

The script is called from this script:

#!/bin/bash
set -x
PS4='$LINENO: '

net="10.17.65."

The loop starts properly

for i in $(seq 1 $DM_COUNT); do
(
name="$PREFIX$DEPLOYMENTNAME$i"
echo "$name"

if [ -z "$DM_NAMES" ]
then
    export DM_NAMES=$name
else
    export DM_NAMES=$DM_NAMES:$name
fi

ip="$net$((4 + i))"
mkdir -p ~/.ssh && touch $_/config &&
tee -a $_ << EOF
Host $name
  Hostname $ip
  User docker
  IdentityFile ~/.docker/machine/machines/$name/id_rsa
EOF

 docker-machine create $name\
                  --driver vmwarevsphere \
                  --vmwarevsphere-cpu-count 4 \
                  --vmwarevsphere-datastore datastore1 \
                  --vmwarevsphere-disk-size 60000 \
                  --vmwarevsphere-memory-size 2048 \
                  --vmwarevsphere-network 'VM\ Network' \
                  --vmwarevsphere-vcenter 10.17.6.218 \
                  --vmwarevsphere-password a \
                  --vmwarevsphere-username root

docker-machine restart "$name"
docker-machine regenerate-certs -f "$name"
echo 'Done provisioning ' "$name"

docker-machine scp -r /certs/ "$name":/root/certs/
docker-machine ssh "$name" sudo mkdir /var/lib/boot2docker/certs

docker-machine ssh "$name" sudo cp /root/certs/*.crt /var/lib/boot2docker/certs/

echo 'Done copying self-signed certificates'

echo "
    # configure eht1 (czlocal1)
    sudo ip addr flush dev eth1
    sudo ip route del default
    sudo ip route add default via 192.168.1.254

    # configure eth2 (czlocal2)
    sudo ip addr flush dev eth0
    sudo ip route add default via 192.168.2.254

    # configure eth3 (czlocaldhcp)
    sudo ip addr flush dev eth0

    # configure eth0 (mgmt)
    sudo ip addr flush dev eth0
    ip addr add 10.17.65.10$i/24 dev eth0
    ip route add 10.17.36.0/24 via 10.17.65.1 dev eth0
    ip route add 10.17.33.0/24 via 10.17.65.1 dev eth0

    # configure eth2 (czlocal2)
    sudo ip addr flush dev eth2
    ip addr add 192.168.2.$i/24

    # configure eth3 (czlocaldhcp)
    sudo ip addr flush dev eth3
" | docker-machine ssh "$name" "sudo tee /var/lib/boot2docker/bootsync.sh"

echo "
    sudo ip route del default
    sudo ip route add default via 192.168.$1.254
" | docker-machine ssh "$name" "sudo tee ~/switch_default_gateway.sh"

echo "
    alias local1='ip route del default && ip route add default via 192.168.1.254'
    alias local2='ip route del default && ip route add default via 192.168.2.254'
    alias dhcp='ip route del default && ip route add default via 172.0.0.254'
" | docker-machine ssh "$name" "sudo tee -a ~/.bashrc > /dev/null"

echo 'Done writing scripts'

docker-machine ssh "$name" "sudo chmod +x /var/lib/boot2docker/bootsync.sh"
docker-machine ssh "$name" "sudo chmod +x ~/switch_default_gateway.sh"
docker-machine ssh "$name" "source ~/.bashrc"

jq '.Driver.IPAddress = $newVal' --arg newVal '10.17.65.10'$i ~/.docker/machine/machines/"$name"/config.json > tmp.$$.json && mv tmp.$$.json ~/.docker/machine/machines/"$name"/config.json

echo 'Done deploying docker machines'

) &
done

Everything until this point executes fine, then the script is stuck. If I provide any input on the shell it exits.

tee -a ~/.ssh/config << EOF
Host *
  StrictHostKeyChecking no
EOF

The last part never gets executed

Upvotes: 1

Views: 597

Answers (1)

Bsquare ℬℬ
Bsquare ℬℬ

Reputation: 4487

Are you sure you don't have a 'non breakable space' (https://en.wikipedia.org/wiki/Non-breaking_space) between your << and EOF?

Anyway, your initial script, and the indicated lines at the end of your question is not the same (the one involving tee command), can you update your question to fit the final version of your script?

Upvotes: 1

Related Questions