-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.functions
633 lines (501 loc) · 20.5 KB
/
.functions
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
#!/usr/bin/env bash
# shellcheck shell=bash
####################################################################################################
# Tim's shell functions
# https://github.com/tdmalone/dotfiles/blob/master/.functions
#
# See also: https://github.com/tdmalone/dotfiles/blob/master/.aliases
#
# Copyright (c) 2018-2019 Tim Malone
# License: https://github.com/tdmalone/dotfiles/blob/master/LICENSE
#
# Sorted by category and then (generally) alphabetically
# Any sensitive information is expected to be provided by environment variables in a separate file
#
# DISCLAIMER:
# These have been written for my use-case only and have not been thoroughly tested in different
# circumstances. I use macOS with zsh and additional GNU utils, and generally try to keep all
# third-party tools fairly up-to-date. Your mileage may vary on different platforms and versions.
####################################################################################################
red="\\033[00;31m"
olive="\\033[00;33m"
blue="\\033[00;36m"
normal="\\033[0m"
####################################################################################################
# Assorted functions
####################################################################################################
##########################
# New unsorted
##########################
# ...
composer216() {
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '756890a4488ce9024fc62c56153228907f1545c228516cbf63f885e036d37e9a59d27d63f46af1d4d07ee0f76181c7d3') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"
}
tpview() {
tf="$(mktemp)"
echo "Generating Terraform plan and opening in code editor, one moment..."
echo "(file will be at ${tf}.txt)..."
tplfo "${tf}" "$@" > /dev/null
tpclean "${tf}"
sed -i .bak '1s/^/'"$(pwd | sed 's/\//\\\//g')"'\n/' "${tf}.txt" # Add CWD to first line of file
code "${tf}.txt"
}
# Same as above except with targetting...
tpviewtarget() {
target="${1}"
tf="$(mktemp)"
echo "Generating Terraform plan for '${target}' and opening in code editor, one moment..."
echo "(file will be at ${tf}.txt)..."
tplfo "${tf}" "$@" > /dev/null
tpclean "${tf}"
sed -i .bak '1s/^/'"$(pwd | sed 's/\//\\\//g')"'\n/' "${tf}.txt" # Add CWD to first line of file
code "${tf}.txt"
}
tpclean() {
filename="${1}.txt"
tfsnc "${1}" > "${filename}"
cp "${filename}" "${filename}.bak"
perl -0pe 's/.*\(known after apply\)\n//g' -i "${filename}"
perl -0pe 's/\[\n\s+.? "(.*?)",\n\s+\]\n/\["$1"\]\n/g' -i "${filename}"
perl -0pe 's/\{\n\s+.? "(.*?)" = "(.*?)"\n\s+\}\n/{"$1" = "$2"}\n/g' -i "${filename}"
perl -0pe 's/\n\s+[a-z].*\s+= \[\n[^\]]+\]//g' -i "${filename}"
perl -0pe 's/\n\s+[a-z].*\s+= \{\n[^\}]+\}//g' -i "${filename}"
perl -0pe 's/\n\s+[a-z].*//g' -i "${filename}"
}
read_app_queue() {
sqs_message="$1"
job_data="$(echo "${sqs_message}" | base64 --decode | jq --raw-output '.body' | base64 --decode)"
job_data="$(echo "${job_data}" | sed 's/\\/\/\//g')" # Replace \ with // so it doesn't become a broken escape.
echo
echo -n "portal_id: " && echo "${job_data}" | jq --raw-output '.kwargs.portal_id'
echo -n "job_name: " && echo "${job_data}" | jq --raw-output '.kwargs.job_name'
echo "${job_data}" | jq .
}
# Cleans up all the annoying '* 2.*' etc. files that iCloud sticks everywhere.
icloud_clean() {
for i in $(seq 2 15); do
find . -name '* '"$i"'*' -print -delete
done
find . -name '*.icloud' -print -delete
}
pull_all() ( # Run in a sub-shell so 'set -e' doesn't kill the main shell
set -euo pipefail
repos=(ace ace-connector admin ansible api-proxy bamboo canopener eve lambdaland terraform tools)
cd "${HOME}/astute"
for repo in ${repos[@]}; do
echo
cd "${repo}"
echo "${repo}"
git stash
git branch
gco master
gpl
git delmerged
cd ..
done
)
tc() {
touch "$1"
code "$1"
}
# Removes multiple blank lines at the end of a file
remove_multiple_blanks() {
find . -type f -name '*.tf' -exec sed -i '' -e :a -e '/^\n*$/{$d;N;};/\n$/ba' {} \;
}
tighten_empty_blocks() {
find . -type f -name '*.tf' | xargs -I % bash -c "sed -e ':a' -e 'N' -e '\$!ba' -e 's/{\n}/{}/g' % > tmp && mv tmp %"
}
function x509exp(){ openssl x509 -noout -enddate -in $1 | sed -e 's/.*=//' }
function x509fp(){ openssl x509 -noout -fingerprint -in $1 | sed -e 's/.*=//' -e 's/://g' }
function x509s(){ openssl x509 -noout -serial -in $1 | sed -e 's/.*=//' }
##########################
# Sorted assorted
##########################
# Make sudo work with aliases. May not work properly though...
# @see https://askubuntu.com/a/22043/421637
# Allows one to pass a an argument via stdin to 'date'
# eg. echo "2019-06-15T03:57:14Z" | date_stdin -juf "%Y-%m-%dT%H:%M:%SZ"
date_stdin() {
read -r d
date "$@" "${d}"
}
# Gives a relative time
relative() {
now="$(date -u +%s)"
then="${1}"
result="$(( now - then ))"
one_second="$(( 1 ))"
one_minute="$(( one_second * 60 ))"
one_hour="$(( one_minute * 60 ))"
one_day="$(( one_hour * 24 ))"
one_week="$(( one_day * 7 ))"
one_month="$(( one_day * 31 ))"
one_year="$(( one_day * 365 ))"
if [ "${result}" -ge "${one_year}" ]; then
result="$(( result / 60 / 60 / 24 / 7 / 31 / 365 ))y"
elif [ "${result}" -ge "${one_month}" ]; then
result="$(( result / 60 / 60 / 24 / 7 / 31 ))mth"
elif [ "${result}" -ge "${one_week}" ]; then
result="$(( result / 60 / 60 / 24 / 7 ))w"
elif [ "${result}" -ge "${one_day}" ]; then
result="$(( result / 60 / 60 / 24 ))d"
elif [ "${result}" -ge "${one_hour}" ]; then
result="$(( result / 60 / 60 ))h"
elif [ "${result}" -ge "${one_minute}" ]; then
result="$(( result / 60 ))m"
else
result+="s"
fi
echo "${result}"
} # relative
# Runs provided command in all the (direct) subdirectories, clearly printing out the directory names
# Skips the current directory, and skips hidden directories
run_in_subs() {
echo
find . -type d -mindepth 1 -maxdepth 1 -not -path '*/\.*' -print0 \
| sort --zero-terminated --human-numeric-sort \
| xargs -0 -I % bash -c "cd % && echo :: % :: && echo && $*; echo"
}
####################################################################################################
# Third party programs & services
####################################################################################################
##########################
# Ansible
##########################
# 'Ansible grep' - usage 'ang something' - designed originally for use in the Astute Ansible repo, but might be useful
# elsewhere as well. Note that the final grep is only used to get colouring back.
ang() {
git grep --ignore-case "$1" | grep -v "\.sql:" | grep -v "\.js:" | grep -v "\.css:" | grep "$1"
}
apmv() {
ANSIBLE_VAULT_PASSWORD_FILE=~/.ansible/$1 apm
}
aves() {
ANSIBLE_VAULT_PASSWORD_FILE=~/.ansible/$1 ansible-vault encrypt_string
}
avds() {
ANSIBLE_VAULT_PASSWORD_FILE=~/.ansible/$1 ansible-vault decrypt
}
##########################
# AWS
# see also: AWS EC2, AWS EKS
##########################
areset() {
unset AWS_ACCESS_KEY_ID
unset AWS_SECRET_ACCESS_KEY
unset AWS_SESSION_TOKEN
export AWS_PROFILE="${AWS_TIM_DESIRED_PROFILE}"
aupd
}
asudo() {
ACCOUNT_NUMBER="${1:-}"
ACCOUNT_FRIENDLY_NAME="${2:-}"
ROLE_NAME="${3:-OrganizationAccountAccessRole}"
if [ -z "${ACCOUNT_NUMBER}" ]; then ACCOUNT_NUMBER="${AWS_WORK_TIM_ACCOUNT_ID}"; fi
amfa
if [ -z "${ACCOUNT_FRIENDLY_NAME}" ]; then ACCOUNT_FRIENDLY_NAME="${ACCOUNT_NUMBER}"; fi
PROFILE_NAME="${AWS_PROFILE}-${ACCOUNT_FRIENDLY_NAME}"
echo "Assuming role..."
CREDS="$(awsar "${ROLE_NAME}" "${ACCOUNT_NUMBER}" "silent")"
awscs aws_access_key_id "$(echo "${CREDS}" | jqr .Credentials.AccessKeyId)" --profile "${PROFILE_NAME}"
awscs aws_secret_access_key "$(echo "${CREDS}" | jqr .Credentials.SecretAccessKey)" --profile "${PROFILE_NAME}"
awscs aws_session_token "$(echo "${CREDS}" | jqr .Credentials.SessionToken)" --profile "${PROFILE_NAME}"
awscs expiration "$(echo "${CREDS}" | jqr .Credentials.Expiration | sed -e 's/T/ /' -e 's/Z/ /')" --profile "${PROFILE_NAME}"
export AWS_PROFILE="${PROFILE_NAME}"
echo
echo -n "Using: " && echo "${CREDS}" | jqr .AssumedRoleUser.Arn
echo -n "Until: " && echo "${CREDS}" | jqr .Credentials.Expiration
} # asudo
awsar() {
ROLE_NAME="${1}"
ACCOUNT_NUMBER="${2:-}"
MODE="${3:-}"
if [ -z "${ACCOUNT_NUMBER}" ]; then ACCOUNT_NUMBER="${AWS_WORK_MASTER_ACCOUNT_ID}"; fi
# TODO: Remove hyphens in account number (to make it easier to copy from a hyphen'ed display).
# TODO: Accept aliases and map them to account numbers, probably stored in env vars?
if [ "${MODE}" != "silent" ]; then
echo
echo "Assuming ${ROLE_NAME} in ${ACCOUNT_NUMBER}..."
fi
sts assume-role --role-arn "arn:aws:iam::${ACCOUNT_NUMBER}:role/${ROLE_NAME}" --role-session-name "$(whoami)"
}
dam() {
sts decode-authorization-message --encoded-message "${1}" | jq '.DecodedMessage | fromjson'
}
# Compares the last two versions of an AWS SSM Parameter.
alias ssmdiff='${TIM_WORK_HOMEDIR}/tools/tim-tools/ssmdiff'
# ssmdiff() {
# parameter_name="${1}"
# data="$(aws ssm get-parameter-history --name "${parameter_name}" --with-decryption)"
# current="$(echo "${data}" | jq -j ".Parameters[-1]")"
# old="$(echo "${data}" | jq -j ".Parameters[-2]")"
# current_value="$(echo "${current}" | jq ".Value | fromjson")"
# old_value="$(echo "${old}" | jq ".Value | fromjson")"
# # Print out a header with the version numbers and dates.
# # The date are timestamps. They're stripped of their decimal points and passed to `date` (which
# # doesn't take stdin and also doesn't strip trailing spaces).
# echo
# echo -n "Comparing version "
# echo "${old}" | jq -j ".Version"
# echo -n " ("
# echo "${old}" | jq ".LastModifiedDate" | sed 's/\..*//' | date_stdin -r | tr -d "\n"
# echo -n ") and "
# echo "${current}" | jq -j ".Version"
# echo -n " ("
# echo "${current}" | jq ".LastModifiedDate" | sed 's/\..*//' | date_stdin -r | tr -d "\n"
# echo ") of ${parameter_name}:"
# echo
# diff <(echo "${old_value}") <(echo "${current_value}") || true # We're always expecting a diff, so we don't want a non-zero exit code here.
# }
##########################
# AWS EC2
# see also: AWS
##########################
# TODO: Ignore terminated instances.
instance_id() {
# Replaces dots with dashes to match our instance Name tags.
INSTANCE_NAME="${1//./-}"
ec2 describe-instances --filters "Name=tag:Name,Values=${INSTANCE_NAME}" | jqr ".Reservations[0].Instances[0].InstanceId"
}
# Outputs available marketplace-listed (aka second-hand) reserved instances to JSON.
# May take some time to gather data from the EC2 API.
#
# https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-reserved-instances-offerings.html
# Might be useful? https://github.com/TerbiumLabs/check-reserved-instances
#
# Note that, if nothing else, 'no up-front' convertible one year RIs are pretty much a no-brainer!
# (probably won't find them on the marketplace though, right?)
#
# Also note: the 'reserved instance saving' correlates with how much the instance will have to be
# running to benefit from that saving. eg. a 41% saving? The instance has to be running at least
# 59% of the time to break even! This means under some circumstances you _can_ still reserve a
# part-time instance.
reserved_instances() {
months="${1:-1}" # Default to 1 month if not provided (1 month is the minimum).
filename="$( mktemp )"
one_day="$((60 * 60 * 24))"
one_month="$((one_day * 30))"
ec2 describe-reserved-instances-offerings --product-description "Linux/UNIX" --filters "Name=marketplace,Values=true" --filters "Name=duration,Values=$(( one_month * months ))" > "${filename}"
# TODO: Include an option for adding the AZ filter '--filters Name=availability-zone,Values=a'?
code "${filename}"
}
snapshot() {
ec2 create-snapshot --volume-id "$( volume_id "${1}" )"
}
start_instance() {
echo
echo "Starting ${1}..."
echo
ec2 start-instances --instance-ids "$( instance_id "${1}" )"
}
stop_instance() {
echo
echo "Stopping ${1}..."
echo
ec2 stop-instances --instance-ids "$( instance_id "${1}" )"
}
terminate_instance() {
echo
echo "About to terminate ${1} - press any key to continue or Ctrl+C to quit..."
read answer
echo
ec2 terminate-instances --instance-ids "$( instance_id "${1}" )"
}
volume_id() {
# Replaces dots with dashes to match our instance Name tags.
VOLUME_NAME="${1//./-}"
ec2 describe-volumes --filters "Name=tag:Name,Values=${VOLUME_NAME}" | jqr ".Volumes[0].VolumeId"
}
##########################
# AWS EKS
# see also: Kubernetes
##########################
kunnel() {
echo
sudo ps ax | grep ssh | grep fNL
sudo ps ax | grep ssh | grep fNL | cut -d " " -f 1 | xargs sudo kill
echo
echo Tunnel killed.
echo
}
kubernetic() {
cluster_name="$1"
if [ -z "${cluster_name}" ]; then echo "Please provide a cluster name."; return 0; fi
token="$(aws-iam-authenticator token --cluster-id "${cluster_name}" | jqr ".status.token")"
kc set-credentials kubernetic --token="${token}"
}
##########################
# GPG
##########################
# Given a filename with a gpg extension, decrypts it and outputs it with the same filename -
# but without the extension.
gpgdc() {
gpg --output "${1%.gpg}" --decrypt "$1"
}
##########################
# Kubernetes
# see also: AWS EKS
##########################
kbash() {
ke "${1}" -it -- "${2:-bash}"
}
# from @droctothorpe on hangops Slack
# @see https://hangops.slack.com/archives/C0GFYENRG/p1561587759269600
kgew() {
kge --all-namespaces --watch \
-o 'go-template={{.lastTimestamp}} ^ {{.involvedObject.kind}} ^ {{.message}} ^ ({{.involvedObject.name}}){{"\n"}}' \
| awk -F^ \
-v black=$(tput setaf 0) \
-v red=$(tput setaf 1) \
-v green=$(tput setaf 2) \
-v yellow=$(tput setaf 3) \
-v blue=$(tput setaf 4) \
-v magenta=$(tput setaf 5) \
-v cyan=$(tput setaf 6) \
-v white=$(tput setaf 7) \
'{
$1=blue $1
$2=green $2
$3=white $3
}
1'
}
# Scales a deployment down to 0 and back up to what it is, effectively restarting it.
krestart() {
deployment_name="${1}"
current_replicas="$(kgd "${deployment_name}" -o json | jq '.spec.replicas')"
k scale --replicas=0 deployment "${deployment_name}"
k scale --replicas="${current_replicas}" deployment "${deployment_name}"
echo && kgd "${deployment_name}"
echo && kp | grep "${deployment_name}"
}
# kupgrade 10001 next
# kupgrade 10002 current
# kupgrade 10003 previous
# kupgrade 10004 current
kupgrade() {
portal_id="${1}"
version_name="${2}"
command="AP_Command_Upgrade"
pod_name="$(kp -l "version-name=${version_name}" -o jsonpath='{.items[0].metadata.name}')"
echo
echo "Running ${command} on ${pod_name} for portal ID ${portal_id}..."
echo
ke "${pod_name}" -- /opt/code/astute/cli/ap.sh --portal_id "${portal_id}" --command "${command}"
}
# Prints out a nice list of nodes with all their pertinent data in columns.
#
# WARNING: This makes use of 'kubectl describe', due to deficiencies in the amount of data available
# through 'kubectl get'. 'describe' does not have a machine-readable output option, so it's possible
# the implementation could change in later releases.
nodes() {
get_data="$(kn --output json)" # This is easily parsable.
describe_data="$(kdn)" # This contains much more detail but is NOT easily parsable.
node_count="$(echo "${get_data}" | jqj '.items | length')"
date_fmt="%Y-%m-%dT%H:%M:%SZ"
out=""
out+="NAME |IP ADDRESS |ALLOC. CPU |ALLOC. MEM |PODS |AGE|H/BEAT|VERSION|INSTANCE|AZ|READY|TAINTS\n"
out+="=============|============|============|=============|=======|===|======|=======|========|==|=====|======\n"
# shellcheck disable=SC2051 # We're writing for zsh
for i in {0..$((node_count - 1))}; do
node_data="$(echo "${get_data}" | jqj '.items['$i']')"
node_name="$(ecn "${node_data}" | jqj '.metadata.name')"
extended_node_data="$(echo "${describe_data}" | sed -En "/Name:[[:space:]]+${node_name}/,/^$/p" )"
allocated_resources="$(echo "${extended_node_data}" | grep -A7 "Allocated resources:")"
pod_count="$(echo "${extended_node_data}" | grep "Non-terminated Pods:" | awk '{print $3}' | sed 's/[^0-9]//g')"
cpu_allocated_percent="$(echo "${allocated_resources}" | grep cpu | awk '{print $3}' | sed 's/[^0-9%]//g')"
mem_allocated_percent="$(echo "${allocated_resources}" | grep memory | awk '{print $3}' | sed 's/[^0-9%]//g')"
age="$(ecn "${node_data}" | jqj '.metadata.creationTimestamp' | date_stdin -juf "${date_fmt}" "+%s")"
heartbeat="$(ecn "${node_data}" | jqj '.status.conditions[] | select(.type == "Ready").lastHeartbeatTime' | date_stdin -juf "${date_fmt}" "+%s")"
mem_kb="$(ecn "${node_data}" | jqj '.status.capacity.memory' | sed 's/[^0-9]//g')"
mem_mb="$(( mem_kb / 1024 ))"
age_relative="$(relative "${age}")"
heartbeat_relative="$(relative "${heartbeat}")"
out+="$(ecn "${node_data}" | jqj '.status.addresses[] | select(.type == "Hostname").address')|"
out+="$(ecn "${node_data}" | jqj '.status.addresses[] | select(.type == "InternalIP").address')|"
out+="${(l:3:)cpu_allocated_percent} / $(ecn "${node_data}" | jqj '.status.capacity.cpu') vCPU|"
out+="${(l:3:)mem_allocated_percent} / ${mem_mb} MB|"
out+="${(l:2:)pod_count} / $(ecn "${node_data}" | jqj '.status.capacity.pods')|"
out+="${(l:3:)age_relative}|"
out+="${(l:3:)heartbeat_relative}|"
out+="$(ecn "${node_data}" | jqj '.status.nodeInfo.kubeletVersion')|"
out+="$(ecn "${node_data}" | jqj '.metadata.labels["beta.kubernetes.io/instance-type"]')|"
out+="$(ecn "${node_data}" | jqj '.metadata.labels["failure-domain.beta.kubernetes.io/zone"]' | sed 's/.*\(.\)/\1/')|"
out+="$(ecn "${node_data}" | jqj '.status.conditions[] | select(.type == "Ready").status')|"
out+="$(ecn "${node_data}" | jqj '.spec.taints[]?.effect')|"
out+="\n"
done
echo "${out}" | column -ts "|"
}
##########################
# Python
##########################
# Simply adds an item to requirements.txt - and sorts and removes duplicates.
# Does not actually install it.
pipadd(){
echo "${1}" >> requirements.txt
# De-dupe.
# @see https://stackoverflow.com/questions/9377040/remove-duplicate-entries-using-a-bash-script
#awk '!a[$0]++' requirements.txt # This works, but can't be redirected back to the same file.
perl -i -ne 'print if ! $a{$_}++' requirements.txt
cat requirements.txt
}
##########################
# Terraform
##########################
tatf() {
echo "Looking for matches..."
resources="$(tfind $1)"
args=""
while read -r resource; do
args="-target '${resource}' ${args}"
done <<< "${resources}"
args="${args//mod./module.}" # Allow 'module' to be shortened as 'mod'
echo "About to run ${olive}terraform apply ${args}${normal}"
echo "Press Ctrl+C within 2 seconds to cancel..."
sleep 2
echo "Waiting for Terraform..."
eval "terraform apply ${args}"
}
# Given one argument with a common Terraform resource address, runs it into the format required for (un)tainting.
# Terraform 0.11 only.
# eg. tf_convert_module taint module.main.module.man.null_resource.provisioner
tf_convert_module() {
command="$1"
full_address="$2"
module_arg=""
resource="${full_address}"
# Allow 'module' to be shortened as 'mod'
full_address="${full_address//mod./module.}"
if [[ ${full_address} =~ module\\. ]]; then
module_arg="-module "
# sed with extended regexps (-E) due to use of +
resource="$(echo "${full_address}" | sed -Ee 's/module\.[^\.]+\.//g' )" # Strip all modules
module_arg+="$(echo "${full_address}" | sed -Ee 's/module\.([^\.]+)/\1/g')" # Remove 'module.' from module names
module_arg="${module_arg%\.${resource}} " # Strip remaining resource name
fi
echo "Converted to 'terraform ${command} ${module_arg}${resource}'"
eval "terraform ${command} ${module_arg}${resource}"
}
# Makes -target command lines shorter by allowing 'mod' instead of 'module'.
# This can probably be made a lot simpler...
tf_target() {
command=${1}
target=${2}
shift 2;
additional_args=$@
command_to_run="$command -target $target $additional_args"
command_to_run="${command_to_run//mod./module.}"
# Remove trailing spaces (probably more complicated than it needs to be...)
command_to_run="${command_to_run%"${command_to_run##*[![:space:]]}"}"
echo -e "Running ${olive}${command_to_run}${normal}..."
eval "$command_to_run"
}
tfsp() {
tempfile=$(mktemp)
t state pull > "${tempfile}"
code "${tempfile}"
}