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
@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 / 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 \
@pwillis-els
pwillis-els / S3_transfer_optimize.md
Created July 17, 2020 16:29
Optimizing S3 file transfer speed

Optimizing S3 file transfer speed

There is a nifty tool https://github.com/larrabee/s3sync which can transfer files very quickly with S3, but it reads all files into memory. If you're transferring lots of large files this process will quickly get killed by the kernel due to OOM.

The next best thing I have found is to tweak the AWS CLI to use aws s3 sync as fast as possible.

# ~/.aws/config
[default]
@pwillis-els
pwillis-els / json2kvrows.sh
Created September 30, 2020 19:58
Python one-liner to convert json stdin into key=value on stdout
#!/bin/sh
python -c 'import sys,json;j=json.load(sys.stdin); print( "\n".join(["%s=%s" % (k, j[k]) for k in j]) )'
@pwillis-els
pwillis-els / json_v.sh
Created September 30, 2020 20:10
Python one-liner to print out the values of keys specified as command-line arguments
#!/bin/sh
python -c 'import sys,json;j=json.load(sys.stdin); a=sys.argv[1:]; print("\n".join([j[k] for k in a]))' "$@"