Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame^] | 1 | #!/bin/bash |
| 2 | |
| 3 | # Bash funcs shared that combine common gcutil actions into single commands |
| 4 | |
| 5 | # remove_instance removes a named instance |
| 6 | # |
| 7 | # remove_instance <project> <instance_name> [<zone>="us-central1-b"] |
| 8 | remove_instance() { |
| 9 | local project=$1 |
| 10 | [[ -n $project ]] || { |
| 11 | echo "$FUNCNAME: missing arg: project" 1>&2 |
| 12 | return 1 |
| 13 | } |
| 14 | local an_instance=$2 |
| 15 | [[ -n $an_instance ]] || { |
| 16 | echo "$FUNCNAME: missing arg: an_instance" 1>&2 |
| 17 | return 1 |
| 18 | } |
| 19 | local zone=$3 |
| 20 | [[ -n $zone ]] || zone="us-central1-b" |
| 21 | |
| 22 | gcloud --project $project --quiet \ |
| 23 | compute instances delete $an_instance --zone=$zone |
| 24 | } |
| 25 | |
| 26 | # has_instance checks if a project contains a named instance |
| 27 | # |
| 28 | # has_instance <project> <instance_name> |
| 29 | has_instance() { |
| 30 | local project=$1 |
| 31 | [[ -n $project ]] || { |
| 32 | echo "$FUNCNAME: missing arg: project" 1>&2 |
| 33 | return 1 |
| 34 | } |
| 35 | local checked_instance=$2 |
| 36 | [[ -n $checked_instance ]] || { |
| 37 | echo "$FUNCNAME: missing arg: checked_instance" 1>&2 |
| 38 | return 1 |
| 39 | } |
| 40 | |
| 41 | instances=$(gcloud --project $project compute instances list \ |
| 42 | | sed -e 's/ \+/ /g' | cut -d' ' -f 1) |
| 43 | for i in $instances |
| 44 | do |
| 45 | if [[ $i == $checked_instance ]] |
| 46 | then |
| 47 | return 0 |
| 48 | fi |
| 49 | done |
| 50 | |
| 51 | return 1 |
| 52 | } |
| 53 | |
| 54 | # find_network_ip finds the ip address of a instance if it is present in the project. |
| 55 | # |
| 56 | # find_network_ip <project> <instance_name> |
| 57 | find_network_ip() { |
| 58 | local project=$1 |
| 59 | [[ -n $project ]] || { |
| 60 | echo "$FUNCNAME: missing arg: project" 1>&2 |
| 61 | return 1 |
| 62 | } |
| 63 | local checked_instance=$2 |
| 64 | [[ -n $checked_instance ]] || { |
| 65 | echo "$FUNCNAME: missing arg: checked_instance" 1>&2 |
| 66 | return 1 |
| 67 | } |
| 68 | |
| 69 | has_instance $project $checked_instance || return 1 |
| 70 | gcloud --project $project compute instances list \ |
| 71 | | grep -e "$checked_instance\s" | sed -e 's/ \+/ /g' | cut -d' ' -f 4 |
| 72 | } |
| 73 | |
| 74 | # delete_disks deletes a bunch of disks matching a pattern |
| 75 | # |
| 76 | # delete_disks <project> <disk_pattern> |
| 77 | delete_disks() { |
| 78 | local project=$1 |
| 79 | [[ -n $project ]] || { |
| 80 | echo "$FUNCNAME: missing arg: project" 1>&2 |
| 81 | return 1 |
| 82 | } |
| 83 | local disk_pattern=$2 |
| 84 | [[ -n $disk_pattern ]] || { |
| 85 | echo "$FUNCNAME: missing arg: disk_pattern" 1>&2 |
| 86 | return 1 |
| 87 | } |
| 88 | |
| 89 | trash_disks=$(gcloud --project=$project compute disks list \ |
| 90 | | sed -e 's/ \+/ /g' | cut -d' ' -f 1 | grep $disk_pattern) |
| 91 | [[ -n $trash_disks ]] && gcloud --project $project \ |
| 92 | --quiet compute disks delete $trash_disks |
| 93 | } |
| 94 | |
| 95 | # has_firewall checks if a project contains a named firewall |
| 96 | # |
| 97 | # has_firewall <project> <checked_firewall> |
| 98 | has_firewall() { |
| 99 | local project=$1 |
| 100 | [[ -n $project ]] || { |
| 101 | echo "$FUNCNAME: missing arg: project" 1>&2 |
| 102 | return 1 |
| 103 | } |
| 104 | local checked_firewall=$2 |
| 105 | [[ -n $checked_firewall ]] || { |
| 106 | echo "$FUNCNAME: missing arg: checked_firewall" 1>&2 |
| 107 | return 1 |
| 108 | } |
| 109 | |
| 110 | instances=$(gcloud --project $project compute firewall-rules list \ |
| 111 | | sed -e 's/ \+/ /g' | cut -d' ' -f 1) |
| 112 | for i in $instances |
| 113 | do |
| 114 | if [[ $i == $checked_firewall ]] |
| 115 | then |
| 116 | return 0 |
| 117 | fi |
| 118 | done |
| 119 | |
| 120 | return 1 |
| 121 | } |
| 122 | |
| 123 | # remove_firewall removes a named firewall from a project. |
| 124 | # |
| 125 | # remove_firewall <project> <checked_firewall> |
| 126 | remove_firewall() { |
| 127 | local project=$1 |
| 128 | [[ -n $project ]] || { |
| 129 | echo "$FUNCNAME: missing arg: project" 1>&2 |
| 130 | return 1 |
| 131 | } |
| 132 | local a_firewall=$2 |
| 133 | [[ -n $a_firewall ]] || { |
| 134 | echo "$FUNCNAME: missing arg: a_firewall" 1>&2 |
| 135 | return 1 |
| 136 | } |
| 137 | |
| 138 | gcloud --project $project --quiet compute firewall-rules delete $a_firewall |
| 139 | } |
| 140 | |
| 141 | # has_network checks if a project contains a named network |
| 142 | # |
| 143 | # has_network <project> <checked_network> |
| 144 | has_network() { |
| 145 | local project=$1 |
| 146 | [[ -n $project ]] || { |
| 147 | echo "$FUNCNAME: missing arg: project" 1>&2 |
| 148 | return 1 |
| 149 | } |
| 150 | local checked_network=$2 |
| 151 | [[ -n $checked_network ]] || { |
| 152 | echo "$FUNCNAME: missing arg: checked_network" 1>&2 |
| 153 | return 1 |
| 154 | } |
| 155 | |
| 156 | instances=$(gcloud --project $project compute networks list \ |
| 157 | | sed -e 's/ \+/ /g' | cut -d' ' -f 1) |
| 158 | for i in $instances |
| 159 | do |
| 160 | if [[ $i == $checked_network ]] |
| 161 | then |
| 162 | return 0 |
| 163 | fi |
| 164 | done |
| 165 | |
| 166 | return 1 |
| 167 | } |
| 168 | |
| 169 | # maybe_setup_dev_network adds a network with the given name with firewalls |
| 170 | # useful to development |
| 171 | # |
| 172 | # - All machines can accessed internally and externally over SSH (port 22) |
| 173 | # - All machines can access one another other the internal network |
| 174 | # - All machines can be accessed externally via port 80, 443, 8080 and 8443 |
| 175 | maybe_setup_dev_network() { |
| 176 | local name=$1 |
| 177 | [[ -n $name ]] || { |
| 178 | echo "$FUNCNAME: missing arg: network name" 1>&2 |
| 179 | return 1 |
| 180 | } |
| 181 | |
| 182 | local project=$2 |
| 183 | [[ -n $project ]] || { |
| 184 | echo "$FUNCNAME: missing arg: project" 1>&2 |
| 185 | return 1 |
| 186 | } |
| 187 | |
| 188 | has_network $project $name || { |
| 189 | echo "creating network '$name'" 1>&2 |
| 190 | gcloud compute --project $project networks create $name || return 1 |
| 191 | } |
| 192 | |
| 193 | # allow instances on the network to connect to each other internally |
| 194 | has_firewall $project "$name-ssh" || { |
| 195 | echo "adding firewall '$name-ssh'" 1>&2 |
| 196 | gcloud compute --project $project firewall-rules create "$name-ssh" \ |
| 197 | --network $name \ |
| 198 | --allow tcp:22 || return 1; |
| 199 | } |
| 200 | |
| 201 | # allow instances on the network to connect to each other internally |
| 202 | has_firewall $project "$name-internal" || { |
| 203 | echo "adding firewall '$name-internal'" 1>&2 |
| 204 | gcloud compute --project $project firewall-rules create "$name-internal" \ |
| 205 | --network $name \ |
| 206 | --source-ranges 10.0.0.0/16 --allow tcp udp icmp || return 1; |
| 207 | } |
| 208 | |
| 209 | # allow instances on the network to be connected to from external ips on |
| 210 | # specific ports |
| 211 | has_firewall $project "$name-external" || { |
| 212 | echo "adding firewall '$name-external'" 1>&2 |
| 213 | gcloud compute --project $project firewall-rules create "$name-external" \ |
| 214 | --network $name \ |
| 215 | --allow tcp:80 tcp:8080 tcp:443 tcp:8443 || return 1; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | # maybe_remove_dev_network removes a network set up by maybe_setup_dev_network |
| 220 | maybe_remove_dev_network() { |
| 221 | local name=$1 |
| 222 | [[ -n $name ]] || { |
| 223 | echo "$FUNCNAME: missing arg: network name" 1>&2 |
| 224 | return 1 |
| 225 | } |
| 226 | |
| 227 | local project=$2 |
| 228 | [[ -n $project ]] || { |
| 229 | echo "$FUNCNAME: missing arg: project" 1>&2 |
| 230 | return 1 |
| 231 | } |
| 232 | |
| 233 | has_network $project $name || { |
| 234 | echo "network $name is not present" |
| 235 | return 0 |
| 236 | } |
| 237 | for i in $(gcloud compute firewall-rules list \ |
| 238 | | grep "$name-" | cut -d' ' -f 1) |
| 239 | do |
| 240 | gcloud compute --quiet firewall-rules delete $i || return 1; |
| 241 | done |
| 242 | gcloud compute --quiet networks delete $name |
| 243 | } |
| 244 | |
| 245 | # find_named_ip finds the external ip address for a given name. |
| 246 | # |
| 247 | # find_named_ip <named-ip-address> |
| 248 | find_named_ip() { |
| 249 | local name=$1 |
| 250 | [[ -n $name ]] || { echo "$FUNCNAME: missing arg: name" 1>&2; return 1; } |
| 251 | [[ $name == 'none' ]] && return 0; |
| 252 | |
| 253 | gcloud compute addresses list | sed -e 's/ \+/ /g' \ |
| 254 | | grep $name | cut -d' ' -f 3 |
| 255 | } |