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; }