-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.sh
125 lines (111 loc) · 3.56 KB
/
common.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# shellcheck shell=bash
[[ "$DEBUG" ]] && set -x
# https://devhints.io/bash
set -euo pipefail # strict
IFS=$'\n\t'
traperr() {
echo "ERROR: ${BASH_SOURCE[1]} at about ${BASH_LINENO[0]}"
}
case $(readlink /proc/$$/exe) in
/bin/bash)
set -o errtrace
trap traperr ERR
;;
esac
function assertDir() {
local dir="${1:?'Please specify a directory!'}"
if [ ! -d "$dir" ]; then
echo "$dir not found"
exit 2 # ENOENT No such file or directory
fi
}
function assertFile() {
local file="${1:?'Please specify a file!'}"
if [ ! -s "$file" ]; then
echo "$file not found"
exit 2 # ENOENT No such file or directory
fi
}
function printFunction() {
local padlimit='80'
local text="$*"
local pad
# shellcheck disable=SC2183
pad="$(printf '%*s' "$padlimit")"
pad="${pad// /-}"
printf '%s' "$text "
printf '%*.*s\n' 0 $((padlimit - "${#text}")) "$pad"
}
# may exceed printFunction's width by 25%
# uses more visually distinctive padding char
function printHeading() {
local padlimit='100'
local text="$*"
local pad
# shellcheck disable=SC2183
pad="$(printf '%*s' "$padlimit")"
pad="${pad// /=}"
printf '%s' "$text "
printf '%*.*s\n' 0 $((padlimit - "${#text}")) "$pad"
}
# shellcheck disable=SC2068
function runCMD() {
echo 'λ' $@
$@
}
function mkpw() {
LC_ALL=C tr -dc '[:graph:]' </dev/urandom | head -c "${1:-24}"
}
function prepareSSH() {
printFunction 'preparing ssh'
local privateKeyData="${1:-"${SSH_KEY:?'Need private key!'}"}"
local privateKeyName="${2:-id_ed25519}"
mkdir -p ~/.ssh
echo "$privateKeyData" >~/.ssh/"$privateKeyName"
chmod 0400 ~/.ssh/"$privateKeyName"
}
function prepareGIT() {
printFunction 'preparing git'
local repo_path="${1:?'Please specify a git repository!'}"
local git_mail="${2:-"${MAILBOX:?'Please specify your mail address!'}"}"
local git_name="${3:-"${GITNAME:?'Please specify your user name!'}"}"
cd "$repo_path" || (echo "$repo_path does not exist" && exit 2) # ENOENT
git config --global --add safe.directory "$(pwd)"
runCMD git config --global user.email "$git_mail"
runCMD git config --global user.name "$git_name"
}
function prepareGPG() {
printFunction "preparing gnupg and it's trustdb"
local gpg_pair="${1:?'Please specify a gpg key pair!'}"
echo "$gpg_pair" | gpg --import
gpg --list-keys --with-colons | awk -F: '/fpr:/ {print $10":6:"}' | gpg --import-ownertrust
gpg --check-trustdb
}
function commitSigned() {
printFunction 'committing changes'
local message="${1:?'Please specify a commit message!'}"
runCMD git add .
runCMD git commit --gpg-sign -m "$message"
}
function initBranch() {
printFunction 'initializing ophaned git branch with secret.gpg'
local branchName="${1:?'Please specify a branch name!'}"
local recipient="${2:?'Please specify a recipient!'}"
runCMD git switch --discard-changes --orphan "$branchName"
runCMD git rm --cached -r . || true
runCMD git clean -df .??* . || true
mkpw 32 | gpg --encrypt --sign --armor --recipient "$recipient" | tee secret.gpg || true
commitSigned "init"
}
function tracerouteSSH() {
printFunction 'tracing route to ssh port of nodes'
local sourceFile="${1:?'Please specify a source file!'}"
local portNumber="${2:-22}"
local searchPatt='(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)'
mkfifo addresses
grep -E -o "$searchPatt" "$sourceFile" >addresses &
while IFS= read -r ipv4; do
runCMD mtr --report-wide --no-dns --tcp --port "$portNumber" "$ipv4"
done <addresses
rm addresses
}