Reduce the number of times gcs.sh ssh's into the VMs
This commit is contained in:
1 parent
77358140d9
commit
da36c78942
2 files changed
+27
-14
No files matched your search
+1
-1
@@ -26,7 +26,7 @@ dependencies:
|
|||||||
docker save weaveworks/weave-build >$WEAVE_BUILD;
|
docker save weaveworks/weave-build >$WEAVE_BUILD;
|
||||||
fi
|
fi
|
||||||
post:
|
post:
|
||||||
- sudo apt-get install bc
|
- sudo apt-get install bc jq
|
||||||
- pip install requests
|
- pip install requests
|
||||||
- curl https://sdk.cloud.google.com | bash
|
- curl https://sdk.cloud.google.com | bash
|
||||||
- bin/setup-circleci-secrets "$SECRET_PASSWORD"
|
- bin/setup-circleci-secrets "$SECRET_PASSWORD"
|
||||||
|
|||||||
+26
-13
@@ -50,12 +50,12 @@ function destroy {
|
|||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
function external_ip {
|
function internal_ip {
|
||||||
gcloud compute instances list $1 --format=yaml | grep "^ natIP\:" | cut -d: -f2 | tr -d ' '
|
jq -r ".[] | select(.name == \"$2\") | .networkInterfaces[0].networkIP" $1
|
||||||
}
|
}
|
||||||
|
|
||||||
function internal_ip {
|
function external_ip {
|
||||||
gcloud compute instances list $1 --format=yaml | grep "^ networkIP\:" | cut -d: -f2 | tr -d ' '
|
jq -r ".[] | select(.name == \"$2\") | .networkInterfaces[0].accessConfigs[0].natIP" $1
|
||||||
}
|
}
|
||||||
|
|
||||||
function try_connect {
|
function try_connect {
|
||||||
@@ -81,32 +81,42 @@ EOF
|
|||||||
ssh -t $name sudo docker run --rm -v /usr/local/bin:/target jpetazzo/nsenter
|
ssh -t $name sudo docker run --rm -v /usr/local/bin:/target jpetazzo/nsenter
|
||||||
}
|
}
|
||||||
|
|
||||||
# Create new set of VMS
|
# Create new set of VMs
|
||||||
function setup {
|
function setup {
|
||||||
destroy
|
destroy
|
||||||
names="$(vm_names)"
|
names="$(vm_names)"
|
||||||
gcloud compute instances create $names --image $TEMPLATE_NAME --zone $ZONE
|
gcloud compute instances create $names --image $TEMPLATE_NAME --zone $ZONE
|
||||||
gcloud compute config-ssh --ssh-key-file $SSH_KEY_FILE
|
gcloud compute config-ssh --ssh-key-file $SSH_KEY_FILE
|
||||||
|
sed -i '/UserKnownHostsFile=\/dev\/null/d' ~/.ssh/config
|
||||||
|
|
||||||
|
# build an /etc/hosts file for these vms
|
||||||
|
hosts=$(mktemp hosts.XXXXXXXXXX)
|
||||||
|
json=$(mktemp json.XXXXXXXXXX)
|
||||||
|
gcloud compute instances list --format=json >$json
|
||||||
|
for name in $names; do
|
||||||
|
echo "$(internal_ip $json $name) $name.$ZONE.$PROJECT" >>$hosts
|
||||||
|
done
|
||||||
|
|
||||||
for name in $names; do
|
for name in $names; do
|
||||||
hostname="$name.$ZONE.$PROJECT"
|
hostname="$name.$ZONE.$PROJECT"
|
||||||
|
|
||||||
# Add the remote ip to the local /etc/hosts
|
# Add the remote ip to the local /etc/hosts
|
||||||
sudo -- sh -c "echo \"$(external_ip $name) $hostname\" >>/etc/hosts"
|
sudo sed -i "/$hostname/d" /etc/hosts
|
||||||
|
sudo sh -c "echo \"$(external_ip $json $name) $hostname\" >>/etc/hosts"
|
||||||
try_connect $hostname
|
try_connect $hostname
|
||||||
|
|
||||||
# Add the local ips to the remote /etc/hosts
|
# Copy the /etc/hosts we built over
|
||||||
for othername in $names; do
|
cat $hosts | ssh -t "$hostname" "sudo -- sh -c \"cat >>/etc/hosts\""
|
||||||
entry="$(internal_ip $othername) $othername.$ZONE.$PROJECT"
|
|
||||||
ssh -t "$hostname" "sudo -- sh -c \"echo \\\"$entry\\\" >>/etc/hosts\""
|
|
||||||
done
|
|
||||||
done
|
done
|
||||||
|
|
||||||
|
rm $hosts $json
|
||||||
}
|
}
|
||||||
|
|
||||||
function make_template {
|
function make_template {
|
||||||
gcloud compute instances create $TEMPLATE_NAME --image $IMAGE --zone $ZONE
|
gcloud compute instances create $TEMPLATE_NAME --image $IMAGE --zone $ZONE
|
||||||
gcloud compute config-ssh --ssh-key-file $SSH_KEY_FILE
|
gcloud compute config-ssh --ssh-key-file $SSH_KEY_FILE
|
||||||
name="$TEMPLATE_NAME.$ZONE.$PROJECT"
|
name="$TEMPLATE_NAME.$ZONE.$PROJECT"
|
||||||
try_connect $name
|
try_connect $name
|
||||||
install_docker_on $name
|
install_docker_on $name
|
||||||
gcloud -q compute instances delete $TEMPLATE_NAME --keep-disks boot --zone $ZONE
|
gcloud -q compute instances delete $TEMPLATE_NAME --keep-disks boot --zone $ZONE
|
||||||
gcloud compute images create $TEMPLATE_NAME --source-disk $TEMPLATE_NAME --source-disk-zone $ZONE
|
gcloud compute images create $TEMPLATE_NAME --source-disk $TEMPLATE_NAME --source-disk-zone $ZONE
|
||||||
@@ -115,14 +125,17 @@ function make_template {
|
|||||||
function hosts {
|
function hosts {
|
||||||
hosts=
|
hosts=
|
||||||
args=
|
args=
|
||||||
|
json=$(mktemp json.XXXXXXXXXX)
|
||||||
|
gcloud compute instances list --format=json >$json
|
||||||
for name in $(vm_names); do
|
for name in $(vm_names); do
|
||||||
hostname="$name.$ZONE.$PROJECT"
|
hostname="$name.$ZONE.$PROJECT"
|
||||||
hosts="$hostname $hosts"
|
hosts="$hostname $hosts"
|
||||||
args="--add-host=$hostname:$(internal_ip $name) $args"
|
args="--add-host=$hostname:$(internal_ip $json $name) $args"
|
||||||
done
|
done
|
||||||
echo export SSH=ssh
|
echo export SSH=ssh
|
||||||
echo export HOSTS=\"$hosts\"
|
echo export HOSTS=\"$hosts\"
|
||||||
echo export WEAVE_DOCKER_ARGS=\"$args\"
|
echo export WEAVE_DOCKER_ARGS=\"$args\"
|
||||||
|
rm $json
|
||||||
}
|
}
|
||||||
|
|
||||||
case "$1" in
|
case "$1" in
|
||||||
|
|||||||
Reference in new issue
Block a user