Skip to content

Instantly share code, notes, and snippets.

View jamesridgway's full-sized avatar

James Ridgway jamesridgway

View GitHub Profile
@jamesridgway
jamesridgway / index.js
Last active April 4, 2020 16:02
Cloudflare worker for fronting Ghost(Pro) and applying security headers
addEventListener('fetch', event => {
event.respondWith(createResponse(event.request))
})
async function createResponse(req) {
let domain = new URL(req.url).hostname.toString();
if (domain !== 'www.jamesridgway.co.uk') {
let redirectHeaders = new Headers()
redirectHeaders.set('Location', 'https://www.jamesridgway.co.uk')
return new Response('', {
@jamesridgway
jamesridgway / inherit-iam-creds.sh
Created May 23, 2019 07:31
Use the AWS Metadata API to extract the IAM Role credentials and generate a traditional AWS config file.
#!/bin/bash
#
# Use the AWS Metadata API to extract the IAM Role credentials and generate a traditional AWS config file.
#
set -e
if [ -z "$1" ]; then
PROFILE_NAME="default"
else
PROFILE_NAME="$1"
@jamesridgway
jamesridgway / associate-eip.sh
Last active April 4, 2019 19:40
Associate EIP with current EC2 instance based on EIP name
#!/bin/bash
# This works on the assumption that the EC2 instance has an IAM role that allows it to call describe-addresse
# and associate-address on the EC2 API
set -e
AWS_DEFAULT_REGION=$(curl -s http://169.254.169.254/latest/dynamic/instance-identity/document | jq -r ".region")
export AWS_DEFAULT_REGION
# TODO: Insert name in the filter argument
ADDRESS_DETAILS=$(aws ec2 describe-addresses --filters "Name=tag:Name,Values=INSERT_NAME_HERE" --query "Addresses[0]")
@jamesridgway
jamesridgway / eslint-stats.sh
Created March 19, 2019 13:34
Calculate total number of errors and warnings from an eslint json output
 jq 'def sum(f): reduce .[] as $row (0; . + ($row|f) );[.[] | {"errorCount": .errorCount, "warningCount": .warningCount}] | . as $in | reduce (.[0] | keys)[] as $key ( {}; . + {($key): ($in | sum(.[$key]))})' eslint.json
@jamesridgway
jamesridgway / LatestAmi.java
Created November 4, 2018 14:26
Find latest AMI based on a name filter
package uk.co.jamesridgway.java.scratchpad;
import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.AmazonEC2ClientBuilder;
import com.amazonaws.services.ec2.model.DescribeImagesRequest;
import com.amazonaws.services.ec2.model.DescribeImagesResult;
import com.amazonaws.services.ec2.model.Filter;
import com.amazonaws.services.ec2.model.Image;
public class LatestAmi {
@jamesridgway
jamesridgway / init.sh
Last active May 8, 2023 20:26
Packer Ubuntu 18.04 setup for Virtual Box and AWS AMI
set -e
echo "The user is: $SUDO_USER"
# System Updates
apt-get update
DEBIAN_FRONTEND=noninteractive apt-get dist-upgrade -yq
apt-get update
DEBIAN_FRONTEND=noninteractive apt-get upgrade -yq
salt-run manage.down removekeys=True
@jamesridgway
jamesridgway / normal_exposure.sh
Created September 7, 2018 06:03
Use exiftool to find CR2 files with a normal AEB Bracket Value
#!/bin/bash
# Can be used as follows to copy all files with a normal exposure to a 'normal_exposure' folder:
# ./normal_exposure.sh | xargs -I '{}' cp '{}' normal_exposure/
set -e pipefail
for file in *.CR2;
do
exiftool "$file" | grep -P "AEB Bracket Value\s+: 0" | true
if [ "${PIPESTATUS[1]}" -ne 1 ]; then
@jamesridgway
jamesridgway / nested_dict.py
Created August 11, 2018 11:09
Use dot notation to recursively lookup data attributes in a dict
"""
NestedDict wrapper
"""
class NestedDict(dict):
def __getitem__(self, key_namespace):
if "." not in key_namespace:
return super(NestedDict, self).__getitem__(key_namespace)
keys = key_namespace.split('.')
val = self
for key in keys:
@jamesridgway
jamesridgway / installed-jenkins-plugins.groovy
Created July 15, 2018 08:54
List installed Jenkins plugins
def instance = Jenkins.getInstance()
def pluginManager = instance.getPluginManager()
def plugins = pluginManager.getPlugins()
def installedPlugins = []
plugins.each {
installedPlugins << it.getShortName()
}