Skip to content

Instantly share code, notes, and snippets.

View ThinGuy's full-sized avatar

craig bender ThinGuy

View GitHub Profile
@ThinGuy
ThinGuy / apt-sources-cred-lines.sh
Created July 2, 2021 16:46
Create Credentialed Apt Sources Lines for Private Repos and PPA to mirror with apt-mirror, deb-mirror, etc.
#!/bin/bash
#
# Create array of credentials and machines
declare -ag PPA_CREDS=();
while IFS= read -er CRED;do
PPA_CREDS+=("${CRED}")
done < <(sudo awk '{print $4":"$6,$2}' /etc/apt/auth.conf.d/*)
# Look up matching matchines in apt sources files and insert matching credential
# then print apt source line with inserted credentials
printf "\n\e[1mCredentialed Apt Sources Lines:\e[0m\n"
@ThinGuy
ThinGuy / test-ua-repo.sh
Last active July 1, 2021 05:16
Test repo access with curl after performing ua attach <token>
# The following will exectute one or more curl commands that will test your credentialed access to
# various Ubuntu Advantage repositories that you are entitled to with your UA subscription.
sudo awk '{print $4":"$6,$2}' /etc/apt/auth.conf.d/90ubuntu-advantage|xargs -rn2 -P0 bash -c 'curl -sSlL -u ${0} https://${1}ubuntu/dists;echo'
@ThinGuy
ThinGuy / del-stuck-ip.sh
Created June 11, 2021 22:00
Delete Observed "stuck" IP from MAAS
#!/bin/bash
[[ -z ${1} || ${1} =~ -h ]] && { printf "\n\e[2GUsage: ${0##*/} <ip_address>\n\n";exit 2; } || { export STUCK_IP=${1}; }
# MAAS IP Address Allocation
# Type Name
# ━━━━ ━━━━
# 0 AUTO
# 1 STICKY
@ThinGuy
ThinGuy / gpu-connector-info
Last active May 15, 2021 04:53
List a GPUs connector names (i.e. HDMI-1), port-index, and status of said port
# Pre-Depends: bsdextrautils, coreutils, findutils
(find /sys/devices -regextype "posix-extended" -maxdepth 6 -regex '.*drm/card[0-9]$' -printf "%p|" -o -regex '.*(DP|HDMI|DVI).*[0-9]$' -printf "%f\n"|\
paste -sd,|\
xargs -rn1 -P1 bash -c 'P=${0%%|*} C=${P##*/} CC=($(echo ${0##*|}|\
sed "s/,/ /g"))
printf "Card,Port-Index,Connector,Status\n"
printf "${C},,,\n"
for con in ${CC[@]};do
(printf ",$(awk '"'"'{print $NF}'"'"' ${P}/${con}/ddc/name),${con#*-},$(cat ${P}/${con}/status)\n")
@ThinGuy
ThinGuy / lxd2hosts-function.sh
Last active May 6, 2022 17:25
Bash function to populate /etc/hosts with IP addresses (v4 and optionally v6) and names of running lxd instances
#!/usr/bin/env bash
# vim: set et ts=2 sw=2 filetype=bash :
[[ $_ != $0 ]] || { printf "\n\e[2G\e[1mPlease source this script!\e[0m ( e.g. \"source ${BASH_SOURCE[0]}\" )\n\n"; sleep 1;exit; }
lxd2hosts() {
local DESC="\n${FUNCNAME}: Populate /etc/hosts with running LXD instances\n\n"
lxd2hosts_usage() {
printf "\n${DESC}\n"
printf "Usage: ${FUNCNAME%%_*} [ Options ] \n\n"
printf "\nOptions:\n\n"
printf "\e[2G -4|--ipv4\e[18GUpdate IPv4 section ( Default: true )\n"
@ThinGuy
ThinGuy / maas-deployment-time.sh
Last active April 13, 2021 23:01
Function to get MAAS Deployment Time for a node
maas-deploy-time() {
[[ -z ${1} || ${1} =~ -h ]] && { printf '\e[2G${FUNCNAME} <hostname>\e[0m\n\n' 1>&2; return 1; } || { local MAAS_NODE=${1}; }
local MAAS_DBCONN="$(sudo cat /var/snap/maas/current/regiond.conf|awk 'BEGIN{ RS = "" ; FS = ": |\n" }/database/{print "postgres://"$10":"$6"@"$2":"$8"/"$4}')"
local PG_OPTS='psql -F"|" -P pager=off -P footer=off'
psql ${MAAS_DBCONN} ${PG_OPTS} -Atc "SELECT DISTINCT EXTRACT(EPOCH FROM DATE_TRUNC('second',created)) as epoch,node_hostname,(description) FROM maasserver_event WHERE node_hostname LIKE '${MAAS_NODE}' AND description SIMILAR TO '(From%Ready%to%Allocated%|From%Deploying%to%Deployed%)' AND description NOT LIKE '' ORDER BY epoch DESC LIMIT 2"| \
awk '{if (/Deployed/) {DV=$1;next};if (/Allocated/) {RV=$1;next}} END {EV=(DV-RV);min = (EV / 60)-((EV % 60)*1/60) ; sec = EV % 60 ; printf "%02dm:%02ds\n",min,sec}'
};export -f maas-deploy-time
@ThinGuy
ThinGuy / centos-lxd-go-ci.yaml
Last active March 11, 2021 02:06
Cloud-Init that sets up go and LXD on a Centos{7,8} Image
#cloud-config
merge_how:
- name: list
settings: [append]
- name: dict
settings: [no_replace, recurse_list]
bootcmd:
- set -x
- cloud-init-per once env1 printf "export PATH=\x22/usr/local/bin\x3a/bin\x3a/usr/bin\x3a/usr/local/sbin\x3a/usr/sbin\x3a\x24HOME/.local/bin\x3a\x24HOME/bin\x3a/var/lib/snapd/snap/bin\x3a/snap/bin\x22"|tee 1>/dev/null /etc/environment
- cloud-init-per once yumup1 yum check-update
@ThinGuy
ThinGuy / pgtail.sh
Last active April 13, 2022 21:07
postgresql tail (pg_tail)
#!/bin/bash
cd ~
sudo apt install libpq-dev -yq;
git clone https://github.com/aaparmeggiani/pg_tail;
cd pg_tail;
make;
sudo su - postgres -c '/home/ubuntu/pg_tail/pg_tail -d maasdb -t maasserver_event -c node_hostname,node_system_id,action,description,username';
@ThinGuy
ThinGuy / maas-show-events.sh
Created March 10, 2021 16:56
Show MAAS events from CLI
maas-show-events() {
maas-show-events_usage() {
printf "\n\e[2GUsage: ${FUNCNAME%%_*} -n <hostname> [options]\e[0m\n\n' 1>&2\n"
printf "\n\e[2GOptions:\n\n"
printf "\e[2G-n, --name\e[25GShow events for the given hostname\n\n"
printf "\e[2G-d, --desc\e[25GEvents listed in descending chronological order ( newest -> oldest )\n\n"
printf "\e[2G-h, --help\e[25GThis message\n\n"
printf "\n"
};export -f maas-show-events_usage
local MSEO='ASC'
@ThinGuy
ThinGuy / maas-subnet-check.sh
Last active May 6, 2022 17:25
Sanity check for maas subnets
# This requires you to be logged into the MAAS cli, have jq and bsdextrautils installed
export MAAS_PROFILE=admin
maas-subnet-check() {
declare -ag COL_HEADERS=() COL_ULINES=()
while IFS= read -r C;do COL_HEADERS+=("${C}");done < <(printf "MAAS ID\nCIDR\nGateway\nDNS\nMAAS Vlan ID\nVlan Tag\nSpace\nDHCP Enabled\nExternal DHCP\nRelayed From\n")
while IFS= read -r U;do COL_ULINES+=("${U}");done < <(for H in "${COL_HEADERS[@]}";do eval printf '%.0s\\u2500' {1..${#H}}; echo;done)
printf "\n\e[0;38;2;0;200;0mMAAS Subnet Check:\e[0m\n\n";
(for i in ${!COL_HEADERS[@]};do printf '%s\n' "${COL_HEADERS[i]}";done|paste -sd"|";for i in ${!COL_ULINES[@]};do printf '%s\n' "${COL_ULINES[i]}";done|paste -sd"|"; maas ${MAAS_PROFILE} subnets read|jq -r '.[]|"\(.id)|\(.cidr)|\(.gateway_ip|tostring|gsub("^null.*$";"unset"))|\(.dns_servers|@csv|gsub("^$";"unset")|gsub("\"";""))|\(.vlan.id)|\(.vlan.vid)|\(.space)|\(.vlan.dhcp_on)|\(.vlan.external_dhcp|tostring|gsub("^null.*$";"false"))|\(.vlan.relay_vlan|tostring|gsub("^