-
Notifications
You must be signed in to change notification settings - Fork 0
/
rebind.php
138 lines (130 loc) · 4.03 KB
/
rebind.php
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
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);
require_once('conf.php');
require_once('eipclient.php');
$params['region'] = isset($_GET['region']) ? strtolower($_GET['region'])
: 'bj';
if (!in_array($params['region'], ['bj', 'gz', 'su', 'bd'])) {
$params['region'] = 'bj';
}
if (!isset($_GET['instance'])) {
echo 'Instance param is required!';
return;
}
$params['instance'] = $_GET['instance'];
$params['bandwidth'] = isset($_GET['bandwidth']) ? $_GET['bandwidth'] : 2;
$params['billing'] = isset($_GET['billing']) ? $_GET['billing']
: 'ByTraffic';
$params['retry'] = isset($_GET['retry']) ? $_GET['retry'] : 5;
$time = time();
//Get EIP list
$tmp = getEipList($ACCESS_KEY, $SECRET_KEY, $params['region']);
if ($tmp->status != 200) {
echo "Error while getting Eip list with Error code {$tmp->status}: {$tmp->response
}\n<br/>";
showUsedTime($time);
return;
}
$eipList = json_decode($tmp->response)->eipList;
$output = json_encode($eipList);
echo "Eip List: \n<br/>{$output}\n<br/>";
// Unbind eip
foreach ($eipList as $eip) {
if ($eip->instanceId === $params['instance']
&& $eip->paymentTiming
!= 'Prepaid'
|| substr($eip->name, 0, 2) === 'e-'
&& $eip->status === 'creating') {
//unbind
$tmp = unbindInstance($ACCESS_KEY, $SECRET_KEY, $params['region'],
$eip->eip);
if ($tmp->status != 200) {
echo "Error while unbind Eip {$eip->eip} with Error code {$tmp->status}: {$tmp->response}\n<br/>";
showUsedTime($time);
// return;
}else{
echo "Successful unbind Eip {$eip->eip} \n<br/>";
showUsedTime($time);
}
}
}
// Delete unused eip
foreach ($eipList as $eipInfo) {
if ($eipInfo->instanceId === NULL
&& $eipInfo->paymentTiming
!= 'Prepaid') {
//delete unused EIP
$tmp = deleteEip($ACCESS_KEY, $SECRET_KEY, $params['region'],
$eipInfo->eip);
if ($tmp->status != 200) {
echo "Error while delete Eip {$eipInfo->eip} with Error code {$tmp->status}: {$tmp->response}\n<br/>";
showUsedTime($time);
// return;
}
else {
echo "Successful delete Eip {$eipInfo->eip} \n<br/>";
showUsedTime($time);
}
}
}
// Purchase new EIP
$tmp = purchaseNewEip($ACCESS_KEY, $SECRET_KEY, $params['region'],
generateEipName(), $params['bandwidth'], $params['billing']);
if ($tmp->status != 200) {
echo "Error while purchase new Eip with Error code {$tmp->status}: {$tmp->response}\n<br/>";
showUsedTime($time);
return;
}
$eip = json_decode($tmp->response)->eip;
echo "Successful purchase new Eip: {$eip}\n<br/>";
showUsedTime($time);
// Bind to instance
for ($i = 0; $i < $params['retry']; $i++) {
$tmp = bindInstance($ACCESS_KEY, $SECRET_KEY, $params['region'], $eip,
$params['instance']);
if ($tmp->status != 200) {
echo "[{$i}] Error while binding Eip with Error code {$tmp->status}: {$tmp->response}\n<br/>";
showUsedTime($time);
sleep(2);
if ($i > $params['retry']) {
return;
}
continue;
}
echo "Successful bind Eip to instance!\n<br/>";
showUsedTime($time);
break;
}
//
// //Get New EIP list
// $tmp = getEipList($ACCESS_KEY, $SECRET_KEY, $params['region']);
// if ($tmp->status != 200) {
// echo "Error while getting new Eip list with Error code {$tmp->status}: {$tmp->response}\n<br/>";
// showUsedTime($time);
// return;
// }
// $eipList = json_decode($tmp->response)->eipList;
//
//
// // Delete unused eip
// foreach ($eipList as $eipInfo) {
// if ($eipInfo->instanceId === NULL && $eipInfo->eip != $eip && $eip->paymentTiming
// != 'Prepaid') {
// //delete unused EIP
// $tmp = deleteEip($ACCESS_KEY, $SECRET_KEY, $params['region'],
// $eipInfo->eip);
// if ($tmp->status != 200) {
// echo "Error while delete Eip {$eipInfo->eip} with Error code {$tmp->status}: {$tmp->response}\n<br/>";
// showUsedTime($time);
// // return;
// }else{
// echo "Successful delete Eip {$eipInfo->eip} \n<br/>";
// showUsedTime($time);
// }
// }
// }
function showUsedTime($time) {
$usedTime = time() - $time;
echo "Time: {$usedTime} s\n<br/>";
}