Skip to content

Instantly share code, notes, and snippets.

View hongkongkiwi's full-sized avatar
🤓

Andy hongkongkiwi

🤓
View GitHub Profile
@hongkongkiwi
hongkongkiwi / app-wrapper
Created August 22, 2021 03:53
A simple app wrapper script which uses https://github.com/xiezhenye/waitpid to wait for the pid to and handles termination of the app if the script terminates. Call the script with ./app-wrapper syslogd <args>
#!/bin/sh
BIN="$1"; shift
[ -z "$BIN" ] && { echo >&2 "ERROR: no app passed"; exit 1; }
APP_NAME=$(basename "$BIN")
PID_FILE=${PID_FILE:-"/var/run/${APP_NAME}.pid"}
function killapp() { [ -n "$APP_PID" ] && kill -s TERM $APP_PID 2>/dev/null >/dev/nulll; }
trap killapp INT TERM SIGTERM EXIT
rm -f "$PID_FILE" 2>/dev/null
APP_DIR=$(dirname "$BIN")
CUR_DIR="$PWD"
@hongkongkiwi
hongkongkiwi / dockerfile_download_verify_file
Created October 19, 2020 08:39
Dockerfile snippet to download a file and verify it against the checksum and ensure that checksum is correct. Terraform used as an example here.
# You can set a version as a build argument to overwrite the detection
FROM ubuntu:latest
ARG TERRAFORM_VERSION=
# This example uses wget, so make sure it's installed first, could be adapted to use curl
RUN if [ -z $TERRAFORM_VERSION ]; then echo "Finding latest Terraform Version..."; TERRAFORM_VERSION=$(curl -s "https://github.com/hashicorp/terraform/releases/latest/download" 2>&1 | grep -Po [0-9]+\.[0-9]+\.[0-9]+); else echo "Terraform version passed in build argument v${TERRAFORM_VERSION}"; fi && \
echo "Downloading Terraform v${TERRAFORM_VERSION}..." && \
wget -O "/tmp/terraform_${TERRAFORM_VERSION}_linux_${PLATFORM_ARCH}.zip" -q "https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_${PLATFORM_ARCH}.zip" && \
wget -O "/tmp/terraform_${TERRAFORM_VERSION}_SHA256SUMS" -q "https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_SHA256SUMS" && \
@hongkongkiwi
hongkongkiwi / dockerfile_detect_platform_arch
Created October 19, 2020 08:25
Dockerfile snippet to detect platform arch
FROM ubuntu:latest
ARG PLATFORM_ARCH=
RUN case "$(uname -m)" in \
x86_32) export PLATFORM_ARCH='386' ;; \
x86_64) export PLATFORM_ARCH='amd64' ;; \
amd64) export PLATFORM_ARCH='amd64' ;; \
armhf) export PLATFORM_ARCH='arm' ;; \
armv7) export PLATFORM_ARCH='arm' ;; \
URL="http://stackoverflow.com/"
# store the whole response with the status at the and
HTTP_RESPONSE=$(curl --silent --write-out "HTTPSTATUS:%{http_code}" -X POST $URL)
# extract the body
HTTP_BODY=$(echo $HTTP_RESPONSE | sed -e 's/HTTPSTATUS\:.*//g')
# extract the status
HTTP_STATUS=$(echo $HTTP_RESPONSE | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
@hongkongkiwi
hongkongkiwi / main.tf
Last active November 16, 2023 23:38
Initialises Terraform HTTP backend to Gitlab. Here's a nice script which will ask for details in an interactive way.
# More information can be found at https://docs.gitlab.com/ee/user/infrastructure/#gitlab-managed-terraform-state
terraform {
backend "http" {
}
}
@hongkongkiwi
hongkongkiwi / import-tfc-variables
Created August 23, 2020 07:14
An incomplete script to import terraform cloud variables.
#!/bin/sh
ORGANIZATION_NAME=`cat backend.tf | grep "organization" | cut -f2 -d'=' | tr -d ' ' | tr -d '"'`
WORKSPACE_NAME=`cat backend.tf | grep ' name = "' | cut -f2 -d'=' | tr -d ' ' | tr -d '"'`
TERRAFORM_CLOUD_TOKEN=${TERRAFORM_CLOUD_TOKEN:-""}
while IFS= read -r VARIABLE; do
PAYLOAD='' read -r -d '' String <<"EOF"
{
"data": {
@hongkongkiwi
hongkongkiwi / generate-x509.sh
Last active August 17, 2020 11:04
Generate a Root CA & Intermediate CA using Google KMS
#!/bin/sh
GOOGLE_KMS_BIN="./google-kms-x509-macos-amd64-v1.1.0"
command -v "$GOOGLE_KMS_BIN" >/dev/null 2>&1 || { echo >&2 "I require $(basename "$GOOGLE_KMS_BIN") but it's not installed. Aborting."; echo >&2 "You can install from: https://github.com/ericnorris/google-kms-x509"; exit 1; }
[ -z "$GOOGLE_APPLICATION_CREDENTIALS" ] && { echo >&2 "Please set GOOGLE_APPLICATION_CREDENTIALS to point to service account JSON"; exit 1; }
COUNTRY=${CA_COUNTRY:-"USA"}
PROVINCE=${CA_PROVINCE:-""}
ORGANIZATION=${CA_ORGANIZATION:-"Widgets Inc"}
@hongkongkiwi
hongkongkiwi / setup_vault.sh
Created July 26, 2020 10:10
Shell script to download and setup vault on a new machine
#!/usr/bin/env bash
set -e
get_arch() {
local myArch
unameArch="$(uname -m)";
case "$unameArch" in
armhf)
myArch='arm'
@hongkongkiwi
hongkongkiwi / add-ssh-hosts
Created March 13, 2020 05:06
Add keys to known hosts file for a list of domains.
#!/usr/bin/env bash
SSH_SCAN_HOSTNAMES=${1:-"${SSH_SCAN_HOSTNAMES}"}
SSH_SCAN_HOSTNAMES=${SSH_SCAN_HOSTNAMES:-"github.com gitlab.com"}
SSH_DIR="$HOME/.ssh"
command -v ssh-keyscan >/dev/null 2>&1 || { echo >&2 "I require ssh-keygen but it's not installed. Aborting."; exit 1; }
command -v tee >/dev/null 2>&1 || { echo >&2 "I require tee but it's not installed. Aborting."; exit 1; }
for SSH_SCAN_HOSTNAME in $SSH_SCAN_HOSTNAMES; do
@hongkongkiwi
hongkongkiwi / update-mergerfs
Created March 7, 2020 06:26
Updates mergerfs to the latest version
#!/usr/bin/env bash
# Read a single char from /dev/tty, prompting with "$*"
# Note: pressing enter will return a null string. Perhaps a version terminated with X and then remove it in caller?
# See https://unix.stackexchange.com/a/367880/143394 for dealing with multi-byte, etc.
function get_keypress {
local REPLY IFS=
>/dev/tty printf '%s' "$*"
[[ $ZSH_VERSION ]] && read -rk1 # Use -u0 to read from STDIN
# See https://unix.stackexchange.com/q/383197/143394 regarding '\n' -> ''