Skip to content

Instantly share code, notes, and snippets.

@stuart-warren
stuart-warren / server.py
Created December 3, 2018 15:59
prometheus instrumented python3 http.server
#!/usr/bin/env python3
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer # python 3.7
from prometheus_client import Histogram, generate_latest # pip install prometheus_client
LATENCY = Histogram('webhook_request_latency_seconds', 'Time for a request webhook.', ['path','method'])
REQUESTS = Counter('webhook_total', 'webhooks requests.', ['path','method'])
def request_metrics(func):
def wrapper(self):
with LATENCY.labels(self.path, self.command).time():
@stuart-warren
stuart-warren / webhook.py
Created November 29, 2018 10:53
kubernetes mutating admission webhook (Python)
#!/usr/bin/env python
from http import HTTPStatus
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
import base64
import json
import logging
import os
import os.path
import ssl
@stuart-warren
stuart-warren / git-file-report.sh
Last active September 27, 2021 11:59
Which files in my git repo change the most often (and I should fix technical debt in first)
#!/bin/sh
# report which files in current git repo are changed most often
# useful for technical debt prioritisation
git log --name-only --oneline | sort | uniq -c | sort -nr
# with bars instead of numbers
git log --name-only --oneline | sort | uniq -c | sort -n | tail -n 20 | xargs -I{} python -c "input = '{}'; out = input.split(' '); bar = '#' * int(out[0]); print(f'{out[1]}\t{bar}')" | column -t
@stuart-warren
stuart-warren / watch.sh
Created October 20, 2017 10:47
Watch a directory of files for changes
# watch the current directory for changes to files
# order the files by size and output the files in 3 columns
# highlight changes between outputs
watch -d 'du -sh * | sort -hr | pr -3 -t'

Keybase proof

I hereby claim:

  • I am stuart-warren on github.
  • I am stuartwarren (https://keybase.io/stuartwarren) on keybase.
  • I have a public key ASBU_9soGfTFWe11KNVYZlZE5JY1tCqwgMfFvtM1iuNZFgo

To claim this, I am signing this object:

@stuart-warren
stuart-warren / git-plugins.sh
Created June 29, 2017 08:29
find git plugins
#!/usr/bin/env bash
compgen -c git-
@stuart-warren
stuart-warren / health.sh
Last active June 14, 2017 07:41
Check kubernetes api server from inside a pod
#!/usr/bin/env bash
/usr/bin/curl -sf -H "Authorization: Bearer $(</var/run/secrets/kubernetes.io/serviceaccount/token)" \
--cacert "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt" \
"https://${KUBERNETES_PORT_443_TCP_ADDR}/version"
# OR
/usr/bin/nc -w3 -z ${KUBERNETES_SERVICE_HOST} ${KUBERNETES_SERVICE_PORT}
@stuart-warren
stuart-warren / .gitlab-ci.yaml
Created March 23, 2017 08:27
Go Build in gitlab-ci
# assumes vendored deps, using eg glide
compile:
image: golang:1.7-alpine3.5
variables:
PKG: "github.com/stuart-warren/mypackage"
stage: compile
script:
- mkdir -p /go/src/$(dirname $PKG)
- ln -s ${PWD} /go/src/${PKG}
- GOOS=linux go build -o yourbinary ${PKG}
@stuart-warren
stuart-warren / TDD-README
Last active September 16, 2021 07:04
Monitor files for changes and run test command (TDD)
$ brew install fswatch
$ alias pywatch='fswatch -r -0 --monitor=poll_monitor $(find . -name *.py) | xargs --null -n1 -I{}'
$ alias gowatch='fswatch -r -0 --monitor=poll_monitor $(find . -name *.go) | xargs --null -n1 -I{}'
$ pywatch python setup.py test
$ gowatch go test -cover
@stuart-warren
stuart-warren / build.sh
Created February 2, 2017 09:49
build kubernetes configmap from files (bash/jq)
#!/usr/bin/env bash
get-configmap-template() {
echo '{"apiVersion": "v1","kind": "ConfigMap","metadata": {"name": ""},"data": {}}'
}
populate-configmap() {
jq --arg filename "$(basename $1)" --arg filecontent "$(<$1)" '.data += {($filename): $filecontent}'
}