-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path.bash_functions
More file actions
278 lines (245 loc) · 8.27 KB
/
Copy path.bash_functions
File metadata and controls
278 lines (245 loc) · 8.27 KB
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
composer() {
if [ -f composer.phar ]; then
php composer.phar "$@"
else
echo -e "\e[1;33m\e[41m404 : composer.phar not found\e[0m"
fi
}
composer-install() {
if [ -f composer.phar ] ; then
php composer.phar install --prefer-dist --ignore-platform-reqs
else
echo -e "\e[1;33m\e[41m404 : composer.phar not found\e[0m"
fi
}
composer-dl() {
if [ -f composer.phar ] ; then
echo -e "\e[1;33m\e[41mcomposer.phar already exists \e[0m"
else
curl -sS https://getcomposer.org/installer | php
fi
}
diff-xxd() {
diff -y <(xxd "$1") <(xxd "$2") --suppress-common-lines
}
search-find() {
find . -name "*$1*" | grep -n "$1"
}
search-grep-file-git-history() {
git rev-list --all $2 | (
while read revision; do
git grep -F $1 $revision $2
done
)
}
xdebug-enable() {
ip="172.17.3.62"
export XDEBUG_CONFIG="idekey=Eclipse remote_host=$ip"
}
xdebug-disable() {
unset XDEBUG_CONFIG
}
docker-psa() {
docker ps -a --format "table {{.ID}}\t{{.Names}}\t{{.Image}}\t{{.CreatedAt}}\t{{.Status}}" "$@"
}
docker-shell() {
local CONTAINER_NAME="$1"
local COMMAND="bash"
if [ "$#" -gt 1 ]; then
COMMAND=$(echo "$@" | cut -d' ' -f2-)
fi
docker exec -ti "${CONTAINER_NAME}" ${COMMAND}
}
docker-pull-registry-tag() {
docker images --format {{.Repository}}:{{.Tag}} | grep $1 | sed 's/$1/$2/' | xargs -n 1 docker pull
}
aws-cloudformation-logs-cli() {
if [ "$#" -lt 2 ]; then
aws-cloudformation-logs-groups $@
elif [ "$#" -eq 2 ]; then
aws-cloudformation-logs-groups $@ | xargs -I '{}'\
echo "awslogs get {} -GS -s 10m"
fi
}
aws-cloudformation-logs-cloudwatch() {
if [ "$#" -lt 2 ]; then
aws-cloudformation-logs-groups $@
elif [ "$#" -eq 2 ]; then
aws-cloudformation-logs-groups $@ | xargs -I '{}'\
echo "https://console.aws.amazon.com/cloudwatch/home#logEventViewer:group={}"
fi
}
aws-cloudformation-logs-groups() {
if [ "$#" -eq 0 ]; then
echo "Choose your aws profile and add it as parameter:"
grep "^\[profile" ~/.aws/config | sed "s/\[profile\s*\(.*\)\]/\1/" | sort | xargs
elif [ "$#" -eq 1 ]; then
echo "Choose your stack arn and add it as parameter:"
aws cloudformation list-stacks --profile "$1" --stack-status-filter CREATE_COMPLETE ROLLBACK_COMPLETE UPDATE_COMPLETE UPDATE_ROLLBACK_COMPLETE | grep -o "arn:\S*" | sort | uniq
elif [ "$#" -eq 2 ]; then
(
aws cloudformation describe-stack-resources --profile "$1" --stack-name "$2" | grep "AWS::Logs::LogGroup" | cut -f3 &&
aws cloudformation describe-stacks --profile "$1" --stack-name "$2" | grep "^OUTPUTS" | grep "LogGroupAccess" | grep -o "logEventViewer:group=\S*" | sed "s/logEventViewer:group=//"
) | sort | uniq
fi
}
aws-cloudformation-ssh-cmd() {
if [ "$#" -eq 0 ]; then
echo "Choose your aws profile and add it as parameter:"
grep "^\[profile" ~/.aws/config | sed "s/\[profile\s*\(.*\)\]/\1/" | sort | xargs
elif [ "$#" -eq 1 ]; then
echo "Choose your stack arn and add it as parameter:"
aws cloudformation list-stacks --profile "$1" --stack-status-filter CREATE_COMPLETE ROLLBACK_COMPLETE UPDATE_COMPLETE UPDATE_ROLLBACK_COMPLETE | grep -o "arn:\S*" | sort | uniq
elif [ "$#" -eq 2 ]; then
aws cloudformation describe-stack-resources --profile "$1" --stack-name "$2" | grep "AWS::EC2::Instance" | cut -f3 | xargs -r\
aws ec2 describe-instances --profile "$1" --instance-ids | grep "^PRIVATEIPADDRESSES\b" | cut -f3 | xargs -I '{}'\
echo "ssh -t bastion-$1 ssh {}"
fi
}
aws-ecs-ssh-cmd() {
if [ "$#" -eq 0 ]; then
echo "Choose your aws profile and add it as parameter:"
grep "^\[profile" ~/.aws/config | sed "s/\[profile\s*\(.*\)\]/\1/" | sort | xargs
elif [ "$#" -eq 1 ]; then
echo "Choose your cluster arn and add it as parameter:"
aws ecs list-clusters --profile "$1" | grep -o "arn:\S*" | sort | uniq
elif [ "$#" -eq 2 ]; then
echo "Choose your service arn and add it as parameter:"
aws ecs list-services --profile "$1" --cluster "$2" | grep -o "arn:\S*" | sort | uniq
elif [ "$#" -eq 3 ]; then
aws ecs list-tasks --profile "$1" --cluster "$2" --service-name "$3" | grep "^TASKARNS\b" | grep -o "arn:\S*" | xargs -r\
aws ecs describe-tasks --profile "$1" --cluster "$2" --tasks | grep "^TASKS\b" | cut -f5 | xargs -r\
aws ecs describe-container-instances --profile "$1" --cluster "$2" --container-instances | grep "^CONTAINERINSTANCES\b" | cut -f4 | xargs -r\
aws ec2 describe-instances --profile "$1" --instance-ids | grep "^PRIVATEIPADDRESSES\b" | cut -f3 | xargs -I '{}'\
echo "ssh -t bastion-$1 ssh {}"
fi
}
aws-profile-get() {
echo "$AWS_PROFILE"
}
aws-profile-set() {
if [ "$#" -gt 0 ]; then
export AWS_PROFILE="$1"
else
unset AWS_PROFILE
fi
}
aws-region-get() {
echo "$AWS_REGION"
}
aws-region-set() {
if [ "$#" -gt 0 ]; then
export AWS_REGION="$1"
else
unset AWS_REGION
fi
}
aws-s3-prune-file-older-than() {
if [ ! "$#" -eq 2 ]; then
echo "Usage:"
echo " aws-s3-prune-file-older-than \"s3://bucket/path/\" \"-7 days\""
echo " aws-s3-prune-file-older-than \"s3://bucket/path/\" \"2019-02-28 00:00:00\""
else
bucketpath="$1"
olderThan=`date -d"$2" +%s`
aws s3 ls $bucketpath | while read -r line; do
createDate=`echo $line|awk {'print $1" "$2'}`
createDate=`date -d"$createDate" +%s`
if [ $createDate -lt $olderThan ]; then
fileName=`echo $line|awk {'print $4'}`
test -n $fileName && aws s3 rm "$bucketpath$fileName"
fi
done
fi
}
tmux-spliter() {
if [ "$#" -eq 0 ]; then
echo "No tmux layout in arguments"
elif [ "$#" -eq 1 ]; then
while read cmd
do
tmux split-window "$cmd"
tmux select-layout $1
done
elif [ "$#" -gt 1 ]; then
for cmd in ${@:2}
do
tmux split-window "$cmd"
tmux select-layout $1
done
fi
}
tmux-tiled() {
tmux-spliter "tiled" $@
}
tmux-vertical() {
tmux-spliter "even-vertical" $@
}
scp-through() {
if [ "$#" -ne 3 ]; then
echo "Incorrect number of arguments: scp-through <bastion> <host> <absolutefilepath>"
elif [[ ! "${@:(-1):1}" = "/"* ]]; then
echo "No absolute file path in last argument: scp-through <bastion> <host> <absolutefilepath>"
else
bastion="${@:1:1}"
host="${@:2:1}"
absolutefilepath="${@:3:1}"
filename=$(basename $absolutefilepath)
echo "scp -ro ProxyCommand=\"ssh $bastion nc $host 22\" $host:$absolutefilepath $host-$filename"
fi
}
ssh-hops-tunneling() {
if [ "$#" -lt 2 ]; then
echo "Not enough argument: ssh-hops-tunneling [hops] <lasthop> <target:port>"
elif [[ ! "${@:(-1):1}" = *":"* ]]; then
echo "No port in last argument: ssh-hops-tunneling [hops] <lasthop> <target:port>"
else
cmd=""
port=$(shuf -i 2000-65000 -n 1)
for hop in ${@:1:(-2)}
do
cmd+="ssh -L${port}:localhost:${port} ${hop} -t "
done
cmd+="ssh -L${port}:${@:(-1):1} -t ${@:(-2):1}"
echo $cmd
fi
}
git-subdir-branches()
{
if [[ -z "$1" ]]; then
echo "Usage: $FUNCNAME <dir>" >&2
return 1
fi
if [[ ! -d "$1" ]]; then
echo "Invalid dir specified: '${1}'"
return 1
fi
# Subshell so we don't end up in a different dir than where we started.
(
cd "$1"
for sub in *; do
[[ -d "${sub}/.git" ]] || continue
echo "$sub [$(cd "$sub"; git branch | grep '^\*' | cut -d' ' -f2)]"
done
)
}
git-subdir-allbranches()
{
if [[ -z "$1" ]]; then
echo "Usage: $FUNCNAME <dir>" >&2
return 1
fi
if [[ ! -d "$1" ]]; then
echo "Invalid dir specified: '${1}'"
return 1
fi
# Subshell so we don't end up in a different dir than where we started.
(
cd "$1"
for sub in *; do
[[ -d "${sub}/.git" ]] || continue
echo "$sub"
echo "$(cd "$sub"; git branch)"
done
)
}