Skip to content

Instantly share code, notes, and snippets.

View urjitbhatia's full-sized avatar

Urjit Singh Bhatia urjitbhatia

View GitHub Profile
@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 / 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 / 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 / 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 / getIfaceIP.sh
Created May 4, 2018 22:33
Get the current external facing interface ip
ip route get 8.8.8.8 | awk '{print $NF; exit}'
@urjitbhatia
urjitbhatia / Send docker image over ssh
Created April 2, 2018 00:40
One liner to send docker image to a remote host over ssh
docker save <image> | bzip2 | pv | ssh user@host 'bunzip2 | docker load'
## Credits: https://stackoverflow.com/questions/23935141/how-to-copy-docker-images-from-one-host-to-another-without-via-repository
@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 / haproxy_cfg.conf
Created March 29, 2018 21:52
haproxy send a percentage of traffic
# send 10% traffic
http-request set-header Host foo.bar.com if { rand(10) eq 0 }
@urjitbhatia
urjitbhatia / ramdisk.sh
Created June 10, 2017 06:57 — forked from rxin/ramdisk.sh
ramdisk create/delete on Mac OS X.
#!/bin/bash
# From http://tech.serbinn.net/2010/shell-script-to-create-ramdisk-on-mac-os-x/
#
ARGS=2
E_BADARGS=99
if [ $# -ne $ARGS ] # correct number of arguments to the script;
then
@urjitbhatia
urjitbhatia / dockerce_dockercompose_install_ubuntu14.sh
Last active August 9, 2017 01:34
one script to install docker-ce and docker-compose on ubuntu 14
#/bin/bash
sudo apt-get update
sudo apt-get install -y \
linux-image-extra-$(uname -r) \
linux-image-extra-virtual
sudo apt-get install -y \
apt-transport-https \