Skip to content

Instantly share code, notes, and snippets.

View atheiman's full-sized avatar
😬

Austin Heiman atheiman

😬
View GitHub Profile
@atheiman
atheiman / install.sh
Created June 26, 2016 02:28 — forked from loren/install.sh
Chef install script with retry logic on dpkg to get around race with unattended upgrades
#!/bin/sh
# WARNING: REQUIRES /bin/sh
#
# - must run on /bin/sh on solaris 9
# - must run on /bin/sh on AIX 6.x
#
# Copyright:: Copyright (c) 2010-2015 Chef Software, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
gfixup() {
set -x
git add --all
git commit --fixup=$(git log --oneline | head -1 | cut -d ' ' -f 1)
git rebase --interactive HEAD~2 --autosquash
set +x
local branch_name=$(git symbolic-ref -q HEAD)
branch_name=${branch_name##refs/heads/}
branch_name=${branch_name:-HEAD}
@atheiman
atheiman / docker_run_nginx.sh
Last active February 11, 2024 01:28
Run nginx in docker container to serve PWD
# Run nginx docker container in background
docker run --name nginx --rm -d -p 8080:80 -v ${PWD}:/usr/share/nginx/html:ro nginx
# Load a file in curent directory
curl http://localhost:8080/index.html
# Stop the docker container (the container will be removed / cleaned up)
docker stop nginx
@atheiman
atheiman / export-files-job.yaml
Last active February 11, 2024 01:28
Generate job artifacts in an initContainer and export the files to workstation afterwards.
# Allows copying of job files to local after execution. Example copy command:
#
# kubectl apply -f ./export-file-job.yaml
# POD=$(kubectl get pod --selector=job-name=export-files -o jsonpath='{.items[0].metadata.name}')
# kubectl wait pod/$POD --for=condition=ready
# until kubectl logs $POD | grep 'Ready for download'; do sleep 2; done
# kubectl cp $POD:files.zip ./job-files-$(date +"%Y%m%d%H%M").zip
#
# Another option would be for the `export-files` container to be a webserver to
# serve of the artifact files to be downloaded with `kubectl port-forward ...`.
@atheiman
atheiman / user_data.sh
Created October 22, 2019 01:44
ec2 user_data snippets
# get instance attrs
INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
PRIVATE_IP=$(curl -s http://169.254.169.254/latest/meta-data/local-ipv4)
# get asg name
ASG_NAME=$(aws autoscaling describe-auto-scaling-instances --region us-east-1 --instance-ids $INSTANCE_ID --query 'AutoScalingInstances[0].AutoScalingGroupName' --output text)
# get tags
aws ec2 describe-tags --region us-east-1 --filters "Name=resource-id,Values=${INSTANCE_ID}"
@atheiman
atheiman / lambda.py
Created August 14, 2020 06:21
Delete SQS message after processing in a Lambda function using Python Boto3
def delete_message(message):
q_region, q_acct_id, q_name = message['eventSourceARN'].split(':')[3:6]
sqs = boto3.client('sqs', region_name=q_region)
q_url = f'https://{q_region}.queue.amazonaws.com/{q_acct_id}/{q_name}'
sqs.delete_message(QueueUrl=q_url, ReceiptHandle=message['receiptHandle'])
@atheiman
atheiman / stackSetOperationWait.sh
Last active February 11, 2024 01:27
Shell function to wait on CloudFormation StackSet operations in CI/CD pipelines
# Example usage:
# OPERATION_ID="$(aws cloudformation create-stack-instances --stack-set-name MyStackSet \
# --deployment-targets "OrganizationalUnitIds=ou-ab12-abcd1234" \
# --regions us-east-1 \
# --output text)"
# stackSetOperationWait MyStackSet "${OPERATION_ID}"
function stackSetOperationWait {
local cmd="aws cloudformation describe-stack-set-operation --stack-set-name ${1?} --operation-id ${2?}"
# print out the operation being performed
@atheiman
atheiman / getCfnExport.sh
Created September 2, 2020 10:54
Get CloudFormation export shell function
# Example usage:
#
# $ getCfnExport MyStack-RoleArn
# arn:aws:iam:us-east-1:111111111111:TheExportedRole
#
function getCfnExport {
local EXPORT_VALUE="$(aws cloudformation list-exports --output text --query "Exports[?Name=='${1?}'].Value")"
if [ -z "$EXPORT_VALUE" ]; then
echo "Could not find CloudFormation export '${1}'" 1>&2
@atheiman
atheiman / ssm_offline_instances.py
Created September 8, 2020 22:59
Dump EC2 instances that do not have SSM PingStatus of 'Online'
import boto3
import json
region = "us-gov-west-1"
all_instance_ids = [
i.id for i in boto3.resource("ec2", region_name=region).instances.all()
]
ssm_inst_paginator = boto3.client("ssm", region_name=region).get_paginator(
@atheiman
atheiman / SsmCommandDocument-RunScript.yml
Last active February 11, 2024 01:26
SSM Command document to run a Powershell command on Windows instances and a shell command on Linux instances. The commands are hard-coded, but could be moved to input parameters.
# SSM Command document to run a Powershell command on Windows instances and a shell command
# on Linux instances. The commands are hard-coded, but could be moved to input parameters.
schemaVersion: "2.2"
description: Run a PowerShell command on Windows instances and a shell command on Linux instances
mainSteps:
- precondition:
StringEquals: [platformType, Windows]
action: "aws:runPowerShellScript"
name: runPowerShellScript