Skip to content

Instantly share code, notes, and snippets.

View bwhaley's full-sized avatar
🐋

Ben Whaley bwhaley

🐋
View GitHub Profile
@bwhaley
bwhaley / gist:3af5e5870d0e42948757957febf8ea60
Created April 11, 2024 23:57
Google Apps scripts that adds a timestamp to a column
'use strict';
// Adds a timestamp to the LAST_MODIFIED_COLUMN whenever the value of any of the preceding columns has changed.
// Deletes the timestamp if all preceding columns are blank.
function onEdit(e) {
if (!e) {
throw new Error('Please do not run the script in the script editor window. It runs automatically when you edit the spreadsheet.');
}
const row = e.range.getRow();
const col = e.range.getColumn();
set nocompatible
set hidden " Hide buffers rather than close them
set confirm
set encoding=utf-8
set showcmd " display incomplete commands
set number " show line numbers
set wrap linebreak nolist " wrap lines
set cursorline " highlight current row
set cursorcolumn " highlight current column
"set tags=tags
@bwhaley
bwhaley / aws_cleanup.py
Last active June 11, 2023 13:47
Quick and dirty script to clean up various types of resources in an AWS account
#!/usr/bin/env python3
import argparse
import boto3
# Cleanup misc AWS resources
# NOTE: this script does not currently handle pagination
def get_regions():
ec2 = boto3.client("ec2")
@bwhaley
bwhaley / gist:7383803e1dec6bff2ea05d8af634d5cb
Created December 4, 2019 06:34
rolling deployment method
"""Put ECS Instances in DRAINING state so that all ECS Tasks running on them are migrated to other Instances.
Batches into chunks of 10 because of AWS api limitations (An error occurred InvalidParameterException when
calling the UpdateContainerInstancesState operation: instanceIds can have at most 10 items)
"""
def put_container_instances_in_draining_state(ecs_client, cluster_name, container_instance_arns):
batch_size = 10
n_batches = math.ceil(len(container_instance_arns)/batch_size)
for i in range(0, len(container_instance_arns), batch_size):
logger.info('Putting batch %d/%d of container instances %s in cluster %s into DRAINING state', i+1, n_batches, container_instance_arns, cluster_name)
ecs_client.update_container_instances_state(cluster=cluster_name, containerInstances=container_instance_arns[i:i + batch_size], status='DRAINING')
@bwhaley
bwhaley / cloudwatch_log_subscriber.py
Created January 3, 2018 01:27
AWS lambda function to subscribe new CloudWatch Log groups to another lambda function
# Lambda function to subscribe all new cloudwatch log groups to a log shipper function
# Used in conjunction with https://github.com/SumoLogic/sumologic-aws-lambda
import os
import logging
import json
import uuid
import boto3
from botocore.exceptions import ClientError
@bwhaley
bwhaley / cassandra-rolling-restart.yml
Created October 30, 2017 22:33
Ansible playbook for graceful rolling restart of the nodes in a cassandra cluster
- hosts: "*"
become: True
serial: 1
tasks:
- name: Initiate graceful shutdown
shell: "nodetool {{item}} && sleep 5"
with_items:
- disablethrift
- disablebinary
- disablegossip
sh ‘’’#!/bin/bash -l
cp do_image.txt pipeline/testing
cd pipeline/testing
terraform apply \\
-var do_image=\”\$(<do_image.txt)\” \\
-var do_token=\”\${DO_TOKEN}\” \\
-var ssh_fingerprint=\”\${SSH_FINGERPRINT}\”
terraform show terraform.tfstate \\
| grep ipv4_address | awk \”{print \$3}\” > ../../do_ip.txt
‘’’
@bwhaley
bwhaley / peer_routes.py
Last active October 6, 2021 17:18
A script to configure routes in in a VPC peering connection
# Quick script to configure routes for a VPC peering connection
# Searches a region for all peering connection and prompts to choose one
# Configures routes between the peered networks for all routing tables
# STS/AssumeRole not implemented cross-account peering. Instead,
# Choose accepter/requestor depending on which credentials are set in the environment
# Enter either IPv4 and IPv6 route destinations
# Example usage:
# ( Assuming boto credentials are configured)
# $ pip install boto3
# $ python3.6 peer_routes.py
@bwhaley
bwhaley / kms_policy.json
Created April 14, 2016 16:43
AWS KMS policy
{
"Version": "2012-10-17",
"Id": "cassandra-key-policy",
"Statement": [
{
"Sid": "Enable IAM User Permissions",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::09876512345:root"
},
@bwhaley
bwhaley / rds-logs.sh
Created March 18, 2016 18:50
bash script to read RDS logs
#!/usr/bin/env/bash
$db = $1
for logfile in $(aws rds describe-db-log-files --db-instance-identifier $db | jq '.DescribeDBLogFiles|.[]|.LogFileName')
do
aws rds download-db-log-file-portion --db-instance-identifier $db --log-file-name ${logfile} --starting-token 0 --output text >> $db-logs.txt
done