Skip to content

Instantly share code, notes, and snippets.

View urjitbhatia's full-sized avatar

Urjit Singh Bhatia urjitbhatia

View GitHub Profile
@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 / 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 / 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 / cf_log_parse_awk.sh
Created September 12, 2019 00:55
Parse CloudFront logs via awk
#!/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 }'
@urjitbhatia
urjitbhatia / consulLock.sh
Last active February 7, 2020 23:43
Consul Lock/Unlock with timeout
#!/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 / wait-for-dependencies.sh
Created February 29, 2020 00:24
Wait for process dependencies
#!/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 / datadog_events.sh
Created June 11, 2020 19:23
Examples of sending datadog events via commandline
# 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 / container_signal_dockersock.sh
Created June 11, 2020 19:26
Send signal to docker container via docker socket from commandline script
# 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 / 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
}
@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; \