Skip to content

Instantly share code, notes, and snippets.

@ThinGuy
ThinGuy / MaaS XPATH Auto-Tagging
Last active February 12, 2018 16:58
Using MaaS automatic tagging features and assign kernel options those machines
[[ ${MAAS_PROFILE} ]] || MAAS_PROFILE=$(maas 2>/dev/null list|awk '{print $1}')
if [[ -z ${MAAS_PROFILE} ]];then
printf "Cannot determine MaaS profile name. Please log in to maas from the command line.\n\n"
#if being called from a function, return
[[ $0 = bash ]] && return 1
#if being called from a script, exit
[[ -f $0 ]] && exit 1
@ThinGuy
ThinGuy / fix_hashsummismatch_error
Last active June 3, 2020 09:42
Fix “Hash sum mismatch” error in maas that can occur when a local mirror and maas' apt-proxy cache get out of sync
#To fix bad hashInsert the following as after "late_commands:" in /etc/maas/preseeds/curtin_userdata
prxyfix_01_cmd: ["curtin", "in-target", "--", "sh", "-c", "/bin/echo 'Acquire::http::Pipeline-Depth 0;' > /etc/apt/apt.conf.d/99FixBadProxy"]
prxyfix_02_cmd: ["curtin", "in-target", "--", "sh", "-c", "/bin/echo 'Acquire::http::No-Cache true;' >> /etc/apt/apt.conf.d/99FixBadProxy"]
prxyfix_03_cmd: ["curtin", "in-target", "--", "sh", "-c", "/bin/echo 'Acquire::BrokenProxy true;' >> /etc/apt/apt.conf.d/99FixBadProxy"]
#Automatically insert above into /etc/maas/preseeds/curtin_userdata using sed
sudo sed -e '/late_commands:/a \ \ prxyfix_01_cmd: ["curtin", "in-target", "--", "sh", "-c", "/bin/echo '"'"'Acquire::http::Pipeline-Depth 0;'"'"' > /etc/apt/apt.conf.d/99FixBadProxy"]\n\ \ prxyfix_02_cmd: ["curtin", "in-target", "--", "sh", "-c", "/bin/echo '"'"'Acquire::http::No-Cache true;'"'"' >> /etc/apt/apt.conf.d/99FixBadProxy"]\n\ \ prxyfix_03_cmd: ["curtin", "in-target", "--", "sh", "-c", "/bin/ec
@ThinGuy
ThinGuy / text-fu.sh
Last active October 5, 2017 12:13
Text related parameters and functions for bash scripts
## Don't change the location of the RSCHK line. It must be first.
RSCHK=$_
TRUE=true
FALSE=false
TEXT_FU_FUNCTIONS_VER=1.0
SOURCEDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
#Ensure that this script is being sourced, rather than being executed
[[ $RSCHK != $0 ]] || { echo "Error: ${SOURCEDIR}/${BASH_SOURCE[0]##*/} is being ran directly! Please \"source\" this script."; sleep 500;return; }
@ThinGuy
ThinGuy / force-del-maas-subnet
Last active February 5, 2018 12:43
Force delete a subnet in maas
#Workaround for 'Subnet matching query does not exist' error trying to delete a subnet in MaaS
#See bug https://bugs.launchpad.net/maas/+bug/1702527
#Usage force-del-maas-subnet <maas subnet name>
#ex force-del-maas-subnet '192.168.122.0/24'
force-del-maas-subnet() {
unset SN_NAME MAAS_SN_ID MAAS_STATICIP_ID MAAS_INTIP_ID MATCH CONT
export MATCH=false
command -v ipcalc > /dev/null 2>&1 || sudo apt install -yq ipcalc
command -v ipcalc > /dev/null 2>&1 || { printf "This function requires ipcalc. Please install it via \"apt install ipcalc -y\"\n";return; }
@ThinGuy
ThinGuy / ubuntu mirror functions
Last active October 11, 2017 10:54
Functions for getting Known Ubuntu Mirrors, group mirrors hosted by Ubuntu, and show local Country mirror if applicable
# get-known mirrors grabs all officially registered ubuntu mirrors from launchpad.net
# Creates the following variables:
# KNOWN_MIRRORS - All mirrors
# HOST_MIRRORS - Mirrors ran by Ubuntu team @ Canonical
# CN_MIRROR - Country specific Hosted Mirror where Country determined by your IP addres
get-known-mirrors() {
unset KNOWN_MIRRORS
local THIS_COUNTRY="$(curl -sSL --connect-timeout 15 --max-time 20 https://whatismycountry.com/|/bin/grep -oP '(?<=>Your Country is )[^<]+')"
export KNOWN_MIRRORS=$(curl -sSL --connect-timeout 15 --max-time 20 https://launchpad.net/ubuntu/+archivemirrors|awk -F'["><]' '/application-summary/,/<\/table>/ { if(/th colspan="2">/){COUNTRY=$5;next} if(/http/||/ftp/||/rsync/){SITE=$3} if(/Mbps/||/Gbps/){SPEED=$3} if(/distromirrorstatus/){STATE=$5;gsub(/1 Gbps/,"1000",SPEED);gsub(/2 Gbps/,"2000",SPEED);gsub(/10 Gbps/,"10000",SPEED);gsub(/20 Gbps/,"20000",SPEED);gsub(/4 Gbps/,"4000",SPEED);gsub(/ Mbps/,"",SPEED);gsub(/behind/,"",STATE);gsub(/Up to date/,"0",STATE);gsub(/One
@ThinGuy
ThinGuy / orangebox.preseed
Created October 12, 2017 22:31
OrangeBox Preseed File
# Preseed file for Canonical OrangeBox
# Part of OBInstaller Automation Scripts
# by Craig Bender <craig.bender@canonical.com>
# Locale
d-i debian-installer/locale string en_US.UTF-8
# No splash
d-i debian-installer/splash boolean false
@ThinGuy
ThinGuy / obkeys.pub
Created October 14, 2017 01:36
pubkeys for netconsole obinstall
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC/ETrjzlKN0DnhkaIOLseBnD5aNfxWIkJQl7BuC0QXAdwg1TQDGswd4NBDfCve1CxXvc6PngOJU+iigLVXy/Bl3jvuZ8g5YYj0MbM/sXMQBSWgmzwwB8qjM6P4Qiu+1KONVTkXde5h/U07EIZus5ls1o8v9S3KeWIo2enSPE6Ol1fxuRqFZoZUhLQZjP6DGurdnAY3dxY7Nk1Y6Smmigqh0Tqmstdicjhu1wfPQaXgpVvluq3NaJvxpBnU7eKpXFsel3v+31xGSoDu9tTX9nhZL4dtz6/2tjwKuSTrTWhAKeZTpNHmPtroBGZZyu7+wjkIQgkBwVScfphMDV3glurt rbu # ssh-import-id lp:1chb1n
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDTbAB9JrOGVROxqCQz8xZoh31XcqrcmxnlMStCITMQBUVoispxLfP8UqrkEfr/MUIytJI+pARTIX/dQtLrcGIWprEOJGYAHU75AagPAZUaMQsCt1QULNegntt1mM7CzBPA8FaddNjTfMXsj0o3WlvTcsIqmJSfy500mY9ImS6KBbIk5dqxHAaQBGH3jsTLPjd+pMR23ybXsiNnm5BZ4XIDhABx7WqGW/nwc05AD/1WA7e1TANzzA3VHxZxglgQQFhGRYxf5WvDxG96mHi4kBIVz1hKGM74p9+75OoMOSb4IPjnjlZA2pueYDp77bP5Gaq3opVcSzLkzK3FKpvXNGsp RB Canonical SSH # ssh-import-id lp:1chb1n
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDb3KIRYql1AxRIxs3oD8Z1crVVrHYyg3dNsZyw2dPSsIM1Z+Tatweqr2p1/xhhqiIdNg6D3lDMOUg3Sys+SGZXc7/ytz73B0YT5RHl1hArhTM58CL+3DyIeKj33q4mm70Sf9e86+MOvlNYD1QF8Lw
@ThinGuy
ThinGuy / maas-creds and login
Last active December 1, 2017 08:23
Auto login to maas - if the right information is there; Set maas creds from maas list
maas-login() {
[[ -z ${MAAS_PROFILE} ]] && export MAAS_PROFILE=$(maas 2>/dev/null list|awk '{print $1}')
if [[ -z ${MAAS_PROFILE} ]];then
printf "\n\e[0;33m WARNING\e[0m:Cannot automatically determine the name of the MaaS profile.\n\n"
read -erp "Please enter the name of the MaaS profile that you created: " MAAS_PROFILE
fi
[[ -z ${MAAS_PROFILE} ]] && { printf "\n\e[0;31mERROR\e[0m: MaaS profile name cannot be blank\n\n";return 1; }
export MAAS_APIKEY=$(sudo maas 2>/dev/null apikey --username ${MAAS_PROFILE})
[[ -z ${MAAS_APIKEY} ]] && { printf "Cannot determine MaaS API Key for ${MAAS_PROFILE}. Did you create a profile?\n\nUse sudo maas-region createadmin\n\n";return 0; }
export MAAS_URL=$(sudo awk '/maas_url/{print $2}' /etc/maas/regiond.conf)
@ThinGuy
ThinGuy / more MaaS auto-tagging
Last active December 1, 2017 08:20
Examples how to have maas auto-tag server models at a high level with "contains", or to you can get specific with exact matches for certain generations of hardware
[[ ${MAAS_PROFILE} ]] || MAAS_PROFILE=$(maas 2>/dev/null list|awk '{print $1}')
if [[ -z ${MAAS_PROFILE} ]];then
printf "Cannot determine MaaS profile name. Please log in to maas from the command line.\n\n"
#if being called from a function, return
[[ $0 = bash ]] && return 1
#if being called from a script, exit
[[ -f $0 ]] && exit 1
@ThinGuy
ThinGuy / rem swap part
Created November 1, 2017 09:01
Removing swap partitions and extend freespace to root filesystem. For servers with large RAM footprint where installer automatically created swap partition equal to the size of ram.
For LVM
--------
1) add your ssh key to root's account
e.g. sudo su - root
ssh-import-id <lp-id>
2) ssh into box as root. Don't do this via sudo!
3) get volume ids of what you want to remove and what you want to add it to
e.g. cat /etc/fstab
3.a) Comment out the lines that contain volume ids of that you are going to remove (i.e. /home or swap)