-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdo-upgrade-plans.php
More file actions
341 lines (260 loc) · 12.6 KB
/
Copy pathdo-upgrade-plans.php
File metadata and controls
341 lines (260 loc) · 12.6 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<?php
define( 'DO_ACCESS_TOKEN', getenv( 'DO_ACCESS_TOKEN' ) );
define( 'DO_API_URL_BASE', 'https://api.digitalocean.com/v2/' );
if ( ! strlen( DO_ACCESS_TOKEN ) ) {
fwrite( STDERR, "Please set your DigitalOcean API access token first.\n" );
exit( 1 );
}
require( __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php' );
use Httpful\Request;
$done_droplets = $droplet_errors = $regions = $sizes = $droplets = $shutdown_droplets = $migrate_droplets = $boot_droplets = [];
// Fetch all sizes.
$url = DO_API_URL_BASE . 'sizes';
do {
echo sprintf( "Fetching URL: %s\n", $url );
$sizes_response = Request::get( $url )->expectsJson()->addHeader( 'Authorization', 'Bearer ' . DO_ACCESS_TOKEN )->send();
$sizes = array_merge( $sizes, $sizes_response->body->sizes );
$url = isset( $sizes_response->body->links->pages->next ) ? $sizes_response->body->links->pages->next : false;
} while ( $url );
echo sprintf( "We have found %d plans.\n", count( $sizes ) );
// Fetch all regions, because the regions registered per plan is limited.
$url = DO_API_URL_BASE . 'regions';
do {
echo sprintf( "Fetching URL: %s\n", $url );
$regions_response = Request::get( $url )->expectsJson()->addHeader( 'Authorization', 'Bearer ' . DO_ACCESS_TOKEN )->send();
$regions = array_merge( $regions, $regions_response->body->regions );
$url = isset( $regions_response->body->links->pages->next ) ? $regions_response->body->links->pages->next : false;
} while ( $url );
echo sprintf( "We have found %d regions.\n", count( $regions ) );
// Loop through all regions, find the plans available and register the region into the plan if not there already.
foreach ( $regions as $region ) {
foreach ( $region->sizes as $region_size ) {
foreach ( $sizes as $key => $size ) {
if ( ! in_array( $region->slug, $size->regions ) ) {
echo sprintf( "Adding the region %s to the size %s.\n", $region->slug, $size->slug );
$sizes[ $key ]->regions[] = $region->slug;
}
}
}
}
// Fetch all droplets.
$url = DO_API_URL_BASE . 'droplets';
do {
echo sprintf( "Fetching URL: %s\n", $url );
$droplets_response = Request::get( $url )->expectsJson()->addHeader( 'Authorization', 'Bearer ' . DO_ACCESS_TOKEN )->send();
$droplets = array_merge( $droplets, $droplets_response->body->droplets );
$url = isset( $droplets_response->body->links->pages->next ) ? $droplets_response->body->links->pages->next : false;
} while ( $url );
echo sprintf( "We have found %d droplets.\n", count( $droplets ) );
foreach ( $droplets as $droplet ) {
// Can this droplet be upgraded to a plan with better specs in the same region?
echo sprintf( "Checking if we can upgrade %s (%s - %s) in %s ...", $droplet->name, $droplet->id, $droplet->size->slug, $droplet->region->slug );
$upgrade_memory = $droplet->size->memory;
$upgrade_vcpus = $droplet->size->vcpus;
$same_price_plans = [];
$new_plan = false;
foreach ( $sizes as $plan ) {
if ( ! $plan->available || ! in_array( $droplet->region->slug, $plan->regions ) ) {
continue;
}
if ( $plan->price_hourly === $droplet->size->price_hourly ) {
if ( $plan->memory > $upgrade_memory && $plan->vcpus > $upgrade_vcpus ) {
// Both RAM and VCPUs are better – than what we’ve found so far.
$new_plan = $plan->slug;
echo sprintf( '%s RAM is more than %s RAM and %s vCPUs is more than %s vCPUs. ', $plan->memory, $upgrade_memory, $plan->vcpus, $upgrade_vcpus );
} elseif ( $plan->memory > $upgrade_memory && $plan->vcpus >= $upgrade_vcpus) {
// RAM is better and VCPUs is at least the same – than what we’ve found so far.
$new_plan = $plan->slug;
echo sprintf( '%s RAM is more than %s RAM and %s vCPUs is more or the same as %s vCPUs. ', $plan->memory, $upgrade_memory, $plan->vcpus, $upgrade_vcpus );
} elseif ( $plan->memory >= $upgrade_memory && $plan->vcpus > $upgrade_vcpus) {
// RAM is at least the same as what we’ve found so far, but VCPUs are better.
$new_plan = $plan->slug;
echo sprintf( '%s RAM is more or the same as %s RAM and %s vCPUs is more than %s vCPUs. ', $plan->memory, $upgrade_memory, $plan->vcpus, $upgrade_vcpus );
}
}
}
if ( $new_plan ) {
echo sprintf( "Yeah, let’s upgrade it to %s.", $new_plan );
$d = new stdClass();
$d->id = $droplet->id;
$d->name = $droplet->name;
$d->new_plan = $new_plan;
// Add the droplet to the shutdown stack.
$shutdown_droplets[] = $d;
} else {
echo "No, it’s cool.";
}
echo PHP_EOL;
}
echo sprintf( "We will upgrade %d droplets:\n", count( $shutdown_droplets ) );
foreach ( $shutdown_droplets as $key => $droplet ) {
echo sprintf( "Shutting down %s (%d).\n", $droplet->name, $droplet->id );
$shutdown_request_body = new stdClass();
$shutdown_request_body->id = $droplet->id;
$shutdown_request_body->type = 'shutdown';
$shutdown_response = Request::post( DO_API_URL_BASE . 'droplets/' . $droplet->id . '/actions' )->sendsJson()->addHeader( 'Authorization', 'Bearer ' . DO_ACCESS_TOKEN )->body( json_encode( $shutdown_request_body ) )->send();
$shutdown_droplets[ $key ]->action_id = $shutdown_response->body->action->id;
}
$done = 0 === count( $shutdown_droplets );
while ( ! $done ) {
echo ( "We’re not done. Let’s do some checks’n’stuff!\n" );
// Take it cool. These steps will take some time.
echo ( "Let’s wait 10 seconds. We’re cool!\n" );
sleep( 10 );
echo sprintf( "We got %s droplets in the shutdown stack.\n", count( $shutdown_droplets ) );
foreach ( $shutdown_droplets as $key => $droplet ) {
echo sprintf( "Checking action id: %s for %s (%s)\n", $droplet->action_id, $droplet->name, $droplet->id );
$action_response = Request::get( DO_API_URL_BASE . 'actions/' . $droplet->action_id )->sendsJson()->addHeader( 'Authorization', 'Bearer ' . DO_ACCESS_TOKEN )->send();
$droplet_is_shutdown = $action_error = false;
switch ( $action_response->body->action->status ) {
case 'completed':
$droplet_is_shutdown = true;
break;
case 'errored':
$action_error = true;
break;
case 'in-progress':
break;
default:
echo "Unknown status: \n";
var_dump( $action_response->body->action->status );
break;
}
if ( $droplet_is_shutdown ) {
// Start the upgrade.
echo sprintf( "Issuing upgrade of %s (%s) to %s.\n", $droplet->name, $droplet->id, $droplet->new_plan );
$resize_request_body = new stdClass();
$resize_request_body->type = 'resize';
$resize_request_body->disk = true;
$resize_request_body->size = $droplet->new_plan;
$resize_response = Request::post( DO_API_URL_BASE . 'droplets/' . $droplet->id . '/actions' )->sendsJson()->addHeader( 'Authorization', 'Bearer ' . DO_ACCESS_TOKEN )->body( json_encode( $resize_request_body ) )->send();
if ( isset( $resize_response->body->action->id ) ) {
$droplet->action_id = $resize_response->body->action->id;
} else {
$action_error = true;
if ( isset( $resize_response->body->message ) ) {
$droplet->action_error_msg = $resize_response->body->message;
}
}
if ( ! $action_error ) {
// Add the droplet to the migration stack.
$migrate_droplets[] = $droplet;
// Remove the droplet from the shutdown stack.
unset( $shutdown_droplets[ $key ] );
}
}
if ( $action_error ) {
// CRAP! Try to power cycle the droplet and remove it from the stack.
echo sprintf( "We had an issue with %s (%s) and will power cycle it.\n", $droplet->name, $droplet->id );
$power_cycle_request_body = new stdClass();
$power_cycle_request_body->id = $droplet->id;
$power_cycle_request_body->type = 'power_cycle';
Request::post( DO_API_URL_BASE . 'droplets/' . $droplet->id . '/actions' )->sendsJson()->addHeader( 'Authorization', 'Bearer ' . DO_ACCESS_TOKEN )->body( json_encode( $power_cycle_request_body ) )->send();
// Add the droplet to the error stack.
$droplet_errors[] = $droplet;
// Remove the droplet from the shutdown stack.
unset( $shutdown_droplets[ $key ] );
}
}
echo sprintf( "We got %s droplets in the migration stack.\n", count( $migrate_droplets ) );
foreach ( $migrate_droplets as $key => $droplet ) {
echo sprintf( "Checking action id: %s for %s (%s)\n", $droplet->action_id, $droplet->name, $droplet->id );
$action_response = Request::get( DO_API_URL_BASE . 'actions/' . $droplet->action_id )->sendsJson()->addHeader( 'Authorization', 'Bearer ' . DO_ACCESS_TOKEN )->send();
$droplet_is_upgraded = $action_error = false;
switch ( $action_response->body->action->status ) {
case 'completed':
$droplet_is_upgraded = true;
break;
case 'errored':
$action_error = true;
break;
case 'in-progress':
break;
default:
echo "Unknown status: \n";
var_dump( $action_response->body->action->status );
break;
}
if ( $droplet_is_upgraded ) {
// Boot the droplet.
$poweron_request_body = new stdClass();
$poweron_request_body->type = 'power_on';
$poweron_response = Request::post( DO_API_URL_BASE . 'droplets/' . $droplet->id . '/actions' )->sendsJson()->addHeader( 'Authorization', 'Bearer ' . DO_ACCESS_TOKEN )->body( json_encode( $poweron_request_body ) )->send();
if ( isset( $poweron_response->body->action->id ) ) {
$droplet->action_id = $poweron_response->body->action->id;
} else {
$action_error = true;
if ( isset( $poweron_response->body->message ) ) {
$droplet->action_error_msg = $poweron_response->body->message;
}
}
if ( ! $action_error ) {
// Add the droplet to the boot stack.
$boot_droplets[] = $droplet;
// Remove the droplet from the migration stack.
unset( $migrate_droplets[ $key ] );
}
}
if ( $action_error ) {
// CRAP! Try to power cycle the droplet and remove it from the stack.
echo sprintf( "We had an issue with %s (%s) and will power cycle it.\n", $droplet->name, $droplet->id );
$power_cycle_request_body = new stdClass();
$power_cycle_request_body->id = $droplet->id;
$power_cycle_request_body->type = 'power_cycle';
Request::post( DO_API_URL_BASE . 'droplets/' . $droplet->id . '/actions' )->sendsJson()->addHeader( 'Authorization', 'Bearer ' . DO_ACCESS_TOKEN )->body( json_encode( $power_cycle_request_body ) )->send();
// Add the droplet to the error stack.
$droplet_errors[] = $droplet;
// Remove the droplet from the migration stack.
unset( $migrate_droplets[ $key ] );
}
}
echo sprintf( "We got %s droplets in the boot stack.\n", count( $boot_droplets ) );
foreach ( $boot_droplets as $key => $droplet ) {
echo sprintf( "Checking action id: %s for %s (%s)\n", $droplet->action_id, $droplet->name, $droplet->id );
$action_response = Request::get( DO_API_URL_BASE . 'actions/' . $droplet->action_id )->sendsJson()->addHeader( 'Authorization', 'Bearer ' . DO_ACCESS_TOKEN )->send();
$droplet_is_booted = $action_error = false;
switch ( $action_response->body->action->status ) {
case 'completed':
$droplet_is_booted = true;
break;
case 'errored':
$action_error = true;
break;
case 'in-progress':
break;
default:
echo "Unknown status: \n";
var_dump( $action_response->body->action->status );
break;
}
if ( $droplet_is_booted ) {
// Add the droplet to the done stack.
$done_droplets[] = $droplet;
// Remove the droplet from the boot stack.
unset( $boot_droplets[ $key ] );
}
if ( $action_error ) {
// CRAP! Try to power cycle the droplet and remove it from the stack.
echo sprintf( "We had an issue with %s (%s) and will power cycle it.\n", $droplet->name, $droplet->id );
$power_cycle_request_body = new stdClass();
$power_cycle_request_body->id = $droplet->id;
$power_cycle_request_body->type = 'power_cycle';
Request::post( DO_API_URL_BASE . 'droplets/' . $droplet->id . '/actions' )->sendsJson()->addHeader( 'Authorization', 'Bearer ' . DO_ACCESS_TOKEN )->body( json_encode( $power_cycle_request_body ) )->send();
// Add the droplet to the error stack.
$droplet_errors[] = $droplet;
// Remove the droplet from the boot stack.
unset( $boot_droplets[ $key ] );
}
}
echo sprintf( "We got %s droplets in the done stack.\n", count( $done_droplets ) );
if ( 0 === count( $shutdown_droplets ) && 0 === count( $migrate_droplets ) && 0 === count( $boot_droplets ) ) {
$done = true;
}
}
if ( count( $droplet_errors ) ) {
echo sprintf( "We had an issue with the following droplets, and have tried to power cycle them. Please check their status manually. You may retry running this script when they are back up.\n" );
foreach ( $droplet_errors as $droplet ) {
echo sprintf( "%s (%s) - %s: https://cloud.digitalocean.com/droplets/%s\n", $droplet->name, $droplet->id, ( isset( $droplet->action_error_msg ) ? $droplet->action_error_msg : 'Unkonwn error' ) , $droplet->id );
}
}
echo "Done!\n";