-
Notifications
You must be signed in to change notification settings - Fork 5
/
shared.bash
593 lines (501 loc) · 17.4 KB
/
shared.bash
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
#!/usr/bin/env bash
function main() {
function setup_aliases() {
alias vim=nvim
alias vi=nvim
alias ll="ls -al"
alias be="bundle exec"
alias bake="bundle exec rake"
alias drm='docker rm $(docker ps -a -q)'
alias drmi='docker rmi $(docker images -q)'
#git aliases
alias gst="git status"
alias gd="git diff"
alias gap="git add -p"
alias gup="git pull -r"
alias gp="git push"
alias ga="git add"
alias gbt="gobosh_target"
alias gbtl="gobosh_target_lite"
alias cft="cf_target"
alias cftl="cf_target local"
alias t="target"
alias rg="ag"
alias h?="history | grep"
alias chg="credhub_get"
}
function setup_environment() {
export CLICOLOR=1
export LSCOLORS exfxcxdxbxegedabagacad
# go environment
export GOPATH=$HOME/go
export GOBIN=$GOPATH/bin
# git duet config
export GIT_DUET_GLOBAL=true
export GIT_DUET_ROTATE_AUTHOR=1
# setup path
export PATH=$GOBIN:$PATH
export EDITOR=nvim
}
function setup_rbenv() {
eval "$(rbenv init -)"
}
function setup_aws() {
# set awscli auto-completion
complete -C aws_completer aws
}
function setup_fasd() {
local fasd_cache
fasd_cache="$HOME/.fasd-init-bash"
if [ "$(command -v fasd)" -nt "$fasd_cache" -o ! -s "$fasd_cache" ]; then
fasd --init posix-alias bash-hook bash-ccomp bash-ccomp-install >| "$fasd_cache"
fi
source "$fasd_cache"
eval "$(fasd --init auto)"
}
function setup_completions() {
[ -f /usr/local/etc/bash_completion ] && . /usr/local/etc/bash_completion
}
function setup_direnv() {
eval "$(direnv hook bash)"
}
function setup_gitprompt() {
if [ -f "$(brew --prefix)/opt/bash-git-prompt/share/gitprompt.sh" ]; then
# git prompt config
export GIT_PROMPT_SHOW_UNTRACKED_FILES=normal
export GIT_PROMPT_ONLY_IN_REPO=0
export GIT_PROMPT_THEME="Custom"
source "$(brew --prefix)/opt/bash-git-prompt/share/gitprompt.sh"
fi
}
function setup_colors() {
local colorscheme
colorscheme="${HOME}/.config/colorschemes/scripts/base16-monokai.sh"
[[ -s "${colorscheme}" ]] && source "${colorscheme}"
}
function setup_ssh_agent() {
if [[ ! -e ~/.ssh_agent ]]; then
if [[ -n ${SSH_AUTH_SOCK} ]]; then
ln -sf $SSH_AUTH_SOCK ~/.ssh_agent
fi
fi
export SSH_AUTH_SOCK=~/.ssh_agent
}
local dependencies
dependencies=(
aliases
environment
colors
rbenv
aws
fasd
completions
direnv
gitprompt
ssh_agent
)
for dependency in ${dependencies[@]}; do
eval "setup_${dependency}"
unset -f "setup_${dependency}"
done
}
function reload() {
source "${HOME}/.bash_profile"
}
function reinstall() {
local workspace
workspace="~/workspace/c2c-workspace"
if [[ ! -d "${workspace}" ]]; then
git clone https://github.com/cloudfoundry-incubator/c2c-workspace "${workspace}"
fi
pushd "${workspace}" > /dev/null
git diff --exit-code > /dev/null
if [[ "$?" = "0" ]]; then
git pull -r
bash -c "./install.sh"
else
echo "Cannot reinstall. There are unstaged changes in the c2c-workspace repo."
git diff
fi
popd > /dev/null
}
function cf_bosh_lite {
if (( $# == 0 ))
then echo usage: cf_bosh_lite password;
else
cf api api.bosh-lite.com --skip-ssl-validation && cf auth admin $1 && cf t -o o -s s
fi
}
function cf_create_org {
cf create-org o && cf t -o o && cf create-space s && cf t -o o -s s
}
function bosh_ssh_c2c {
if (( $# != 1 ))
then echo "Usage: bosh_ssh_c2c <env>"
else
bosh target bosh.$1.c2c.cf-app.com
bosh download manifest $1-diego /tmp/$1-diego.yml
bosh -d /tmp/$1-diego.yml ssh --gateway_host bosh.$1.c2c.cf-app.com --gateway_user vcap --gateway_identity_file ~/workspace/cf-networking-deployments/environments/$1/keypair/id_rsa_bosh
fi
}
cf_target ()
{
if [ $# = 0 ]; then
echo "missing environment-name"
echo ""
echo "example usage:"
echo "cft environment-name"
return
fi
env=$1
workspace=$2
if [ "$env" = "ci" ]; then
echo "no CF deployed in ci env."
return
fi
if [ "$env" = "local" ] || [ "$env" = "lite" ]; then
password=$(grep cf_admin_password "${HOME}/workspace/cf-networking-deployments/environments/${env}/deployment-vars.yml" | cut -d" " -f2)
else
password=$(credhub get -n "/bosh-${env}/cf/cf_admin_password" | bosh int --path /value -)
fi
if [ "$workspace" = "routing" ]; then
system_domain="${env}.routing.cf-app.com"
elif [ "$env" = "local" ] || [ "$env" = "lite" ]; then
system_domain="bosh-lite.com"
else
system_domain="${env}.c2c.cf-app.com"
fi
cf api "api.${system_domain}" --skip-ssl-validation
cf auth admin "${password}"
}
gobosh_target ()
{
gobosh_untarget
if [ $# = 0 ]; then
return
fi
export BOSH_ENV=$1
if [ "$BOSH_ENV" = "local" ] || [ "$BOSH_ENV" = "lite" ]; then
gobosh_target_lite
return
fi
if [[ "${BOSH_ENV}" == "ci" ]]; then
pushd $(mktemp -d) > /dev/null
gsutil cp gs://c2c-bbl-states/ci ci.tgz
tar xf ci.tgz
eval "$(bbl print-env)"
popd > /dev/null
export BOSH_DEPLOYMENT="concourse"
return
fi
workspace=$2
if [ "$workspace" = "pcf" ]; then
export BOSH_DIR=~/workspace/pcf-networking-deployments/environments/$BOSH_ENV
elif [ "$workspace" = "routing" ]; then
export BOSH_DIR=~/workspace/deployments-routing/$BOSH_ENV/bbl-state
else
export BOSH_DIR=~/workspace/cf-networking-deployments/environments/$BOSH_ENV
fi
pushd $BOSH_DIR 1>/dev/null
eval "$(bbl print-env)"
popd 1>/dev/null
export BOSH_DEPLOYMENT="cf"
}
gobosh_untarget ()
{
unset BOSH_ENV
unset BOSH_DIR
unset BOSH_USER
unset BOSH_PASSWORD
unset BOSH_ENVIRONMENT
unset BOSH_GW_HOST
unset BOSH_GW_PRIVATE_KEY
unset BOSH_CA_CERT
unset BOSH_DEPLOYMENT
unset BOSH_CLIENT
unset BOSH_CLIENT_SECRET
}
target ()
{
gobosh_target ${@}
cf_target ${@}
}
gobosh_target_lite ()
{
gobosh_untarget
export BOSH_DIR=~/workspace/cf-networking-deployments/environments/local
pushd $BOSH_DIR >/dev/null
export BOSH_CLIENT="admin"
export BOSH_CLIENT_SECRET="$(bosh int ./creds.yml --path /admin_password)"
export BOSH_ENVIRONMENT="vbox"
export BOSH_CA_CERT=/tmp/bosh-lite-ca-cert
bosh int ./creds.yml --path /director_ssl/ca > $BOSH_CA_CERT
popd 1>/dev/null
unset BOSH_ALL_PROXY
export BOSH_DEPLOYMENT=cf;
if [ "$env" = "ci" ]; then
export BOSH_DEPLOYMENT=concourse
fi
}
readd_local_route ()
{
ips="10.244.0.0/16"
gw="192.168.50.6"
sudo route delete -net "$ips" "$gw"
sudo route add -net "$ips" "$gw"
}
ssh_bosh_lite_director ()
{
local creds=~/workspace/cf-networking-deployments/environments/local/creds.yml
bosh int $creds --path /jumpbox_ssh/private_key > /tmp/jumpbox.key
chmod 600 /tmp/jumpbox.key
ssh [email protected] -i /tmp/jumpbox.key
}
gobosh_build_manifest ()
{
bosh -d cf build-manifest -l=$BOSH_DIR/deployment-env-vars.yml --var-errs ~/workspace/cf-deployment/cf-deployment.yml
}
gobosh_patch_manifest ()
{
pushd ~/workspace/cf-deployment 1>/dev/null
git apply ../cf-networking-ci/netman-cf-deployment.patch
popd 1>/dev/null
}
extract_manifest ()
{
bosh task $1 --debug | deployment-extractor
}
create_upload ()
{
bosh create-release --force --timestamp-version && bosh upload-release
}
upload_bosh_stemcell () {
STEMCELL_VERSION="$(bosh int ~/workspace/cf-deployment/cf-deployment.yml --path=/stemcells/0/version)"
echo "will upload stemcell ${STEMCELL_VERSION}"
bosh -e vbox upload-stemcell "https://bosh.io/d/stemcells/bosh-warden-boshlite-ubuntu-trusty-go_agent?v=${STEMCELL_VERSION}"
}
deploy_bosh_lite ()
{
bosh deploy --no-redact -n ~/workspace/cf-deployment/cf-deployment.yml \
-o ~/workspace/cf-deployment/operations/bosh-lite.yml \
-o ~/workspace/cf-networking-deployments/environments/local/instance-count-overrides.yml \
-o ~/workspace/cf-deployment/operations/enable-service-discovery.yml \
-o ~/workspace/cf-networking-release/manifest-generation/opsfiles/use-latest.yml \
-o ~/workspace/silk-release/opsfiles/use-latest.yml \
-o $BOSH_DIR/opsfile.yml \
--vars-store ~/workspace/cf-networking-deployments/environments/local/deployment-vars.yml \
-v system_domain=bosh-lite.com
}
gobosh_deploy ()
{
bosh deploy -n ~/workspace/cf-deployment/cf-deployment.yml \
-o ~/workspace/cf-deployment/operations/use-compiled-releases.yml \
-o ~/workspace/cf-networking-release/manifest-generation/opsfiles/cf-networking.yml \
-o ~/workspace/cf-networking-release/manifest-generation/opsfiles/use-latest.yml \
-o $BOSH_DIR/opsfile.yml \
--vars-store $BOSH_DIR/vars-store.yml \
-v system_domain=$(echo "${BOSH_DIR}" | cut -f 7 -d '/').c2c.cf-app.com
}
create_bosh_lite ()
{
gobosh_target_lite;
bosh create-env ~/workspace/bosh-deployment/bosh.yml \
--state ~/workspace/cf-networking-deployments/environments/local/state.json \
-o ~/workspace/bosh-deployment/virtualbox/cpi.yml \
-o ~/workspace/bosh-deployment/virtualbox/outbound-network.yml \
-o ~/workspace/bosh-deployment/bosh-lite.yml \
-o ~/workspace/bosh-deployment/bosh-lite-runc.yml \
-o ~/workspace/bosh-deployment/jumpbox-user.yml \
-o ~/workspace/bosh-deployment/local-dns.yml \
--vars-store ~/workspace/cf-networking-deployments/environments/local/creds.yml \
-v director_name="Bosh Lite Director" \
-v internal_ip=192.168.50.6 \
-v internal_gw=192.168.50.1 \
-v internal_cidr=192.168.50.0/24 \
-v outbound_network_name="NatNetwork"
bosh -e 192.168.50.6 --ca-cert <(bosh int ~/workspace/cf-networking-deployments/environments/local/creds.yml --path /director_ssl/ca) alias-env vbox
export BOSH_CLIENT="admin"
export BOSH_CLIENT_SECRET="$(bosh int ~/workspace/cf-networking-deployments/environments/local/creds.yml --path /admin_password)"
export BOSH_ENVIRONMENT="vbox"
export BOSH_DEPLOYMENT="cf"
export BOSH_CA_CERT="/tmp/bosh-lite-ca-cert"
bosh int ~/workspace/cf-networking-deployments/environments/local/creds.yml --path /director_ssl/ca > ${BOSH_CA_CERT}
STEMCELL_VERSION="$(bosh int ~/workspace/cf-deployment/cf-deployment.yml --path=/stemcells/0/version)"
echo "will upload stemcell ${STEMCELL_VERSION}"
bosh -e vbox upload-stemcell "https://bosh.io/d/stemcells/bosh-warden-boshlite-ubuntu-trusty-go_agent?v=${STEMCELL_VERSION}"
bosh -e vbox -n update-cloud-config ~/workspace/cf-deployment/iaas-support/bosh-lite/cloud-config.yml
}
delete_bosh_lite ()
{
bosh delete-env ~/workspace/bosh-deployment/bosh.yml \
--state ~/workspace/cf-networking-deployments/environments/local/state.json \
-o ~/workspace/bosh-deployment/virtualbox/cpi.yml \
-o ~/workspace/bosh-deployment/virtualbox/outbound-network.yml \
-o ~/workspace/bosh-deployment/bosh-lite.yml \
-o ~/workspace/bosh-deployment/bosh-lite-runc.yml \
-o ~/workspace/bosh-deployment/jumpbox-user.yml \
--vars-store ~/workspace/cf-networking-deployments/environments/local/creds.yml \
-v director_name="Bosh Lite Director" \
-v internal_ip=192.168.50.6 \
-v internal_gw=192.168.50.1 \
-v internal_cidr=192.168.50.0/24 \
-v outbound_network_name="NatNetwork"
}
unbork_consul ()
{
bosh vms | grep consul | cut -d ' ' -f1 > /tmp/consul-vms
cat /tmp/consul-vms | xargs -n1 bosh ssh -c "sudo /var/vcap/bosh/bin/monit stop consul_agent"
cat /tmp/consul-vms | xargs -n1 bosh ssh -c "sudo rm -rf /var/vcap/store/consul_agent/*"
cat /tmp/consul-vms | xargs -n1 bosh ssh -c "sudo /var/vcap/bosh/bin/monit start consul_agent"
}
function windows_port_forward() {
echo "Port forwarding from $1"
ssh -f -L 3389:$1:3389 -N -i ${BOSH_GW_PRIVATE_KEY} ${BOSH_GW_USER}@${BOSH_GW_HOST}
}
function story() {
if [ -n "$TRACKER_API_TOKEN" ]; then
STORY_TITLE=" $(curl -s -H "X-TrackerToken: $TRACKER_API_TOKEN" \
"https://www.pivotaltracker.com/services/v5/projects/$TRACKER_PROJECT/stories/${1/\#/}" \
| jq -r .name)"
else
STORY_TITLE=''
fi
printf "\n\n[$1]$STORY_TITLE" > ~/.git-tracker-story
}
default_hours() {
local current_hour=$(date +%H | sed 's/^0//')
local result=$((17 - current_hour))
if [[ ${result} -lt 1 ]]; then
result=1
fi
echo -n ${result}
}
set_key() {
local hours=$1
/usr/bin/ssh-add -D
echo "Setting hours to: $hours"
lpass show --notes 'ProductivityTools/id_rsa' | /usr/bin/ssh-add -t ${hours}H -
}
set-git-keys() {
local email=$1
local hours=$2
if [[ -z ${email} ]]; then
echo "Usage: $0 [LastPass email or git author initials] [HOURS (optional)]"
return
fi
if git_author_path "/authors/$email" >/dev/null 2>&1; then
echo "Adding key for $(bosh int ${HOME}/.git-authors --path="/authors/$email" | sed 's/;.*//')"
email="$(bosh int ${HOME}/.git-authors --path="/authors/$email" | sed 's/;.*//')@$(bosh int ${HOME}/.git-authors --path="/email/domain")"
fi
if [[ -z ${hours} ]]; then
hours=$(default_hours)
fi
if ! [[ $(lpass status) =~ $email ]]; then
lpass login "$email"
fi
set_key ${hours}
}
function current_branch() { # Gets current branch
git rev-parse --abbrev-ref HEAD
}
function parse_branch() { # Gets current branch with parens around it for some legacy things
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
function gh_remote_path() { # Parses the 'remote path' of the repo: username/repo
REMOTE=${1:-origin}
GH_PATH=`git remote -v | tr ':' ' ' | tr '.' ' ' | grep $REMOTE | awk '/push/ {print $4}'`
echo ${GH_PATH#com/}
}
function gh() { # Opens current branch on Github, works for all repos
REMOTE=${1:-origin}
echo 'Opening branch on Github...'
open "https://github.com/$(gh_remote_path $REMOTE)/tree/$(current_branch)"
}
function newpr() { # Opens current branch on Github in the "Open a pull request" compare view
echo 'Opening compare on Github...'
open "https://github.com/$(gh_remote_path)/compare/$(current_branch)?expand=1"
}
function gpu() { # Push upstream
git push --set-upstream origin `current_branch`
}
function mkd() { # Create a new directory and enter it
mkdir -p "$@" && cd "$_";
}
function loop() { # Repeats a given command forever
local i=2 t=1 cond
[ -z ${1//[0-9]/} ] && i=$1 && shift
[ -z ${1//[0-9]/} ] && t=$1 && shift && cond=1
while [ $t -gt 0 ]; do
sleep $i
[ $cond ] && : $[--t]
$@
done
}
function server() { # Create webserver from current directory
local port="${1:-8000}";
sleep 1 && open "http://localhost:${port}/" &
# Set the default Content-Type to `text/plain` instead of `application/octet-stream`
# And serve everything as UTF-8 (although not technically correct, this doesn’t break anything for binary files)
python -c $'import SimpleHTTPServer;\nmap = SimpleHTTPServer.SimpleHTTPRequestHandler.extensions_map;\nmap[""] = "text/plain";\nfor key, value in map.items():\n\tmap[key] = value + ";charset=UTF-8";\nSimpleHTTPServer.test();' "$port";
}
function nuke() { # Straight up murders all processes matching first arg
ps ax | grep $1 | awk '{print $1}' | xargs kill -9
}
function politely_nuke() { # As above but nicely
ps ax | grep $1 | awk '{print $1}' | xargs kill
}
function smart_bomb() { # Don't use this
killall -15 $1 2> /dev/null || killall -2 $1 2> /dev/null || killall -1 $1 2> /dev/null || killall -9 $1 2> /dev/null
}
function clear_port() { # Finds whatever is using a given port (except chrome) and kills it
lsof -t -i tcp:$1 | ag -v "$(ps aux|ag Chrome|tr -s ' '|cut -d ' ' -f 2|fmt -1024|tr ' ' '|')"| xargs kill -9
}
function v() { # Use fasd to open a file in vim from anywhere
nvim `f "$1" | awk "{print $2}"`
}
function credhub_get() {
credhub get -n `credhub find -n "$1" | grep "name" | head -n 1 | sed 's/- name: //'`
}
function standup() {
open "https://pivotal.zoom.us/j/589840978"
open "https://pair.ist/cf-networking/current"
}
function toolsmiths() {
echo "Opening https://environments.toolsmiths.cf-app.com..."
open https://environments.toolsmiths.cf-app.com
echo "NOTE: Run create_service_account_key_for_toolsmiths_env <story_id>"
echo " to create a service account with a key for toolsmiths env."
}
function create_service_account_key_for_toolsmiths_env() {
if [[ $# != 1 ]]; then
echo "Usage: create_service_account_key_for_toolsmiths_env <story_id>"
return 1
fi
local story_id="${1}"
local service_account_name="opsman-${story_id}"
local service_account_email="${service_account_name}@cf-container-networking-gcp.iam.gserviceaccount.com"
local gcp_project_name="cf-container-networking-gcp"
local key_file_name="$(mktemp)"
gcloud iam service-accounts create "${service_account_name}" \
--display-name "${service_account_name}" 1> /dev/null
gcloud projects add-iam-policy-binding "${gcp_project_name}" \
--member "serviceAccount:${service_account_email}" \
--role roles/editor 1> /dev/null
gcloud iam service-accounts keys create "${key_file_name}" \
--iam-account "${service_account_email}" 1> /dev/null
echo "NOTE: Remember to clean up your key after your done using remove_service_account_key_for_toolsmiths_env." 1>&2
cat "${key_file_name}"
rm "${key_file_name}"
}
function remove_service_account_key_for_toolsmiths_env() {
if [[ $# != 1 ]]; then
echo "Usage: remove_service_account_key_for_toolsmiths_env <story_id>"
return 1
fi
local story_id="${1}"
local service_account_email="opsman-${story_id}@cf-container-networking-gcp.iam.gserviceaccount.com"
gcloud iam service-accounts delete "${service_account_email}"
}
main
unset -f main
export PATH="/usr/local/opt/[email protected]/bin:$PATH"