Skip to content

Instantly share code, notes, and snippets.

View nlm's full-sized avatar
🚀

Nicolas Limage nlm

🚀
View GitHub Profile
@nlm
nlm / kubectl-shell
Created March 4, 2022 13:33
Kubectl extension to spawn a shell on a k8s node
#!/bin/bash -eu
# This script starts a new pod with a privileged container to run admin commands
if [ "$#" -ne 1 ]; then
echo "usage: $0 nodeName" >&2
exit 1
fi
node=${1:?}
nodeName=$(kubectl get node ${node} -o template --template='{{index .metadata.labels "kubernetes.io/hostname"}}')
podName=${USER:-unknown}-shell-${nodeName}
exec kubectl run ${podName:?} --restart=Never -it --rm --image overriden --overrides '
@nlm
nlm / with_context.py
Created September 23, 2020 12:12
decorator for context management outside functions
from functools import partial
from contextlib import ExitStack
from types import SimpleNamespace
from collections import namedtuple
def with_context(**contexts):
def decorator(func):
def wrapper(*args, **kwargs):
with ExitStack() as stack:
#! /bin/bash -eu
# curl -sL https://gist.github.com/nlm/dfd81e1793da4bf7f3b5f9022468d204/raw/30039fe9d1266b99ab6fd60ec0c63c275bba0d19/install_docker_on_ubuntu.sh | bash -eu
apt-get update
apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg-agent \
software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -

Keybase proof

I hereby claim:

  • I am nlm on github.
  • I am nlm (https://keybase.io/nlm) on keybase.
  • I have a public key ASB8RuHIrkhrI3_3DVp-RrZ8yPGOhYIBJMdc-35nXfLCvAo

To claim this, I am signing this object:

@nlm
nlm / codegenerator.py
Created August 7, 2017 09:11
Jinja2 custom code generator to detect unused variables
from jinja2.compiler import CodeGenerator
class CustomCodeGenerator(CodeGenerator):
def __init__(self, *args, **kwargs):
super(CustomCodeGenerator, self).__init__(*args, **kwargs)
self._vars_tracker = set()
def visit_Template(self, *args, **kwargs):
super(CustomCodeGenerator, self).visit_Template(*args, **kwargs)
@nlm
nlm / fan_control.ino
Created August 4, 2017 12:06
Arduino Fan Control
#include <math.h>
/* PWM fan control for arduino */
const int rlyPin = 3; // digital 3 (optional relay)
const int pwmPin = 4; // digital 4 (connected to fan pwm pin)
const int ctrPin = A5; // analog 5 (connected to potentiometer measurement pin)
const int tempPin = A6; // analog 6 (connceted to temperature probe)
const int runCycles = 10000; // check new value every 10k cycles (2/sec)
const uint32_t cycleDuration = 50; // 50us for 20kHz
@nlm
nlm / my_state.sls
Last active July 3, 2017 13:05
Saltstack pattern to trigger commands when a pillar changes without storing it on the host in clear-text
intermediate-state-file:
file.managed:
- name: /etc/.intermediate_state_file
- contents: {{ salt['hashutil.sha256_digest'](salt['pillar.get']('my_pillar')|string) }}
my-cmd-1:
cmd.wait:
- name: "echo a command you want to run only when a my_pillar changes, but without storing it raw on the host"
- watch:
- file: intermediate-state-file
@nlm
nlm / macaddr.py
Created May 12, 2017 09:31
mac address <=> integer conversions in python
import re
def mac_to_int(mac):
res = re.match('^((?:(?:[0-9a-f]{2}):){5}[0-9a-f]{2})$', mac.lower())
if res is None:
raise ValueError('invalid mac address')
return int(res.group(0).replace(':', ''), 16)
def int_to_mac(macint):
if type(macint) != int:
@nlm
nlm / disktest.sh
Last active May 4, 2017 15:24
Hard drive health testing on linux
#!/bin/bash -u
usage()
{
echo "usage: $0 [OPTIONS] DEVICE [DEVICE...]"
echo
echo "Options:"
echo " -s|--smart do a smart short test"
echo " -e|--smarterrors do a smart errors test"
echo " -d|--devstat do a smart devstat test"
@nlm
nlm / serialsh.bash
Created January 20, 2017 14:47
Login Shell to automatically connect to serial port via ssh
#!/bin/bash
name="$(basename $0)"
echo "$name"
if [[ "$name" =~ serialsh-(tty[a-zA-Z0-9]+)(-([0-9]+))? ]]; then
tty="${BASH_REMATCH[1]}"
speed="${BASH_REMATCH[3]}"
exec screen -dR "serialsh-${tty}" "/dev/${tty}" $speed
else
echo "usage: $0"
echo "symlink this file as serialsh-tty0 to access tty0"