dP.  .dP .d8888b. .d8888b. 88d888b. d888888b
 `8bd8'  88'  `"" Y8ooooo. 88'  `88    .d8P'
 .d88b.  88.  ...       88 88        .Y8P   
dP'  `dP `88888P' `88888P' dP       d888888P
    
PROGRAMMER /ˈprōˌɡramər/
A person who solves problems you didn't know you had in ways you don't understand.
public wərk snippets contact

Code Snippets

  • Sometimes you just need to delete everything in an elasticsearch cluster and you don't have access to do the stop-rm-rf-start thing.
    for idx in $(curl -sS https://${DOMAIN}:${PORT}/_cat/indices | awk '{print $3}'); do curl -XDELETE "https://${DOMAIN}:${PORT}/${idx}"; done
  • Building Armagetron on macos (worked on Catalina)

    Install Dependencies

    brew install pkg-config autoconf automake sdl2 sdl2_image sdl2_mixer protobuf-c glew boost ftgl dylibbundler create-dmg
    

    Setup

    ./bootstrap.sh
    

    Configure

    ./configure --disable-debug \
                --disable-dependency-tracking \
                --disable-silent-rules \
                --disable-zthreadtest \
                --disable-sysinstall \
                --disable-uninstall \
                --disable-restoreold \
                --enable-automakedefaults \
                --disable-useradd \
                --disable-initscripts \
                --disable-etc
    

    Build

    REL_TOP_SRCDIR=. sh desktop/os-x/build_bundle.sh
    

    References

    • https://wiki.armagetronad.org/index.php?title=MacOS
    • https://gitlab.com/armagetronad/armagetronad/-/blob/trunk/documentation/installation.txt
    • https://gitlab.com/armagetronad/armagetronad/-/issues/119
  • Sometimes you just want a list of timezones with their offsets. From this you can find the offset for a given timezone on a specific date.
    from datetime import datetime
    import pytz
    UTC = pytz.timezone("UTC")
    for tzn in pytz.common_timezones:
    tz = pytz.timezone(tzn)
    print(
    tzn,
    UTC.localize(datetime(2020, 12, 15)).astimezone(tz).strftime('%z'),
    UTC.localize(datetime(2020, 6, 15)).astimezone(tz).strftime('%z'),
    )
  • This script prints out the current list of Amazon2 Linux AMI's by region (kernel 5.10, HVM, x86_64, gp2). Changing the image variable and the Path filter make this capable of searching for any public/shared image.
    import boto3
    from json import dumps
    from yaml import dump
    image = 'amzn2-ami-kernel-5.10-hvm-x86_64-gp2'
    regions = [
    'ap-northeast-1',
    'ap-northeast-2',
    'ap-northeast-3',
    'ap-south-1',
    'ap-southeast-1',
    'ap-southeast-2',
    'ca-central-1',
    'eu-central-1',
    'eu-north-1',
    'eu-west-1',
    'eu-west-2',
    'eu-west-3',
    'sa-east-1',
    'us-east-1',
    'us-east-2',
    'us-west-1',
    'us-west-2',
    ]
    amis = {}
    for region in regions:
    client = boto3.client('ssm', region)
    paginator = client.get_paginator('get_parameters_by_path')
    iterator = paginator.paginate(
    Path='/aws/service/ami-amazon-linux-latest'
    )
    matches = []
    for batch in iterator:
    matches.extend(list(filter(lambda det: image in det['Name'], batch['Parameters'])))
    if len(matches) < 1:
    print('no matches found in', region)
    continue
    elif len(matches) > 1:
    print('multiple matches found in', region, '- perhaps you should narrow your results')
    for match in matches:
    print("\t-", match)
    # grab the first one if we have more than one
    print('found', matches[0]['Name'], matches[0]['Value'], 'in', region)
    amis[region] = {'AMI': matches[0]['Value']}
    print(dumps(amis, indent=4))
    print(dump(amis))
  • Reconcile descrepencies in document counts within elasticsearch
    from requests import get
    from json import dumps, loads
    from prettytable import PrettyTable
    src_server = 'http://localhost:9200'
    def docCount(idx=None, typ=None):
    url = src_server
    if idx:
    url += "/%s" % idx
    if typ:
    url += "/%s" % typ
    url += "/_count"
    res = loads(get(url).content)
    return res['count'] if 'count' in res else 0
    tbl = PrettyTable(['Index', 'Count on Index', 'Docs In Types'])
    tbl.add_row(['entire cluster', docCount(), 'n/a'])
    tbl.add_row(['=====','=====','====='])
    src_state=loads(get("%s/_cluster/state" % (src_server), timeout=600).content)
    docsCountedInIndexes = 0
    docsCountedInTypes = 0
    for idx in src_state['metadata']['indices']:
    docsInIndex = docCount(idx)
    docsCountedInIndexes += docsInIndex
    docsInTypes = 0
    for typ in src_state['metadata']['indices'][idx]['mappings']:
    docsInTypes += docCount(idx, typ)
    docsCountedInTypes += docsInTypes
    tbl.add_row([idx, docsInIndex, docsInTypes])
    tbl.add_row(['-----','------','-----'])
    tbl.add_row(['Totals', docsCountedInIndexes, docsCountedInTypes])
    print(tbl)