-
Notifications
You must be signed in to change notification settings - Fork 0
/
awsm-exec.sh
123 lines (102 loc) · 2.35 KB
/
awsm-exec.sh
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
set -e
set +u
: ${AWSM_SESSION_FILE=.awsm-session}
: ${AWSM_EXEC_FILE=.awsm-exec}
if [ -f $AWSM_SESSION_FILE ]; then
. $AWSM_SESSION_FILE
fi
if [ -f $AWSM_EXEC_FILE ]; then
. $AWSM_EXEC_FILE
fi
awsm_session_file=$AWSM_HOME/session
if [ -f $awsm_session_file ]; then
. $awsm_session_file
fi
function _select_instance {
local instance_line=$(instances | $FUZZY_FILTER)
echo $(echo $instance_line | read_inputs)
}
function _ssh_exec {
if [ -n "${AWSM_INSTANCE_IP-}" ]; then
local instance_id=$AWSM_INSTANCE_IP;
else
local instance_id=$(_select_instance)
fi
local sudo_command=""
if [ -n "$AWSM_EXEC_SUDO_USER" ]; then
local sudo_command="sudo -i -u $AWSM_EXEC_SUDO_USER"
fi
$SSH_BIN $AWSM_SSH_USER@$instance_id -t $sudo_command $@
}
function _rails_check_app_dir {
if [ -z "${1-}" ]; then
echo 'Rails app directory must be supplied'
exit 1
fi
}
function _rails_deploy_dir {
if [ -n "$AWSM_APP_DEPLOY_DIR" ]; then
local app_dir=$AWSM_APP_DEPLOY_DIR
else
local app_dir=$1
fi
echo $app_dir
}
function _rails_console {
local rails_deploy_dir=$(_rails_deploy_dir ${1-})
_rails_check_app_dir $rails_deploy_dir
_ssh_exec "bash -c 'cd $rails_deploy_dir; bundle exec rails c'"
}
function _rails_db {
local rails_deploy_dir=$(_rails_deploy_dir ${1-})
_rails_check_app_dir $rails_deploy_dir
_ssh_exec "bash -c 'cd $rails_deploy_dir; bundle exec rails db'"
}
## PUBLIC FUNCTIONS
function diskspace {
_ssh_exec 'df -h'
}
function memory {
_ssh_exec 'vmstat'
}
function exec {
_ssh_exec "$@"
}
# TODO - Make session directory dependent?
function session {
if [ $# == 1 ]; then
if [ $1 == "clear" ]; then
echo "" > $awsm_session_file;
fi
else
echo "AWSM_INSTANCE_IP=$(_select_instance)" > $awsm_session_file
fi
}
function app_exec {
if [ -z $AWSM_APP_DEPLOY_DIR ]; then
echo 'AWSM_APP_DEPLOY_DIR must be set in .awsm-profile or globally'
exit 1
fi
_ssh_exec "bash -c 'cd $AWSM_APP_DEPLOY_DIR; $@'"
}
function rails {
if [ $# -lt 1 ]; then
echo 'Need to specify a command (either console, c or db)'
exit 1
fi
case "$1" in
console)
_rails_console ${2-}
;;
c)
_rails_console ${2-}
;;
db)
_rails_db ${2-}
;;
*)
echo "Only rails console, rails c, or rails db supported. '$@' unknown"
exit 1
esac
}
set -u