-
Notifications
You must be signed in to change notification settings - Fork 1
/
oh-my-guish.plugin.zsh
945 lines (816 loc) · 30.5 KB
/
oh-my-guish.plugin.zsh
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
# Functions
function lbuild() {
python3 ../SwaggerCodegen/buildScripts/build.py local --microservice_name ${1%_SERVER/}
}
## Yoink Doink
function yoink() {
open -R $1
osascript -e 'tell application "System Events" to keystroke "c" using command down'
osascript -e 'tell application "System Events" to keystroke "w" using command down'
osascript -e 'tell application "System Events" to keystroke tab using command down'
}
function doink() {
open .
osascript -e 'tell application "System Events" to keystroke "v" using command down'
osascript -e 'tell application "System Events" to keystroke "w" using command down'
osascript -e 'tell application "System Events" to keystroke tab using command down'
}
## Python
function piprequires() {
if [[ -z "$1" ]]; then
echo "You are missing the package name!"
else
pip3 index versions $1 | grep LATEST | cut -d ":" -f 2 | tr -s " " | cut -d " " -f 2 | xargs -n1 -I % curl https://pypi.org/pypi/$1/%/json | jq '.info.requires_dist'
fi
}
function pipsha_download_latest() {
if [[ -z "$1" ]]; then
echo "You are missing the package name!"
else
latest_version=$(pip3 index versions $1 | grep LATEST | cut -d ":" -f 2 | tr -s " " | cut -d " " -f 2)
echo $latest_version | xargs -n1 -I % curl https://pypi.org/pypi/$1/%/json | jq -r '.releases."'$latest_version'"[] | select(.packagetype == "sdist") | "\(.digests.sha256) \(.url)"' | xargs zsh -c 'echo "url \"$1\"\nsha256 \"$0\""'
fi
}
## Git
function grw() {
git reset $(glc)
}
function glc() {
if [[ -z "$1" ]]; then
git log --oneline | head -n 2 | tail +2 | cut -d " " -f 1
else
git log --oneline | head -n $((2 + $1)) | tail +$((2 + $1)) | cut -d " " -f 1
fi
}
## Docker
function docker_build_run() {
buildoutput=$(docker build .)
echo $buildoutput | tail -1 | cut -d " " -f 3 | xargs -n 1 -I % docker run -p 8080:80 %
}
## Math
function avg() {
if (($# == 0)); then
array=$(cat)
else
array=$@
fi
count=0
total=0
IFS=$'\n'
arr=($(echo -e "$array"))
for i in ${arr[@]}; do
total=$(echo $total+$i | bc)
((count++))
done
echo "scale=2; $total / $count" | bc
}
function math_sum() {
if (($# == 0)); then
array=$(cat)
else
array=$@
fi
total=0
IFS=$'\n'
arr=($(echo -e "$array"))
for i in ${arr[@]}; do
re='^[+-]?[0-9]+([.][0-9]+)?$'
if ! [[ $i =~ $re ]]; then
echo "error: '$i' is not a number" >&2
break
fi
total=$(echo $total+$i | bc)
done
echo $total
}
## System
function kill_group() {
ps -eo pgid,command | grep $1 | head -n 1 | sed -r 's/^([^.]+).*$/\1/; s/^[^0-9]*([0-9]+).*$/\1/' | xargs -I % kill -9 -%
}
function mkdircd() {
mkdir $1 && cd $1
}
function randomdocker() {
curl https://frightanic.com/goodies_content/docker-names.php
}
function moveallup() {
for file in $(ls); do
if [[ ! -f $file ]]; then
mv ./$file/*.* ./
fi
done
}
## K8s
function convertk8smemory() {
KUBERNETES_MEMORY=$1
OUTPUT_FORMAT=$2
if [[ -z "$OUTPUT_FORMAT" ]]; then
OUTPUT_FORMAT="G"
fi
if [[ "$KUBERNETES_MEMORY" =~ .*"G".* ]]; then
if [[ "$KUBERNETES_MEMORY" =~ .*"Gi".* ]]; then
KUBERNETES_MEMORY=$(echo "$KUBERNETES_MEMORY" | sed 's/Gi//g')
KUBERNETES_MEMORY=$(echo "$KUBERNETES_MEMORY * 1024 * 1024 * 1024" | bc -l)
else
KUBERNETES_MEMORY=$(echo "$KUBERNETES_MEMORY" | sed 's/G//g')
KUBERNETES_MEMORY=$(echo "$KUBERNETES_MEMORY * 1000 * 1000 * 1000" | bc -l)
fi
elif [[ "$KUBERNETES_MEMORY" =~ .*"M".* ]]; then
if [[ "$KUBERNETES_MEMORY" =~ .*"Mi".* ]]; then
KUBERNETES_MEMORY=$(echo "$KUBERNETES_MEMORY" | sed 's/Mi//g')
KUBERNETES_MEMORY=$(echo "$KUBERNETES_MEMORY * 1024 * 1024" | bc -l)
else
KUBERNETES_MEMORY=$(echo "$KUBERNETES_MEMORY" | sed 's/M//g')
KUBERNETES_MEMORY=$(echo "$KUBERNETES_MEMORY * 1000 * 1000" | bc -l)
fi
else
if [[ "$KUBERNETES_MEMORY" =~ .*"K".* ]]; then
KUBERNETES_MEMORY=$(echo "$KUBERNETES_MEMORY" | sed 's/Ki//g')
KUBERNETES_MEMORY=$(echo "$KUBERNETES_MEMORY * 1024" | bc -l)
else
KUBERNETES_MEMORY=$(echo "$KUBERNETES_MEMORY" | sed 's/K//g')
KUBERNETES_MEMORY=$(echo "$KUBERNETES_MEMORY * 1000" | bc -l)
fi
fi
if [[ "$OUTPUT_FORMAT" == "G" ]]; then
echo "$KUBERNETES_MEMORY / (1000 * 1000 * 1000)" | bc -l
elif [[ "$OUTPUT_FORMAT" == "Gi" ]]; then
echo "$KUBERNETES_MEMORY / (1024 * 1024 * 1024)" | bc -l
elif [[ "$OUTPUT_FORMAT" == "M" ]]; then
echo "$KUBERNETES_MEMORY / (1000 * 1000)" | bc -l
elif [[ "$OUTPUT_FORMAT" == "Mi" ]]; then
echo "$KUBERNETES_MEMORY / (1024 * 1024)" | bc -l
elif [[ "$OUTPUT_FORMAT" == "K" ]]; then
echo "$KUBERNETES_MEMORY / (1000)" | bc -l
elif [[ "$OUTPUT_FORMAT" == "Ki" ]]; then
echo "$KUBERNETES_MEMORY / (1024)" | bc -l
fi
}
function convertk8scpu() {
CPU_KUBERNETES=$1
if [[ "$CPU_KUBERNETES" =~ .*"m".* ]]; then
CPU_KUBERNETES=$(echo $CPU_KUBERNETES | sed 's/m//g')
else
CPU_KUBERNETES=$(echo "$CPU_KUBERNETES*1000" | bc -l)
fi
echo $CPU_KUBERNETES
}
function kdreq() {
namespace=$1
if [[ -z "$namespace" ]]; then
namespace=$(kubectl config view --minify -o jsonpath='{..namespace}')
fi
deployments=($(kgd -n $namespace -o json | jq -r ".items[].metadata.name"))
TOTAL_NS_CPU_REQUEST=0
TOTAL_NS_MEMORY_REQUEST=0
TOTAL_NS_CPU_LIMIT=0
TOTAL_NS_MEMORY_LIMIT=0
for deployment in $deployments; do
replicas=$(kgd -n $namespace -o json $deployment | jq ".spec.replicas")
echo "*** Deployment $deployment: ($replicas replicas) ***"
containers=($(kgd -n $namespace -o json $deployment | jq -r ".spec.template.spec.containers[].name"))
TOTAL_CPU_REQUEST=0
TOTAL_MEMORY_REQUEST=0
TOTAL_CPU_LIMIT=0
TOTAL_MEMORY_LIMIT=0
for container in $containers; do
CPU_REQUESTS=$(kgd -n $namespace -o json $deployment | jq -r ".spec.template.spec.containers[] | select(.name == \"$container\") | .resources.requests.cpu")
CPU_REQUESTS=$(convertk8scpu $CPU_REQUESTS)
MEMORY_REQUESTS=$(kgd -n $namespace -o json $deployment | jq -r ".spec.template.spec.containers[] | select(.name == \"$container\") | .resources.requests.memory")
MEMORY_REQUESTS=$(convertk8smemory $MEMORY_REQUESTS)
CPU_LIMITS=$(kgd -n $namespace -o json $deployment | jq -r ".spec.template.spec.containers[] | select(.name == \"$container\") | .resources.limits.cpu")
CPU_LIMITS=$(convertk8scpu $CPU_LIMITS)
MEMORY_LIMITS=$(kgd -n $namespace -o json $deployment | jq -r ".spec.template.spec.containers[] | select(.name == \"$container\") | .resources.limits.memory")
MEMORY_LIMITS=$(convertk8smemory $MEMORY_LIMITS)
echo "Container $container:"
echo "\tRequests: CPU: ${CPU_REQUESTS}m; Memory: ${MEMORY_REQUESTS}G"
echo "\tLimits: CPU: ${CPU_LIMITS}m; Memory: ${MEMORY_LIMITS}G"
TOTAL_CPU_REQUEST=$(echo "$TOTAL_CPU_REQUEST + ($CPU_REQUESTS * $replicas)" | bc -l)
TOTAL_MEMORY_REQUEST=$(echo "$TOTAL_MEMORY_REQUEST + ($MEMORY_REQUESTS * $replicas)" | bc -l)
TOTAL_CPU_LIMIT=$(echo "$TOTAL_CPU_LIMIT + ($CPU_LIMITS * $replicas)" | bc -l)
TOTAL_MEMORY_LIMIT=$(echo "$TOTAL_MEMORY_LIMIT + ($MEMORY_LIMITS * $replicas)" | bc -l)
done
echo "
TOTAL FOR $deployment:
CPU Requests: ${TOTAL_CPU_REQUEST}m
Memory Requests: ${TOTAL_MEMORY_REQUEST}G
CPU Limits: ${TOTAL_CPU_LIMIT}m
Memory Limits: ${TOTAL_MEMORY_LIMIT}G"
TOTAL_NS_CPU_REQUEST=$(echo "$TOTAL_NS_CPU_REQUEST + $TOTAL_CPU_REQUEST" | bc -l)
TOTAL_NS_MEMORY_REQUEST=$(echo "$TOTAL_NS_MEMORY_REQUEST + $TOTAL_MEMORY_REQUEST" | bc -l)
TOTAL_NS_CPU_LIMIT=$(echo "$TOTAL_NS_CPU_LIMIT + $TOTAL_CPU_LIMIT" | bc -l)
TOTAL_NS_MEMORY_LIMIT=$(echo "$TOTAL_NS_MEMORY_LIMIT + $TOTAL_MEMORY_LIMIT" | bc -l)
done
echo "
TOTAL FOR $namespace:
CPU Requests: ${TOTAL_NS_CPU_REQUEST}m
Memory Requests: ${TOTAL_NS_MEMORY_REQUEST}G
CPU Limits: ${TOTAL_NS_CPU_LIMIT}m
Memory Limits: ${TOTAL_NS_MEMORY_LIMIT}G"
}
function kreportns() {
namespace=$1
if [[ -z "$namespace" ]]; then
toppodsresults=$(ktp | tail +2 | tr -s " ")
else
toppodsresults=$(ktp -n $namespace | tail +2 | tr -s " ")
fi
avgcpu=$(echo $toppodsresults | cut -d " " -f 2 | cut -d "m" -f 1 | avg)
totalcpu=$(echo $toppodsresults | cut -d " " -f 2 | cut -d "m" -f 1 | math_sum)
avgmem=$(echo $toppodsresults | cut -d " " -f 3 | cut -d "M" -f 1 | avg)
totalmem=$(echo $toppodsresults | cut -d " " -f 3 | cut -d "M" -f 1 | math_sum)
echo "
NS TOTAL USAGE:
Memory (MB) = $totalmem
CPU (mCPU) = $totalcpu
NS AVG USAGE:
Memory (MB) = $avgmem
CPU (mCPU) = $avgcpu
"
}
function kreport() {
# getting data from K8s
topnoderesults=$(ktopno | tail +2 | tr -s " ")
allpods=$(kgpall | tail +2)
nodeinformation=$(kgno -o json)
# transforming data
numallpods=$(echo $allpods | wc -l)
numsystempods=$(echo $allpods | grep kube-system | wc -l)
numpodsnotsystem=$(echo $allpods | grep -v kube-system | wc -l)
cpupercent=$(echo $topnoderesults | cut -d " " -f 3 | cut -d "%" -f 1 | avg)
mempercent=$(echo $topnoderesults | cut -d " " -f 5 | cut -d "%" -f 1 | avg)
cpunum=$(echo $topnoderesults | cut -d " " -f 2 | cut -d "m" -f 1 | avg)
memnum=$(echo $topnoderesults | cut -d " " -f 4 | cut -d "M" -f 1 | avg)
totalcpu=$(echo $nodeinformation | jq -r '.items[].status.capacity.cpu' | math_sum)
totalmemory=$(echo "$(echo $nodeinformation | jq -r '.items[].status.capacity.memory' | cut -d 'K' -f 1 | math_sum) /(1024*1024)" | bc)
numnodes=$(echo $topnoderesults | wc -l)
cpuinuse=$(echo "$totalcpu * $cpupercent / 100" | bc)
mcpuinuse=$(echo "$totalcpu * 1000 * $cpupercent / 100" | bc)
memoryinuse=$(echo "$totalmemory * $mempercent / 100" | bc)
memoryinusemb=$(echo "$totalmemory * 1024 * $mempercent / 100" | bc)
# output
echo "
#all pods: $numallpods
#pods in kube-system: $numsystempods
#pods elsewhere: $numpodsnotsystem
#nodes: $numnodes
Total CPU: $totalcpu
Total Memory(GB): $totalmemory
CPU per Node: $(echo $totalcpu / $numnodes | bc)
Memory per Node: $(echo $totalmemory / $numnodes | bc)
CPU in use: $cpuinuse
Memory in use(GB): $memoryinuse
mCPU/POD: $(echo $mcpuinuse / $numallpods | bc)
Memory(MB)/POD: $(echo $memoryinusemb / $numallpods | bc)
CPU avg(m cpu): $cpunum = $cpupercent%
RAM avg(MB): $memnum = $mempercent%"
}
function kreportnode() {
node=$1
nodeinformation=$(kgno -o json)
if [[ -z "$node" ]]; then
nodes=$(echo $nodeinformation | jq -r '.items[].metadata.name')
if command -v fzf >/dev/null; then
node=$(echo $nodes | fzf)
else
echo "Please call this report with a node from this list:"
echo $nodes
return 1
fi
else
if [[ -z "$(echo $nodeinformation | jq -r ".items[] | select(.metadata.name == \"$node\")")" ]]; then
echo "Node $node not found!"
return 1
fi
fi
totalcpu=$(echo $nodeinformation | jq -r ".items[] | select(.metadata.name == \"$node\") | .status.capacity.cpu")
totalmemory=$(echo "$(echo $nodeinformation | jq -r ".items[] | select(.metadata.name == \"$node\") | .status.capacity.memory" | cut -d 'K' -f 1) /(1024*1024)" | bc)
echo "
Here's the information for $node:
Total CPU: $totalcpu
Total Memory(Gi): $totalmemory"
}
_encode() {
local _length="${#1}"
for ((_offset = 0; _offset < _length; _offset++)); do
_print_offset="${1:_offset:1}"
case "${_print_offset}" in
[a-zA-Z0-9.~_-]) printf "${_print_offset}" ;;
'/') printf '/' ;;
':') printf ':' ;;
*) printf '%%%X' "'${_print_offset}" ;;
esac
done
}
function holog() {
since=$1
if [[ -z "$since" ]]; then
since="10m"
fi
hopod=$(kubectl get pods --all-namespaces | tr -s " " | cut -d " " -f 2 | grep helm-operator)
if [[ ! -z "$hopod" ]]; then
hons=$(kubectl get pods --all-namespaces | grep $hopod | cut -d " " -f 1)
kubectl logs -f $hopod -n $hons --since $since
else
echo "Helm Operator pod not found! ◉_◉"
fi
}
function kshell() {
if [[ -z "$1" ]]; then
kubectl run oh-my-guish-pod -it --rm --image gcarrarom/kshell:0.0.17
else
kubectl run oh-my-guish-pod -it --rm --image $1
fi
}
function kdelpn() {
kgp | grep $1 | cut -d " " -f 1 | xargs -n 1 -I % kubectl delete pod %
}
function old_helm() {
helm_older_versions_path="$HOME/.helm/older_versions/"
if [[ ! -d "$helm_older_versions_path" ]]; then
if [[ ! -d "$HOME/.helm" ]]; then
mkdir $HOME/.helm
fi
mkdir $HOME/.helm/older_versions
fi
version="$1"
older_version_file="$(echo $helm_older_versions_path)helm$version"
if [[ ! -f "$older_version_file" ]]; then
curl https://get.helm.sh/helm-v$version-darwin-amd64.tar.gz --output "$older_version_file.tar.gz"
tar -xvf "$older_version_file.tar.gz" -C $helm_older_versions_path
rm -rf "$older_version_file.tar.gz"
mv "$(echo $helm_older_versions_path)darwin-amd64/helm" $older_version_file
rm -rf "$(echo $helm_older_versions_path)darwin-amd64"
chmod +x $older_version_file
fi
helm_command=$(echo "$@" | sed s/"$1"//g)
printf -v arguments_for_command ' %s' "${helm_command[@]}"
$older_version_file ${arguments_for_command:1}
}
## Flux
if command -v fluxctl >/dev/null; then
function flw() {
flux_configuration_path="$HOME/.flux/"
flux_configuration_file_name="config"
namespace_selected=$1
if [[ ! -d "$flux_configuration_path" ]]; then
mkdir $flux_configuration_path
fi
if [[ ! -f "$flux_configuration_path$flux_configuration_file_name" ]]; then
touch $flux_configuration_path$flux_configuration_file_name
echo "{}" >$flux_configuration_path$flux_configuration_file_name
fi
configuration=$(cat $flux_configuration_path$flux_configuration_file_name)
if [[ -z "$namespace_selected" ]]; then
namespace_selected=$(echo $configuration | jq -r ".namespace")
if [[ -z "$namespace_selected" ]]; then
fluxctl list-workloads -n $(kubectl config get-contexts | grep $(kubectl config current-context) | rev | cut -d ' ' -f 1 | rev)
else
fluxctl --k8s-fwd-ns $namespace_selected list-workloads -n $(kubectl config get-contexts | grep $(kubectl config current-context) | rev | cut -d ' ' -f 1 | rev)
fi
else
fluxctl --k8s-fwd-ns $namespace_selected list-workloads -n $(kubectl config get-contexts | grep $(kubectl config current-context) | rev | cut -d ' ' -f 1 | rev)
fi
}
function flwall() {
flux_configuration_path="$HOME/.flux/"
flux_configuration_file_name="config"
namespace_selected=$1
if [[ ! -d "$flux_configuration_path" ]]; then
mkdir $flux_configuration_path
fi
if [[ ! -f "$flux_configuration_path$flux_configuration_file_name" ]]; then
touch $flux_configuration_path$flux_configuration_file_name
echo "{}" >$flux_configuration_path$flux_configuration_file_name
fi
configuration=$(cat $flux_configuration_path$flux_configuration_file_name)
if [[ -z "$namespace_selected" ]]; then
namespace_selected=$(echo $configuration | jq -r ".namespace")
if [[ -z "$namespace_selected" ]]; then
fluxctl list-workloads --all-namespaces
else
fluxctl --k8s-fwd-ns $namespace_selected list-workloads --all-namespaces
fi
else
fluxctl --k8s-fwd-ns $namespace_selected list-workloads --all-namespaces
fi
}
function fsync() {
flux_configuration_path="$HOME/.flux/"
flux_configuration_file_name="config"
namespace_selected=$1
if [[ ! -d "$flux_configuration_path" ]]; then
mkdir $flux_configuration_path
fi
if [[ ! -f "$flux_configuration_path$flux_configuration_file_name" ]]; then
touch $flux_configuration_path$flux_configuration_file_name
echo "{}" >$flux_configuration_path$flux_configuration_file_name
fi
configuration=$(cat $flux_configuration_path$flux_configuration_file_name)
if [[ -z "$namespace_selected" ]]; then
namespace_selected=$(echo $configuration | jq -r ".namespace")
if [[ -z "$namespace_selected" ]]; then
fluxctl sync
else
fluxctl --k8s-fwd-ns $namespace_selected sync
fi
else
fluxctl --k8s-fwd-ns $namespace_selected sync
fi
}
function fluxns() {
flux_configuration_path="$HOME/.flux/"
flux_configuration_file_name="config"
namespace_selected=$1
if [[ ! -d "$flux_configuration_path" ]]; then
mkdir $flux_configuration_path
fi
if [[ ! -f "$flux_configuration_path$flux_configuration_file_name" ]]; then
touch $flux_configuration_path$flux_configuration_file_name
echo "{}" >$flux_configuration_path$flux_configuration_file_name
fi
configuration=$(cat $flux_configuration_path$flux_configuration_file_name)
if [[ -z "$namespace_selected" ]]; then
namespace_selected=$(kubectl get namespaces | cut -d " " -f 1 | tail +2 | fzf)
fi
if [[ -z "$namespace_selected" ]]; then
echo "no namespace selected!"
else
echo $configuration | jq ".namespace = \"$namespace_selected\"" >$flux_configuration_path$flux_configuration_file_name
FLUX_FORWARD_NAMESPACE=$namespace_selected
fi
}
fi
## Azure
function clone_aad_group_memberships() {
unset error
if [[ ! -z "$1" ]]; then
user_from="$1"
else
echo "The user you're cloning from cannot be empty!"
error=1
fi
if [[ ! -z "$2" ]]; then
user_to="$2"
else
echo "The user you're cloning to cannot be empty!"
error=2
fi
if [[ ! -z "$error" ]]; then
return $error
fi
echo "Searching for user $user_from"
user_from_id=$(az ad user show --id "$user_from" | jq -r ".objectId")
if [[ -z "$user_from_id" ]]; then
echo "User $user_from not found in AAD!"
return 1
fi
echo "Searching for user $user_to"
user_to_id=$(az ad user show --id "$user_to" | jq -r ".objectId")
if [[ -z "$user_to_id" ]]; then
echo "User $user_to not found in AAD!"
return 1
fi
groups_ids=($(az ad user get-member-groups --id $user_from_id | jq -r '.[].objectId'))
group_count=0
echo "Adding $user_to to ${#groups_ids[@]} groups"
for group_id in $groups_ids; do
group_count=$(echo "$group_count + 1" | bc)
printf "Adding.... ($group_count/${#groups_ids[@]})\r"
az ad group member add --group $group_id --member-id $user_to_id
done
}
function getresourcegroup() {
selected_group=$(az configure --list-defaults | jq ".[] | select(.name == \"group\")")
if [[ -z "$selected_group" ]]; then
az account set --subscription $(az account list -o json | jq -r '.[].name' | fzf)
azgroup
selected_group=$(az configure --list-defaults | jq ".[] | select(.name == \"group\")")
fi
echo $selected_group
}
function getclustername() {
clusters=$(az aks list -o json)
if [[ $(echo $clusters | jq ". | length") -gt 1 ]]; then
clusterName=$(echo $clusters | jq -r '.[].name' | fzf)
else
clusterName=$(echo $clusters | jq -r '.[].name')
fi
echo $clusterName
}
function getcredentialsaks-admin() {
getresourcegroup
clusterName=$(getclustername)
az aks get-credentials --name $clusterName --admin
}
function getcredentialsaks() {
getresourcegroup
clusterName=$(getclustername)
az aks get-credentials --name $clusterName
}
if ! command -v azacc >/dev/null; then
function azacc() {
if [[ -z "$1" ]]; then
az account set --subscription "$(az account list -o json | jq -r '.[].name' | fzf)"
else
az account set --subscription "$1"
fi
azgroup all
}
function azgroup() {
if [[ -z "$1" ]]; then
group=$(az group list -o json | jq -r '.[].name' | fzf)
az configure --defaults group=$group
elif [[ "$1" == "all" ]]; then
az configure --defaults group=
else
az configure --defaults group=$1
fi
}
fi
## numi
function numi() {
if [[ "$@" == "open" ]]; then
open -a numi
elif [[ $# == 0 ]]; then
while read -r data; do
numi "${data}"
done
else
data=$@
curl -G --data-urlencode "q=$data" http://localhost:15055
echo ""
fi
}
# Aliases
## Code Server
if command -v "code-server" >/dev/null; then
alias code='code-server'
fi
## Abak https://github.com/gcarrarom/fancy-abak
if command -v abak >/dev/null; then
alias a='abak'
alias atl="a timesheet list"
alias atlwide="a timesheet list -o wide"
alias atlid='atl --show-id'
alias atlprevious="atl --previous"
alias atlpreviouswide="atlprevious -o wide"
alias atlwideprevious="atlprevious -o wide"
alias atltotal='atl --show-totals'
alias atltotalprevious='atl --show-totals --previous'
alias atlprevioustotal='atl --show-totals --previous'
alias ats="a timesheet set"
alias atd="a timesheet delete"
if command -v ata >/dev/null; then
alias abakta="abak timesheet approve"
else
alias ata="a timesheet approve"
fi
alias acs='a context select'
alias ado='a do'
fi
## Numi
alias n="numi"
## Python
alias pip="pip3"
alias python="python3"
## Helm
alias hls="helm list"
alias hlsall="helm list --all-namespaces"
## Jiractl - https://github.com/gcarrarom/fancy-jira
if command -v jira >/dev/null; then
alias jgi="jira get issues"
alias jci="jira create issue"
alias jcc="jira create comment"
alias jui="jira update issue"
alias jconfig="jira config show"
alias jconfigs="jira config set"
alias jconfigr="jira config remove"
fi
## exa - https://github.com/ogham/exa
if command -v exa >/dev/null; then
alias ls="exa"
alias la="ls -al"
fi
## zoxide - https://github.com/ajeetdsouza/zoxide
if command -v z >/dev/null; then
alias cd="z"
fi
## terraform
alias tf="terraform"
alias tfinit="terraform init"
alias tfapply="terraform apply"
alias tfplan="terraform plan"
## Flux
alias f="flux"
### Helm Releases
alias fghr="f get hr"
alias fghrall="f get hr -A"
### Reconcile
alias fr="f reconcile"
alias frhr="fr helmrelease"
alias fri="fr image"
alias frir="fri repository"
alias friu="fri update"
alias frs="fr source"
alias frsb="frs bucket"
alias frsg="frs git"
alias frsh="frs helm"
## K8s
### kubecolor - https://github.com/hidetatz/kubecolor
if command -v kubecolor; then
alias kubectl="kubecolor"
compdef kubecolor=kubectl
fi >/dev/null
### Kustomization
alias kgk="kubectl get kustomizations"
alias kdk="kubectl describe kustomizations"
### Pods
alias kgpall="kubectl get pods --all-namespaces"
alias kgpallwide="kubectl get pods --all-namespaces -o wide"
alias kdelpall="kubectl delete pods --all"
alias kgpwatch="watch -d kubectl get pods"
alias kgpwatchwide="watch -d kubectl get pods -o wide"
alias kgpallwatch="watch -d kubectl get pods --all-namespaces"
alias kgpallwatchwide="watch -d kubectl get pods --all-namespaces -o wide"
alias kgpjson="kubectl get pods -o json"
alias ktp="kubectl top pods"
alias ktpwatch="watch -d kubectl top pods"
alias kpop="echo \"\nHere are your pods, m'lady...\n*Tips Fedora*\n(♡-_-♡)\n\n\" && kgp && echo \"\nヽ(♡‿♡)ノ\""
### CC - Kubernetes Operator for cluster configuration - not open source yet.
if $OHMYGUISH_CLUSTERCONFIGURATOR_ENABLE; then
alias kgcc="kubectl get cc"
alias kgccall="kubectl get cc --all-namespaces"
alias kdcc="kubectl describe cc"
alias kdelcc="kubectl delete cc"
alias kecc="kubectl edit cc"
fi
### HelmReleases
alias kghr="kubectl get hr"
alias kghrall="kubectl get hr --all-namespaces"
alias kdhr="kubectl describe hr"
alias kdelhr="kubectl delete hr"
alias kehr="kubectl edit hr"
alias kgh="kubectl get helmreleases"
alias kghall="kubectl get helmreleases --all-namespaces"
alias kghallwatch="watch -d kubectl get helmreleases --all-namespaces"
alias kghwatchall="watch -d kubectl get helmreleases --all-namespaces"
alias kghwatch="watch -d kubectl get helmreleases"
### CertManager
#### Certificates
alias kgc="kubectl get certificates"
alias kgcall="kubectl get certificates -A"
alias kgcert="kubectl get certificates"
alias kgcertall="kubectl get certificates -A"
alias kdc="kubectl describe certificates"
alias kec="kubectl edit certificates"
alias kdcert="kubectl describe certificates"
#### Certificate Requests
alias kgcr="kubectl get certificaterequests"
alias kgcrall="kubectl get certificaterequests -A"
alias kdcr="kubectl describe certificaterequests"
#### Issuers
alias kgiss="kubectl get issuers"
alias kgissall="kubectl get issuers -A"
#### Cluster Issuers
alias kgci="kubectl get clusterissuers"
alias kgciall="kubectl get clusterissuers -A"
### DaemonSets
alias kgds="kubectl get daemonsets"
alias kgdsall="kubectl get daemonsets --all-namespaces"
alias kdelds="kubectl delete daemonsets "
alias keds="kubectl edit daemonsets "
### Namespace
alias kcns="kubectl create namespace"
alias kgcurrentnamespace="kubectl config get-contexts | grep $(kubectl config current-context) | rev | cut -d ' ' -f 1 | rev"
alias kgnswatch="watch -d kubectl get namespaces"
### Deployments
alias kgdall="kubectl get deployments --all-namespaces"
alias kgdwatchall="watch -d kubectl get deployments --all-namespaces"
alias kgdallwatch="watch -d kubectl get deployments --all-namespaces"
alias kgdwatch="watch -d kubectl get deployments"
### Nodes
alias kgnowatch="watch -d kubectl get nodes"
alias kgnowide="kubectl get nodes -o wide"
alias kgnowidewatch="watch -d kubectl get nodes -o wide"
alias ktopno="kubectl top nodes"
alias ktopnowatch="watch -d kubectl top nodes"
### Services
alias kgswatch="watch -d kubectl get services"
alias kgsall="kubectl get services --all-namespaces"
### Secrets
alias kgsecyaml="kubectl get secret -o yaml"
### Service Accounts
alias kconfigsa="kubectl view-serviceaccount-kubeconfig"
alias kgsa="kubectl get serviceaccounts"
alias kgsaall="kubectl get serviceaccounts --all-namespaces"
### Ingress
alias kgiall="kubectl get ingress --all-namespaces"
alias kgiwatch="watch -d kubectl get ingress"
alias kgiallwatch="watch -d kubectl get ingress --all-namespaces"
### Config Maps
alias kgcm="kubectl get configmaps"
alias kgcmall="kubectl get configmaps --all-namespaces"
alias kecm="kubectl edit configmap"
### Roles
alias kgr="kubectl get roles"
alias kgrall="kubectl get roles --all-namespaces"
### RoleBindings
alias kgrb="kubectl get rolebindings"
alias kgrball="kubectl get rolebindings --all-namespaces"
### Cluster Role Bindings
alias kgcrb="kubectl get clusterrolebindings"
alias kecrb="kubectl edit clusterrolebinding"
alias kccrb="kubectl create clusterrolebinding"
alias kdelcrb="kubectl delete clusterrolebinding"
### Network Policies
alias kgnp="kubectl get networkpolicies"
alias kgnpall="kubectl get networkpolicies --all-namespaces"
alias kgnpwatch="watch -d kubectl get networkpolicies"
alias kgnpallwatch="watch -d kubectl get networkpolicies --all-namespaces"
alias kdnp="kubectl describe networkpolicies"
alias kenp="kubectl edit networkpolicies"
### Events
alias kge="kubectl get events --sort-by='.metadata.creationTimestamp'"
## Azure
alias aacc="az account show -o json | jq -r '.name'"
## OSX
alias lock="pmset displaysleepnow"
## System
if command -v gping; then alias ping="gping"; fi >/dev/null
alias please="sudo"
# pbcopy/paste on linux
if command -v lsb_release >/dev/null; then
alias pbcopy='xsel --clipboard --input'
alias pbpaste='xsel --clipboard --output'
fi
alias copy_last_command="fc -ln -1 | pbcopy"
alias reload="source ~/.zshrc"
## thefuck - https://github.com/nvbn/thefuck
if command -v fuck; then alias sorry="fuck"; fi >/dev/null
## Terminal
### Colors
Color_Off='\033[0m' # Text Reset
### Regular Colors
Black='\033[0;30m' # Black
Red='\033[0;31m' # Red
Green='\033[0;32m' # Green
Yellow='\033[0;33m' # Yellow
Blue='\033[0;34m' # Blue
Purple='\033[0;35m' # Purple
Cyan='\033[0;36m' # Cyan
White='\033[0;37m' # White
### Bold
BBlack='\033[1;30m' # Black
BRed='\033[1;31m' # Red
BGreen='\033[1;32m' # Green
BYellow='\033[1;33m' # Yellow
BBlue='\033[1;34m' # Blue
BPurple='\033[1;35m' # Purple
BCyan='\033[1;36m' # Cyan
BWhite='\033[1;37m' # White
### Underline
UBlack='\033[4;30m' # Black
URed='\033[4;31m' # Red
UGreen='\033[4;32m' # Green
UYellow='\033[4;33m' # Yellow
UBlue='\033[4;34m' # Blue
UPurple='\033[4;35m' # Purple
UCyan='\033[4;36m' # Cyan
UWhite='\033[4;37m' # White
### Background
On_Black='\033[40m' # Black
On_Red='\033[41m' # Red
On_Green='\033[42m' # Green
On_Yellow='\033[43m' # Yellow
On_Blue='\033[44m' # Blue
On_Purple='\033[45m' # Purple
On_Cyan='\033[46m' # Cyan
On_White='\033[47m' # White
### High Intensity
IBlack='\033[0;90m' # Black
IRed='\033[0;91m' # Red
IGreen='\033[0;92m' # Green
IYellow='\033[0;93m' # Yellow
IBlue='\033[0;94m' # Blue
IPurple='\033[0;95m' # Purple
ICyan='\033[0;96m' # Cyan
IWhite='\033[0;97m' # White
### Bold High Intensity
BIBlack='\033[1;90m' # Black
BIRed='\033[1;91m' # Red
BIGreen='\033[1;92m' # Green
BIYellow='\033[1;93m' # Yellow
BIBlue='\033[1;94m' # Blue
BIPurple='\033[1;95m' # Purple
BICyan='\033[1;96m' # Cyan
BIWhite='\033[1;97m' # White
### High Intensity backgrounds
On_IBlack='\033[0;100m' # Black
On_IRed='\033[0;101m' # Red
On_IGreen='\033[0;102m' # Green
On_IYellow='\033[0;103m' # Yellow
On_IBlue='\033[0;104m' # Blue
On_IPurple='\033[0;105m' # Purple
On_ICyan='\033[0;106m' # Cyan
On_IWhite='\033[0;107m' # White