File indexing completed on 2024-12-22 05:36:31

0001 <?php
0002 /**
0003  * @category   Zend
0004  * @package    Zend_Cloud
0005  * @subpackage Infrastructure
0006  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0007  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0008  */
0009 
0010 /**
0011  * Instance of an infrastructure service
0012  *
0013  * @package    Zend_Cloud
0014  * @subpackage Infrastructure
0015  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0016  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0017  */
0018 class Zend_Cloud_Infrastructure_Instance 
0019 {
0020     const STATUS_RUNNING       = 'running';
0021     const STATUS_STOPPED       = 'stopped';
0022     const STATUS_SHUTTING_DOWN = 'shutting-down';
0023     const STATUS_REBOOTING     = 'rebooting';
0024     const STATUS_TERMINATED    = 'terminated';
0025     const STATUS_PENDING       = 'pending';
0026     const STATUS_REBUILD       = 'rebuild';
0027     const INSTANCE_ID          = 'id';
0028     const INSTANCE_IMAGEID     = 'imageId';
0029     const INSTANCE_NAME        = 'name';
0030     const INSTANCE_STATUS      = 'status';
0031     const INSTANCE_PUBLICDNS   = 'publicDns';
0032     const INSTANCE_CPU         = 'cpu';
0033     const INSTANCE_RAM         = 'ram';
0034     const INSTANCE_STORAGE     = 'storageSize';
0035     const INSTANCE_ZONE        = 'zone';
0036     const INSTANCE_LAUNCHTIME  = 'launchTime';
0037     const MONITOR_CPU          = 'CpuUsage';
0038     const MONITOR_RAM          = 'RamUsage';
0039     const MONITOR_NETWORK_IN   = 'NetworkIn';
0040     const MONITOR_NETWORK_OUT  = 'NetworkOut';
0041     const MONITOR_DISK         = 'DiskUsage';
0042     const MONITOR_DISK_WRITE   = 'DiskWrite';
0043     const MONITOR_DISK_READ    = 'DiskRead';
0044     const MONITOR_START_TIME   = 'StartTime';
0045     const MONITOR_END_TIME     = 'EndTime';
0046     const SSH_USERNAME         = 'username';
0047     const SSH_PASSWORD         = 'password';
0048     const SSH_PRIVATE_KEY      = 'privateKey';
0049     const SSH_PUBLIC_KEY       = 'publicKey';
0050     const SSH_PASSPHRASE       = 'passphrase';
0051 
0052     /**
0053      * @var Zend_Cloud_Infrastructure_Adapter
0054      */
0055     protected $adapter;
0056 
0057     /**
0058      * Instance's attribute
0059      * 
0060      * @var array 
0061      */
0062     protected $attributes;
0063 
0064     /**
0065      * Attributes required for an instance
0066      * 
0067      * @var array 
0068      */
0069     protected $attributeRequired = array(
0070         self::INSTANCE_ID,
0071         self::INSTANCE_STATUS,
0072         self::INSTANCE_IMAGEID,
0073         self::INSTANCE_ZONE
0074     );
0075 
0076     /**
0077      * Constructor
0078      * 
0079      * @param  Adapter $adapter
0080      * @param  array $data 
0081      * @return void
0082      */
0083     public function __construct($adapter, $data = null)
0084     {
0085         if (!($adapter instanceof Zend_Cloud_Infrastructure_Adapter)) {
0086             // require_once 'Zend/Cloud/Infrastructure/Exception.php';
0087             throw new Zend_Cloud_Infrastructure_Exception("You must pass a Zend_Cloud_Infrastructure_Adapter instance");
0088         }
0089 
0090         if (is_object($data)) {
0091             if (method_exists($data, 'toArray')) {
0092                 $data= $data->toArray();
0093             } elseif ($data instanceof Traversable) {
0094                 $data = iterator_to_array($data);
0095             }
0096         }
0097         
0098         if (empty($data) || !is_array($data)) {
0099             // require_once 'Zend/Cloud/Infrastructure/Exception.php';
0100             throw new Zend_Cloud_Infrastructure_Exception("You must pass an array of parameters");
0101         }
0102 
0103         foreach ($this->attributeRequired as $key) {
0104             if (empty($data[$key])) {
0105                 // require_once 'Zend/Cloud/Infrastructure/Exception.php';
0106                 throw new Zend_Cloud_Infrastructure_Exception(sprintf(
0107                     'The param "%s" is a required param for %s', 
0108                     $key,
0109                     __CLASS__
0110                 ));
0111             }
0112         }
0113 
0114         $this->adapter    = $adapter;
0115         $this->attributes = $data;
0116     }
0117 
0118     /**
0119      * Get Attribute with a specific key
0120      *
0121      * @param array $data
0122      * @return misc|false
0123      */
0124     public function getAttribute($key) 
0125     {
0126         if (!empty($this->attributes[$key])) {
0127             return $this->attributes[$key];
0128         }
0129         return false;
0130     }
0131 
0132     /**
0133      * Get all the attributes
0134      * 
0135      * @return array
0136      */
0137     public function getAttributes()
0138     {
0139         return $this->attributes;
0140     }
0141 
0142     /**
0143      * Get the instance's id
0144      * 
0145      * @return string 
0146      */
0147     public function getId()
0148     {
0149         return $this->attributes[self::INSTANCE_ID];
0150     }
0151 
0152     /**
0153      * Get the instance's image id
0154      * 
0155      * @return string 
0156      */
0157     public function getImageId()
0158     {
0159         return $this->attributes[self::INSTANCE_IMAGEID];
0160     }
0161 
0162     /**
0163      * Get the instance's name
0164      * 
0165      * @return string 
0166      */
0167     public function getName()
0168     {
0169         return $this->attributes[self::INSTANCE_NAME];
0170     }
0171 
0172     /**
0173      * Get the status of the instance
0174      * 
0175      * @return string|boolean 
0176      */
0177     public function getStatus()
0178     {
0179         return $this->adapter->statusInstance($this->attributes[self::INSTANCE_ID]);
0180     }
0181 
0182     /**
0183      * Wait for status $status with a timeout of $timeout seconds
0184      * 
0185      * @param  string $status
0186      * @param  integer $timeout 
0187      * @return boolean
0188      */
0189     public function waitStatus($status, $timeout = Adapter::TIMEOUT_STATUS_CHANGE)
0190     {
0191         return $this->adapter->waitStatusInstance($this->attributes[self::INSTANCE_ID], $status, $timeout);
0192     }
0193 
0194     /**
0195      * Get the public DNS of the instance
0196      * 
0197      * @return string 
0198      */
0199     public function getPublicDns()
0200     {
0201         if (!isset($this->attributes[self::INSTANCE_PUBLICDNS])) {
0202             $this->attributes[self::INSTANCE_PUBLICDNS] =  $this->adapter->publicDnsInstance($this->attributes[self::INSTANCE_ID]);
0203         }
0204         return $this->attributes[self::INSTANCE_PUBLICDNS];
0205     }
0206 
0207     /**
0208      * Get the instance's CPU
0209      * 
0210      * @return string
0211      */
0212     public function getCpu()
0213     {
0214         return $this->attributes[self::INSTANCE_CPU];
0215     }
0216 
0217     /**
0218      * Get the instance's RAM size
0219      * 
0220      * @return string
0221      */
0222     public function getRamSize()
0223     {
0224         return $this->attributes[self::INSTANCE_RAM];
0225     }
0226 
0227     /**
0228      * Get the instance's storage size
0229      * 
0230      * @return string
0231      */
0232     public function getStorageSize()
0233     {
0234         return $this->attributes[self::INSTANCE_STORAGE];
0235     }
0236 
0237     /**
0238      * Get the instance's zone
0239      * 
0240      * @return string 
0241      */
0242     public function getZone()
0243     {
0244         return $this->attributes[self::INSTANCE_ZONE];
0245     }
0246 
0247     /**
0248      * Get the instance's launch time
0249      * 
0250      * @return string
0251      */
0252     public function getLaunchTime()
0253     {
0254         return $this->attributes[self::INSTANCE_LAUNCHTIME];
0255     }
0256 
0257     /**
0258      * Reboot the instance
0259      * 
0260      * @return boolean 
0261      */
0262     public function reboot()
0263     {
0264         return $this->adapter->rebootInstance($this->attributes[self::INSTANCE_ID]);
0265     }
0266 
0267     /**
0268      * Stop the instance
0269      * 
0270      * @return boolean 
0271      */
0272     public function stop()
0273     {
0274         return $this->adapter->stopInstance($this->attributes[self::INSTANCE_ID]);
0275     }
0276 
0277     /**
0278      * Start the instance
0279      * 
0280      * @return boolean 
0281      */
0282     public function start()
0283     {
0284         return $this->adapter->startInstance($this->attributes[self::INSTANCE_ID]);
0285     }
0286 
0287     /**
0288      * Destroy the instance
0289      * 
0290      * @return boolean 
0291      */
0292     public function destroy()
0293     {
0294         return $this->adapter->destroyInstance($this->attributes[self::INSTANCE_ID]);
0295     }
0296 
0297     /**
0298      * Return the system informations about the $metric of an instance
0299      * 
0300      * @param  string $metric
0301      * @param  null|array $options
0302      * @return array|boolean
0303      */ 
0304     public function monitor($metric, $options = null)
0305     {
0306         return $this->adapter->monitorInstance($this->attributes[self::INSTANCE_ID], $metric, $options);
0307     }
0308 
0309     /**
0310      * Run arbitrary shell script on the instance
0311      *
0312      * @param  array $param
0313      * @param  string|array $cmd
0314      * @return string|array
0315      */
0316     public function deploy($params, $cmd)
0317     {
0318         return $this->adapter->deployInstance($this->attributes[self::INSTANCE_ID], $params, $cmd);
0319     }
0320 }