Skip to content

Instantly share code, notes, and snippets.

@kholisrag
Last active August 26, 2024 10:38
Show Gist options
  • Save kholisrag/d4333041bfc41185ecce174d5d84c192 to your computer and use it in GitHub Desktop.
Save kholisrag/d4333041bfc41185ecce174d5d84c192 to your computer and use it in GitHub Desktop.
bash script to update disk labels based on yaml input
#!/bin/bash
set -e
# YAML config file path
CONFIG_FILE="./x-disks-labels.yaml"
while read disk_name <&3
do
echo "Disk: $disk_name"
while read disk_zone <&4 && \
read disk_labels <&5 && \
read disk_project <&6
do
echo "Zone: $disk_zone"
echo "Labels: $disk_labels"
if [[ ! -z "$disk_project" ]]; then
echo "Project: $disk_project"
gcloud --project $disk_project compute disks update "$disk_name" \
--zone="$disk_zone" \
--update-labels="$disk_labels"
else
# Apply the labels to the disk
gcloud compute disks update "$disk_name" \
--zone="$disk_zone" \
--update-labels="$disk_labels"
fi
done 4< <(yq eval '.disks[] | select(.name == "'$disk_name'") | .zone' "$CONFIG_FILE") \
5< <(yq eval '.disks[] | select(.name == "'$disk_name'") | .labels' "$CONFIG_FILE" | yq eval 'to_entries | map(.key + "=" + .value) | join(",")') \
6< <(yq eval '.disks[] | select(.name == "'$disk_name'") | .project' "$CONFIG_FILE")
done 3< <(yq eval '.disks[].name' "$CONFIG_FILE")
disks:
- name: disk1
zone: asia-southeast1-a
labels:
team: "sre"
env: "prod"
environment: "prod"
component: "test"
application: "test"
system: "test"
service: "test"
cluster: "test"
- name: disk2
zone: asia-southeast1-b
project: xxx
labels:
team: "sre"
env: "stage"
environment: "stage"
component: "stage-test"
application: "stage-test"
system: "stage-test"
service: "stage-test"
cluster: "stage-test"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment