Skip to content

Instantly share code, notes, and snippets.

@sportebois
sportebois / find-bom.sh
Last active Jan 27, 2021
Remove BOM from UTF8 files
View find-bom.sh
#!/usr/bin/env bash
set -eu
# find-bom searches for nonbinary files starting with a BOM marker
# Path of BOM-encoded files is print to stdout
# Exit status is 0 if no BOM files has been found
# Exit status is 1 if at least one BOM file has been found
readonly TARGET_ROOT=${1:-$PWD}
echo "Searching for BOM in $TARGET_ROOT"
@sportebois
sportebois / api_helpers.py
Created Oct 13, 2020
Flask custom test client class overridden open()
View api_helpers.py
from flask.testing import FlaskClient
class ApiTestClient(FlaskClient):
def open(self, *args, **kw):
resp: Response = super().open(*args, **kw)
# Send a copy of the request to the sink so that it’s visible for the Akita Agent
resp.freeze()
_publish_to_exposer(kw, deepcopy(resp))
return resp
@sportebois
sportebois / akita-flask-article-snippet-03-new-fixtures.py
Created Oct 13, 2020
Automatically start/stop the exposer in a thread
View akita-flask-article-snippet-03-new-fixtures.py
@fixture(scope="session")
def exposer_thread(request):
start_exposer_thread()
def close():
stop_exposer_thread()
request.addfinalizer(close)
@sportebois
sportebois / akita-flask-article-snippet-02-fixtures.diff
Last active Oct 13, 2020
Update the common fixture to trigger a new one which will started an exposer thread
View akita-flask-article-snippet-02-fixtures.diff
@pytest.fixture
- def test_client():
+ def test_client(exposer_thread):
configure_app(flask_app, config_name=Environments.TESTS)
# I use this a lot for custom Test client classes to inject custom things
my_project.app.test_client_class = CustomApiTestClient
client = my_project.app.test_client
yield client
@sportebois
sportebois / akita-flask-article-snippet-01-fixtures.py
Last active Oct 13, 2020
Common pytest fixture to use Flask test client
View akita-flask-article-snippet-01-fixtures.py
@pytest.fixture
def client():
my_project.app.config['TESTING'] = True
with my_project.app.test_client() as client:
with my_project.app.app_context():
# Do some initialization stuff
yield client
# Or
@sportebois
sportebois / api_helpers.py
Last active Oct 13, 2020
Akita SuperLearn - expose Flask requests/responses from pytests and Flask test client
View api_helpers.py
# file: tests/api_helpers.py
import logging
import threading
from copy import deepcopy
from threading import Thread
from typing import Optional
from uuid import uuid4
import requests
@sportebois
sportebois / ec2-small-inventory.sh
Created Dec 11, 2019
Dumps a CSV of EC2 instances (no pagination, up to 200 per region)
View ec2-small-inventory.sh
#!/usr/bin/env bash
set -eu
dumpInstances() {
local readonly region="$1"
local readonly date_ts="$(date +%Y%m%d-%H%M)"
aws ec2 describe-instances \
--max-items 200 \
@sportebois
sportebois / ec2inventory.sh
Created Jul 22, 2019
List EC2 instances and create CSV files with the desired fields (AZ, Tag, ...)
View ec2inventory.sh
#!/usr/bin/env bash
set -eu
dumpInstances() {
local readonly region="$1"
local readonly date_ts="$(date +%Y%m%d-%H%M)"
aws ec2 describe-instances \
--max-items 200 \
@sportebois
sportebois / fmtnomad.sh
Last active Nov 21, 2018
Nomad job formatter (chmox+x and symlink it in your $PATH)
View fmtnomad.sh
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
#/ Description:
#/ Run Terraform fmt on nomad file to format HCL, and update the file in place.
#/
#/ Arguments:
#/ $1 the nomad flie
#/ --diff if set, then do not write the changes, but show the diff between current and cacnonical format
View vernemq_prometheus_metrics_sorted.txt
# ---- BYTES
# HELP bytes_received The total number of bytes received.
# TYPE bytes_received counter
bytes_received{node="nodeName"} 8504294780
# HELP bytes_sent The total number of bytes sent.
# TYPE bytes_sent counter
bytes_sent{node="nodeName"} 7981953590
# ---- CLIENT