Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / Vagrantfile
Created February 27, 2017 18:52
Vagrant file for installing ruby with rbenv
# -*- mode: ruby -*-
# vi: set ft=ruby :
RUBY_V = File.open("./.ruby-version") { |f| f.read }.chomp
$apt_script = <<SCRIPT
sudo apt-get update
sudo apt-get install -y git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev python-software-properties libffi-dev libmysqlclient-dev
SCRIPT
@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 / add_users.yml
Created May 5, 2017 15:11
Ansible tasks for adding users to hosts, and adding their authorized keys to other users so they can login as those users
---
- name: add users
user:
name: "{{ item.name }}"
state: present
groups: "{{ item.groups }}"
shell: /bin/bash
with_items: "{{ users }}"
- name: add authorized keys
@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.