Skip to content

Instantly share code, notes, and snippets.

@xandout
xandout / mutex_test.sh
Last active August 5, 2020 16:06
Mutex, Singleton, Only one instance of bash script running at a time
#!/bin/bash
# Credit: https://unix.stackexchange.com/a/479309
singleton(){
# get absolute path to the script itself
script=`realpath $0`
# open bash script using file descriptor 6
exec 6< "$script"
# lock file descriptor 6 OR show error message if script is already running
@xandout
xandout / add-to-bashrc.sh
Created May 4, 2020 18:48
Generate random password in bash
# newpass generates a new random password of 32 or $1 chars long, up to 92 characters
newpass() { date +%s | sha256sum | base64 | tr -d '\n' | head -c "${1:-32}" ; echo ; }
# USAGE
# 18:47 # newpass 10
# N2U3YzliOT
# 18:47 # newpass 20
# YmM0MTk3MWFlMjM1ODMx
# 18:47 # newpass 30
# YzBlNDFhMmUyYzEwN2RlOThmOWRhNz
# 18:47 # newpass
@xandout
xandout / notes.md
Created May 1, 2020 12:45
SSH via Bastion

Disclaimer

⚠️ This guide does NOT use individual SSH keys, which is recommended for security purposes. This guide also does not show the usage of password-protected keys, you should use password-protection.

Definitions

  • Bastion server: Sometimes called a "jump server", this server is reachable by your laptop. Sometimes the bastion server is protected by a VPN, sometimes not but should always have a firewall. This is essentially your front door. Always lock your doors.

  • SSH Agent: This is a program that runs on your laptop and keeps your SSH private keys loaded in memory. eval $(ssh-agent)

  • SSH Keys: These consist of 2 parts
@xandout
xandout / theia-all.yml
Last active April 30, 2020 18:25
Run Theia IDE in Kubernetes
---
apiVersion: v1
kind: Service
metadata:
name: theia-svc
spec:
ports:
- port: 3000
targetPort: 3000
selector:
@xandout
xandout / enc_denc.sh
Last active April 14, 2020 20:35
Encrypt and decrypt strings in bash
# https://superuser.com/a/1508949/266021
# Accept STDIN as $1
# Add the content of this to your RC or source the file
VAULT_PASS=${VAULT_PASS:-password}
denc() {
if [[ $# -eq 0 ]] #in case of standard input
then
@xandout
xandout / podboom.sh
Created April 8, 2020 19:27
PodBoom - A way to bounce a K8S deployment.
#/bin/bash
# Add this to your bashrc or equivalent
podboom(){
TO_BOOM=${1:-your-deployment}
kubectl patch deployments ${TO_BOOM} -p '{"spec": {"template": {"metadata": { "labels": { "redeployed": "by-'$(whoami)'"}}}}}'
}
@xandout
xandout / docker-volume-notes.md
Last active April 1, 2020 21:22
Docker volume notes for a friend

cleanup

docker volume rm docker-volume-example -f

create a new volume to be managed by docker.

on your HOST_FS at /var/lib/docker/volumes/docker-volume-example/_data/

docker volume create docker-volume-example

Run an ubuntu container with the volume we made. Create new data inside volume

docker run --rm -v docker-volume-example:/path/inside/container -it ubuntu bash -c 'echo $(date) > /path/inside/container/newly_created_data._dat'

Because of the --rm flag, the container is completely gone. Because of the volume mount, we are able to persist data outside of the container and across multiple container creations.

cat /var/lib/docker/volumes/docker-volume-example/_data/newly_created_data._dat

@xandout
xandout / metrccheck.sh
Created March 16, 2020 14:59
Check METRC API status
#!/bin/bash
declare -a metrcs=(
"https://api-ca.metrc.com"
"https://api-co.metrc.com"
"https://api-ma.metrc.com"
"https://api-mi.metrc.com"
"https://api-mt.metrc.com"
"https://api-nv.metrc.com"
"https://api-or.metrc.com"
@xandout
xandout / ingress-ua-blocking.yaml
Created March 13, 2020 16:39
K8S Ingress showing how to block access based on the client's User-Agent
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: web-app-ingress
annotations:
nginx.ingress.kubernetes.io/configuration-snippet: |
if ($http_user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36" ) {
# use more_set_headers here instead of add_header for kubernetes(openresty).
more_set_headers "Content-Type: application/json";
@xandout
xandout / productboard-tls-proxy.yml
Created March 11, 2020 18:41
K8S Ingress/SVC for "TLS Termination Proxy" - productboard
---
apiVersion: v1
kind: Service
metadata:
name: ext-productboard-tls-proxy
spec:
type: ExternalName
externalName: portal.productboard.com
---
apiVersion: extensions/v1beta1