-
Notifications
You must be signed in to change notification settings - Fork 1
/
port6379
executable file
·185 lines (163 loc) · 4.72 KB
/
port6379
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
#!/bin/bash
# cloud
if [ "$1" = "-c" ]; then
shift
readonly PORT6379_CLOUD="$1"
shift
fi
# env
readonly PORT6379_HOME="$HOME/.port6379"
readonly PORT6379_CONFIG=$PORT6379_HOME/config.sh
readonly PORT6379_VERSION="0.9.1"
[ -z $PORT6379_CLOUD ] && readonly PORT6379_CLOUD="amazon-us-east"
[ -z $PORT6379_API_ENDPOINT ] && readonly PORT6379_API_ENDPOINT="https://api.${PORT6379_CLOUD}.port6379.com"
# we should probably not run as root
if [ `whoami` = "root" ]; then
echo "WARNING: You're currently running as root; probably by accident."
echo "Press control-C to abort or Enter to continue as root."
read _
fi
# add some padding on the top of output
echo
# load config
[[ ! -d $PORT6379_HOME ]] && mkdir -p $PORT6379_HOME
#from lein2
# normalize $0 on certain BSDs
# if [ "$(dirname "$0")" = "." ]; then
# SCRIPT="$(which $(basename "$0"))"
# else
# SCRIPT="$0"
# fi
SCRIPT="$0"
# resolve symlinks to the script itself portably (thanx Leiningen!)
while [ -h "$SCRIPT" ] ; do
ls=`ls -ld "$SCRIPT"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
SCRIPT="$link"
else
SCRIPT="$(dirname "$SCRIPT"$)/$link"
fi
done
# choose and config curl or wget (thanx lein2!)
HTTP_CLIENT="wget --no-check-certificate -O"
if type -p curl >/dev/null 2>&1; then
if [ "$https_proxy" != "" ]; then
CURL_PROXY="-x $https_proxy"
fi
HTTP_CLIENT="curl $CURL_PROXY --insecure -f -L -o"
fi
# Commands
if [ "$1" = "test" ]; then
echo "Running $SCRIPT"
exit 0
elif [ "$1" = "version" ]; then
echo $PORT6379_VERSION
exit 0
elif [ "$1" = "status" ]; then
curl -s -H "Accept-content-type: text/plain" "http://status.port6379.com"
echo; echo
exit 0
elif [ "$1" = "upgrade" ]; then
if [ ! -w "$SCRIPT" ]; then
echo "You do not have permission to upgrade the installation in $SCRIPT"
exit 1
else
TARGET_VERSION="stable"
echo "The script at $SCRIPT ($PORT6379_VERSION) will be upgraded to the latest $TARGET_VERSION version."
echo -n "Do you want to continue [Y/n]? "
read RESP
case "$RESP" in
y|Y|"")
echo
echo "Upgrading..."
echo "Using $HTTP_CLIENT"
TARGET="/tmp/port6379-$$-upgrade"
PORT6379_SCRIPT_URL="https://github.com/wallrat/port6379-cli/raw/master/port6379"
$HTTP_CLIENT "$TARGET" "$PORT6379_SCRIPT_URL" \
&& mv "$TARGET" "$SCRIPT" \
&& chmod +x "$SCRIPT" \
&& echo && echo "Now running" `$SCRIPT version`
exit $?;;
*)
echo "Aborted."
exit 1;;
esac
fi
elif [ "$1" = "login" ]; then
USER=$2
if [[ -z $USER ]]; then
echo -n "Email: "
read USER
fi
echo -n "Password: "
read -s PASSWORD
echo
## login
RESPONSE=$(curl -s -L -d "u=$USER" --data-urlencode "p=$PASSWORD" "$PORT6379_API_ENDPOINT/login")
if [ "$?" = "7" ]; then
echo "Could not contact API endpoint at $PORT6379_API_ENDPOINT"
echo
exit 1
fi
# validate credentials
CHECK=$(curl -s -L -u "$USER:$RESPONSE" "$PORT6379_API_ENDPOINT/check-credentials")
if [[ ! "ok" = "$CHECK" ]]; then
echo $RESPONSE
echo
exit 1
fi
echo "PORT6379_API_USER=\"$USER\"" > $PORT6379_CONFIG
echo "PORT6379_API_KEY=\"$RESPONSE\"" >> $PORT6379_CONFIG
echo "Stored api-key \"$RESPONSE\" to $PORT6379_CONFIG"
echo
exit 0
fi
# source config
if [ ! -f $PORT6379_CONFIG ]; then
echo "$PORT6379_CONFIG not found"
echo "Run $0 login to add your credentials"
echo
exit 1
fi
source $PORT6379_CONFIG
# api test call?
if [ "$1" = "api" ]; then
shift
if [ "$1" = "" ]; then
echo "Usage: $0 api [curl options] resource"
echo
exit 1
fi
CMD="curl -i -L -u $PORT6379_API_USER:$PORT6379_API_KEY $PORT6379_API_ENDPOINT$*"
echo $CMD
$CMD
echo
exit 0
fi
# openssl is available on most linux and OSX, but not cygwin
if type -p openssl >/dev/null 2>&1; then
B64=$(echo -n "$*" | openssl enc -base64 | tr -d '\n')
elif type -p base64 >/dev/null 2>&1; then
B64=$(echo -n "$*" | base64 -w 0)
else
echo "Error: Could not find 'base64' or 'openssl'"
exit 1
fi
# normal CLI call
CMD="curl -f -s -L -d base64=$B64 -u $PORT6379_API_USER:$PORT6379_API_KEY $PORT6379_API_ENDPOINT/cli/cmd"
RESPONSE=$($CMD)
CMDRESULT=$?
if [ "$CMDRESULT" = "7" ] || [ "$CMDRESULT" = "22" ]; then
echo "Could not contact API endpoint at $PORT6379_API_ENDPOINT"
echo "Trying to get current status from http://status.port6379.com"
exec $0 status
fi
if [ ! "$CMDRESULT" = "0" ]; then
echo "Error $CMDRESULT"
echo "$RESPONSE"
echo
exit 1
fi
echo "$RESPONSE"
echo