Skip to content

Instantly share code, notes, and snippets.

@creisor
creisor / aws_metadata_endpoint.go
Created August 14, 2023 21:51
Golang to access the AWS metadata endpoint
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
)
@creisor
creisor / pw_generator.py
Last active March 28, 2023 16:14
A python (python3) password generator, possibly overkill
import string
import secrets
# https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_passwords_account-policy.html#default-policy-details
SYMBOLS='!@#$%^&*()_+-=[]{}|'
def gen_password(length=20, symbols=True, uppercase=True, lowercase=True, digits=True):
choices = ''
if symbols:
choices += SYMBOLS
@creisor
creisor / user_prompt.py
Last active October 26, 2023 20:41
A somewhat flexible (could be better) user question response
#!/usr/bin/env python3
def ask(question, response_dict):
choices = '/'.join(response_dict.keys())
response = input(f'{question} ({choices}): ').lower().strip()
try:
default = response_dict[[k for k in response_dict.keys() if k.isupper()][0]]
except IndexError:
default = None
@creisor
creisor / ldap_test.py
Created March 14, 2022 15:05
How to use MagicMock and some fixtures for python testing
from unittest.mock import MagicMock
from pathlib import Path
# this creates an ldap server entry, which has an info method
# and populates it with some static fixture data
with open(Path(__file__).parent / 'testdata/user_entry1.json') as f:
ldap_server_entry_json = f.read()
ldap_server_entry = MagicMock(**{'info.return_value': ldap_server_entry_json})
# this uses the entry, mocking the search method on the ldap_server object
@creisor
creisor / abstract_base_classes.py
Created November 19, 2021 21:52
Here's a simple example of how one can use the abc (Abstract Base Class) library in Python3 as an alternative to "raise NotImplementedError" when creating abstract classes
#!/usr/bin/env python3
import abc
class Task(abc.ABC):
@abc.abstractmethod
def do(self):
pass
@abc.abstractmethod
@creisor
creisor / check_journalctl.py
Created November 3, 2021 15:20
Nagios check for journalctl
#!/usr/bin/env python3
"""A Nagios check for entries with a certain log level in journalctl.
For example, you might want to alert if there have been 5 logs with level 'error' in the journal in the past 5 minutes:
check_journalctl -u my-cool-app --since 5m --log-level error --warning 2.0 --critical 5.0
"""
import sys
import re
@creisor
creisor / Makefile
Last active June 9, 2020 15:05
This is how one can prompt for user input in a Makefile (credit: https://stackoverflow.com/a/47839479)
.PHONY: help
help: ## Shows the help
@echo 'Usage: make <TARGETS>'
@echo ''
@echo 'Available targets are:'
@echo ''
@grep -E '^[ a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | \
awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
@echo ''
@echo ''
@creisor
creisor / just-tell-me-how-to-use-go-modules.md
Last active November 22, 2019 15:57
just-tell-me-how-to-use-go-modules - one of the most useful blog posts rescued from 404 obliteration

JUST TELL ME HOW TO USE GO MODULES

This was not written by me. It was very useful and then one day I got a 404, so I dug it up from the wayback machine: https://web.archive.org/web/20190425012016/http://www.kablamo.com.au/blog/2018/12/10/just-tell-me-how-to-use-go-modules


I recently started using Go’s new inbuilt vendoring tool, Go Modules, and having come from both govendor and dep, this new tool was more of a change than I expected.

I’m a fan of quick guides – just tell me what to do so I can start using it now. I don’t need an essay on why I should be using it or painful detail on the tool’s inner workings.

@creisor
creisor / playbook_inception.yml
Created February 15, 2019 22:44
This is how you run a playbook within a playbook, and only register change when the "inner playbook" registers a change.
- name: Run AWX Installer
local_action: "command ansible-playbook -i awx_repo/installer/inventory awx_repo/installer/install.yml --limit \"{{ ansible_limit }}\""
become: no
tags:
- awx_deploy
- awx-deploy
- deploy
- install
register: awx_installer_output
changed_when: ((awx_installer_output.stdout_lines[-1] | regex_search('changed=(\\d+)', '\\1'))[0] | int) > 0
@creisor
creisor / macos_bash_reminder.sh
Created May 8, 2017 20:36
A function for popping up a dialog at you
function reminder() {
# reminder 5 "remove tea bag"
seconds=$(($1 * 60))
sleep $seconds && osascript -e "tell app \"System Events\" to display dialog \"$2\"" 2>&1 > /dev/null
}