public static function cleararchivedvappversions($appid, $fromindex) { if (!is_numeric($appid) || $appid <= 0) { return "Invalid vapplication id"; } if (!is_numeric($fromindex) || $fromindex <= 0) { return "Invalid index value"; } if ($fromindex < 20) { //Make sure it will never delete all of the vapp archived versions $fromindex = 20; } db()->setFetchMode(Zend_Db::FETCH_BOTH); $res = db()->query("select vapp_old_archived_versions(?,?);", array($appid, $fromindex))->fetchAll(); $result = array(); if (count($res) > 0) { foreach ($res as $r) { $vappversions = new Default_Model_VAversions(); $vappversions->filter->id->equals($r[0]); if (count($vappversions->items) > 0) { $vappversion = $vappversions->items[0]; $deleted = VMCaster::deleteVersion($vappversion); if ($deleted !== true) { error_log("[VMCaster::cleararchivedvappversions]: " . $deleted); } else { $result[] = $vappversion->id; } } } } return implode(",", $result); }
public function parseVAppVersion($xml, $parent = null) { $m = $this->getItem("vaversion", $xml); if (!$this->isPUT() && $this->isexternalrequest === false && (!is_numeric($m->id) || intval($m->id) <= 0)) { return $this->_setErrorMessage("No virtual appliance version given"); } //Check if retrieved version if (is_numeric($m->id) && intval($m->id) > 0) { //if version belongs to this vappliance if ($m->vappid !== $this->vappid) { return $this->_setErrorMessage("Given version does not belong in this virtual appliance"); } //check if is to toogle enable if (strlen(trim(strval($xml->attributes()->enabled))) > 0 && $this->isPUT() === false) { $enabled = strtolower(trim(strval($xml->attributes()->enabled))); if ($enabled == "true" && $m->enabled === false) { $this->createVersionState($m); $m->enabled = true; $m->save(); $this->vappversion_state->setVersionNewState($m); return $m; } else { if ($enabled == "false" && $m->enabled === true) { $this->createVersionState($m); $m->enabled = false; $m->save(); $this->vappversion_state->setVersionNewState($m); return $m; } } } //Check if published if ($m->published == true && $m->archived == false && $m->status == "verified") { if ($this->isexternalrequest === true || $this->isPUT() === true) { $ident = $m->guid; $m = new Default_Model_VAversion(); $m->guid = $ident; } else { return $this->_setErrorMessage("Cannot edit published version"); } } $workingver = $this->getWorkingVersion(); //Compare to working if ($workingver !== null) { //if not the same as working then archive working version if ($workingver->id !== $m->id) { $delver = VMCaster::deleteVersion($workingver); if ($delver !== true) { if ($delver === false) { $delver = ""; } return $this->_setErrorMessage("Could not delete working version." . $delver); } } } } if (strlen(trim(strval($xml->attributes()->version))) > 0) { $m->version = trim(strval($xml->attributes()->version)); $m->version = strip_tags($m->version); if ($this->validateVersion($m->version) === false) { return $this->_setErrorMessage("Invalid version value for virtual appliance version."); } if ($this->isUsedVaVersion($this->appid, $m->version)) { return $this->_setErrorMessage("Version value is already used in a previous version of the virtual appliance."); } } /* * Create old data set and create VApplianceVersionState manager */ $this->createVersionState($m); if (is_numeric($m->id) && intval($m->id) > 0) { if (strlen(trim(strval($xml->attributes()->enabled))) > 0) { $enabled = strtolower(trim(strval($xml->attributes()->enabled))); if ($enabled == "true") { $m->enabled = true; } else { if ($m->enabled == "false") { $m->enabled = false; } } } } if (strlen(trim(strval($xml->attributes()->status))) > 0) { $status = strtolower(trim(strval($xml->attributes()->status))); if ($this->isPUT() && !$m->status) { $m->status = 'init'; } $validstatus = $this->validateStatusChange($m, $status); if ($validstatus === true) { $m->status = $status; } else { if (strlen(trim(strval($validstatus))) > 0) { return $this->_setErrorMessage($validstatus); } else { return $this->_setErrorMessage("Invalid status value given."); } } } if (strlen(trim(strval($xml->attributes()->published))) > 0) { $published = strtolower(trim(strval($xml->attributes()->published))); $m->published = $published === "true" ? true : false; } if (count($xml->xpath('./virtualization:notes')) > 0) { $m->notes = $xml->xpath('./virtualization:notes'); $m->notes = (string) $m->notes[0]; if ($this->validateNotes($m->notes) === false) { return $this->_setErrorMessage("Invalid notes value for virtual appliance version."); } } if (strlen(trim(strval($xml->attributes()->createdon))) > 0) { $m->createdon = strval($xml->attributes()->createdon); } if (strlen(trim(strval($xml->attributes()->expireson))) > 0) { $m->expireson = strval($xml->attributes()->expireson); } //In case of a new vaversion check if user has given an identifier if (!$m->guid) { if (count($xml->xpath('./virtualization:identifier')) > 0) { $guid = $xml->xpath('./virtualization:identifier'); $guid = (string) $guid[0]; $guid = trim($guid); if (strlen($guid) > 0) { $m->guid = $guid; } } //check if user-defined identifier is in use from other entities(images, other application's vappliance versions etc if ($m->guid) { //check that identifier is not used by other images anywhere $images = new Default_Model_VMIinstances(); $images->filter->guid->equals($m->guid); if (count($images->items) > 0) { return $this->_setErrorMessage("Version identifier " . $m->guid . " is already in use by another virtual appliance image."); } //check that identifier is not used by other VMI $vmis = new Default_Model_VMIs(); $vmis->filter->guid->equals($m->guid); if (count($vmis->items) > 0) { return $this->_setErrorMessage("Version identifier " . $m->guid . " is already in use by another virtual appliance image group."); } //check that identifier is not used by other vaversions outside this application $vaversions = new Default_Model_VAversions(); $vaversions->filter->guid->equals($m->guid); if (count($vaversions->items) > 0) { $vaversion = $vaversions->items[0]; $va = $vaversion->getVa(); if ($va && $va->id !== $this->vappid) { $app = $va->getApplication(); if ($app) { return $this->_setErrorMessage("Version identifier " . $m->guid . " is already used by " . $app->name . " virtual appliance."); } } return $this->_setErrorMessage("Version identifier " . $m->guid . " is already used by another version."); } //Check if identifier is used inside a VO wide image list $voexists = self::VOGuidExists($m->guid); if ($voexists === true) { return $this->_setErrorMessage("Version identifier " . $m->guid . " is already used in a virtual organization image list."); } else { if (is_string($voexists)) { return $this->_setErrorMessage("Version identifier " . $m->guid . " is already used by " . $voexists . " virtual organization image list."); } } } } if (!$this->validateVersion($m->version)) { return $this->_setErrorMessage("Invalid version value for virtual appliance version."); } if (!$this->validateNotes($m->notes)) { return $this->_setErrorMessage("Invalid notes value for virtual appliance version."); } if (strlen(trim($m->guid)) > 0 && $this->validateIdentifier($m->guid) === false) { return $this->_setErrorMessage("Invalid identifier value for virtual appliance version."); } if ($this->isexternalrequest === true) { if (strlen(trim(strval($xml->attributes()->submissionid))) > 0) { $submissionid = strtolower(trim(strval($xml->attributes()->submissionid))); $m->submissionID = intval($submissionid); } $m->isexternal = true; $m->status = 'init'; } $m->vappid = $this->vappid; $m->save(); //Check virtual appliance version state (eg publish,enabled etc) $validversionstate = $this->vappversion_state->setVersionNewState($m); if ($validversionstate !== true) { if ($validversionstate !== false) { return $this->_setErrorMessage($validversionstate); } return $this->_setErrorMessage("Invalid state for virtual appliance version"); } //Store locally for future use $this->vappversionid = $m->id; //Retrieve current VMIs ids. $oldvmiids = $m->getVMIIds(); //Check if user requests to delete all vmis and their contents (vmi instances etc) if (count($xml->xpath('/virtualization:image[@xsi:nil="true"]')) != 0) { $this->deleteVMIs($oldvmiids); return $m; } //Get VMIs of VA Version. $ximages = $xml->xpath("./virtualization:image"); $vmis = array(); if (count($ximages) > 0) { for ($i = 0; $i < count($ximages); $i += 1) { $v = $this->parseVAppImage($ximages[$i], $m); if ($v === false) { return false; } $vmis[] = $v; } } /*Check for VMIs for deletion*/ for ($i = 0; $i < count($vmis); $i += 1) { $item = $vmis[$i]; $index = -1; //find in old values for ($j = 0; $j < count($oldvmiids); $j += 1) { if ($item->id == $oldvmiids[$j]) { $index = $j; break; } } //Remove current vmi item from old values array if ($index > -1) { unset($oldvmiids[$index]); $oldvmiids = array_values($oldvmiids); } } /*Remove old vmis that are no longer defined in the request xml */ $this->deleteVMIs($oldvmiids); if ($this->versionHasImages($m) === false) { return $this->_setErrorMessage("Version must contain at least one image"); } if ($this->versionHasUniqueUrls() === false) { return $this->_setErrorMessage("Version contains images with same url"); } return $m; }