Skip to content

Instantly share code, notes, and snippets.

@ThinGuy
ThinGuy / private-sks-lxd.sh
Last active April 6, 2018 06:13
Build a private keyserver in a LXD container
#!/bin/bash
#Small function to give text status of OK/FAIL to commands
tstatus() {
RETCODE=$(echo $?)
[[ $RETCODE -eq 0 ]] && printf '\e['$(($(tput cols)-10))'G [ \e[32mOK\e[0m ]\n'
[[ $RETCODE -eq 1 ]] && printf '\e['$(($(tput cols)-10))'G [\e[31mFAIL\e[0m]\n'
return $RETCODE
}
@ThinGuy
ThinGuy / maxcol-length.awk
Created June 11, 2018 12:29
awk one liner: get max column lengths across all records
#This will print the length of the longest field in a column across all records
# awk -F"|" '{ for (i=1; i<=NF; i++) { max[i] = length($i) > max[i] ? length($i) : max[i] ;ncols = i > ncols ? i : ncols }} END { for (col=1; col <= ncols; col++) {printf "%d\n", max[col]}}'
##This example has pipe separated record adjust field separator as needed
##This example only prints the max length of each column (in order) with a CR, adjust printf statement as needed
### Sample data ####
FOO='
Host|SysID|DevID|Name|Model|Part ID|UUID|LABEL|FS|MNT|Bootable
node01ob20|pk3fqr|73|sda|KINGSTON SV300S3|48|f87c08cb-c444-4f81-af8a-5ed22947f63d|efi|fat32|/boot/efi|true
node01ob20|pk3fqr|73|sda|KINGSTON SV300S3|49|3a8024f2-0aa0-4005-8400-cf8d4a3d175d|root|ext4|/|false
@ThinGuy
ThinGuy / juju-missing-gpg
Last active June 16, 2018 01:18
Fix missing GPG key/fix NO PUBKEY errors while juju is deploying
juju-fix-missing-gpg-keys() {
#search juju debug log for missing GPG keys and apply them to all servers in the model
local -a MISSING_KEYS=($((juju debug-log --no-tail --replay|awk '/GPG error/{print $NF}')|sort -u))
local -a MACHINES=($(juju machines|awk '/^[0-9]+/&&/started/{print $1}'))
for k in ${MISSING_KEYS[@]};do printf "%s $k\n" ${MACHINES[@]};done|\
xargs -n2 -P0 bash -c 'juju 2>/dev/null ssh $0 sudo apt-key 2>/dev/null adv --recv-key --keyserver keyserver.ubuntu.com $1'
}
@ThinGuy
ThinGuy / maas-ready-nics.src
Last active June 18, 2018 18:45
Print relevant NIC information from Ready MaaS nodes, Useful when automating the creation of bridges, bonds, etc.
maas-ready-nic-info () {
( printf "System|ID|VLAN|Tag|MAC|Name|Child of|Parent to|Hostname|Available Subnets on current VLAN\n";
sudo -u postgres psql -P pager=off -F"|" --no-align -t maasdb -c \
"SELECT node.system_id,interface.id,interface.vlan_id,vlan.vid,interface.mac_address,interface.name,ifparent.parent_id,ifchild.child_id,node.hostname, array_to_string(array_agg(distinct subnet.cidr),',') AS niclist \
FROM maasserver_interface interface \
LEFT OUTER JOIN maasserver_vlan vlan ON vlan.id = interface.vlan_id \
LEFT OUTER JOIN maasserver_subnet subnet ON subnet.vlan_id = interface.vlan_id \
LEFT OUTER JOIN maasserver_node node ON interface.node_id = node.id \
LEFT OUTER JOIN maasserver_interfacerelationship ifparent ON interface.id = ifparent.child_id \
LEFT OUTER JOIN maasserver_interfacerelationship ifchild ON interface.id = ifchild.parent_id \
@ThinGuy
ThinGuy / insert-boot-param
Last active August 15, 2018 00:52
MAAS: Insert a new boot parameter into all existing tags' kernel_opts
maas-insert-boot-param() {
local DESC="\e[1m${FUNCNAME}\e[0m: Insert a boot paramter into a specific, or all existing tags.\n"
maas-insert-boot-param_usage() {
printf "\n${DESC}\n"
printf "\e[1mUsage\e[0m: ${FUNCNAME%%_*} -p <param> [-t <tag>]\n\n"
printf "\e[1mOptions\e[0m:\n"
printf "\e[2G -p, --param \e[20GRequired: boot parameter to add\n"
printf "\e[20GEx: apparmor=0\n"
printf "\e[20GTip: Enclose multiple params in single quotes\n\n"
printf "\e[2G -t, --tag \e[20GOptional: Add only to a specific tag\n"
@ThinGuy
ThinGuy / os_functions.sh
Created February 19, 2018 19:04
Basic openstack tests as functions
####OpenStack Test Functions######
#This file should be sourced, not ran
###Prettyfication section####
tstatus() {
RC=$(echo $?)
[[ $RC -eq 0 ]] && { printf '\e[75G\e[1;37m[ \e[1;32mOK\e[1;37m ]\e[0m\n';return $RC; }
[[ $RC -eq 1 ]] && { printf '\e[75G\e[1;37m[\e[1;31mFAIL\e[1;37m]\e[0m\n';return $RC; }
}
@ThinGuy
ThinGuy / loc-info.sh
Created September 12, 2018 18:52
Location info via web api
curl -sSL https://timezoneapi.io/api/ip|\
jq -r 'keys[0] as $k|.[$k]|to_entries[]|select((.key != "timezone" and .key != "datetime"))|"\(.key): \(.value)"'
@ThinGuy
ThinGuy / snap-refresh.sh
Last active October 5, 2018 15:58
Update all snaps in parallel
# This command will refresh all installed snaps in parallel.
# If snap was installed with classic confinment, but snap list
# does not report that fact, just put that snap in the first awk
# if statement (pipe separated)
snap list|awk '/^[a-z]/{if (/classic|charm/) print $1 " --classic";if (!/classic/) print $1}'|xargs -L1 -P0 sudo snap refresh
@ThinGuy
ThinGuy / showrgb-func
Last active October 6, 2018 17:29
run showrgb and show the name in the appropriate RGB color
#Updated to print full names when they include spaces and to display in columns
showrgb| \
awk '{printf "%s", "\"\\e[1;38;2;"$1";"$2";"$3"m"; for(i=4;i<=NF;i++){printf "%s ", $i};printf "\\e[0m\"\n"}'| \
xargs -L1 echo -e| \
column
@ThinGuy
ThinGuy / spacer.sh
Last active October 7, 2018 00:56
small function to create n spaces between words. Useful with utils like sed/awk/cat
sp() { printf "%0.0s " $(seq 1 $1); }
Example:
# Say you need to add two lines near the end of /etc/security/limits.conf
Orig file:
#<domain> <type> <item> <value>
#
#* soft core 0
#root hard core 100000