added bin and wrote backup script, restore isnt finished

This commit is contained in:
LuKe Tidd 2022-01-14 14:19:09 -05:00
parent e15a76ec40
commit d6c388bfb5
15 changed files with 378 additions and 0 deletions

35
backup Executable file
View File

@ -0,0 +1,35 @@
#!/bin/bash
declare -A to_copy to_rsync
repo="$(pwd)"
# files to copy
to_copy["${HOME}/.bashrc"]='shell/bashrc'
# directories to copy
to_rsync["${HOME}/.config/aliases"]='aliases'
# directories to copy
to_rsync["${HOME}/bin"]='bin'
for src in "${!to_copy[@]}"; do
dst="${to_copy[$src]}"
dir="$(dirname "$dst")"
file="$(basename "$dst")"
if [[ ! -d "${repo}/${dir}" ]]; then
mkdir -p "${repo}/${dir}"
fi
rsync -a "$src" "${repo}/${dir}/${file}"
if [[ $? != 0 ]]; then
printf 'Copying "%s" to "%s" had an error.\n' "$src" "$dst" >&2
fi
done
for src in "${!to_rsync[@]}"; do
dst="${to_rsync[$src]}"
if [[ ! -d "$src" ]]; then
printf '"%s" does not exist. Can not copy.\n' "$src" >&2
else
(cd "$src" && rsync -a * "${repo}/${dst}/")
fi
done

7
bin/chrome Executable file
View File

@ -0,0 +1,7 @@
#!/bin/bash
logfile="/dev/shm/chrome_log_$$"
google-chrome-stable --enable-features=UseOzonePlatform --ozone-platform=wayland "$@" &> "$logfile"
if [[ $? != 0 ]]; then
printf 'Check logfile: %s\n' "$logfile"
exit $?
fi

1
bin/mtx Symbolic link
View File

@ -0,0 +1 @@
tmux_control

1
bin/mx Symbolic link
View File

@ -0,0 +1 @@
tmux_control

4
bin/nethup Executable file
View File

@ -0,0 +1,4 @@
#!/bin/bash
doas systemctl restart iwd
doas systemctl restart dhcpcd

160
bin/power Executable file
View File

@ -0,0 +1,160 @@
#!/usr/bin/env python3
import argparse
import logging
import pexpect
import pprint
import sys
class power(object):
def __init__(self):
self.strip_handle = None
self.usb_port_to_device = {
'1': 'pi3battery'
}
self.usb_device_to_port = self.reverse_usb_ports()
self.strip_port_to_device = {
'AA2': '10g_netgear',
'AA6': 'monitor',
'AA16': 'ndanknasty',
'AA17': 'muppet',
'AA19': 'usb_hub',
'AA21': 'ultra_arikui',
'AA22': 'nodegrid',
'AA23': 'nodegrid:PS2',
'AA24': 'office_wap_poe',
'AA25': 'tray_switch',
'AA26': 'den_wap_poe',
'AA27': 'studio_wap_poe',
'AA28': '1g_netgear',
'AA29': 'danknasty',
}
self.strip_device_to_port = self.reverse_strip_ports()
print(self.strip_port_to_device.values())
self.device_types = {'rpm': device for device in self.strip_port_to_device.values()}
for i in self.device_types:
print(i, self.device_types[i])
sys.exit(1)
def list_devices(self):
print('Configured devices:')
for device in self.strip_device_to_port:
print(f' {device}')
def list_ports(self):
print('Configured ports:')
for port in self.strip_port_to_device:
print(f' {port}')
def strip_command(self, cmd):
if not self.strip_handle:
self.strip_connect()
self.strip_con
def process(self, args):
if args.debug:
logging.basicConfig(level=logging.DEBUG)
logging.debug('Logging set to DEBUG')
logging.debug(args)
if args.on:
logging.debug(f'on: {args.on}')
self.on(args.on)
if args.off:
logging.debug(f'off: {args.off}')
self.off(args.off)
if args.listall:
logging.debug('displaying all config')
self.list_all()
def list_all(self):
print()
print('Power Strip:')
for k, v in self.strip_device_to_port.items():
print(k, ', '.join(v))
print()
print('USB Hub:')
for k, v in self.usb_device_to_port.items():
print(k, v)
def on(self, device_list):
devices = device_list.split(',')
for device in devices:
if device not in self.strip_device_to_port:
logging.error(f'"{device}" not a known device.')
self.list_devices()
sys.exit(1)
def off(self, args):
sys.exit('bye')
devices = device_list.split(',')
logging.debug(f'turning off {devices}')
def status(self, args):
pass
def reverse_usb_ports(self):
return {device: port for (port, device) in self.usb_port_to_device.items()}
def reverse_strip_ports(self):
strip_device_to_port = {}
for strip_port, device in self.strip_port_to_device.items():
if device not in strip_device_to_port:
strip_device_to_port[device] = []
strip_device_to_port[device].append(strip_port)
return strip_device_to_port
def exp(self, p, results, timeout=30):
results[pexpect.TIMEOUT] = 'timeout'
results[pexpect.EOF] = 'disconnect'
list_res = list(results.keys())
res = p.expect(list_res, timeout=timeout)
text_res = results[list_res[res]]
if text_res == 'timeout':
sys.exit('timeout')
elif text_res == 'disconnect':
sys.exit('disconnect')
return text_res
def ssh_connect(self, host):
cmd = ['ssh', host]
p = pexpect.spawnu(' '.join(cmd))
p.logfile = sys.stderr
results = {'Password: ': 'prompt'}
res = exp(p, results)
p.sendline('admn')
results = {'Switched PDU: ': 'prompt'}
res = exp(p, results)
def main():
parser = argparse.ArgumentParser()
actions = parser.add_argument_group('actions')
action = actions.add_mutually_exclusive_group(required=True)
action.add_argument('--status', metavar='DEVICES', help='get power status of a device', type=str)
action.add_argument('--off', metavar='DEVICES', help='turn off device', type=str)
action.add_argument('--on', metavar='DEVICES', help='turn on device', type=str)
action.add_argument('--reboot', metavar='DEVICES', help='reboot a device', type=str)
action.add_argument('--portoff', metavar='PORTS', help='turn off a port', type=str)
action.add_argument('--porton', metavar='PORTS', help='turn on port', type=str)
action.add_argument('--listdev', help='list devices', action='store_true')
action.add_argument('--listport', help='list ports', action='store_true')
action.add_argument('--listall', help='show all config', action='store_true')
options = parser.add_argument_group('options')
options.add_argument('--output', help='choose output: text, json', type=str, choices=['text', 'json'])
options.add_argument('--debug', help='turn on debug output', action='store_true')
args = parser.parse_args()
p = power()
p.process(args)
if __name__ == '__main__':
main()

2
bin/record_nosound Executable file
View File

@ -0,0 +1,2 @@
#!/bin/bash
wf-recorder -g "$(slurp)" --file=$(date "+%s").mp4

2
bin/record_sound Executable file
View File

@ -0,0 +1,2 @@
#!/bin/bash
wf-recorder -g "$(slurp)" --audio --file=$(date "+%s").mp4

1
bin/stx Symbolic link
View File

@ -0,0 +1 @@
tmux_control

1
bin/sx Symbolic link
View File

@ -0,0 +1 @@
tmux_control

1
bin/tls Symbolic link
View File

@ -0,0 +1 @@
tmux_control

128
bin/tmux_control Executable file
View File

@ -0,0 +1,128 @@
#!/usr/bin/env bash
function link_or_rm() {
local env="$1"
local tmp="$2"
if [[ -z "${env}" ]]; then
# No auth sock; remove symlink, if any.
rm -f -- "${tmp}"
elif [[ "${env}" != "${tmp}" ]]; then
# Construct expected symlink to point to auth sock.
ln -snf -- "${env}" "${tmp}"
fi
}
# Get invocation, stx or mtx
zero="$0"
invocation=$(basename "$zero")
case "$invocation" in
tmux_control)
declare -a tmux_links
tmux_links=( stx mtx sx mx tls )
for link in "${tmux_links[@]}"; do
if [[ ! -h "${HOME}/bin/${link}" ]]; then
(cd "${HOME}/bin" && ln -s "tmux_control" "${link}")
fi
done
echo 'stx or sx for inner sessions; mtx or mx for outer sessions, tls to list all sessions' >&2
exit 1
;;
sx)
socket="tmux_${USER}_stx"
tmux -L "${socket}" "$@"
exit $?
;;
mx)
socket="tmux_${USER}_mtx"
tmux -L "${socket}" "$@"
exit $?
;;
tls)
declare -a sockets
readarray -d '' sockets < <(find "/tmp/tmux-$(id -u)" -maxdepth 1 -type s -print0)
for socket in ${sockets[*]}; do
printf '%s\n' "$socket"
tmux -L "$(basename "${socket}")" list-sessions | sed 's/^/ /'
done
exit 0
esac
socket="tmux_${USER}_${invocation}"
style="inner"
if [[ "${invocation}" == mtx ]]; then
style="outer"
fi
config="${HOME}/.tmux.conf_${style}"
session_name="default"
if [[ -n $1 ]]; then
session_name="$1"
fi
if [[ "$1" == "test" ]]; then
echo "invocation: ${invocation}"
echo "config: ${config}"
echo "session: ${session_name}"
exit 0
fi
if [[ ! -f "${config}" ]]; then
echo "Didn't find ${config}" >&2
exit 1
fi
# Make the temp directory if it doesn't exist
home_sock_folder="${HOME}/.tmp"
if ! [ -d "${home_sock_folder}" ]; then
mkdir -m 700 "${home_sock_folder}"
fi
# Symlinks to sockets that might change out from under tmux
if [[ -z "${TMUX}" ]] || [[ "$style" == "inner" && "$TMUX" =~ mtx ]]; then
ssh_auth_sock_symlink="${home_sock_folder}/ssh_auth_sock"
fwd_ssh_sock_symlink="${home_sock_folder}/fwd_ssh_auth_sock"
sway_sock_symlink="${home_sock_folder}/sway_sock"
i3_sock_symlink="${home_sock_folder}/i3_sock"
link_or_rm "${SSH_AUTH_SOCK}" "${ssh_auth_sock_symlink}"
link_or_rm "${FWD_SSH_AUTH_SOCK}" "${fwd_ssh_sock_symlink}"
# SWAYSOCK == I3SOCK, probably some Xwayland compatability thing
link_or_rm "${SWAYSOCK}" "${sway_sock_symlink}"
link_or_rm "${SWAYSOCK}" "${i3_sock_symlink}"
# See if there are any sessions on this socket
echo running: tmux -L "${socket}" list-sessions
tmux -L "${socket}" list-sessions
echo results: $?
if [[ $? != 0 ]]; then
# No tmux session @ $socket
exec env \
I3SOCK="${i3_sock_symlink}" \
SWAYSOCK="${sway_sock_symlink}" \
SSH_AUTH_SOCK="${ssh_auth_sock_symlink}" \
FWD_SSH_AUTH_SOCK="${f}" \
tmux -f "${config}" -L "${socket}" ${args} new-session -s "${session_name}"
elif tmux -L "${socket}" list-sessions 2>/dev/null | grep "^${session_name}:" &>/dev/null; then
# A named session exists
exec env \
I3SOCK="${i3_sock_symlink}" \
SWAYSOCK="${sway_sock_symlink}" \
SSH_AUTH_SOCK="${ssh_auth_sock_symlink}" \
FWD_SSH_AUTH_SOCK="${f}" \
tmux -f "${config}" -L "${socket}" ${args} attach -t "${session_name}"
else
# Some session lives at that socket but it's a different name than is given
exec env \
I3SOCK="${i3_sock_symlink}" \
SWAYSOCK="${sway_sock_symlink}" \
SSH_AUTH_SOCK="${ssh_auth_sock_symlink}" \
FWD_SSH_AUTH_SOCK="${f}" \
tmux -f "${config}" -L "${socket}" ${args} new-session -s "${session_name}"
fi
else
echo "Already in a tmux session. Giving up."
exit 1
fi

35
restore Executable file
View File

@ -0,0 +1,35 @@
#!/bin/bash
declare -A to_copy to_rsync
repo="$(pwd)"
# files to copy
to_copy["${HOME}/.bashrc"]='shell/bashrc'
# directories to copy
to_rsync["${HOME}/.config/aliases"]='aliases'
# directories to copy
to_rsync["${HOME}/bin"]='bin'
for dst in "${to_copy[@]}"; do
src="${to_copy[$dst]}"
dir="$(dirname "$src")"
file="$(basename "$src")"
if [[ ! -d "${dir}" ]]; then
mkdir -p "${dir}"
fi
(cd "$dir" && rsync -a "$file" "${dir}"
if [[ $? != 0 ]]; then
printf 'Copying "%s" to "%s" had an error.\n' "$src" "$dst" >&2
fi
done
for dst in "${to_rsync[@]}"; do
src="${to_rsync[$dst]}"
if [[ ! -d "$dst" ]]; then
printf '"%s" does not exist. Can not copy.\n' "$src" >&2
else
(cd "$src" && rsync -a * "${repo}/${dst}/")
fi
done