Skip to content

Instantly share code, notes, and snippets.

View chrisdpa-tvx's full-sized avatar

Chris Atkinson chrisdpa-tvx

View GitHub Profile
@chrisdpa-tvx
chrisdpa-tvx / dos2unix.md
Last active May 23, 2017 15:28
Convert between UNIX and DOS line endings
@chrisdpa-tvx
chrisdpa-tvx / gnupg.py
Created May 31, 2017 10:57
Encrypt+Sign then decrypt using gpg
import gnupg
import cStringIO
gpg = gnupg.GPG()
gpg.encoding = 'utf-8'
me = {'name_real': 'alice',
'name_email': 'alice@example.com',
'expire_date': '2024-04-01',
'passphrase': 'alicespassword'}
@chrisdpa-tvx
chrisdpa-tvx / ecs_sad.sh
Last active July 24, 2017 09:38
Find Unhappy services
CLUSTER=$1
sts aws ecs list-services --cluster ${CLUSTER} |\
jq -r '.[] | .[]' | xargs -J % -n10 sts \
aws ecs describe-services --cluster ${CLUSTER} \
--services % | jq -r '.services[] | select(.desiredCount!=.runningCount) | .serviceArn' > services
while read r;do
sts aws ecs update-service \
--cluster ${CLUSTER} \
--service ${r} \
@chrisdpa-tvx
chrisdpa-tvx / cluster_images.sh
Created July 26, 2017 14:00
Drill to docker image from a cluster
CLUSTER=$1
aws ecs list-services --cluster ${CLUSTER} | jq -r '.[] | .[]' | \
xargs -J % -n10 aws ecs describe-services --cluster ${CLUSTER} --services % | jq -r '.services[] | .serviceArn' | \
xargs -J % -n10 aws ecs describe-services --cluster ${CLUSTER} --services % | jq -r '.services[]|.taskDefinition' | \
xargs -J % -n1 aws ecs describe-task-definition --task-definition % | jq -r '.taskDefinition|.containerDefinitions[]|.image'
set -exu
SPOTFLEETREQ=$1
CLUSTER=$2
AZ=$3
fleet_sz=$(aws ec2 describe-spot-fleet-instances --spot-fleet-request-id ${SPOTFLEETREQ} | jq -r '.ActiveInstances|length')
container_sz=$(aws ecs list-container-instances --cluster ${CLUSTER} | jq -r '.containerInstanceArns[]' | \
xargs -n10 -J % aws ecs describe-container-instances --cluster ${CLUSTER} --container-instances % | \
jq -r ".containerInstances[].attributes[]|select(.name==\"ecs.availability-zone\" and .value==\"${AZ}\")|.value"|wc -l | xargs)
@chrisdpa-tvx
chrisdpa-tvx / kms encrypt-decrypt of text or file
Last active September 14, 2017 11:09
Encrypt a file using KMS keys
aws kms create-key
# Encrypt the contents of the file
aws kms encrypt \
--key-id ${key_id_from_create_key_step} \
--plaintext fileb://super_secret_file \
--output text \
--query CiphertextBlob > super_secret_file.enc.b64
# Decrypt the contents of the file
@chrisdpa-tvx
chrisdpa-tvx / remove_volumes.sh
Last active October 18, 2017 15:19
Remove unattached volumes
# Remove any volumes that are not attached to an AWS instance
aws ec2 describe-volumes | \
jq -r '.Volumes[] | select( (.Attachments|length)==0 ) | .VolumeId ' | \
xargs -J % -n 1 aws ec2 delete-volume --volume-id %
# Remove DB snapshots that are older than a month
aws rds describe-db-snapshots | \
jq -r ".DBSnapshots[] | select( (.SnapshotCreateTime<\"$(date -v-1m -u +%Y-%m-%dT%H:%M:%S.000Z)\") and (.SnapshotType==\"manual\")) | .DBSnapshotIdentifier" | \
xargs -J % -n 1 sts aws rds delete-db-snapshot --db-snapshot-identifier %
See netstat detail of a running docker container:
nsenter -t $(docker inspect -f '{{.State.Pid}}' _container_id_) -n netstat -tunapp
@chrisdpa-tvx
chrisdpa-tvx / sendmail.py
Created March 12, 2018 15:04
Send Mail using docker as a relay
#!/usr/bin/env python3
# Start a mail relay using docker:
#
# docker run -p 25:25 namshi/smtp
#
import smtplib
from email.message import EmailMessage
from email.headerregistry import Address
@chrisdpa-tvx
chrisdpa-tvx / Resize-PartitionMax.ps1
Created March 27, 2018 11:10
AWS Resize a windows volume
Resize-Partition -DiskNumber 3 -PartitionNumber 2 -Size $((get-disk -Number 3 | Get-PartitionSupportedSize -PartitionNumber 2).sizemax)