Skip to content

Instantly share code, notes, and snippets.

View hongkongkiwi's full-sized avatar
🤓

Andy hongkongkiwi

🤓
View GitHub Profile
@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' -> ''
@hongkongkiwi
hongkongkiwi / pre-commit
Created March 2, 2020 08:23
Pre-commit hook to validate a .gitlab-ci.yml by uploading to gitlab server and validating using API
#!/usr/bin/env bash
###########################
# Validate .gitlab-ci.yml #
# by Peter Weinert #
###########################
lif [[ "$OSTYPE" == "darwin"* ]]; then
SED_BIN="gsed"
command -v "gsed" >/dev/null 2>&1 || { echo >&2 "I require gsed but it's not installed. Install with 'brew install gnu-sed'. Aborting."; exit 1; }
@hongkongkiwi
hongkongkiwi / update-docker-machine
Created March 2, 2020 07:25
Helper script to update docker-machine to latest version from github. Could be run in cron if you want to keep up to date. Can modify to be applicable to any app
#!/usr/bin/env bash
[ "$EUID" -ne 0 ] && { echo >&2 "Please run as root or with sudo"; exit 2; }
INSTALL_LOCATION="/usr/local/bin/docker-machine"
REPO=docker/machine
VERSION=`curl -s "https://github.com/${REPO}/releases/latest/download" 2>&1 | grep -Po [0-9]+\.[0-9]+\.[0-9]+`
[ -z "$VERSION" ] && echo >&2 "Could not get latest version from ${REPO}!"
if command -v docker-machine >/dev/null 2>&1; then