protected function run8($stage) { $knownOses = []; //Retrieves the list of all known OSes foreach (Entity\Os::all() as $os) { /* @var $os Entity\Os */ $knownOses[$os->id] = $os; } $role = new Entity\Role(); //Trying to clarify the operating system of the Roles using Images which are associated with them. //If all Images have the same operating system it will be considered as acceptable for the Role at latter will be updated. $rs = $this->db->Execute("\n SELECT " . $role->fields('r', true) . ", GROUP_CONCAT(t.os_id) `osids`\n FROM roles r JOIN (\n SELECT DISTINCT ri.role_id, i.os_id\n FROM images i\n JOIN role_images ri ON i.id = ri.image_id\n AND i.platform = ri.platform\n AND i.cloud_location = ri.cloud_location\n ) t ON t.role_id = r.id\n WHERE r.os_id = ?\n GROUP BY r.id\n HAVING osids != r.os_id\n ", ['unknown-os']); if ($rs->RecordCount()) { $this->console->out("Found %d Roles the OS value of which can be filled from the Images. Updating...", $rs->RecordCount()); } while ($row = $rs->FetchRow()) { $role = new Entity\Role(); $role->load($row, 'r'); if (!empty($row['osids'])) { if (isset($knownOses[$row['osids']])) { //Updating OS value of the Role $role->osId = $row['osids']; $role->save(); } else { $this->console->warning("Role %s (%d) is associated with the Images with either different or unknown OS: %s", $role->name, $role->id, $row['osids']); } } } $image = new Entity\Image(); //Trying to clarify the operating sytem of the Images using Roles which are associated with them. $rs = $this->db->Execute("\n SELECT " . $image->fields('i', true) . ", GROUP_CONCAT(t.os_id) `osids`\n FROM images i JOIN (\n SELECT DISTINCT ri.image_id, ri.platform, ri.cloud_location, r.os_id\n FROM roles r\n JOIN role_images ri ON ri.role_id = r.id\n ) t ON t.image_id = i.id AND t.platform = i.platform AND t.cloud_location = i.cloud_location\n WHERE i.os_id = ?\n GROUP BY i.hash\n HAVING osids != i.os_id\n ", ['unknown-os']); if ($rs->RecordCount()) { $this->console->out("Found %d Images the OS value of which can be filled from the Roles. Updating...", $rs->RecordCount()); } while ($row = $rs->FetchRow()) { $image = new Entity\Image(); $image->load($row, 'i'); if (!empty($row['osids'])) { if (isset($knownOses[$row['osids']])) { //Updating OS value of the Image $image->osId = $row['osids']; $image->save(); } else { $this->console->warning("Image (%s) imageId: %s, platform: %s, cloudLocation: %s is associated with the Roles with either different or unknown OS: %s", $image->hash, $image->id, $image->platform, $image->cloudLocation, $row['osids']); } } } }
/** * Gets the Image Entity * * @return Image|null Returns the Image that corresponds to the Server */ public function getImage() { if (empty($this->_image) && !empty($this->roleId) && !empty($this->platform)) { $i = new Image(); $ri = new RoleImage(); $rec = $this->db()->GetRow("\n SELECT {$i->fields()}\n FROM {$i->table()}\n LEFT JOIN {$ri->table()} ON {$i->columnPlatform} = {$ri->columnPlatform}\n AND {$i->columnCloudLocation} = {$ri->columnCloudLocation}\n AND {$i->columnId} = {$ri->columnImageId}\n WHERE {$ri->columnRoleId} = ?\n AND {$ri->columnPlatform} = ?\n AND {$ri->columnCloudLocation} = ?\n AND ({$i->columnAccountId} IS NULL OR {$i->columnAccountId} = ?\n AND ({$i->columnEnvId} IS NULL OR {$i->columnEnvId} = ?)\n )\n ", [$this->roleId, $this->platform, in_array($this->platform, [SERVER_PLATFORMS::GCE, SERVER_PLATFORMS::AZURE]) ? '' : $this->cloudLocation, $this->getFarm()->accountId, $this->getFarm()->envId]); if ($rec) { $this->_image = $i; $this->_image->load($rec); } } return $this->_image; }