Skip to content

Instantly share code, notes, and snippets.

@hashbrowncipher
hashbrowncipher / assumerole.py
Created December 15, 2019 00:56
subclass of botocore.session.Session that assumes roles and refreshes credentials
from botocore.session import Session
credentials_cache = dict()
class Session(Session):
def __init__(self, role_arn, *args, **kwargs):
super().__init__(*args, **kwargs)
self._config = dict(
profiles=dict(
default=dict(
$ sudo -u cassandra jstat -gcutil $(pgrep -f Cassandra) 100ms
S0 S1 E O M CCS YGC YGCT FGC FGCT GCT
0.00 100.00 100.00 100.00 97.96 95.10 21 8.678 11 140.498 149.176
0.00 100.00 100.00 100.00 97.96 95.10 21 8.678 11 140.498 149.176
0.00 100.00 100.00 100.00 97.96 95.10 21 8.678 11 140.498 149.176
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <time.h>
#include <unistd.h>
#include <sys/resource.h>
@hashbrowncipher
hashbrowncipher / linux_tempfile.py
Created September 20, 2019 15:12
Atomically creates a file in Linux
@contextmanager
def atomic_temporary_file(destination, *, mode="w+b", flags=0, **kwargs):
fd = os.open(destination.parent, os.O_TMPFILE | os.O_RDWR | flags)
with io.open(fd, mode, **kwargs) as fh:
yield fh
os.link("/proc/self/fd/{}".format(fd), str(destination), src_dir_fd=0)
@hashbrowncipher
hashbrowncipher / tokens.py
Created September 4, 2019 02:08
Cassandra tokens calculator
from fractions import Fraction as f
def factors_of_two(n):
return n & ~(n - 1)
def _tokens_slice(count, zones):
# We cannot have zones be a multiple of two and also have tokens with
# equal spacing from each other.
@hashbrowncipher
hashbrowncipher / docker2squash.sh
Created May 5, 2019 07:53
generates a squashfs from a docker image
#!/usr/bin/fakeroot /bin/bash
set -o errexit
set -o nounset
set -o pipefail
OUTFILE=$2.squashfs
TMPDIR=$(mktemp -d -p .)
cleanup() {
rm -fr $TMPDIR
}
@hashbrowncipher
hashbrowncipher / init
Created April 21, 2019 16:42
load a squashfs and run it
#!/bin/bash
set -x
set -o errexit
set -o nounset
set -o pipefail
function poweroff_before_panic {
poweroff -f
}
trap poweroff_before_panic EXIT
@hashbrowncipher
hashbrowncipher / unlambda.py
Created October 22, 2018 20:30
Disassembles simple Python lambdas
import dis
from itertools import chain
from types import CodeType
def _get_func_args(stack, count):
return reversed([stack.pop() for i in range(count)])
def visit_func(stack, isn):
@hashbrowncipher
hashbrowncipher / multicore_server.py
Last active September 21, 2018 09:00
Multiprocess WSGI server using gevent==1.3.6, demonstrating sendfile
#!/usr/bin/python3
"""
Copyright 2018 Josh Snyder
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
#include <time.h>
int main() {
struct timespec start;
struct timespec end;
clock_gettime(CLOCK_REALTIME, &start);
for(int i = 0; i < 5000000; i++) {
clock_gettime(CLOCK_REALTIME, &end);
}
printf("%lu %lu\n", end.tv_sec - start.tv_sec, end.tv_nsec - start.tv_nsec);