-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaliases
317 lines (242 loc) · 10 KB
/
aliases
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#!/bin/bash
# look for NOMAD_ADDR and NOMAD_TOKEN
[ -e $HOME/.config/nomad ] && source $HOME/.config/nomad
# If not running interactively, don't setup autocomplete
if [ ! -z "$PS1" ]; then
# nomad/consul autocompletes
if [ "$ZSH_VERSION" = "" ]; then
which nomad >/dev/null && complete -C $(which nomad) nomad
which consul >/dev/null && complete -C $(which consul) consul
else
# https://apple.stackexchange.com/questions/296477/
( which compdef 2>&1 |fgrep -q ' not found' ) && autoload -Uz compinit && compinit
which nomad >/dev/null && autoload -U +X bashcompinit && bashcompinit
which nomad >/dev/null && complete -o nospace -C $(which nomad) nomad
which consul >/dev/null && complete -o nospace -C $(which consul) consul
fi
fi
function nom-app() {
# finds the webapp related to given job/CWD and opens it in browser
[ $# -eq 1 ] && JOB=$1
[ $# -ne 1 ] && JOB=$(nom-job-from-cwd)
_nom-url
URL=$(echo "$URL" |head -1)
[ "$URL" = "" ] && echo "URL not found - is service running? try:\n nomad status $JOB" && return
open "$URL"
}
function nom-ssh() {
# simple way to pop in (ssh-like) to a given job
# Usage: [job name, eg: x-thumb] -OR- no args will use CWD to determine job
[ $# -ge 1 ] && JOB=$1
[ $# -lt 1 ] && JOB=$(nom-job-from-cwd)
[ $# -ge 2 ] && TASK=$2 # for rarer TaskGroup case where 2+ Tasks spec-ed in same Job
[ $# -lt 2 ] && TASK=http
ALLOC=$(nomad job status $JOB |egrep -m1 '\srun\s' |cut -f1 -d' ')
echo "nomad alloc exec -i -t -task $TASK $ALLOC"
if [ $# -ge 3 ]; then
shift
shift
nomad alloc exec -i -t -task $TASK $ALLOC "$@"
else
nomad alloc exec -i -t -task $TASK $ALLOC \
sh -c '([ -e /bin/zsh ] && zsh) || ([ -e /bin/bash ] && bash) || ([ -e /bin/sh ] && sh)'
fi
}
function nom-sshn() {
# simple way to pop in (ssh-like) to a given job with 2+ allocations/containers
local N=${1:?"Usage: [container/allocation number, starting with 1]"}
local ALLOC=$(nomad job status $JOB |egrep '\srun\s' |head -n $N |tail -1 |cut -f1 -d' ')
echo "nomad alloc exec -i -t $ALLOC"
nomad alloc exec -i -t $ALLOC \
sh -c '([ -e /bin/zsh ] && zsh) || ([ -e /bin/bash ] && bash) || ([ -e /bin/sh ] && sh)'
}
function nom-cp() {
# copies a laptop local file into running deploy (avoids full pipeline just to see changes)
# first, see if this is vscode sync-rsync
local VSCODE=
[ "$#" -ge 4 ] && ( echo "$@" |fgrep -q .vscode ) && VSCODE=1
if [ $VSCODE ]; then
# fish out file name from what VSCode 'sync-rsync' package sends us -- should be 2nd to last arg
local FILE=$(echo "$@" |rev |tr -s ' ' |cut -f2 -d' ' |rev)
# switch dirs to make aliases work
local DIR=$(dirname "$FILE")
cd "$DIR"
local BRANCH=$(git rev-parse --abbrev-ref HEAD)
local JOB=$(nom-job-from-cwd)
local ALLOC=$(nom-job-to-alloc)
local TASK=http
cd -
else
local FILE=${1:?"Usage: [src file, locally qualified while 'cd'-ed inside a repo]"}
local BRANCH=$(git rev-parse --abbrev-ref HEAD)
local JOB=$(nom-job-from-cwd)
local ALLOC=$(nom-job-to-alloc)
[ $# -ge 2 ] && TASK=$2 # for rarer TaskGroup case where 2+ Tasks spec-ed in sam Job
[ $# -lt 2 ] && TASK=http
fi
# now split the FILE name into two pieces -- 'the root of the git tree' and 'the rest'
local DIR=$(dirname "$FILE")
local TOP=$(git -C "$DIR" rev-parse --show-toplevel)
local REST=$(echo "$FILE" | perl -pe "s|^$TOP||; s|^/+||;")
for var in FILE DIR TOP REST BRANCH JOB ALLOC; do
echo $var="${(P)var}"
done
echo
if [ $VSCODE ]; then
local MAIN=
[ "$BRANCH" = "main" ] && MAIN=true
[ "$BRANCH" = "master" ] && MAIN=true
local RSYNC=
[ $MAIN ] && RSYNC=true
[ ! $MAIN ] && [ "$RSYNC_BRANCHES" ] && RSYNC=true
[ $RSYNC ] && ( set -x; rsync "$@" )
# this is a special exception project where we DONT want to ALSO copy file to nomad deploy
[ $MAIN ] && [ "$JOB" = "ia-petabox" ] && [ ! $NOM_CP_PETABOX_MAIN ] && exit 0
fi
if [ "$JOB" = "" -o "$ALLOC" = "" ]; then
# no relevant job & alloc found - nothing to do
echo 'has this branch run a full pipeline and deployed a Review App yet?'
return
fi
# HinD updated nomad clusters w/ latest nomad seem to *not* get the stdin close properly
# (and thus hang). So timeout/kill after 2s :( tracey 2024/3 )
set +e
cat "$FILE" | ( set -x; set +e; nomad alloc exec -i -task $TASK "$ALLOC" sh -c "timeout 2 cat >| '$REST'" )
echo SUCCESS
}
function nom-logs() {
# simple way to view logs for a given job
[ $# -eq 1 ] && JOB=$1
[ $# -ne 1 ] && JOB=$(nom-job-from-cwd)
# NOTE: the 2nd $JOB is useful for when a job has 2+ tasks (eg: `kv` or DB/redis, etc.)
nomad alloc logs -f -job $JOB http
}
function nom-logs-err() {
# simple way to view logs for a given job
[ $# -eq 1 ] && JOB=$1
[ $# -ne 1 ] && JOB=$(nom-job-from-cwd)
nomad alloc logs -stderr -f -job $JOB http
}
function nom-status() {
# prints detailed status for a repo's service and deployment
[ $# -eq 1 ] && JOB=$1
[ $# -ne 1 ] && JOB=$(nom-job-from-cwd)
line
echo "nomad status $JOB"
line
nomad status $JOB | grep --color=always -iE 'unhealthy|healthy|$'
line
echo 'nomad alloc status -stats $(nom-job-to-alloc '$JOB')'
line
nomad alloc status -stats $(nom-job-to-alloc $JOB) | grep --color=always -iE 'unhealthy|healthy|Job Version.*|Node Name.*|$'
line
}
function nom-urls() {
# Lists all current urls for the services deployed to current nomad cluster (eg: webapps)
# Ideally, this is a faster single-shot call. But to avoid requiring either `consul` addr
# and ACL token _in addition_ to `nomad` - we'll just use `nomad` directly instead.
# consul catalog services -tags
for JOB in $(curl -sH "X-Nomad-Token: ${NOMAD_TOKEN?}" ${NOMAD_ADDR?}/v1/jobs \
| jq -r '.[] | select(.Type=="service") | "\(.Name)"')
do
_nom-url
echo $URL
done |sort
}
function _nom-url() {
# logically private helper function
URL=$(curl -sH "X-Nomad-Token: ${NOMAD_TOKEN?}" ${NOMAD_ADDR?}/v1/job/$JOB \
| jq -r '.TaskGroups[0].Services[0].Tags' \
| fgrep . |fgrep -v redirect=308 |tr -d '", ' |perl -pe 's/:443//; s=^urlprefix\-=https://=;'
)
}
function nom-resubmit() {
# Retrieves current job spec from nomad cluster and resubmits it to nomad.
# Useful for when a job has exceedded a setup timeout, is (nonideally) marked 'dead', etc.
[ $# -eq 1 ] && JOB=$1
[ $# -ne 1 ] && JOB=$(nom-job-from-cwd)
nomad inspect ${JOB?} |perl -pe 's/"Stop": true,//' |tee .$JOB
# in case we are trying to _move_ an active/OK deploy
nomad stop ${JOB?}
sleep 5
curl -XPOST -H "Content-Type: application/json" -H "X-Nomad-Token: $NOMAD_TOKEN" -d @.${JOB?} \
$NOMAD_ADDR/v1/jobs
rm -f .$JOB
}
function d() {
# show docker running containers and local images
[ "$#" = "0" ] && clear -x
local SUDO=
[ $(uname) = "Linux" ] && local SUDO=sudo
[ ! -e /usr/bin/docker ] && local docker=podman
$SUDO $docker ps -a --format "table {{.ID}}\t{{.Image}}\t{{.Status}}\t{{.Names}}\t{{.State}}" | $SUDO cat >| $HOME/.dockps
chmod 666 $HOME/.dockps
for i in STATE running restarting created paused removing exited dead; do
cat $HOME/.dockps |egrep "$i$" |perl -pe 's/'$i'$//'
done
rm -f $HOME/.dockps
line
$SUDO $docker images
}
function nom() {
# quick way to get an overview of a nomad server when ssh-ed into it
d
line
nomad server members
line
nomad status
line
}
function nom-job-from-cwd() {
# print the nomad job name based on the current project
# parse out repo info, eg: 'ia-petabox' -- ensure clone-over-ssh or clone-over-https work
local GURL TMP GROUP_PROJECT PROJECT BRANCH SLUG JOB
GURL=$(git config --get remote.origin.url)
[[ "$GURL" =~ https:// ]] && TMP=$(echo "$GURL" |cut -f4- -d/)
[[ "$GURL" =~ https:// ]] || TMP=$(echo "$GURL" |rev |cut -f1 -d: |rev)
GROUP_PROJECT=$(echo "$TMP" |perl -pe 's/\.git//' |tr A-Z a-z |tr / -)
PROJECT=$(git rev-parse --absolute-git-dir |egrep --color -o '.*?.git' |rev |cut -f2 -d/ |rev)
BRANCH=$(git rev-parse --abbrev-ref HEAD)
SLUG=$(echo "$BRANCH" |tr '/_.' '-' |tr A-Z a-z)
JOB=$GROUP_PROJECT
[ "$SLUG" = "main" -o "$SLUG" = "master" -o "$SLUG" = "staging" -o "$SLUG" = "production" ] || JOB="${JOB}-${SLUG}"
if [ $NOM_JOB_SUFFIX_RM_EXT ]; then
JOB=$(echo "$JOB" |perl -pe 's/\-ext$/$1/')
fi
echo $(echo "$JOB" |cut -b1-63)
}
function nom-image-from-cwd() {
# print the registry image based on the current project
# parse out repo info, eg: 'ia-petabox' -- ensure clone-over-ssh or clone-over-https work
local GURL GROUP_PROJECT BRANCH SLUG JOB
GURL=$(git config --get remote.origin.url)
[[ "$GURL" =~ https:// ]] && GROUP_PROJECT=$(echo "$GURL" |cut -f4- -d/)
[[ "$GURL" =~ https:// ]] || GROUP_PROJECT=$(echo "$GURL" |rev |cut -f1 -d: |rev)
BRANCH=$(git rev-parse --abbrev-ref HEAD)
SLUG=$(echo "$BRANCH" |tr '/_.' '-' |tr A-Z a-z)
echo $(echo "registry.archive.org/$GROUP_PROJECT/$SLUG" |cut -b1-63)
}
function nom-job-to-alloc() {
# prints alloc of a given job (when in high-availability and 2+ allocations, picks one at random)
# Usage: [job name, eg: x-thumb] -OR- no args will use CWD to determine job
[ $# -eq 1 ] && JOB=$1
[ $# -ne 1 ] && JOB=$(nom-job-from-cwd)
nomad job status $JOB |egrep -m1 '\srun\s' |cut -f1 -d' '
}
function line () {
# horizontal line break
perl -e 'print "_"x100; print "\n\n";'
}
function nom-tunnel() {
# Sets up an ssh tunnel in the background to be able to talk to nomad cluster's consul.
[ "$NOMAD_ADDR" = "" ] && echo "Please set NOMAD_ADDR environment variable first" && return
local HOST=$(echo "$NOMAD_ADDR" | sed 's/:4646\/*$//' |sed 's/^https*:\/\///')
ssh -fNA -L 8500:localhost:8500 $HOST
}
function web-logs-tail() {
# admin script that can more easily "tail -f" the caddy (JSON) web logs
(
set -x
tail -f /var/log/caddy/access.log | jq -r '"\(.request.host)\(.request.uri)\t\t\(.request.headers."User-Agent")"'
)
}