Skip to content

Instantly share code, notes, and snippets.

Avatar

Urjit Singh Bhatia urjitbhatia

View GitHub Profile
@urjitbhatia
urjitbhatia / ptonline.sh
Created Apr 21, 2021
pt-online-schema-change
View ptonline.sh
# 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 Apr 20, 2021
Aurora mysql delete in chunk
View mysql_delete_chunked.sql
--- 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 / get-dind-sibling-ip
Created Aug 14, 2020
Fetch the ip address of a sibling container from a docker-in-docker run
View get-dind-sibling-ip
$(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 Aug 12, 2020
make recipe to tag docker images with git tags
View makefile
### 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 Jun 11, 2020
Catching and throwing errors in bash
View bash_trap_example.sh
#!/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
}
@urjitbhatia
urjitbhatia / container_signal_dockersock.sh
Created Jun 11, 2020
Send signal to docker container via docker socket from commandline script
View container_signal_dockersock.sh
# This script will send a signal to a container
# Sleep for a random time b/w 1 to 3 seconds to avoid restarting at the exact same time
# This bc format of finding random doesn't print scientific notation which sometimes sleep doesn't like a lot
/bin/sleep "$(echo "scale=3; 0.5 + 2.5 * $RANDOM / 32767" | bc)s"
## Running in silent curl mode (-s and > /dev/null) - remember while debugging
if curl --silent --show-error --output /dev/null --unix-socket /var/run/docker.sock -X POST "http/containers/${CONTAINER_ID}/kill?signal=SIGUSR2" ; then
echo "Successfully sent signal to $CONTAINER_ID"
else
echo "Failed to send signal to $CONTAINER_ID. Triggering alarms."
@urjitbhatia
urjitbhatia / datadog_events.sh
Created Jun 11, 2020
Examples of sending datadog events via commandline
View datadog_events.sh
# https://help.datadoghq.com/hc/en-us/articles/206441345-Send-metrics-and-events-using-dogstatsd-and-the-shell
# Send an event to DD based on success or failure
echo "_e{${#title},${#text}}:$title|$text|t:${alert_type}" | nc -u -w1 "${HOST_IP}" 8125
if [ "$CMD_EXIT_CODE" -eq 0 ];
echo "foo.bar.success:1|g" | nc -u -w1 "${HOST_IP}" 8125
then
echo "foo.bar.failure:1|g" | nc -u -w1 "${HOST_IP}" 8125
fi
@urjitbhatia
urjitbhatia / wait-for-dependencies.sh
Created Feb 29, 2020
Wait for process dependencies
View wait-for-dependencies.sh
#!/bin/bash
######################################################################
# Waits for Mongodb, Redis and Mysql to be ready
# usage examples:
# wait-for-dependencies.sh echo "READY TO GO"
# wait-for-dependencies.sh make build
# Exit Codes: 0 for success, 2 if wait command timed out
######################################################################
@urjitbhatia
urjitbhatia / consulLock.sh
Last active Feb 7, 2020
Consul Lock/Unlock with timeout
View consulLock.sh
#!/bin/bash
set -e
export CONSUL_HTTP_ADDR="https://my-consul-address"
VALUE=$(consul kv get <key>)
if [ "$VALUE" != "locked" ]; then
# Unlocked, check the timeout
TIMEOUT=$(echo $VALUE | jq 'reduce .[] as $num (0; .+$num)')
@urjitbhatia
urjitbhatia / cf_log_parse_awk.sh
Created Sep 12, 2019
Parse CloudFront logs via awk
View cf_log_parse_awk.sh
#!/bin/bash
HTTP_STATUS_CODE=421
CF_DISTRIBUTION_NAME=foobar
s3cmd "s3://${S3_BUCKET}/${LOGS_PREFIX}"
## OR s3cmd "s3://${S3_BUCKET}/${LOGS_PREFIX}/${CF_DISTRIBUTION_NAME}" to download only specific distributions
zcat *.gz | awk 'match($9, /421/) && match($7, /foobar/) { print $7" "$9" "$16" "$10 }'