Skip to content

Instantly share code, notes, and snippets.

@stefanotorresi
stefanotorresi / libvirt-vacuum.sh
Last active February 19, 2020 12:05
Libvirt vacuum cleaner
#!/bin/bash
set -u
EXCLUDE_PATTERN="jenkins-swarm|hana_cockpit|nfs-|angi"
VOLUME_POOLS=(default terraform)
# most code borrowed from https://github.com/openshift/installer scripts
printf 'Warning: This will destroy effectively all libvirt resources\nContinue [yN]? '
@stefanotorresi
stefanotorresi / xdebug
Created May 15, 2019 13:07
bash script to enable xdebug inside a PHP Docker container
#!/usr/bin/env bash
set -euo pipefail
USER=$(id -u)
XDEBUG_INI_FILE="/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini"
if [[ ${USER} != 0 ]]; then
echo "This script must be run as root"
exit 1
@stefanotorresi
stefanotorresi / helm-release-shortener
Last active February 6, 2019 17:01
bash script to shorten helm release names
#!/usr/bin/env bash
set -euo pipefail
RELEASE_NAME=$1
MAX_LENGTH=${2-53}
HASH_SIZE=${3-8}
if [[ $MAX_LENGTH -le $(($HASH_SIZE+1)) ]]; then
echo "max length must be bigger than hash size + 1"
@stefanotorresi
stefanotorresi / rotate-do-keys
Last active February 4, 2019 19:23
Bash script to rotate DigitalOcean Kubernetes keys
#!/usr/bin/env bash
set -euo pipefail
if ! [[ -x $(command -v doctl) ]]; then
echo "This script needs doctl: https://github.com/digitalocean/doctl"
exit 1
fi
if ! [[ -x $(command -v jq) ]]; then
@stefanotorresi
stefanotorresi / clean-up-generated-droplets
Created September 28, 2018 12:17
Gitlab CI runner cleanup script
#!/bin/bash
set -euo pipefail
export PATH=$PATH:/usr/local/bin:/usr/local/sbin
echo "*****"
date +%F\ %T
echo "stopping gitlab-runner service..."
@stefanotorresi
stefanotorresi / composer.json
Created February 17, 2018 12:17
Barebone example of a PSR-15 middleware stack.
{
"require": {
"psr/http-server-middleware": "^1.0",
"zendframework/zend-diactoros": "^1.7"
}
}
@stefanotorresi
stefanotorresi / Psr15MiddlewareFromAnonFunc.php
Last active February 7, 2018 09:28
How to use old style single pass anonymous function middleware with the PSR-15 spec.
<?php
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Server\RequestHandlerInterface as Handler;
use Psr\Http\Server\MiddlewareInterface as Middleware;
$middleware = function(Request $req, callable $next): Response {
$res = $next($req);
return $res->withHeader('X-Foo', 'Bar');
@stefanotorresi
stefanotorresi / databases.sh
Last active February 1, 2018 07:52
Initialising multiple databases inside a Docker MySQL image
#!/usr/bin/env bash
# ** usage **
# initialize a MYSQL_DATABASES env variable in the container with space separated db names
# e.g. MYSQL_DATABASES="foo bar"
# assumes that MYSQL_ROOT_PASSWORD and MYSQL_USER are set
function create_database() {
local DATABASE=$1
@stefanotorresi
stefanotorresi / amp-process.php
Created July 21, 2017 21:22
Run multiple processes concurrently with amphp/process and coroutines
<?php
declare(strict_types=1);
use Amp\ByteStream\Message;
use Amp\Loop;
use Amp\Process\Process;
use function Amp\coroutine;
use function Amp\Promise\all;
@stefanotorresi
stefanotorresi / config.php
Created March 22, 2017 15:04
Psysh default config that adds Composer autoloader automagically. Save it in `~/.config/psysh`.
<?php
$defaultIncludes = [];
$composerAutoload = getcwd() . DIRECTORY_SEPARATOR . '/vendor/autoload.php';
if (is_file($composerAutoload)) {
$defaultIncludes[] = $composerAutoload;
}
return [