-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhive_api
executable file
·146 lines (108 loc) · 3.67 KB
/
hive_api
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
#!/bin/bash
if [ -z $1 ]; then
echo "usage: ./hive_api function_name parameters"
echo "function: all_workers_stat | worker_stat | fs_list | oc_list | fs_apply FS_ID | oc_apply OC_ID"
exit
fi
baseUrl='https://api2.hiveos.farm/api/v2'
login="you_login"
password="you_passwd"
TOKEN="path to token file"
FARM_ID=you_FARM_ID #Not worker ID!!!
RIG_ID=you_RIG_ID #Worker ID
accessToken=""
function login {
response=`curl -s -w "\n%{http_code}" \
-H "Content-Type: application/json" \
-X POST \
-d "{\"login\":\"$login\",\"password\":\"$password\",\"remember\": true}" \
"$baseUrl/auth/login"`
[ $? -ne 0 ] && (>&2 echo 'Curl error') && exit 1
statusCode=`echo "$response" | tail -1`
response=`echo "$response" | sed '$d'`
[[ $statusCode -lt 200 || $statusCode -ge 300 ]] && { echo "$response" | jq 1>&2; } && exit 1
# Extract access token
accessToken=`echo "$response" | jq --raw-output '.access_token'`
echo "accessToken=\"$accessToken\"" > $TOKEN
}
function check_token {
response=`curl -s -w "\n%{http_code}" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $accessToken" \
"$baseUrl/auth/check"`
[ $? -ne 0 ] && (>&2 echo 'Curl error') && exit 1
statusCode=`echo "$response" | tail -1`
[[ $statusCode -lt 200 || $statusCode -ge 300 ]] && { echo "$response" | jq 1>&2; } && exit 1
if [ $statusCode -eq 204 ]; then
echo true
else
echo false
fi
}
function all_workers_stat {
response=`curl -s -w "\n%{http_code}" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $accessToken" \
"$baseUrl/farms/$FARM_ID/workers"`
[ $? -ne 0 ] && (>&2 echo 'Curl error') && exit 1
statusCode=`echo "$response" | tail -1`
response=`echo "$response" | sed '$d'`
[[ $statusCode -lt 200 || $statusCode -ge 300 ]] && { echo "$response" | jq 1>&2; } && exit 1
echo "$response"
}
function worker_stat {
response=`curl -s -w "\n%{http_code}" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $accessToken" \
"$baseUrl/farms/$FARM_ID/workers/$RIG_ID"`
[ $? -ne 0 ] && (>&2 echo 'Curl error') && exit 1
statusCode=`echo "$response" | tail -1`
response=`echo "$response" | sed '$d'`
[[ $statusCode -lt 200 || $statusCode -ge 300 ]] && { echo "$response" | jq 1>&2; } && exit 1
echo "$response"
}
function fs_list {
response=`curl -s -w "\n%{http_code}" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $accessToken" \
"$baseUrl/farms/$FARM_ID/fs"`
echo "$response"
}
function oc_list {
response=`curl -s -w "\n%{http_code}" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $accessToken" \
"$baseUrl/farms/$FARM_ID/oc"`
echo "$response"
}
function fs_apply {
response=`curl -s -w "\n%{http_code}" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $accessToken" \
-X PATCH \
-d "{\"fs_id\": $1}" \
"$baseUrl/farms/$FARM_ID/workers/$RIG_ID"`
[ $? -ne 0 ] && (>&2 echo 'Curl error') && exit 1
statusCode=`echo "$response" | tail -1`
response=`echo "$response" | sed '$d'`
[[ $statusCode -lt 200 || $statusCode -ge 300 ]] && { echo "$response" | jq 1>&2; } && exit 1
echo "$response"
}
function oc_apply {
response=`curl -s -w "\n%{http_code}" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $accessToken" \
-X PATCH \
-d "{\"oc_id\": $1}" \
"$baseUrl/farms/$FARM_ID/workers/$RIG_ID"`
[ $? -ne 0 ] && (>&2 echo 'Curl error') && exit 1
statusCode=`echo "$response" | tail -1`
response=`echo "$response" | sed '$d'`
[[ $statusCode -lt 200 || $statusCode -ge 300 ]] && { echo "$response" | jq 1>&2; } && exit 1
echo "$response"
}
[ -f $TOKEN ] && source $TOKEN
[ -z $accessToken ] && login
[ ! $(check_token) ] && login
echo $($1 $2) | jq .
exit 0