public static function createFromBundleTask(BundleTask $BundleTask) { $db = \Scalr::getDb(); $DBServer = DBServer::LoadByID($BundleTask->serverId); if ($BundleTask->prototypeRoleId) { $proto_role = $db->GetRow("SELECT * FROM roles WHERE id=? LIMIT 1", array($BundleTask->prototypeRoleId)); if (!$proto_role['architecture']) { $proto_role['architecture'] = $DBServer->GetProperty(SERVER_PROPERTIES::ARCHITECTURE); } } else { $behaviors = array_unique(explode(',', $DBServer->GetProperty(SERVER_PROPERTIES::SZR_IMPORTING_BEHAVIOR))); sort($behaviors); $proto_role = array("behaviors" => join(',', $behaviors), "architecture" => $DBServer->GetProperty(SERVER_PROPERTIES::ARCHITECTURE), "name" => "*import*"); } if (!$proto_role['architecture']) { $proto_role['architecture'] = 'x86_64'; } if (!$BundleTask->cloudLocation) { if ($DBServer) { $BundleTask->cloudLocation = $DBServer->GetCloudLocation(); } } $osId = $BundleTask->osId; $meta = $BundleTask->getSnapshotDetails(); if (!$osId) { if ($proto_role) { $osId = $proto_role['os_id']; } elseif ($meta['os'] && $meta['os']->version) { /* if ($meta['os']->version == '2008Server') { $osInfo->name = 'Windows 2008 Server'; $osInfo->family = 'windows'; $osInfo->generation = '2008'; $osInfo->version = '2008Server'; } elseif ($meta['os']->version == '2008ServerR2') { $osInfo->name = 'Windows 2008 Server R2'; $osInfo->family = 'windows'; $osInfo->generation = '2008'; $osInfo->version = '2008ServerR2'; }*/ //TODO: } } if ($proto_role['cat_id']) { $catId = $proto_role['cat_id']; } else { $catId = ROLE_BEHAVIORS::GetCategoryId($proto_role['behaviors']); } $db->Execute("INSERT INTO roles SET\n name\t\t\t= ?,\n origin\t\t\t= ?,\n dtadded = NOW(),\n client_id\t\t= ?,\n env_id\t\t\t= ?,\n cat_id = ?,\n description\t\t= ?,\n behaviors\t\t= ?,\n generation\t\t= ?,\n added_by_email = ?,\n added_by_userid = ?,\n os_id\t\t\t= ?\n ", array($BundleTask->roleName, ROLE_TYPE::CUSTOM, $BundleTask->clientId, $BundleTask->envId, $catId, $BundleTask->description, $proto_role['behaviors'], 2, $BundleTask->createdByEmail, $BundleTask->createdById, $osId)); $role_id = $db->Insert_Id(); $BundleTask->roleId = $role_id; $BundleTask->Save(); $BundleTask->Log(sprintf("Created new role. Role name: %s. Role ID: %s", $BundleTask->roleName, $BundleTask->roleId)); $role = self::loadById($role_id); $behaviors = explode(",", $proto_role['behaviors']); foreach ($behaviors as $behavior) { $db->Execute("INSERT IGNORE INTO role_behaviors SET\n role_id\t\t\t= ?,\n behavior\t\t= ?\n ", array($role_id, $behavior)); } // Set image $role->__getNewRoleObject()->setImage($BundleTask->platform, $BundleTask->cloudLocation, $BundleTask->snapshotId, $BundleTask->createdById, $BundleTask->createdByEmail); // Set params if ($proto_role['id']) { $dbSecRules = $db->GetAll("SELECT * FROM role_security_rules WHERE role_id = ?", array($proto_role['id'])); foreach ($dbSecRules as $dbSecRule) { $db->Execute("INSERT INTO role_security_rules SET role_id = ?, rule = ?", array($role_id, $dbSecRule['rule'])); } $props = $db->GetAll("SELECT * FROM role_properties WHERE role_id=?", array($proto_role['id'])); foreach ($props as $prop) { $role->setProperty($prop['name'], $prop['value']); } $scripts = $db->GetAll("SELECT * FROM role_scripts WHERE role_id=?", array($proto_role['id'])); foreach ($scripts as &$script) { $script['params'] = unserialize($script['params']); } $role->setScripts($scripts); $variables = new Scalr_Scripting_GlobalVariables($BundleTask->clientId, $proto_role['env_id'], ScopeInterface::SCOPE_ROLE); $variables->setValues($variables->getValues($proto_role['id']), $role->id); } $role->syncAnalyticsTags(); return $role; }
public function CreateServerSnapshot(BundleTask $BundleTask) { $DBServer = DBServer::LoadByID($BundleTask->serverId); $BundleTask->status = SERVER_SNAPSHOT_CREATION_STATUS::IN_PROGRESS; $BundleTask->bundleType = SERVER_SNAPSHOT_CREATION_TYPE::CSTACK_DEF; $msg = new Scalr_Messaging_Msg_Rebundle($BundleTask->id, $BundleTask->roleName, array()); if (!$DBServer->SendMessage($msg)) { $BundleTask->SnapshotCreationFailed("Cannot send rebundle message to server. Please check event log for more details."); return; } else { $BundleTask->Log(sprintf(_("Snapshot creating initialized (MessageID: %s). Bundle task status changed to: %s"), $msg->messageId, $BundleTask->status)); } $BundleTask->setDate('started'); $BundleTask->Save(); }
public function CreateServerSnapshot(BundleTask $BundleTask) { $DBServer = DBServer::LoadByID($BundleTask->serverId); $euca = $DBServer->GetEnvironmentObject()->eucalyptus($DBServer); if (!$BundleTask->prototypeRoleId) { $protoImageId = $DBServer->GetProperty(EUCA_SERVER_PROPERTIES::EMIID); } else { $protoImageId = DBRole::loadById($BundleTask->prototypeRoleId)->getImageId(SERVER_PLATFORMS::EUCALYPTUS, $DBServer->GetProperty(EUCA_SERVER_PROPERTIES::REGION)); } $ami = $euca->ec2->image->describe($protoImageId)->get(0); $platfrom = $ami->platform; $rootDeviceType = $ami->rootDeviceType; if ($rootDeviceType == 'ebs') { $BundleTask->bundleType = SERVER_SNAPSHOT_CREATION_TYPE::EUCA_EBS; $BundleTask->Log(sprintf(_("Selected platfrom snapshoting type: %s"), $BundleTask->bundleType)); $BundleTask->SnapshotCreationFailed("Not supported yet"); return; } else { if ($platfrom == 'windows') { //TODO: Windows platfrom is not supported yet. $BundleTask->bundleType = SERVER_SNAPSHOT_CREATION_TYPE::EUCA_WIN; $BundleTask->Log(sprintf(_("Selected platfrom snapshoting type: %s"), $BundleTask->bundleType)); $BundleTask->SnapshotCreationFailed("Not supported yet"); return; } else { $BundleTask->status = SERVER_SNAPSHOT_CREATION_STATUS::IN_PROGRESS; $BundleTask->bundleType = SERVER_SNAPSHOT_CREATION_TYPE::EUCA_WSI; $BundleTask->Save(); $BundleTask->Log(sprintf(_("Selected platfrom snapshoting type: %s"), $BundleTask->bundleType)); $msg = new Scalr_Messaging_Msg_Rebundle($BundleTask->id, $BundleTask->roleName, array()); if (!$DBServer->SendMessage($msg)) { $BundleTask->SnapshotCreationFailed("Cannot send rebundle message to server. Please check event log for more details."); return; } else { $BundleTask->Log(sprintf(_("Snapshot creation started (MessageID: %s). Bundle task status changed to: %s"), $msg->messageId, $BundleTask->status)); } } } $BundleTask->setDate('started'); $BundleTask->Save(); }
public function CreateServerSnapshot(BundleTask $BundleTask) { $DBServer = DBServer::LoadByID($BundleTask->serverId); $RDSClient = $this->getRdsClient($DBServer->GetEnvironmentObject(), $DBServer->GetProperty(RDS_SERVER_PROPERTIES::REGION)); try { $RDSClient->CreateDBSnapshot($BundleTask->roleName, $DBServer->GetProperty(RDS_SERVER_PROPERTIES::INSTANCE_ID)); $BundleTask->status = SERVER_SNAPSHOT_CREATION_STATUS::IN_PROGRESS; $BundleTask->bundleType = SERVER_SNAPSHOT_CREATION_TYPE::RDS_SPT; $BundleTask->snapshotId = $BundleTask->roleName; $BundleTask->Log(sprintf(_("Snapshot creation initialized. SnapshotID: %s"), $BundleTask->snapshotId)); $BundleTask->setDate('started'); $BundleTask->Save(); } catch (Exception $e) { $BundleTask->SnapshotCreationFailed($e->getMessage()); } }
public static function createFromBundleTask(BundleTask $BundleTask) { $db = Core::GetDBInstance(); if ($BundleTask->prototypeRoleId) { $proto_role = $db->GetRow("SELECT * FROM roles WHERE id=?", array($BundleTask->prototypeRoleId)); $DBServer = DBServer::LoadByID($BundleTask->serverId); } else { $DBServer = DBServer::LoadByID($BundleTask->serverId); if ($DBServer->platform != SERVER_PLATFORMS::RDS) { $proto_role = array("behaviors" => $DBServer->GetProperty(SERVER_PROPERTIES::SZR_IMPORTING_BEHAVIOR), "architecture" => $DBServer->GetProperty(SERVER_PROPERTIES::ARCHITECTURE), "name" => "*import*"); } } if (!$BundleTask->cloudLocation) { if ($DBServer) { $BundleTask->cloudLocation = $DBServer->GetCloudLocation(); } } $meta = $BundleTask->getSnapshotDetails(); $dist = new stdClass(); if ($meta) { if ($meta['os']) { $os = $meta['os']->version; } if ($meta['dist']) { $dist = $meta['dist']; } } else { $os = $proto_role['os']; } $db->Execute("INSERT INTO roles SET\r\n\t\t\t\tname\t\t\t= ?,\r\n\t\t\t\torigin\t\t\t= ?,\r\n\t\t\t\tclient_id\t\t= ?,\r\n\t\t\t\tenv_id\t\t\t= ?,\r\n\t\t\t\tdescription\t\t= ?,\r\n\t\t\t\tbehaviors\t\t= ?,\r\n\t\t\t\tarchitecture\t= ?,\r\n\t\t\t\tis_stable\t\t= '1',\r\n\t\t\t\thistory\t\t\t= ?,\r\n\t\t\t\tapproval_state\t= ?,\r\n\t\t\t\tgeneration\t\t= ?,\r\n\t\t\t\tos\t\t\t\t= ?,\r\n\t\t\t\tszr_version\t\t= ?\r\n\t\t\t", array($BundleTask->roleName, ROLE_TYPE::CUSTOM, $BundleTask->clientId, $BundleTask->envId, $BundleTask->description, $proto_role['behaviors'], $proto_role['architecture'], trim("{$proto_role['history']},{$proto_role['name']}", ","), APPROVAL_STATE::APPROVED, $DBServer->IsSupported("0.5") ? 2 : 1, $os ? $os : "Unknown", $meta['szr_version'])); $role_id = $db->Insert_Id(); $BundleTask->roleId = $role_id; $BundleTask->Save(); $BundleTask->Log(sprintf("Created new role. Role name: %s. Role ID: %s", $BundleTask->roleName, $BundleTask->roleId)); $role = self::loadById($role_id); $behaviors = explode(",", $proto_role['behaviors']); foreach ($behaviors as $behavior) { $db->Execute("INSERT INTO role_behaviors SET\r\n\t\t\t\t\trole_id\t\t\t= ?,\r\n\t\t\t\t\tbehavior\t\t= ?\r\n\t\t\t\t", array($role_id, $behavior)); } // Set image $role->setImage($BundleTask->snapshotId, $BundleTask->platform, $BundleTask->cloudLocation, $meta['szr_version'], $dist->distributor, $dist->codename, $dist->release, $proto_role['architecture']); // Set params if ($proto_role['id']) { $dbParams = $db->GetAll("SELECT name,type,isrequired,defval,allow_multiple_choice,options,hash,issystem\r\n\t\t\t\t\tFROM role_parameters WHERE role_id = ?", array($proto_role['id'])); $role->setParameters($dbParams); $dbSecRules = $db->GetAll("SELECT * FROM role_security_rules WHERE role_id = ?", array($proto_role['id'])); foreach ($dbSecRules as $dbSecRule) { $db->Execute("INSERT INTO role_security_rules SET role_id = ?, rule = ?", array($role_id, $dbSecRule['rule'])); } $props = $db->GetAll("SELECT * FROM role_properties WHERE role_id=?", array($proto_role['id'])); foreach ($props as $prop) { $role->setProperty($prop['name'], $prop['value']); } } else { if ($role->hasBehavior(ROLE_BEHAVIORS::NGINX)) { // Add nginx parameter $params[] = array('name' => 'Nginx HTTPS Vhost Template', 'type' => 'textarea', 'required' => '1', 'defval' => @file_get_contents(dirname(__FILE__) . "/../templates/services/nginx/ssl.vhost.tpl")); $role->setParameters($params); } } // Set software if ($meta) { $software = array(); foreach ((array) $meta['software'] as $soft) { $software[$soft->name] = $soft->version; } $role->setSoftware($software); $role->setTags((array) $meta['tags']); if ($BundleTask->platform == SERVER_PLATFORMS::NIMBULA) { $props = array(array('name' => self::PROPERTY_NIMBULA_INIT_ROOT_USER, 'value' => $meta['init_root_user']), array('name' => self::PROPERTY_NIMBULA_INIT_ROOT_PASS, 'value' => $meta['init_root_pass']), array('name' => self::PROPERTY_NIMBULA_ENTRY, 'value' => '')); foreach ($props as $prop) { $role->setProperty($prop['name'], $prop['value']); } } } return $role; }
public function CreateServerSnapshot(BundleTask $BundleTask) { $DBServer = DBServer::LoadByID($BundleTask->serverId); if ($BundleTask->osFamily == 'windows' || $DBServer->osType == 'windows') { if ($BundleTask->status == SERVER_SNAPSHOT_CREATION_STATUS::PENDING) { $BundleTask->bundleType = SERVER_SNAPSHOT_CREATION_TYPE::OSTACK_WINDOWS; $BundleTask->Log(sprintf(_("Selected platfrom snapshoting type: %s"), $BundleTask->bundleType)); $BundleTask->status = SERVER_SNAPSHOT_CREATION_STATUS::PREPARING; $BundleTask->Save(); try { $msg = $DBServer->SendMessage(new Scalr_Messaging_Msg_Win_PrepareBundle($BundleTask->id), false, true); if ($msg) { $BundleTask->Log(sprintf(_("PrepareBundle message sent. MessageID: %s. Bundle task status changed to: %s"), $msg->messageId, $BundleTask->status)); } else { throw new Exception("Cannot send message"); } } catch (Exception $e) { $BundleTask->SnapshotCreationFailed("Cannot send PrepareBundle message to server."); return false; } } elseif ($BundleTask->status == SERVER_SNAPSHOT_CREATION_STATUS::PREPARING) { $BundleTask->Log(sprintf(_("Selected platform snapshot type: %s"), $BundleTask->bundleType)); $createImage = true; } } else { $BundleTask->bundleType = SERVER_SNAPSHOT_CREATION_TYPE::OSTACK_LINUX; $createImage = false; $BundleTask->status = SERVER_SNAPSHOT_CREATION_STATUS::IN_PROGRESS; $msg = new Scalr_Messaging_Msg_Rebundle($BundleTask->id, $BundleTask->roleName, array()); if (!$DBServer->SendMessage($msg)) { $BundleTask->SnapshotCreationFailed("Cannot send rebundle message to server. Please check event log for more details."); return; } else { $BundleTask->Log(sprintf(_("Snapshot creating initialized (MessageID: %s). Bundle task status changed to: %s"), $msg->messageId, $BundleTask->status)); } $BundleTask->setDate('started'); $BundleTask->Save(); } if ($createImage) { try { $client = $this->getOsClient($DBServer->GetEnvironmentObject(), $DBServer->GetProperty(OPENSTACK_SERVER_PROPERTIES::CLOUD_LOCATION)); $imageId = $client->servers->createImage($DBServer->GetProperty(OPENSTACK_SERVER_PROPERTIES::SERVER_ID), $BundleTask->roleName . "-" . date("YmdHi")); $BundleTask->status = SERVER_SNAPSHOT_CREATION_STATUS::IN_PROGRESS; $BundleTask->snapshotId = $imageId; $BundleTask->Log(sprintf(_("Snapshot creating initialized (ImageID: %s). Bundle task status changed to: %s"), $BundleTask->snapshotId, $BundleTask->status)); $BundleTask->setDate('started'); $BundleTask->Save(); } catch (Exception $e) { $BundleTask->SnapshotCreationFailed($e->getMessage()); return; } } return true; }
public static function createFromBundleTask(BundleTask $BundleTask) { $db = \Scalr::getDb(); $DBServer = DBServer::LoadByID($BundleTask->serverId); if ($BundleTask->prototypeRoleId) { $proto_role = $db->GetRow("SELECT * FROM roles WHERE id=? LIMIT 1", array($BundleTask->prototypeRoleId)); if (!$proto_role['architecture']) { $proto_role['architecture'] = $DBServer->GetProperty(SERVER_PROPERTIES::ARCHITECTURE); } } else { $proto_role = array("behaviors" => $DBServer->GetProperty(SERVER_PROPERTIES::SZR_IMPORTING_BEHAVIOR), "architecture" => $DBServer->GetProperty(SERVER_PROPERTIES::ARCHITECTURE), "name" => "*import*"); } if (!$proto_role['architecture']) { $proto_role['architecture'] = 'x86_64'; } if (!$BundleTask->cloudLocation) { if ($DBServer) { $BundleTask->cloudLocation = $DBServer->GetCloudLocation(); } } $osInfo = $BundleTask->getOsDetails(); $meta = $BundleTask->getSnapshotDetails(); if (!$osInfo->family || !$osInfo->generation) { $osInfo = new stdClass(); if ($proto_role) { $osInfo->name = $proto_role['os']; $osInfo->family = $proto_role['os_family']; $osInfo->generation = $proto_role['os_generation']; $osInfo->version = $proto_role['os_version']; } elseif ($meta['os'] && $meta['os']->version) { if ($meta['os']->version == '2008Server') { $osInfo->name = 'Windows 2008 Server'; $osInfo->family = 'windows'; $osInfo->generation = '2008'; $osInfo->version = '2008Server'; } elseif ($meta['os']->version == '2008ServerR2') { $osInfo->name = 'Windows 2008 Server R2'; $osInfo->family = 'windows'; $osInfo->generation = '2008'; $osInfo->version = '2008ServerR2'; } } } if ($proto_role['cat_id']) { $catId = $proto_role['cat_id']; } else { $catId = ROLE_BEHAVIORS::GetCategoryId($proto_role['behaviors']); } $db->Execute("INSERT INTO roles SET\n name\t\t\t= ?,\n origin\t\t\t= ?,\n dtadded = NOW(),\n client_id\t\t= ?,\n env_id\t\t\t= ?,\n cat_id = ?,\n description\t\t= ?,\n behaviors\t\t= ?,\n history\t\t\t= ?,\n generation\t\t= ?,\n added_by_email = ?,\n added_by_userid = ?,\n os\t\t\t\t= ?,\n os_family = ?,\n os_version = ?,\n os_generation = ?\n ", array($BundleTask->roleName, ROLE_TYPE::CUSTOM, $BundleTask->clientId, $BundleTask->envId, $catId, $BundleTask->description, $proto_role['behaviors'], trim("{$proto_role['history']},{$proto_role['name']}", ","), 2, $BundleTask->createdByEmail, $BundleTask->createdById, $osInfo->name, $osInfo->family, $osInfo->version, $osInfo->generation)); $role_id = $db->Insert_Id(); $BundleTask->roleId = $role_id; $BundleTask->Save(); $BundleTask->Log(sprintf("Created new role. Role name: %s. Role ID: %s", $BundleTask->roleName, $BundleTask->roleId)); $role = self::loadById($role_id); $behaviors = explode(",", $proto_role['behaviors']); foreach ($behaviors as $behavior) { $db->Execute("INSERT INTO role_behaviors SET\n role_id\t\t\t= ?,\n behavior\t\t= ?\n ", array($role_id, $behavior)); } // Set image $role->setImage($BundleTask->snapshotId, $BundleTask->platform, $BundleTask->platform != SERVER_PLATFORMS::GCE ? $BundleTask->cloudLocation : "", $meta['szr_version'], $proto_role['architecture']); // Set params if ($proto_role['id']) { $dbParams = $db->GetAll("SELECT name,type,isrequired,defval,allow_multiple_choice,options,hash,issystem\n FROM role_parameters WHERE role_id = ?", array($proto_role['id'])); $role->setParameters($dbParams); $dbSecRules = $db->GetAll("SELECT * FROM role_security_rules WHERE role_id = ?", array($proto_role['id'])); foreach ($dbSecRules as $dbSecRule) { $db->Execute("INSERT INTO role_security_rules SET role_id = ?, rule = ?", array($role_id, $dbSecRule['rule'])); } $props = $db->GetAll("SELECT * FROM role_properties WHERE role_id=?", array($proto_role['id'])); foreach ($props as $prop) { $role->setProperty($prop['name'], $prop['value']); } $scripts = $db->GetAll("SELECT * FROM role_scripts WHERE role_id=?", array($proto_role['id'])); foreach ($scripts as &$script) { $script['params'] = unserialize($script['params']); } $role->setScripts($scripts); $variables = new Scalr_Scripting_GlobalVariables($proto_role['env_id'], Scalr_Scripting_GlobalVariables::SCOPE_ROLE); $variables->setValues($variables->getValues($proto_role['id']), $role->id); } // Set software if ($meta) { $software = array(); foreach ((array) $meta['software'] as $soft) { $software[$soft->name] = $soft->version; } $role->setSoftware($software); $role->setTags((array) $meta['tags']); if ($BundleTask->platform == SERVER_PLATFORMS::NIMBULA) { $props = array(array('name' => self::PROPERTY_NIMBULA_INIT_ROOT_USER, 'value' => $meta['init_root_user']), array('name' => self::PROPERTY_NIMBULA_INIT_ROOT_PASS, 'value' => $meta['init_root_pass']), array('name' => self::PROPERTY_NIMBULA_ENTRY, 'value' => '')); foreach ($props as $prop) { $role->setProperty($prop['name'], $prop['value']); } } } return $role; }
public function CreateServerSnapshot(BundleTask $BundleTask) { $DBServer = DBServer::LoadByID($BundleTask->serverId); $EC2Client = Scalr_Service_Cloud_Aws::newEc2($DBServer->GetProperty(EC2_SERVER_PROPERTIES::REGION), $DBServer->GetEnvironmentObject()->getPlatformConfigValue(self::PRIVATE_KEY), $DBServer->GetEnvironmentObject()->getPlatformConfigValue(self::CERTIFICATE)); if (!$BundleTask->prototypeRoleId) { $proto_image_id = $DBServer->GetProperty(EC2_SERVER_PROPERTIES::AMIID); } else { $proto_image_id = DBRole::loadById($BundleTask->prototypeRoleId)->getImageId(SERVER_PLATFORMS::EC2, $DBServer->GetProperty(EC2_SERVER_PROPERTIES::REGION)); } $DescribeImagesType = new DescribeImagesType(); $DescribeImagesType->imagesSet->item[] = array("imageId" => $proto_image_id); $ami_info = $EC2Client->DescribeImages($DescribeImagesType); $platfrom = (string) $ami_info->imagesSet->item->platform; if ($platfrom == 'windows') { if ((string) $ami_info->imagesSet->item->rootDeviceType != 'ebs') { $BundleTask->SnapshotCreationFailed("Only EBS root filesystem supported for Windows servers."); return; } if ($BundleTask->status == SERVER_SNAPSHOT_CREATION_STATUS::PENDING) { $BundleTask->bundleType = SERVER_SNAPSHOT_CREATION_TYPE::EC2_WIN200X; $BundleTask->Log(sprintf(_("Selected platfrom snapshoting type: %s"), $BundleTask->bundleType)); $BundleTask->status = SERVER_SNAPSHOT_CREATION_STATUS::PREPARING; try { $msg = $DBServer->SendMessage(new Scalr_Messaging_Msg_Win_PrepareBundle($BundleTask->id)); if ($msg) { $BundleTask->Log(sprintf(_("PrepareBundle message sent. MessageID: %s. Bundle task status changed to: %s"), $msg->messageId, $BundleTask->status)); } else { throw new Exception("Cannot send message"); } } catch (Exception $e) { $BundleTask->SnapshotCreationFailed("Cannot send PrepareBundle message to server."); return false; } } elseif ($BundleTask->status == SERVER_SNAPSHOT_CREATION_STATUS::PREPARING) { $BundleTask->Log(sprintf(_("Selected platform snapshot type: %s"), $BundleTask->bundleType)); try { $CreateImageType = new CreateImageType($DBServer->GetProperty(EC2_SERVER_PROPERTIES::INSTANCE_ID), $BundleTask->roleName . "-" . date("YmdHi"), $BundleTask->roleName, false); $result = $EC2Client->CreateImage($CreateImageType); $BundleTask->status = SERVER_SNAPSHOT_CREATION_STATUS::IN_PROGRESS; $BundleTask->snapshotId = $result->imageId; $BundleTask->Log(sprintf(_("Snapshot creating initialized (AMIID: %s). Bundle task status changed to: %s"), $BundleTask->snapshotId, $BundleTask->status)); } catch (Exception $e) { $BundleTask->SnapshotCreationFailed($e->getMessage()); return; } } } else { $BundleTask->status = SERVER_SNAPSHOT_CREATION_STATUS::IN_PROGRESS; if ((string) $ami_info->imagesSet->item->rootDeviceType == 'ebs') { if ((string) $ami_info->imagesSet->item->virtualizationType == 'hvm') { $BundleTask->bundleType = SERVER_SNAPSHOT_CREATION_TYPE::EC2_EBS_HVM; } else { $BundleTask->bundleType = SERVER_SNAPSHOT_CREATION_TYPE::EC2_EBS; } } else { $BundleTask->bundleType = SERVER_SNAPSHOT_CREATION_TYPE::EC2_S3I; } $BundleTask->Save(); $BundleTask->Log(sprintf(_("Selected platfrom snapshoting type: %s"), $BundleTask->bundleType)); if ($BundleTask->bundleType == SERVER_SNAPSHOT_CREATION_TYPE::EC2_EBS_HVM) { try { $CreateImageType = new CreateImageType($DBServer->GetProperty(EC2_SERVER_PROPERTIES::INSTANCE_ID), $BundleTask->roleName . "-" . date("YmdHi"), $BundleTask->roleName, false); $result = $EC2Client->CreateImage($CreateImageType); $BundleTask->status = SERVER_SNAPSHOT_CREATION_STATUS::IN_PROGRESS; $BundleTask->snapshotId = $result->imageId; $BundleTask->Log(sprintf(_("Snapshot creating initialized (AMIID: %s). Bundle task status changed to: %s"), $BundleTask->snapshotId, $BundleTask->status)); } catch (Exception $e) { $BundleTask->SnapshotCreationFailed($e->getMessage()); return; } } else { $msg = new Scalr_Messaging_Msg_Rebundle($BundleTask->id, $BundleTask->roleName, array()); $metaData = $BundleTask->getSnapshotDetails(); if ($metaData['rootVolumeSize']) { $msg->volumeSize = $metaData['rootVolumeSize']; } if (!$DBServer->SendMessage($msg)) { $BundleTask->SnapshotCreationFailed("Cannot send rebundle message to server. Please check event log for more details."); return; } else { $BundleTask->Log(sprintf(_("Snapshot creation started (MessageID: %s). Bundle task status changed to: %s"), $msg->messageId, $BundleTask->status)); } } } $BundleTask->setDate('started'); $BundleTask->Save(); }
/** * {@inheritdoc} * @see IPlatformModule::CreateServerSnapshot() */ public function CreateServerSnapshot(BundleTask $BundleTask) { $DBServer = DBServer::LoadByID($BundleTask->serverId); $aws = $DBServer->GetEnvironmentObject()->aws($DBServer); if (!$BundleTask->prototypeRoleId) { $proto_image_id = $DBServer->GetProperty(EC2_SERVER_PROPERTIES::AMIID); } else { $protoRole = DBRole::loadById($BundleTask->prototypeRoleId); $proto_image_id = $protoRole->getImageId(SERVER_PLATFORMS::EC2, $DBServer->GetProperty(EC2_SERVER_PROPERTIES::REGION)); $details = $protoRole->getImageDetails(SERVER_PLATFORMS::EC2, $DBServer->GetProperty(EC2_SERVER_PROPERTIES::REGION)); if (in_array($details['os_family'], array('oel', 'redhat', 'scientific'))) { $BundleTask->bundleType = SERVER_SNAPSHOT_CREATION_TYPE::EC2_EBS_HVM; } } $ami = $aws->ec2->image->describe($proto_image_id)->get(0); $platfrom = $ami->platform; if ($platfrom == 'windows') { if ($ami->rootDeviceType != 'ebs') { $BundleTask->SnapshotCreationFailed("Only EBS root filesystem supported for Windows servers."); return; } if ($BundleTask->status == SERVER_SNAPSHOT_CREATION_STATUS::PENDING) { $BundleTask->bundleType = SERVER_SNAPSHOT_CREATION_TYPE::EC2_WIN200X; $BundleTask->Log(sprintf(_("Selected platfrom snapshoting type: %s"), $BundleTask->bundleType)); $BundleTask->status = SERVER_SNAPSHOT_CREATION_STATUS::PREPARING; try { $msg = $DBServer->SendMessage(new Scalr_Messaging_Msg_Win_PrepareBundle($BundleTask->id)); if ($msg) { $BundleTask->Log(sprintf(_("PrepareBundle message sent. MessageID: %s. Bundle task status changed to: %s"), $msg->messageId, $BundleTask->status)); } else { throw new Exception("Cannot send message"); } } catch (Exception $e) { $BundleTask->SnapshotCreationFailed("Cannot send PrepareBundle message to server."); return false; } } elseif ($BundleTask->status == SERVER_SNAPSHOT_CREATION_STATUS::PREPARING) { $BundleTask->Log(sprintf(_("Selected platform snapshot type: %s"), $BundleTask->bundleType)); try { $request = new CreateImageRequestData($DBServer->GetProperty(EC2_SERVER_PROPERTIES::INSTANCE_ID), $BundleTask->roleName . "-" . date("YmdHi")); $request->description = $BundleTask->roleName; $request->noReboot = false; $imageId = $aws->ec2->image->create($request); $BundleTask->status = SERVER_SNAPSHOT_CREATION_STATUS::IN_PROGRESS; $BundleTask->snapshotId = $imageId; $BundleTask->Log(sprintf(_("Snapshot creating initialized (AMIID: %s). Bundle task status changed to: %s"), $BundleTask->snapshotId, $BundleTask->status)); } catch (Exception $e) { $BundleTask->SnapshotCreationFailed($e->getMessage()); return; } } } else { $BundleTask->status = SERVER_SNAPSHOT_CREATION_STATUS::IN_PROGRESS; if (!$BundleTask->bundleType) { if ($ami->rootDeviceType == 'ebs') { if ($ami->virtualizationType == 'hvm') { $BundleTask->bundleType = SERVER_SNAPSHOT_CREATION_TYPE::EC2_EBS_HVM; } else { $BundleTask->bundleType = SERVER_SNAPSHOT_CREATION_TYPE::EC2_EBS; } } else { $BundleTask->bundleType = SERVER_SNAPSHOT_CREATION_TYPE::EC2_S3I; } } $BundleTask->Save(); $BundleTask->Log(sprintf(_("Selected platfrom snapshoting type: %s"), $BundleTask->bundleType)); if ($BundleTask->bundleType == SERVER_SNAPSHOT_CREATION_TYPE::EC2_EBS_HVM) { try { $request = new CreateImageRequestData($DBServer->GetProperty(EC2_SERVER_PROPERTIES::INSTANCE_ID), $BundleTask->roleName . "-" . date("YmdHi")); $request->description = $BundleTask->roleName; $request->noReboot = false; $imageId = $aws->ec2->image->create($request); $BundleTask->status = SERVER_SNAPSHOT_CREATION_STATUS::IN_PROGRESS; $BundleTask->snapshotId = $imageId; $BundleTask->Log(sprintf(_("Snapshot creating initialized (AMIID: %s). Bundle task status changed to: %s"), $BundleTask->snapshotId, $BundleTask->status)); } catch (Exception $e) { $BundleTask->SnapshotCreationFailed($e->getMessage()); return; } } else { $msg = new Scalr_Messaging_Msg_Rebundle($BundleTask->id, $BundleTask->roleName, array()); $metaData = $BundleTask->getSnapshotDetails(); if ($metaData['rootVolumeSize']) { $msg->volumeSize = $metaData['rootVolumeSize']; } if (!$DBServer->SendMessage($msg)) { $BundleTask->SnapshotCreationFailed("Cannot send rebundle message to server. Please check event log for more details."); return; } else { $BundleTask->Log(sprintf(_("Snapshot creation started (MessageID: %s). Bundle task status changed to: %s"), $msg->messageId, $BundleTask->status)); } } } $BundleTask->setDate('started'); $BundleTask->Save(); }