| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- <?php
- /**
- * @category Zend
- * @package Zend_Cloud
- * @subpackage Infrastructure
- * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- */
- /**
- * Instance of an infrastructure service
- *
- * @package Zend_Cloud
- * @subpackage Infrastructure
- * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- */
- class Zend_Cloud_Infrastructure_Instance
- {
- const STATUS_RUNNING = 'running';
- const STATUS_STOPPED = 'stopped';
- const STATUS_SHUTTING_DOWN = 'shutting-down';
- const STATUS_REBOOTING = 'rebooting';
- const STATUS_TERMINATED = 'terminated';
- const STATUS_PENDING = 'pending';
- const STATUS_REBUILD = 'rebuild';
- const INSTANCE_ID = 'id';
- const INSTANCE_IMAGEID = 'imageId';
- const INSTANCE_NAME = 'name';
- const INSTANCE_STATUS = 'status';
- const INSTANCE_PUBLICDNS = 'publicDns';
- const INSTANCE_CPU = 'cpu';
- const INSTANCE_RAM = 'ram';
- const INSTANCE_STORAGE = 'storageSize';
- const INSTANCE_ZONE = 'zone';
- const INSTANCE_LAUNCHTIME = 'launchTime';
- const MONITOR_CPU = 'CpuUsage';
- const MONITOR_RAM = 'RamUsage';
- const MONITOR_NETWORK_IN = 'NetworkIn';
- const MONITOR_NETWORK_OUT = 'NetworkOut';
- const MONITOR_DISK = 'DiskUsage';
- const MONITOR_DISK_WRITE = 'DiskWrite';
- const MONITOR_DISK_READ = 'DiskRead';
- const MONITOR_START_TIME = 'StartTime';
- const MONITOR_END_TIME = 'EndTime';
- const SSH_USERNAME = 'username';
- const SSH_PASSWORD = 'password';
- const SSH_PRIVATE_KEY = 'privateKey';
- const SSH_PUBLIC_KEY = 'publicKey';
- const SSH_PASSPHRASE = 'passphrase';
- /**
- * @var Zend_Cloud_Infrastructure_Adapter
- */
- protected $adapter;
- /**
- * Instance's attribute
- *
- * @var array
- */
- protected $attributes;
- /**
- * Attributes required for an instance
- *
- * @var array
- */
- protected $attributeRequired = array(
- self::INSTANCE_ID,
- self::INSTANCE_STATUS,
- self::INSTANCE_IMAGEID,
- self::INSTANCE_ZONE
- );
- /**
- * Constructor
- *
- * @param Adapter $adapter
- * @param array $data
- * @return void
- */
- public function __construct($adapter, $data = null)
- {
- if (!($adapter instanceof Zend_Cloud_Infrastructure_Adapter)) {
- require_once 'Zend/Cloud/Infrastructure/Exception.php';
- throw new Zend_Cloud_Infrastructure_Exception("You must pass a Zend_Cloud_Infrastructure_Adapter instance");
- }
- if (is_object($data)) {
- if (method_exists($data, 'toArray')) {
- $data= $data->toArray();
- } elseif ($data instanceof Traversable) {
- $data = iterator_to_array($data);
- }
- }
-
- if (empty($data) || !is_array($data)) {
- require_once 'Zend/Cloud/Infrastructure/Exception.php';
- throw new Zend_Cloud_Infrastructure_Exception("You must pass an array of parameters");
- }
- foreach ($this->attributeRequired as $key) {
- if (empty($data[$key])) {
- require_once 'Zend/Cloud/Infrastructure/Exception.php';
- throw new Zend_Cloud_Infrastructure_Exception(sprintf(
- 'The param "%s" is a required param for %s',
- $key,
- __CLASS__
- ));
- }
- }
- $this->adapter = $adapter;
- $this->attributes = $data;
- }
- /**
- * Get Attribute with a specific key
- *
- * @param array $data
- * @return misc|false
- */
- public function getAttribute($key)
- {
- if (!empty($this->attributes[$key])) {
- return $this->attributes[$key];
- }
- return false;
- }
- /**
- * Get all the attributes
- *
- * @return array
- */
- public function getAttributes()
- {
- return $this->attributes;
- }
- /**
- * Get the instance's id
- *
- * @return string
- */
- public function getId()
- {
- return $this->attributes[self::INSTANCE_ID];
- }
- /**
- * Get the instance's image id
- *
- * @return string
- */
- public function getImageId()
- {
- return $this->attributes[self::INSTANCE_IMAGEID];
- }
- /**
- * Get the instance's name
- *
- * @return string
- */
- public function getName()
- {
- return $this->attributes[self::INSTANCE_NAME];
- }
- /**
- * Get the status of the instance
- *
- * @return string|boolean
- */
- public function getStatus()
- {
- return $this->adapter->statusInstance($this->attributes[self::INSTANCE_ID]);
- }
- /**
- * Wait for status $status with a timeout of $timeout seconds
- *
- * @param string $status
- * @param integer $timeout
- * @return boolean
- */
- public function waitStatus($status, $timeout = Adapter::TIMEOUT_STATUS_CHANGE)
- {
- return $this->adapter->waitStatusInstance($this->attributes[self::INSTANCE_ID], $status, $timeout);
- }
- /**
- * Get the public DNS of the instance
- *
- * @return string
- */
- public function getPublicDns()
- {
- if (!isset($this->attributes[self::INSTANCE_PUBLICDNS])) {
- $this->attributes[self::INSTANCE_PUBLICDNS] = $this->adapter->publicDnsInstance($this->attributes[self::INSTANCE_ID]);
- }
- return $this->attributes[self::INSTANCE_PUBLICDNS];
- }
- /**
- * Get the instance's CPU
- *
- * @return string
- */
- public function getCpu()
- {
- return $this->attributes[self::INSTANCE_CPU];
- }
- /**
- * Get the instance's RAM size
- *
- * @return string
- */
- public function getRamSize()
- {
- return $this->attributes[self::INSTANCE_RAM];
- }
- /**
- * Get the instance's storage size
- *
- * @return string
- */
- public function getStorageSize()
- {
- return $this->attributes[self::INSTANCE_STORAGE];
- }
- /**
- * Get the instance's zone
- *
- * @return string
- */
- public function getZone()
- {
- return $this->attributes[self::INSTANCE_ZONE];
- }
- /**
- * Get the instance's launch time
- *
- * @return string
- */
- public function getLaunchTime()
- {
- return $this->attributes[self::INSTANCE_LAUNCHTIME];
- }
- /**
- * Reboot the instance
- *
- * @return boolean
- */
- public function reboot()
- {
- return $this->adapter->rebootInstance($this->attributes[self::INSTANCE_ID]);
- }
- /**
- * Stop the instance
- *
- * @return boolean
- */
- public function stop()
- {
- return $this->adapter->stopInstance($this->attributes[self::INSTANCE_ID]);
- }
- /**
- * Start the instance
- *
- * @return boolean
- */
- public function start()
- {
- return $this->adapter->startInstance($this->attributes[self::INSTANCE_ID]);
- }
- /**
- * Destroy the instance
- *
- * @return boolean
- */
- public function destroy()
- {
- return $this->adapter->destroyInstance($this->attributes[self::INSTANCE_ID]);
- }
- /**
- * Return the system informations about the $metric of an instance
- *
- * @param string $metric
- * @param null|array $options
- * @return array|boolean
- */
- public function monitor($metric, $options = null)
- {
- return $this->adapter->monitorInstance($this->attributes[self::INSTANCE_ID], $metric, $options);
- }
- /**
- * Run arbitrary shell script on the instance
- *
- * @param array $param
- * @param string|array $cmd
- * @return string|array
- */
- public function deploy($params, $cmd)
- {
- return $this->adapter->deployInstance($this->attributes[self::INSTANCE_ID], $params, $cmd);
- }
- }
|