-
Notifications
You must be signed in to change notification settings - Fork 0
/
beanstalk
executable file
·169 lines (147 loc) · 3.58 KB
/
beanstalk
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
#!/bin/bash
agent=beanstalk-cli
# Process ARGV
command=$1
value=$2
data=$3
# $1 = method
# $2 = path
# $3 = data
function request(){
if [ $1 == GET ]; then
curl -G -X ${1} \
--user-agent $agent \
-H "Accept: application/json" \
-H "Content-type: application/json" \
-d "$3" \
${domain}/api/${2}.json
exit
fi
curl -X ${1} \
--user-agent $agent \
-H "Accept: application/json" \
-H "Content-type: application/json" \
-d "$3" \
${domain}/api/${2}.json
}
# Baisc Authetication method
function auth(){
echo 'Account name:'
read account
echo 'Username:'
read username
echo 'Password:'
read -s password
}
# List users
# $value= USER_ID
function users(){
if [ $value ]; then
request GET users/${value} | jq '.user' | jq '.name, .email'
exit
fi
request GET users | jq '.[].user' | jq '.id, .name, .email'
}
# Create user using invitations
# $value=name
# $data=email
function create_user(){
name=$value
email=$data
template='{"invitation":{"user": {"email": "val-email","name": "val-name"}}}'
## Replace
body=${template/val-email/$email}
body=${template/val-name/$name}
echo $body
##request POST invitations "$body"
}
# List repositories
# $value = REPOSITORY_ID
function repositories(){
if [ $value ]; then
request GET repositories/${value} | jq '.repository' | jq '.title, .repository_url'
exit
fi
request GET repositories | jq '.[].repository' | jq '.id, .title, .repository_url'
}
# Alias for repositories
function repos(){
repositories
}
# $value = REPOSITORY_ID
function envs(){
request GET ${value}/server_environments | jq '.[].server_environment' | jq '.id, .name'
}
# $value = REPOSITORY_ID
# $data = environament
function servers(){
#request GET ${value}/release_servers/$data
#request GET ${value}/release_servers environment_id=${data}
echo ${value}/release_servers environment_id=${data}
}
# Find All Public Keys
# $value = USER_ID
function keys(){
if [ $value ]; then
request GET public_keys user_id=${value} | jq '.[].public_key' | jq '.id, .name, .content'
exit
fi
request GET public_keys | jq '.[].public_key' | jq '.id, .name, .content'
}
# Find Public Key
## Admins can view other users’ keys. Regular users can view their own keys only.
## $value=PUBLIC_KEY_ID
function key(){
request GET public_keys/${value} | jq '.public_key' | jq '.content'
}
# Download some kye
## $value=PUBLIC_KEY_ID
function download_key(){
key | sed s/\"//g >> ${value}_id_rsa.pub
}
# Add a key to authorized keys list
## $value=path/to/key
function append_key(){
file=$1
content=$(cat $file)
content=$(echo -e "\n $content")
echo -e "\n"${content} >> ~/.ssh/authorized_keys
}
# Download and authorize local key
## $value=KEY_ID
function allow_key(){
file=${value}_id_rsa.pub
download_key $value
append_key $file
rm -rf file
}
# Push the key located at ~/.ssh/id_rsa.pub to Beanstalk
function upload_key(){
if [ "$value" ]; then
file=$value
else
file="$HOME/.ssh/id_rsa.pub"
fi
key=$(cat $file)
name=$(hostname)
data='{"public_key":{"name": "val-name","content": "val-key"}}'
## Replacements
data=${data/val-name/$name}
data=${data/val-key/$key}
request POST public_keys "$data"
}
# Add generic beanstalk public key as authorized
function allow_deploy(){
curl -O https://${account}.beanstalkapp.com/beanstalk_rsa.pub
append_key beanstalk_rsa.pub
rm -rf beanstalk_rsa.pub
}
# Dispatcher method
function main(){
echo 'Welcome to Beanstalk-Cli, please give your Beanstalk credentials'
echo 'Make sure you have allowed the API access in your Beanstalk Account'
auth
domain=https://${username}:${password}@${account}.beanstalkapp.com
$command
}
main