Server.php
19.2 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
<?php
/**
* PHP OpenCloud library.
*
* @copyright 2014 Rackspace Hosting, Inc. See LICENSE for information.
* @license https://www.apache.org/licenses/LICENSE-2.0
* @author Glen Campbell <glen.campbell@rackspace.com>
* @author Jamie Hannaford <jamie.hannaford@rackspace.com>
*/
namespace OpenCloud\Compute\Resource;
use OpenCloud\Common\PersistentObject;
use OpenCloud\Volume\Resource\Volume;
use OpenCloud\Common\Exceptions;
use OpenCloud\Common\Lang;
use OpenCloud\Compute\Service;
use OpenCloud\Compute\Constants\ServerState;
use OpenCloud\Common\Http\Message\Formatter;
/**
* A virtual machine (VM) instance in the Cloud Servers environment.
*
* @note This implementation supports extension attributes OS-DCF:diskConfig,
* RAX-SERVER:bandwidth, rax-bandwidth:bandwith.
*/
class Server extends PersistentObject
{
/**
* The server status. {@see \OpenCloud\Compute\Constants\ServerState} for supported types.
*
* @var string
*/
public $status;
/**
* @var string The time stamp for the last update.
*/
public $updated;
/**
* The compute provisioning algorithm has an anti-affinity property that
* attempts to spread customer VMs across hosts. Under certain situations,
* VMs from the same customer might be placed on the same host. $hostId
* represents the host your server runs on and can be used to determine this
* scenario if it is relevant to your application.
*
* @var string
*/
public $hostId;
/**
* @var type Public and private IP addresses for this server.
*/
public $addresses;
/**
* @var array Server links.
*/
public $links;
/**
* The Image for this server.
*
* @link http://docs.rackspace.com/servers/api/v2/cs-devguide/content/List_Images-d1e4435.html
* @var type
*/
public $image;
/**
* The Flavor for this server.
*
* @link http://docs.rackspace.com/servers/api/v2/cs-devguide/content/List_Flavors-d1e4188.html
* @var type
*/
public $flavor;
/**
* @var type
*/
public $networks = array();
/**
* @var string The server ID.
*/
public $id;
/**
* @var string The user ID.
*/
public $user_id;
/**
* @var string The server name.
*/
public $name;
/**
* @var string The time stamp for the creation date.
*/
public $created;
/**
* @var string The tenant ID.
*/
public $tenant_id;
/**
* @var string The public IP version 4 access address.
*/
public $accessIPv4;
/**
* @var string The public IP version 6 access address.
*/
public $accessIPv6;
/**
* The build completion progress, as a percentage. Value is from 0 to 100.
* @var int
*/
public $progress;
/**
* @var string The root password (only populated on server creation).
*/
public $adminPass;
/**
* @var mixed Metadata key and value pairs.
*/
public $metadata;
protected static $json_name = 'server';
protected static $url_resource = 'servers';
/** @var string|object Keypair or string representation of keypair name */
public $keypair;
/**
* @var array Uploaded file attachments
*/
private $personality = array();
/**
* @var type Image reference (for create)
*/
private $imageRef;
/**
* @var type Flavor reference (for create)
*/
private $flavorRef;
/**
* Cloud-init boot executable code
* @var string
*/
public $user_data;
/**
* Creates a new Server object and associates it with a Compute service
*
* @param mixed $info
* * If NULL, an empty Server object is created
* * If an object, then a Server object is created from the data in the
* object
* * If a string, then it's treated as a Server ID and retrieved from the
* service
* The normal use case for SDK clients is to treat it as either NULL or an
* ID. The object value parameter is a special case used to construct
* a Server object from a ServerList element to avoid a secondary
* call to the Service.
* @throws ServerNotFound if a 404 is returned
* @throws UnknownError if another error status is reported
*/
public function __construct(Service $service, $info = null)
{
// make the service persistent
parent::__construct($service, $info);
// the metadata item is an object, not an array
$this->metadata = $this->metadata();
}
/**
* Returns the primary external IP address of the server
*
* This function is based upon the accessIPv4 and accessIPv6 values.
* By default, these are set to the public IP address of the server.
* However, these values can be modified by the user; this might happen,
* for example, if the server is behind a firewall and needs to be
* routed through a NAT device to be reached.
*
* @api
* @param integer $type the type of IP version (4 or 6) to return
* @return string IP address
*/
public function ip($type = null)
{
switch ($type) {
default:
case 4:
$value = $this->accessIPv4;
break;
case 6:
$value = $this->accessIPv6;
break;
}
return $value;
}
/**
* {@inheritDoc}
*/
public function create($params = array())
{
$this->id = null;
$this->status = null;
return parent::create($params);
}
/**
* Rebuilds an existing server
*
* @api
* @param array $params - an associative array of key/value pairs of
* attributes to set on the new server
*/
public function rebuild($params = array())
{
if (!isset($params['adminPass'])) {
throw new Exceptions\RebuildError(
Lang::Translate('adminPass required when rebuilding server')
);
}
if (!isset($params['image'])) {
throw new Exceptions\RebuildError(
Lang::Translate('image required when rebuilding server')
);
}
$object = (object) array(
'rebuild' => (object) array(
'imageRef' => $params['image']->id(),
'adminPass' => $params['adminPass'],
'name' => (array_key_exists('name', $params) ? $params['name'] : $this->name)
)
);
return $this->action($object);
}
/**
* Reboots a server
*
* A "soft" reboot requests that the operating system reboot itself; a "hard" reboot is the equivalent of pulling
* the power plug and then turning it back on, with a possibility of data loss.
*
* @api
* @param string $type A particular reboot State. See Constants\ServerState for string values.
* @return \Guzzle\Http\Message\Response
*/
public function reboot($type = null)
{
if (!$type) {
$type = ServerState::REBOOT_STATE_HARD;
}
$object = (object) array('reboot' => (object) array('type' => $type));
return $this->action($object);
}
/**
* Creates a new image from a server
*
* @api
* @param string $name The name of the new image
* @param array $metadata Optional metadata to be stored on the image
* @return boolean|Image New Image instance on success; FALSE on failure
* @throws Exceptions\ImageError
*/
public function createImage($name, $metadata = array())
{
if (empty($name)) {
throw new Exceptions\ImageError(
Lang::translate('Image name is required to create an image')
);
}
// construct a createImage object for jsonization
$object = (object) array('createImage' => (object) array(
'name' => $name,
'metadata' => (object) $metadata
));
$response = $this->action($object);
if (!$response || !($location = $response->getHeader('Location'))) {
return false;
}
return new Image($this->getService(), basename($location));
}
/**
* Schedule daily image backups
*
* @api
* @param mixed $retention - false (default) indicates you want to
* retrieve the image schedule. $retention <= 0 indicates you
* want to delete the current schedule. $retention > 0 indicates
* you want to schedule image backups and you would like to
* retain $retention backups.
* @return mixed an object or FALSE on error
* @throws Exceptions\ServerImageScheduleError if an error is encountered
*/
public function imageSchedule($retention = false)
{
$url = $this->getUrl('rax-si-image-schedule');
if ($retention === false) {
// Get current retention
$request = $this->getClient()->get($url);
} elseif ($retention <= 0) {
// Delete image schedule
$request = $this->getClient()->delete($url);
} else {
// Set image schedule
$object = (object) array('image_schedule' =>
(object) array('retention' => $retention)
);
$body = json_encode($object);
$request = $this->getClient()->post($url, self::getJsonHeader(), $body);
}
$body = Formatter::decode($request->send());
return (isset($body->image_schedule)) ? $body->image_schedule : (object) array();
}
/**
* Initiates the resize of a server
*
* @api
* @param Flavor $flavorRef a Flavor object indicating the new server size
* @return boolean TRUE on success; FALSE on failure
*/
public function resize(Flavor $flavorRef)
{
// construct a resize object for jsonization
$object = (object) array(
'resize' => (object) array('flavorRef' => $flavorRef->id)
);
return $this->action($object);
}
/**
* confirms the resize of a server
*
* @api
* @return boolean TRUE on success; FALSE on failure
*/
public function resizeConfirm()
{
$object = (object) array('confirmResize' => null);
$response = $this->action($object);
$this->refresh($this->id);
return $response;
}
/**
* reverts the resize of a server
*
* @api
* @return boolean TRUE on success; FALSE on failure
*/
public function resizeRevert()
{
$object = (object) array('revertResize' => null);
return $this->action($object);
}
/**
* Sets the root password on the server
*
* @api
* @param string $newPassword The new root password for the server
* @return boolean TRUE on success; FALSE on failure
*/
public function setPassword($newPassword)
{
$object = (object) array(
'changePassword' => (object) array('adminPass' => $newPassword)
);
return $this->action($object);
}
/**
* Puts the server into *rescue* mode
*
* @api
* @link http://docs.rackspace.com/servers/api/v2/cs-devguide/content/rescue_mode.html
* @return string the root password of the rescue server
* @throws Exceptions\ServerActionError if the server has no ID (i.e., has not
* been created yet)
*/
public function rescue()
{
$this->checkExtension('os-rescue');
if (empty($this->id)) {
throw new Exceptions\ServerActionError(
Lang::translate('Server has no ID; cannot Rescue()')
);
}
$data = (object) array('rescue' => 'none');
$response = $this->action($data);
$body = Formatter::decode($response);
return (isset($body->adminPass)) ? $body->adminPass : false;
}
/**
* Takes the server out of RESCUE mode
*
* @api
* @link http://docs.rackspace.com/servers/api/v2/cs-devguide/content/rescue_mode.html
* @return HttpResponse
* @throws Exceptions\ServerActionError if the server has no ID (i.e., has not
* been created yet)
*/
public function unrescue()
{
$this->checkExtension('os-rescue');
if (!isset($this->id)) {
throw new Exceptions\ServerActionError(Lang::translate('Server has no ID; cannot Unescue()'));
}
$object = (object) array('unrescue' => null);
return $this->action($object);
}
/**
* Retrieves the metadata associated with a Server.
*
* If a metadata item name is supplied, then only the single item is
* returned. Otherwise, the default is to return all metadata associated
* with a server.
*
* @api
* @param string $key - the (optional) name of the metadata item to return
* @return ServerMetadata object
* @throws Exceptions\MetadataError
*/
public function metadata($key = null)
{
return new ServerMetadata($this, $key);
}
/**
* Returns the IP address block for the Server or for a specific network.
*
* @api
* @param string $network - if supplied, then only the IP(s) for the
* specified network are returned. Otherwise, all IPs are returned.
* @return object
* @throws Exceptions\ServerIpsError
*/
public function ips($network = null)
{
$url = Lang::noslash($this->Url('ips/'.$network));
$response = $this->getClient()->get($url)->send();
$body = Formatter::decode($response);
return (isset($body->addresses)) ? $body->addresses :
((isset($body->network)) ? $body->network : (object) array());
}
/**
* Attaches a volume to a server
*
* Requires the os-volumes extension. This is a synonym for
* `VolumeAttachment::create()`
*
* @api
* @param OpenCloud\Volume\Resource\Volume $volume The volume to attach. If
* "auto" is specified (the default), then the first available
* device is used to mount the volume (for example, if the primary
* disk is on `/dev/xvhda`, then the new volume would be attached
* to `/dev/xvhdb`).
* @param string $device the device to which to attach it
*/
public function attachVolume(Volume $volume, $device = 'auto')
{
$this->checkExtension('os-volumes');
return $this->volumeAttachment()->create(array(
'volumeId' => $volume->id,
'device' => ($device == 'auto' ? null : $device)
));
}
/**
* Removes a volume attachment from a server
*
* Requires the os-volumes extension. This is a synonym for
* `VolumeAttachment::delete()`
* @param OpenCloud\Volume\Resource\Volume $volume The volume to remove
*/
public function detachVolume(Volume $volume)
{
$this->checkExtension('os-volumes');
return $this->volumeAttachment($volume->id)->delete();
}
/**
* Returns a VolumeAttachment object
*
*/
public function volumeAttachment($id = null)
{
$resource = new VolumeAttachment($this->getService());
$resource->setParent($this)->populate($id);
return $resource;
}
/**
* Returns a Collection of VolumeAttachment objects
* @return Collection
*/
public function volumeAttachmentList()
{
return $this->getService()->collection(
'OpenCloud\Compute\Resource\VolumeAttachment', null, $this
);
}
/**
* Adds a "personality" file to be uploaded during create() or rebuild()
*
* @api
* @param string $path The path where the file will be stored on the
* target server (up to 255 characters)
* @param string $data the file contents (max size set by provider)
* @return void
*/
public function addFile($path, $data)
{
$this->personality[$path] = base64_encode($data);
}
/**
* Returns a console connection
* Note: Where is this documented?
*
* @codeCoverageIgnore
*/
public function console($type = 'novnc')
{
$action = (strpos('spice', $type) !== false) ? 'os-getSPICEConsole' : 'os-getVNCConsole';
$object = (object) array($action => (object) array('type' => $type));
$response = $this->action($object);
$body = Formatter::decode($response);
return (isset($body->console)) ? $body->console : false;
}
protected function createJson()
{
// Convert some values
$this->metadata->sdk = $this->getService()->getClient()->getUserAgent();
if (!empty($this->image) && $this->image instanceof Image) {
$this->imageRef = $this->image->id;
}
if (!empty($this->flavor) && $this->flavor instanceof Flavor) {
$this->flavorRef = $this->flavor->id;
}
// Base object
$server = (object) array(
'name' => $this->name,
'imageRef' => $this->imageRef,
'flavorRef' => $this->flavorRef
);
if ($this->metadata->count()) {
$server->metadata = $this->metadata->toArray();
}
// Networks
if (is_array($this->networks) && count($this->networks)) {
$server->networks = array();
foreach ($this->networks as $network) {
if (!$network instanceof Network) {
throw new Exceptions\InvalidParameterError(sprintf(
'When creating a server, the "networks" key must be an ' .
'array of OpenCloud\Compute\Network objects with valid ' .
'IDs; variable passed in was a [%s]',
gettype($network)
));
}
if (empty($network->id)) {
$this->getLogger()->warning('When creating a server, the '
. 'network objects passed in must have an ID'
);
continue;
}
// Stock networks array
$server->networks[] = (object) array('uuid' => $network->id);
}
}
// Personality files
if (!empty($this->personality)) {
$server->personality = array();
foreach ($this->personality as $path => $data) {
// Stock personality array
$server->personality[] = (object) array(
'path' => $path,
'contents' => $data
);
}
}
// Keypairs
if (!empty($this->keypair)) {
if (is_string($this->keypair)) {
$server->key_name = $this->keypair;
} elseif (isset($this->keypair['name']) && is_string($this->keypair['name'])) {
$server->key_name = $this->keypair['name'];
} elseif ($this->keypair instanceof Keypair && $this->keypair->getName()) {
$server->key_name = $this->keypair->getName();
}
}
// Cloud-init executable
if (!empty($this->user_data)) {
$server->user_data = $this->user_data;
}
return (object) array('server' => $server);
}
protected function updateJson($params = array())
{
return (object) array('server' => (object) $params);
}
}