Welcome
My name is Ulysses. Sr. Cloud Engineer at Cardinal Health.
About This Website
This is my static site based on Hugo.
My name is Ulysses. Sr. Cloud Engineer at Cardinal Health.
This is my static site based on Hugo.
Here’s how to add labels to snapshots. It will cycle through from a list of disks. #!/bin/bash ts=$(date +%Y%m%d-%H%M%S) log="add-labels-logs-$ts" >$log disks='manual-disks.txt' now=$(date +%s) epoch=$(date +%s -d "+14 days") while read -r project snapshot; do echo "Project: $project" gcloud compute snapshots add-labels $snapshot --labels=delete-after=$epoch --project $project done
Here’s an example of how to restore all the disks of a GCP VM from snapshots. #!/bin/bash # Set variables PROJECT_ID="your-project-id" INSTANCE_NAME="your-instance-name" # Get list of disks attached to the instance DISKS=$(gcloud compute instances describe ${INSTANCE_NAME} \ --project ${PROJECT_ID} \ --format="value(disks.deviceName)") # Loop through each disk and restore from most recent snapshot for DISK in $DISKS do # Get most recent snapshot for the disk SNAPSHOT=$(gcloud compute snapshots list \ --project ${PROJECT_ID} \ --filter="sourceDisk=${INSTANCE_NAME}/${DISK} AND status=READY" \ --sort-by="~creationTimestamp" \ --format="value(name)" | head -n 1) # Restore the disk from the snapshot gcloud compute disks create ${DISK} \ --project ${PROJECT_ID} \ --source-snapshot ${SNAPSHOT} \ --zone=us-central1-a \ --labels restored-from=${SNAPSHOT} # Attach the disk to the instance gcloud compute instances attach-disk ${INSTANCE_NAME} \ --disk=${DISK} \ --project ${PROJECT_ID} \ --zone=us-central1-a done
Here’s how to read a file line by line with multiple columns. with open('file.txt') as file: for line in file: line = line.strip() columns = line.split() col1 = columns[0] col2 = columns[1] some_function(col1, col2)
The sed utility on a Mac OS running Z shell or zsh is a little different compared to gnu’s sed. Install gnu sed on Mac OS. brew install gnu-sed Use gsed instead of sed. gsed --version
Here’s the Terraform code that creates an AWS IAM policy and attaches it to an existing role (ROLENAME). # CREDENTIALS provider "aws" { shared_credentials_files = ["~/.aws/credentials"] shared_config_files = ["~/.aws/config"] profile = "default" region = "us-east-1" } # CREATE POLICY resource "aws_iam_policy" "s3-policy" { name = "s3-policy" description = "Allow role to write to bucket" policy = "${file("policy.json")}" } # ATTACH POLICY TO ROLE resource "aws_iam_role_policy_attachment" "s3-policy-attachment" { role = "ROLENAME" policy_arn = aws_iam_policy.s3-policy.arn } Here’s the policy.json file. ...
Here’s the command to add labels to a snapshot. gcloud compute snapshots add-labels SNAPSHOTNAME --labels=KEY=VALUE --project PROJECT
Here’s a handy little script that has multiple applications to calculate date from a year ago. #!/bin/bash now=$(date +%Y%m%d-%H%M%S) # mac os year_old=$(date -v-1y +%Y%m%d-%H%M%S) # linux os year_old=$(date -d "-1 year" +%Y%m%d-%H%M%S) echo $now echo $year_old Mac Os and Linux has slightly different formats. Pick one based on your OS. Results 20230618-084940 20220618-084940
Here’s how to list snapshots older than a certain date. In this example, it’s snapshots older than 2023-12-14. gcloud compute snapshots list --project $project --filter="creationTimestamp < 2023-12-14" --format="get(name,creationTimestamp)"
Here’s how to make a hostname stick on Amazon Linux. Set preserve_hostname to true in /etc/cloud/cloud.cfg. preserve_hostname: true Set the hostname in /etc/sysconfig/network. HOSTNAME=server.domain.com NETWORKING=yes NOZEROCONF=yes Reboot the server to make sure hostname sticks after a reboot. shutdown -r now Login to confirm hostname sticks.
Here’s how to change a VM’s service account in GCP #!/bin/bash read -p "project : " project read -p "instance : " instance read -p "srvAcct email : " serviceaccount # stop instance gcloud compute instances stop $instance --project $project # change service account gcloud compute instances set-service-account $instance --service-account $serviceaccount --scopes cloud-platform --project $project # start instance gcloud compute instances start $instance --project $project