-
Notifications
You must be signed in to change notification settings - Fork 0
/
dot-bashrc
226 lines (188 loc) · 6.34 KB
/
dot-bashrc
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
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
# append to the history file, don't overwrite it
shopt -s histappend
# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize
# aliases
alias ls='ls -FG'
alias sshaws='awsssh'
alias rdpaws='awsrdp'
alias cddev='cd ~/Development/'
alias cdtop='cd $(git rev-parse --show-toplevel)'
# My Exports
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/Development/python/
export ALTERNATE_EDITOR=vim EDITOR=vim VISUAL=vim
export GREP_OPTIONS='--color=auto'
export HISTCONTROL=ignoredups
export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python
export PIP_RESPECT_VIRTUALENV=true
export PIP_REQUIRE_VIRTUALENV=true
# Set my history size
HISTSIZE=10000
HISTFILESIZE=1000
HISTTIMEFORMAT='%d-%m-%y %T '
export PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"
export HISTIGNORE="&:ls:[bf]g:exit:pwd:history:clear"
if [ -f $HOME/.http_proxy ];then
source $HOME/.http_proxy
fi
function aws_check_args () {
if [ "$#" -le 1 ]; then
echo "Not enough arguments, (instanceid, profile, [public, private])"
return 1
fi
i_id=$1
if [ ${i_id:0:2} != "i-" ]; then
i_id="i-$i_id"
fi
echo "$i_id"
}
function awsgetconsole () {
i_id=$(aws_check_args "$@")
aws ec2 get-console-output --instance-id $1 --profile $2 --output text
}
function awsrdp () {
scheme='PublicIpAddress'
i_id=$(aws_check_args "$@")
if [ "$3" == 'private' ]; then
scheme='PrivateIpAddress'
fi
output=$(aws ec2 describe-instances --profile $2 --instance-ids $i_id --query "Reservations[0].Instances[0].{RUNNING:State.Name,IPADDR:$scheme}" --output json)
ip=$(echo $output | awk '{print $5}' | sed -e "s/\"//g" -e "s/,//g")
running=$(echo $output |grep -c running)
if [ "$(uname -s)" = "Darwin" ]; then
if [ ! -f /Applications/Microsoft\ Remote\ Desktop.app/Contents/MacOS/Microsoft\ Remote\ Desktop ]; then
echo "RDP client not installed"
return 1
fi
echo -e "full address:s:$ip\n username:s:Administrator" > /tmp/${ip}.rdp
if [ $running -gt 0 ]; then
/usr/bin/open /tmp/$ip.rdp
else
echo "Host $i_id unavailable"
return 1
fi
fi
}
function awsssh () {
scheme='PrivateIpAddress'
USERNAME="ec2-user"
i_id=$(aws_check_args "$@")
if [ "$3" == 'public' ]; then
scheme='PublicIpAddress'
fi
#running=$(aws ec2 describe-instances --instance-ids $i_id --profile $2 | grep -c '"Name": "running"')
#ip=$(aws ec2 describe-instances --instance-ids $i_id --profile $2 --output json | egrep -o '"PrivateIpAddress": "([0-9]{1,3}[\.]){3}[0-9]{1,3}"' |uniq| awk -F\" '{print $(NF-1)}')
#key=$(aws ec2 describe-instances --instance-ids $i_id --profile $2 --output json | egrep -o '"KeyName": ".*?"' | awk -F\" '{print $(NF-1)}')
output=$(aws ec2 describe-instances --instance-ids $i_id --profile $2 --query "Reservations[0].Instances[0].{RUNNING:State.Name,IPADDR:$scheme,KEY:KeyName}" --output json)
running=$(echo $output |grep -c running)
ip=$(echo $output | awk '{print $5}' | sed -e "s/\"//g" -e "s/,//g")
key=$(echo $output | awk '{print $7}' | sed -e "s/\"//g" -e "s/,//g")
if [ ! $running -gt 0 ]; then
echo "Host $1 not available"
elif [ -z "$ip" ]; then
echo "No Ip address could be found"
elif [ ! -e ~/.ssh/"$key"* ]; then
echo "SSH key %key could not be found in ~/.ssh/"
else
ssh -i ~/.ssh/"$key"* $USERNAME@$ip
fi
}
function awslookup (){
if [ "$#" -ne 2 ]; then
echo "Not enough arguments, arguments $1 Tag name value, $2 profile name"
return 1
fi
tag=*$1*
aws ec2 describe-instances \
--output table \
--filters "Name=tag-key,Values=Name" "Name=tag-value,Values=$tag" "Name=instance-state-code,Values=16" \
--query 'Reservations[].Instances[].[Tags[?Key==`Name`] | [0].Value, InstanceId]' \
--profile $2
}
function git_branch (){
branch=$(git branch 2> /dev/null | grep \* | awk '{print $NF}')
if [[ $branch != "" ]]; then
echo "[$branch]"
fi
}
function git_stash (){
stash=$(git stash list 2>/dev/null)
if [[ $stash != "" ]]; then
echo "-stashedChanges"
fi
}
export PS1=`echo '\033[0;31;1m[\u@\h] \033[0;36;1m[\T] \033[0;32;1m \w \033[0;0m''$(git_branch)' '$(git_stash)'" \n$ "`
if [ -f ~/.git-completion.bash ]; then
. ~/.git-completion.bash
fi
if [ -f /usr/local/bin/virtualenvwrapper.sh ]; then
source /usr/local/bin/virtualenvwrapper.sh
fi
PATH=${PATH}:/usr/share/bcc/tools/
if [ "$(uname)" = "SunOS" ]; then
PATH=${PATH}:/usr/bin:/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/usr/ucb/:/usr/local/samba/bin
MANPATH=/usr/share/man:/usr/sfw/man
export TERM=xtermc
fi
# Set autocomplete for aws CLI
complete -C aws_completer aws
# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if ! shopt -oq posix; then
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
elif [ -f /etc/bash_completion ]; then
. /etc/bash_completion
elif [ -f $(brew --prefix)/etc/bash_completion ]; then
. $(brew --prefix)/etc/bash_completion
fi
fi
# Added bash auto-completion
# pip bash completion start
_pip_completion()
{
COMPREPLY=( $( COMP_WORDS="${COMP_WORDS[*]}" \
COMP_CWORD=$COMP_CWORD \
PIP_AUTO_COMPLETE=1 $1 ) )
}
complete -o default -F _pip_completion pip
# pip bash completion end
_ssh_completion()
{
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
opts=$(grep '^Host' ~/.ssh/config | awk '{print $2}')
COMPREPLY=( $(compgen -W "$opts" -- ${cur}) )
return 0
}
_awslookup_completion()
{
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
opts=$(cat ~/.aws/tags | tr -d '{}"'|awk -F':' '{print $1}')
COMPREPLY=( $(compgen -W "$opts" -- ${cur}) )
return 0
}
function cddev()
{
cd "$HOME/Development/$1"
}
complete -F _awslookup_completion awslookup
complete -F _ssh_completion ssh
complete -C ~/.dev_folder_completion.rb -o filenames -o nospace cddev
# For site specific bash configs
if [ -f ~/.bash_site_specific ]; then
source ~/.bash_site_specific
fi