Skip to content

Instantly share code, notes, and snippets.

@mpontillo
mpontillo / example.py
Created February 10, 2017 21:47
Django temporary logging example
import logging
l = logging.getLogger('django.db.backends')
l.setLevel(logging.DEBUG)
l.addHandler(logging.StreamHandler())
# Code to execute QuerySet goes here.
l.setLevel(logging.INFO)
@mpontillo
mpontillo / mirror.sh
Last active March 21, 2017 16:58
Example of how to create a container to mirror Trusty and Xenial (complete with boot images for MAAS).
CONTAINER=mirror
lxc launch ubuntu:x $CONTAINER
# Wait for the container to come online.
wait-for-systemd-container.sh $CONTAINER
# Ensure packages are up to date and install dependencies.
lxc exec $CONTAINER -- apt-get update
lxc exec $CONTAINER -- apt-get dist-upgrade -yu
@mpontillo
mpontillo / dmidecode_json.sh
Last active February 25, 2017 00:16
Shell script to grab the strings dmidecode supports and display them as JSON with jq
#!/bin/bash
# via http://git.savannah.nongnu.org/cgit/dmidecode.git/tree/dmiopt.c#n142
DMI_STRINGS="
bios-vendor
bios-version
bios-release-date
system-manufacturer
system-product-name
system-version
#!/bin/bash
OUTFILE=/root/dmi.bin
dmidecode -u --dump-bin $OUTFILE && (
echo "-----BEGIN DMI DATA-----" ;
base64 $OUTFILE
echo "-----END DMI DATA-----"
) || (echo "Unable to read DMI information."; exit 0)
@mpontillo
mpontillo / dump_hw_info.sh
Last active March 4, 2017 00:45
Gathers information about attached hardware and disks.
#!/bin/bash -e
# This script attempts to dump (non-personal) information about the current
# hardware platform. It expects to be run on a modern Linux distribution
# (such as Ubuntu).
DMI_OUTFILE="$(mktemp)"
function finish {
rm -f "$DMI_OUTFILE"
@mpontillo
mpontillo / virsh-start-and-wait-for-idle.sh
Last active March 3, 2017 21:58
Script to start a virtual machine using virsh, and sample its $pc register until it seems to be idle.
#!/bin/bash -e
# Check if we believe the VM has started after this number of samples.
CHECK_EVERY=500
# Consider the machine to be idle if MIN_IDLE_PCT% of sampled program counters
# were identical after at least MIN_IDLE_SAMPLES.
#
MIN_IDLE_SAMPLES=450
MIN_IDLE_PCT=60
@mpontillo
mpontillo / maas_allocated_ip_addresses.sql
Last active July 17, 2017 21:46
List IP addresses allocated in MAAS.
SELECT
sip.ip,
CASE
WHEN sip.alloc_type = 0 THEN 'AUTO'
WHEN sip.alloc_type = 1 THEN 'STICKY'
WHEN sip.alloc_type = 4 THEN 'USER_RESERVED'
WHEN sip.alloc_type = 5 THEN 'DHCP'
WHEN sip.alloc_type = 6 THEN 'DISCOVERED'
ELSE CAST(sip.alloc_type as CHAR)
END "alloc_type",
@mpontillo
mpontillo / maas-cli-commands.sh
Last active March 19, 2025 12:45
MAAS jq tricks and how to move interfaces between fabrics (via changing the interface's VLAN)
maas refresh
profile=$(maas list | head -1 | awk '{ print $1 }')
# This generates a single JSON object.
maas $PROFILE machines read | jq '[.[] | {hostname:.hostname, system_id: .system_id, status:.status}]'
...
maas $PROFILE machines read | jq '.[] | {hostname:.hostname, system_id: .system_id, status:.status}' --compact-output
{"hostname":"pxe-bond1","system_id":"xrey6h","status":4}
@mpontillo
mpontillo / clear_unknown_interfaces.py
Created March 15, 2017 17:21
How to clear out unknown interfaces in MAAS
# sudo maas-region shell
>>> from maasserver.models import Interface
>>> unknown_interfaces = Interface.objects.filter(type='unknown')
>>> unknown_interfaces.delete()
>>> quit()
@mpontillo
mpontillo / check-apt-signatures.sh
Created March 22, 2017 18:48
Check the signature on each downloaded apt Release file.
#!/bin/bash
# Import apt keys into GPG keyring for ease of use.
apt-key list | grep ^/ | xargs -n1 gpg --import > /dev/null 2>&1
bad=0
# Check the signature on each apt list.
for file in /var/lib/apt/lists/{*_Release,*_InRelease}; do
gpg --verify $file > /dev/null 2>&1