Skip to content

Instantly share code, notes, and snippets.

View ThinGuy's full-sized avatar

craig bender ThinGuy

View GitHub Profile
@ThinGuy
ThinGuy / mib-custom.ks
Last active February 29, 2024 15:04
Custom Kickstart file to further tweak RHEL and CentOS MAAS images using maas-image-builder
# Note: This further tweaks rhel and centos images
# built by the maas-image-builder
#
# This is supplemental to the kickstart file
# that gets auto-generated by the maas-image-builder.
# It does not supercede those settings, unless
# you purposely reverse a change, like unlocking
# the root user's account
#
# Since it is supplemental, there are no "required"
@ThinGuy
ThinGuy / maas-parallel.sh
Created November 12, 2018 17:02
Run commands in parallel on deployed machines
# Run dist-upgrade all deployed machines running ubuntu
(maas admin machines read|jq -r '.[]|select((.status==6) and .osystem=="ubuntu").hostname')|\
xargs -n1 -P0 bash -c 'ssh $0 "sudo 2>/dev/null bash -c '"'"'apt update;apt dist-upgrade -y;apt autoremove -y'"'"'"'
# Run dist-upgrade all deployed machines running ubuntu with a certain tag
# To see list of tags, run: maas admin tags read|jq -r '.[].name'
(maas admin tag machines ${TAG}|jq -r '.[]|select((.status==6) and .osystem=="ubuntu").hostname')|\
xargs -n1 -P0 bash -c 'ssh $0 "sudo 2>/dev/null bash -c '"'"'apt update;apt dist-upgrade -y;apt autoremove -y'"'"'"'
@ThinGuy
ThinGuy / snap-cleanup.sh
Last active September 6, 2019 03:47
Remove unused snap revisions
cleanup-snaps() {
for SNAP in $(sudo find 2>/dev/null /snap -maxdepth 2 -type l -name current |awk -F/ '{print $3}');do
local -a OLD_REVS=($(sudo find 2>/dev/null /snap/${SNAP} -maxdepth 1 ! -name "$(readlink /snap/${SNAP}/current)" ! -name ${SNAP} -type d -exec basename {} \;))
[[ ${#OLD_REVS[@]} -ge 1 ]] && { printf '%s %s\n' ${SNAP} ${OLD_REVS[@]}|\
xargs -rn2 -P1 bash -c 'sudo snap remove $0 --revision=$1|\
sed -r "s/^.*\(|\)//g;s/removed/of snap $0 &/g;s/^.*$/"$(printf "\e[1m&\e[0m")"/g;s/revision/Revision/g"'
[[ $? -eq 0 ]] && { printf "\e[1A\e[60G\e[38;2;0;230;0mOK\e[0m\n"; } || { printf "\e[58G\e[38;2;240;0;0mFAILED\e[0m\n";} ; }
[[ ${#OLD_REVS[@]} -lt 1 ]] && printf "\e[38;2;150;150;150msnap \"${SNAP}\" only has a single revision\e[60GOK\e[0m\n" || true
done
}
@ThinGuy
ThinGuy / show-uca-repos
Last active November 15, 2018 20:26
Edit to print correct apt source string for OpenStack releases
show-uca-repos() {
local PROPOSED=false
local OS_REL=
local ALL=false
local DESC="\e[1m${FUNCNAME}\e[0m: Prints correct apt repo string for a given OpenStack release"
show-uca-repos_usage() {
printf "\n\e[2G${DESC}\n\n"
printf "\e[2G\e[1mUsage\e[0m: ${FUNCNAME%%_*} [-p ] [-a ] [-r <release>] \n\n"
printf "\e[4G -p, --proposed\e[24GShow strings for proposed repos (Default: false)\n"
printf "\e[4G -a, --all \e[24GShow strings for both released and proposed repos (Default: false)\n"
@ThinGuy
ThinGuy / add-loops.sh
Last active January 6, 2020 01:08
Function to add loop devices, that ensures max_loop is not exceed
add-loops() {
[[ -n ${1} && ${1} =~ ^[0-9]+$ ]] && local C=${1} || { printf "${FUNCNAME} <count>\n";return 2; }
[[ ${EUID} = 0 ]] && export SU= || export SU=sudo
local -a LOOP_DEVS=($(find /dev -maxdepth 1 -iregex '.*loop[0-9]+'|sort -V))
local M=$(cat 2>/dev/null /proc/cmdline|grep -oP '(?<=max_loop=)[^ ]+')
[[ -n ${M} && ${M} =~ ^[0-9]+$ ]] || local M=256
[[ $((${#LOOP_DEVS[@]}+${C})) -gt ${M} ]] && { printf "Can only add up to $(($M-${#LOOP_DEVS[@]})) new devices\n";return 2; }
local L=${LOOP_DEVS[-1]//[^0-9]/}
local S=$(($L+1))
local E=$(($S+$C))
@ThinGuy
ThinGuy / fix-bind-options.sh
Last active June 16, 2019 20:31
Fix dnsmasq "Address already in use" error when running lxc network create; updated to deal with disabled ipv6 and lxd snap
# This error happens when dnsmasq tries to start on a host that:
# 1) Is running the bind9.service (named)
# 2) Host's /etc/bind/named.conf.options is missing or has "any" set for listen-on[-v6] declarations.
#
# named creates a listener for the virtual ethernet that conflicts with the lisenter created by dnsmasq
#
# Fix is to tell named to only listen to IPs on physical nics
#
# The function below tries to determine phyical and loopback nics and edits /etc/bind/named.conf.options accordingly
@ThinGuy
ThinGuy / microclouds.sh
Last active February 4, 2019 05:00
Microcloud demo using MAAS 2.5, KVM Pods and LXD Clustering
microcloud-tag-cleanup() {
local OTAG=$1
local NTAG=$2
# remove ready machines from a given tag ${OTAG}
# delete given tags ${OTAG} that do not contain machines
# Exclude a given tag ${NTAG} from deletion
[[ -z $OTAG || $OTAG =~ [-]+h ]] && { printf "${FUNCNAME}: <tag_name> [ exclude_name ]\n";return 1; }
local MAAS_INFO=$(maas 2>/dev/null ${MAAS_PROFILE} machines read)
jq 2>/dev/null -r '.[]|"\(.tag_names|to_entries[]|select(.value|startswith("'${OTAG}'")).value) \(select(.status==4).system_id)"' <<< ${MAAS_INFO}|xargs -n2 -P0 bash -c 'maas 2>/dev/null ${MAAS_PROFILE} tag update-nodes $0 remove=$1'
@ThinGuy
ThinGuy / add-max-loops.sh
Last active December 11, 2019 14:25
One-liner to add max loop back devices
export MAX=256;for ((i=$(sudo find 2>/dev/null /dev -iname "loop[0-9]*"|wc -l); i<$MAX; i++));do sudo mknod -m0660 /dev/loop${i} b 7 ${i} && sudo chown root.disk /dev/loop${i};done
@ThinGuy
ThinGuy / microclouds2.sh
Created January 16, 2019 18:07
Microcloud with virtip
gen-sa-ip() {
gen-sa-ip_usage() { printf "\nFunction: ${FUNCNAME%_*}\n\nPurpose: Generates a random IP address using the self-assigned range (169.254.0.0-169.254.255.255)\n\n\tUsage: ${FUNCNAME%_*}\n\n" 1>&2;return; }
[[ $1 = '-h' || $1 = '--help' ]] && { ${FUNCNAME}_usage; return; }
printf '169.254.%01d.%01d\n' $((RANDOM%256)) $((RANDOM%256))
}
export -f gen-sa-ip
wrap() {
local DESC="${FUNCNAME}: Like fold, but with indents\n"
[[ $1 = '--desc' ]] && { printf "${DESC}";return; }
@ThinGuy
ThinGuy / ipmi-consoles.sh
Last active January 18, 2019 21:44
bash function script to connect to all ipmi consoles using byobu (tmux) given an array of IPs. 6 consoles per window. Tmux limit of 15 windows.
ipmi-consoles() {
declare -ag IPMI_IP_LIST=($(printf 10.38.14."%s\n" {4..39}))
byobu new-session -d -s CONSOLES
SPLITS=6
WIN=0
export IPMI_USER=root
export IPMI_PASSWORD=calvin
for ((i=0; i<${#IPMI_IP_LIST[@]}; i+=${SPLITS}));do
PANES=("${IPMI_IP_LIST[@]:i:${SPLITS}}")
for p in ${!PANES[@]};do