Skip to content

Instantly share code, notes, and snippets.

View urjitbhatia's full-sized avatar

Urjit Singh Bhatia urjitbhatia

View GitHub Profile
@urjitbhatia
urjitbhatia / getec2lifecycle.sh
Created May 3, 2019 18:52
Check if an ec2 instance is spot or normal lifecycle
#!/bin/bash
aws ec2 describe-spot-instance-requests \
--filters Name=instance-id,Values="$(wget -q -O - http://169.254.169.254/latest/meta-data/instance-id)" \
--region us-east-1 | \
jq -r '.SpotInstanceRequests | if length > 0 then "spot" else "normal" end'
@urjitbhatia
urjitbhatia / nomadTerminator.sh
Last active December 23, 2021 18:38
Drain a nomad EC2 spot instance node on termination
#!/bin/bash
CMD="curl --write-out %{http_code} --silent --output /dev/null http://169.254.169.254/latest/meta-data/spot/termination-time"
while true
do
if [ "$(${CMD})" != "404" ]; then
# 2 minute warning received. Do all your cleanup work.
echo "2 minute termination warning. Draining nomad node..."
nomad node-drain -yes -self -enable
echo "Hasta la vista baby, I will be back"
break
@urjitbhatia
urjitbhatia / dnsquerylogger.sh
Last active May 19, 2021 18:52
easy way to continuously log dns queries
#!/bin/bash
# replace any with a specific interface you want to monitor or just use any to capture all interfaces
tcpdump -i any 'dst port 53' >> /var/log/dnsqueries.log
#### If using supervisord, use this template for setting up the program entry:
# [program:dnsworker]
# command=bash dnsquerylogger.sh
# autostart=true
# autorestart=true
@urjitbhatia
urjitbhatia / ptonline.sh
Created April 21, 2021 23:16
pt-online-schema-change
# Command to run pt online schema change
pt-online-schema-change \
D=<dbname>,t=<table_name>,h=<hostname>,u=<username> \
--alter="<alter sql>" \
--ask-pass \
--execute \
--max-load 100 \
--critical-load 120
@urjitbhatia
urjitbhatia / mysql_delete_chunked.sql
Created April 20, 2021 00:56
Aurora mysql delete in chunk
--- Simple table to keep track of delete chunks
create table _delete_log
(
log varchar(200) null,
createdAt timestamp null
);
------------ Stored proc definition ---------------
DROP PROCEDURE IF EXISTS delete_chunks;
DELIMITER $$
@urjitbhatia
urjitbhatia / custom_download_strategy.rb
Created January 18, 2019 22:14
A custom vendored github private release homebrew tap formula
require "json"
require "rexml/document"
require "time"
require "unpack_strategy"
require "lazy_object"
require "cgi"
class AbstractDownloadStrategy
extend Forwardable
include FileUtils
@urjitbhatia
urjitbhatia / bash_debug.sh
Created August 25, 2019 02:28
Bash debug trick
#!/bin/bash
# exit when any command fails
set -e
current_command=$BASH_COMMAND
last_command=""
# keep track of the last executed command
trap 'last_command=$current_command; current_command=$BASH_COMMAND' DEBUG
@urjitbhatia
urjitbhatia / get-dind-sibling-ip
Created August 14, 2020 00:53
Fetch the ip address of a sibling container from a docker-in-docker run
$(docker inspect my-sibling-dind-container --format '{{ .NetworkSettings.IPAddress }}')
-- Make sure both run on the same "network" (bridge by default in most cases)
@urjitbhatia
urjitbhatia / makefile
Last active August 12, 2020 01:49
make recipe to tag docker images with git tags
### example for setting up a make workflow that can tag images based on the git tag
### the trailing ; are important: runs the commands in the same "shell" and the $$tag variable flows through
docker-build:
@echo "Latest 3 tags: "; \
git ls-remote --sort='v:refname' --tags ./. | tail -n 3; \
read -p "Enter New Tag:" tag; \
echo "Releasing new tag: $$tag"; \
git tag $$tag; \
git push origin $$tag -f; \
@urjitbhatia
urjitbhatia / bash_trap_example.sh
Last active June 11, 2020 19:31
Catching and throwing errors in bash
#!/usr/bin/env bash
###### ERROR TRAP - DONT WRITE OTHER COMMANDS BEFORE THIS ############
set -eEx
function HANDLE_ERROR() {
echo "SCRIPT FAILED"
echo "FAILED AT: $(caller)"
# Do any other commands here
exit 1
}