Skip to content

Instantly share code, notes, and snippets.

@pwillis-els
pwillis-els / example.sh
Last active March 11, 2019 16:36
Scrape and print the variables used in a shell script
cat foo.sh | perl -lne 'print $1 if /\$\{?([a-z0-9_]+)\}?/i' | sort -u
@pwillis-els
pwillis-els / get_keys_from_inst_metadata.sh
Created July 22, 2019 21:24
Get IAM access key from AWS EC2 instance profile metadata
#!/bin/sh
set -e
AWS_INST_PROFILE=$(curl -sL http://169.254.169.254/latest/meta-data/iam/security-credentials/ | tail)
curl -sL http://169.254.169.254/latest/meta-data/iam/security-credentials/${AWS_INST_PROFILE} \
| python -c 'import sys,json;j=json.load(sys.stdin);print("AWS_ACCESS_KEY_ID=%s\nAWS_SECRET_ACCESS_KEY=%s\n" % (j["AccessKeyId"],j["SecretAccessKey"]))'
@pwillis-els
pwillis-els / Fix-dot-jenkins-run-time-problem.md
Created September 20, 2019 14:56
Fix Jenkins to stop running from a ".jenkins" hidden directory

Fixing Jenkins to stop adding /.jenkins to your JENKINS_HOME path

When running Jenkins, you may try to set the JENKINS_HOME path that Jenkins uses at start-up, only to find the following output message:

Jenkins home directory: /var/jenkins_home/.jenkins found at: $user.home/.jenkins

You're trying to get Jenkins to read its files from /var/jenkins_home, but it keeps tacking on a /.jenkins hidden folder, and you can't get it to stop.

You've googled around and read all the documentation, but none of it describes how to fix this.

@pwillis-els
pwillis-els / install_newrelic_agent.sh
Created September 23, 2019 11:08
Install NewRelic agent on EC2
#!/bin/bash
if [ ! -n "$LICENSE_KEY" ] ; then
read -p "NewRelic license key: " LICENSE_KEY
fi
if [ -z "$LICENSE_KEY" ] ; then
echo "Error: failed to read license key" ; exit 1
fi
echo "license_key: $LICENSE_KEY" | sudo tee -a /etc/newrelic-infra.yml
if grep "Amazon Linux 2" /etc/os-release ; then
sudo curl -s -o /etc/yum.repos.d/newrelic-infra.repo https://download.newrelic.com/infrastructure_agent/linux/yum/el/7/x86_64/newrelic-infra.repo

SELECT (1-filter(uniqueCount(uuid), WHERE category='Crash') / uniqueCount(uuid)) * 100 as Crash Free FROM MobileSession, MobileCrash WHERE crashFingerprint NOT IN () where appName like 'PROD%' SINCE 1 week AGO LIMIT 1000

SELECT average(cpuPercent) AS '' FROM SystemSample FACET entityId SINCE 1 hour ago

SELECT average(databaseCallCount) FROM Transaction SINCE last month until this month COMPARE WITH 1 month ago WITH TIMEZONE 'Europe/London'

SELECT average(databaseDuration) FROM Transaction SINCE last month until this month COMPARE WITH 1 month ago WITH TIMEZONE 'Europe/London'

SELECT average(duration+backendDuration) AS 'Total', average(duration) AS 'Frontend', average(backendDuration) AS 'Backend' FROM PageView WHERE countryCode = 'NZ' AND appName = 'appname' SINCE 1 day ago

@pwillis-els
pwillis-els / how-to-switch-roles-aws-adfs.md
Last active June 23, 2023 07:05
How to set up AWS CLI profiles to switch between roles while using Federated SAML authentication

Using profiles, assume-role, and Federated SAML authentication with AWS CLI

Let's say you use a Federated authentication method for AWS (like ADFS), and by default you have access to multiple roles and accounts. You want to be able to easily switch between accounts, roles, and even assume a second role after assuming a first one. The following guide explains how this works using [aws-adfs][1] and the [AWS CLI][2].

Background info about profiles

Profiles are how AWS CLI configures the settings for individual credentials, and allows you to switch between them. You can specify a profile either by passing the --profile NAME option to AWS CLI, or with an environment variable AWS_PROFILE=name.

@pwillis-els
pwillis-els / build-go-software-docker.md
Created January 15, 2020 01:52
Building Go software in Docker

(this can probably be greatly improved...)

Run the following command to enter a Docker container with a Go environment set up:

$ docker run --rm -it -v "$PWD":/usr/src/myapp -w /usr/src/myapp golang:1.8 bash

Inside the container, run the 'go get' command for the software you want to build and install:

root@3aac13b2bce8:/usr/src/myapp# go get github.com/itchio/gothub
@pwillis-els
pwillis-els / bash_cheat_sheet.md
Last active May 7, 2020 19:14
Bash cheat sheet

Bash Cheat Sheet

Strings

Check if a substring exists in a string

name="somethinglong"
substr="thing"
if [ ! "${name/$substr/}" = "$name" ] ; then
    echo "'$substr' found in '$name'"
@pwillis-els
pwillis-els / Heap_Dump_Java_AWS_ECS.md
Created June 29, 2020 14:13
Dumping heap of a Java process running in AWS ECS

Heap dumping a Java process running in AWS ECS

Note: this guide is designed for AWS ECS services, but starting from Step 4 is functionally equivalent to any Docker container on a Linux host.

Step 1. Look up ECS service

  1. Log into the AWS Console using the appropriate AWS account
  2. Navigate to AWS ECS service clusters (https://console.aws.amazon.com/ecs/home)
  3. Make sure you are in the correct region, if not, switch to the correct region (second drop-down menu in top right corner)
  4. Select the correct cluster (ex: https://console.aws.amazon.com/ecs/home?region=us-east-1#/clusters//services)
  5. In the Services tab, In the 'Filter in this page' text box, type the name of the service
@pwillis-els
pwillis-els / install_docker-ce_ubuntu.sh
Created June 29, 2020 16:24
Install Docker CE on Ubuntu
#!/bin/sh
set -e
# commands taken from https://docs.docker.com/engine/install/ubuntu/
sudo apt-get remove docker docker-engine docker.io containerd runc || true
sudo apt-get update
sudo apt-get install \
apt-transport-https \
ca-certificates \