Scalr\Service\Aws\Ec2\DataType\VolumeData::attach PHP Метод

attach() публичный Метод

Attaches an Amazon EBS volume to a running instance and exposes it to the instance with the specified device name. For a list of supported device names, see Attaching the Volume to an Instance. Any device names that aren't reserved for instance store volumes can be used for Amazon EBS volumes. Note! If a volume has an AWS Marketplace product code: -The volume can only be attached to the root device of a stopped instance. -You must be subscribed to the AWS Marketplace code that is on the volume. -The configuration (instance type, operating system) of the instance must support that specific AWS Marketplace code. For example, you cannot take a volume from a Windows instance and attach it to a Linux instance. -AWS Marketplace product codes are copied from the volume to the instance.
public attach ( string $instanceId, string $device ) : AttachmentSetResponseData
$instanceId string The ID of the Instance. The instance must be running.
$device string The device name as exposed to the instance
Результат AttachmentSetResponseData Returns AttachmentSetResponseData on success
    public function attach($instanceId, $device)
    {
        $this->throwExceptionIfNotInitialized();
        return $this->getEc2()->volume->attach($this->volumeId, $instanceId, $device);
    }

Usage Example

Пример #1
0
 /**
  * Attaches volume to server
  *
  * It uses request params and can't be used without UI request
  *
  * @param    VolumeData $info AWS EBS Volume info
  * @throws   Exception
  */
 protected function attachVolumeToServer(VolumeData $info)
 {
     $dBServer = DBServer::LoadByID($this->getParam('serverId'));
     //Check access permission to specified server
     $this->request->checkPermissions($dBServer->GetFarmObject()->__getNewFarmObject(), Acl::PERM_FARMS_SERVERS);
     $errmsg = null;
     try {
         $dbEbsVolume = DBEBSVolume::loadByVolumeId($this->getParam('volumeId'));
         if ($dbEbsVolume->isManual == 0) {
             $errmsg = sprintf(_("This volume was automatically created for role '%s' on farm '%s' and cannot be re-attahced manually."), $this->db->GetOne("\n                        SELECT name FROM roles\n                        JOIN farm_roles ON farm_roles.role_id = roles.id\n                        WHERE farm_roles.id=?\n                        LIMIT 1\n                    ", array($dbEbsVolume->farmRoleId)), $this->db->GetOne("SELECT name FROM farms WHERE id=? LIMIT 1", array($dbEbsVolume->farmId)));
         }
     } catch (Exception $e) {
     }
     if (!empty($errmsg)) {
         throw new Exception($errmsg);
     }
     $image = Image::findOne([['platform' => $dBServer->platform], ['id' => $dBServer->imageId], ['cloudLocation' => $dBServer->GetCloudLocation()]]);
     $device = $dBServer->GetFreeDeviceName($image->isEc2HvmImage());
     $res = $info->attach($dBServer->GetProperty(EC2_SERVER_PROPERTIES::INSTANCE_ID), $device);
     if ($this->getParam('attachOnBoot') == 'on') {
         $dbEbsVolume = new DBEBSVolume();
         $dbEbsVolume->attachmentStatus = EC2_EBS_ATTACH_STATUS::ATTACHING;
         $dbEbsVolume->isManual = true;
         $dbEbsVolume->volumeId = $info->volumeId;
         $dbEbsVolume->ec2AvailZone = $info->availabilityZone;
         $dbEbsVolume->ec2Region = $this->getParam('cloudLocation');
         $dbEbsVolume->deviceName = $device;
         $dbEbsVolume->farmId = $dBServer->farmId;
         $dbEbsVolume->farmRoleId = $dBServer->farmRoleId;
         $dbEbsVolume->serverId = $dBServer->serverId;
         $dbEbsVolume->serverIndex = $dBServer->index;
         $dbEbsVolume->size = $info->size;
         $dbEbsVolume->snapId = $info->snapshotId;
         $dbEbsVolume->mount = $this->getParam('mount') == 1;
         $dbEbsVolume->mountPoint = $this->getParam('mountPoint');
         $dbEbsVolume->mountStatus = $this->getParam('mount') == 1 ? EC2_EBS_MOUNT_STATUS::AWAITING_ATTACHMENT : EC2_EBS_MOUNT_STATUS::NOT_MOUNTED;
         $dbEbsVolume->clientId = $this->user->getAccountId();
         $dbEbsVolume->envId = $this->getEnvironmentId();
         $dbEbsVolume->Save();
     }
     //Updates/Creates AWS Tags of Volume
     $tags = [];
     foreach ($dBServer->getAwsTags() as $k => $v) {
         $tags[] = ['key' => $k, 'value' => $v];
     }
     if (!empty($tags)) {
         $info->createTags($tags);
     }
 }