OLD | NEW |
(Empty) | |
| 1 #!/bin/bash |
| 2 # Run this script from its directory, to correctly pick up nat_startup.sh: |
| 3 # cd scripts |
| 4 # ./nat_setup.sh [command] |
| 5 |
| 6 PROJECT="chrome-infra-mon-proxy" |
| 7 REGION="us-central1" |
| 8 INSTANCE_TYPE="n1-standard-2" |
| 9 GAE_VM1_TAG="managed-gae-vm1" |
| 10 GAE_VM2_TAG="managed-gae-vm2" |
| 11 GAE_VM3_TAG="managed-gae-vm3" |
| 12 |
| 13 function delete_instance { |
| 14 # delete_instance instance-name zone-name |
| 15 echo "Deleting $1" |
| 16 gcloud compute -q --project "$PROJECT" instances delete "$1" --zone "$2" |
| 17 } |
| 18 function create_instance { |
| 19 # create_instance instance-name zone-name ip-name |
| 20 echo "Creating $1" |
| 21 gcloud compute -q --project "$PROJECT" instances create $1 --project "$PROJE
CT" --machine-type $INSTANCE_TYPE --zone $2 --image ubuntu-14-04 --network defau
lt --can-ip-forward --tags nat --address $3 --metadata-from-file startup-script=
nat_startup.sh |
| 22 } |
| 23 |
| 24 function create_nat_route { |
| 25 # create_nat_route route-name tag instance-name instance-zone |
| 26 echo "Creating new route to hijack traffic from VM tag $2" |
| 27 gcloud compute --project "$PROJECT" routes create "$1" --network default --d
estination-range 0.0.0.0/0 --next-hop-instance "$3" --next-hop-instance-zone "$4
" --tags "$2" --priority 800 |
| 28 } |
| 29 |
| 30 function delete_nat_route { |
| 31 echo "Deleting route $1" |
| 32 gcloud compute --project "$PROJECT" routes delete "$1" |
| 33 } |
| 34 |
| 35 function delete_nat_backend { |
| 36 # Deletes NAT routes. |
| 37 delete_nat_route managed-vm1-nat-route |
| 38 delete_nat_route managed-vm2-nat-route |
| 39 delete_nat_route managed-vm3-nat-route |
| 40 } |
| 41 |
| 42 function create_nat_backend { |
| 43 create_nat_route managed-vm1-nat-route "$GAE_VM1_TAG" nat-box1 "$REGION-a" |
| 44 create_nat_route managed-vm2-nat-route "$GAE_VM2_TAG" nat-box2 "$REGION-b" |
| 45 create_nat_route managed-vm3-nat-route "$GAE_VM3_TAG" nat-box3 "$REGION-f" |
| 46 } |
| 47 |
| 48 # Respin the NAT box VMs. Controlled individually, in case only some |
| 49 # of them have problems. |
| 50 function respin_nat1 { |
| 51 delete_instance nat-box1 "$REGION-a" |
| 52 create_instance nat-box1 "$REGION-a" proxy1 |
| 53 } |
| 54 |
| 55 function respin_nat2 { |
| 56 delete_instance nat-box2 "$REGION-b" |
| 57 create_instance nat-box2 "$REGION-b" proxy2 |
| 58 } |
| 59 |
| 60 function respin_nat3 { |
| 61 delete_instance nat-box3 "$REGION-f" |
| 62 create_instance nat-box3 "$REGION-f" proxy3 |
| 63 } |
| 64 |
| 65 case $1 in |
| 66 nat1) |
| 67 respin_nat1 |
| 68 ;; |
| 69 nat2) |
| 70 respin_nat2 |
| 71 ;; |
| 72 nat3) |
| 73 respin_nat3 |
| 74 ;; |
| 75 create-routes) |
| 76 create_nat_backend |
| 77 ;; |
| 78 delete-routes) |
| 79 delete_nat_backend |
| 80 ;; |
| 81 *) |
| 82 echo "Unknown command: $1. Use nat[1-3], create-routes, delete-routes." |
| 83 esac |
OLD | NEW |