/** * Update server's information * * @param jsonData $servers optional List of servers to check against * @throws \Scalr_Exception_InsufficientPermissions */ public function xListServersUpdateAction(JsonData $servers = null) { if (!$this->request->isAllowed([Acl::RESOURCE_FARMS, Acl::RESOURCE_TEAM_FARMS, Acl::RESOURCE_OWN_FARMS]) && !$this->request->isAllowed(Acl::RESOURCE_IMAGES_ENVIRONMENT, Acl::PERM_IMAGES_ENVIRONMENT_MANAGE)) { throw new \Scalr_Exception_InsufficientPermissions(); } $props = $retval = []; $args = $servers = (array) $servers; $stmt = "\n SELECT s.server_id, s.status, s.remote_ip, s.local_ip, s.dtadded, s.dtinitialized AS uptime\n FROM servers s\n LEFT JOIN farms f ON f.id = s.farm_id\n WHERE s.server_id IN (" . implode(",", array_fill(0, count($servers), "?")) . ")\n AND s.env_id = ?\n "; $args[] = $this->getEnvironmentId(); $where = ["s.farm_id IS NOT NULL AND " . $this->request->getFarmSqlQuery()]; if ($this->request->isAllowed(Acl::RESOURCE_IMAGES_ENVIRONMENT, Acl::PERM_IMAGES_ENVIRONMENT_MANAGE)) { $where[] = "s.farm_id IS NULL AND s.status IN (?, ?)"; $args[] = Entity\Server::STATUS_IMPORTING; $args[] = Entity\Server::STATUS_TEMPORARY; } $stmt .= " AND (" . join(" OR ", $where) . ")"; if (!empty($servers)) { $srs = $this->db->Execute($stmt, $args); $neededProps = [Entity\Server::REBOOTING, Entity\Server::MISSING, Entity\Server::SZR_IS_INIT_FAILED, Entity\Server::LAUNCH_ERROR, Entity\Server::SZR_VESION]; foreach (Entity\Server\Property::fetch($servers, $neededProps) as $resRow) { if (!array_key_exists($resRow->serverId, $props)) { $props[$resRow->serverId] = []; } $props[$resRow->serverId][$resRow->name] = $resRow->value; } while ($server = $srs->FetchRow()) { if (!array_key_exists($server["server_id"], $props)) { $props[$server["server_id"]] = []; } $status = $server["status"]; if (in_array($status, [Entity\Server::STATUS_RUNNING, Entity\Server::STATUS_SUSPENDED])) { if (array_key_exists(Entity\Server::REBOOTING, $props[$server["server_id"]]) && $props[$server["server_id"]][Entity\Server::REBOOTING] != 0) { $server["status"] = "Rebooting"; } if (array_key_exists(Entity\Server::MISSING, $props[$server["server_id"]]) && $props[$server["server_id"]][Entity\Server::MISSING] != 0) { $server["status"] = "Missing"; } } if (array_key_exists(Entity\Server::SZR_IS_INIT_FAILED, $props[$server["server_id"]]) && $props[$server["server_id"]][Entity\Server::SZR_IS_INIT_FAILED] == 1 && in_array($server["status"], [Entity\Server::STATUS_INIT, Entity\Server::STATUS_PENDING])) { $server["isInitFailed"] = 1; } if (array_key_exists(Entity\Server::LAUNCH_ERROR, $props[$server["server_id"]]) && $props[$server["server_id"]][Entity\Server::LAUNCH_ERROR] == 1) { $server["launch_error"] = "1"; } $server["agent_version"] = $props[$server["server_id"]][Entity\Server::SZR_VESION]; if ($status === Entity\Server::STATUS_RUNNING) { $server['uptime'] = \Scalr_Util_DateTime::getHumanReadableTimeout(time() - strtotime($server['uptime']), false); } else { $server['uptime'] = ''; } $retval[$server["server_id"]] = $server; } } $this->response->data(["servers" => $retval]); }
/** * Calculates difference between two dates. * * @param DateTime|string $value DateTime object or string that represents time. * @param bool $humanReadable Return number of seconds or human readable string * @return string|int Returns difference between dates. */ public static function getDateTimeDiff($dateTime1, $dateTime2, $humanReadable = true) { if (!$dateTime1 instanceof DateTime) { $dateTime1 = new DateTime($dateTime1); } if (!$dateTime2 instanceof DateTime) { $dateTime2 = new DateTime($dateTime2); } $diff = $dateTime1->getTimestamp() - $dateTime2->getTimestamp(); if ($humanReadable) { $diff = Scalr_Util_DateTime::getHumanReadableTimeout($diff); } return $diff; }
public function xListServersAction() { if (!$this->request->isAllowed(Acl::RESOURCE_FARMS_SERVERS) && !$this->request->isAllowed(Acl::RESOURCE_FARMS_ROLES, Acl::PERM_FARMS_ROLES_CREATE)) { throw new Scalr_Exception_InsufficientPermissions(); } $this->request->defineParams(array('roleId' => array('type' => 'int'), 'farmId' => array('type' => 'int'), 'farmRoleId' => array('type' => 'int'), 'serverId', 'hideTerminated' => array('type' => 'bool'), 'sort' => array('type' => 'json'))); $sql = 'SELECT servers.*, farms.name AS farm_name, roles.name AS role_name, farm_roles.alias AS role_alias FROM servers LEFT JOIN farms ON servers.farm_id = farms.id LEFT JOIN roles ON roles.id = servers.role_id LEFT JOIN farm_roles ON farm_roles.id = servers.farm_roleid WHERE servers.env_id = ? AND :FILTER:'; $args = array($this->getEnvironmentId()); if ($this->getParam('cloudServerId')) { $sql = str_replace('WHERE', 'LEFT JOIN server_properties ON servers.server_id = server_properties.server_id WHERE', $sql); $sql .= ' AND ('; $sql .= 'server_properties.name = ? AND server_properties.value = ?'; $args[] = CLOUDSTACK_SERVER_PROPERTIES::SERVER_ID; $args[] = $this->getParam('cloudServerId'); $sql .= ' OR server_properties.name = ? AND server_properties.value = ?'; $args[] = EC2_SERVER_PROPERTIES::INSTANCE_ID; $args[] = $this->getParam('cloudServerId'); $sql .= ' OR server_properties.name = ? AND server_properties.value = ?'; $args[] = EUCA_SERVER_PROPERTIES::INSTANCE_ID; $args[] = $this->getParam('cloudServerId'); $sql .= ' OR server_properties.name = ? AND server_properties.value = ?'; $args[] = GCE_SERVER_PROPERTIES::SERVER_ID; $args[] = $this->getParam('cloudServerId'); $sql .= ' OR server_properties.name = ? AND server_properties.value = ?'; $args[] = OPENSTACK_SERVER_PROPERTIES::SERVER_ID; $args[] = $this->getParam('cloudServerId'); $sql .= ' OR server_properties.name = ? AND server_properties.value = ?'; $args[] = RACKSPACE_SERVER_PROPERTIES::SERVER_ID; $args[] = $this->getParam('cloudServerId'); $sql .= ')'; } if ($this->getParam('cloudServerLocation')) { if (!strstr($sql, 'LEFT JOIN server_properties ON servers.server_id = server_properties.server_id')) { $sql = str_replace('WHERE', 'LEFT JOIN server_properties ON servers.server_id = server_properties.server_id WHERE', $sql); } $sql .= ' AND ('; $sql .= 'server_properties.name = ? AND server_properties.value = ?'; $args[] = CLOUDSTACK_SERVER_PROPERTIES::CLOUD_LOCATION; $args[] = $this->getParam('cloudServerLocation'); $sql .= ' OR server_properties.name = ? AND server_properties.value = ?'; $args[] = EC2_SERVER_PROPERTIES::REGION; $args[] = $this->getParam('cloudServerLocation'); $sql .= ' OR server_properties.name = ? AND server_properties.value = ?'; $args[] = EUCA_SERVER_PROPERTIES::REGION; $args[] = $this->getParam('cloudServerLocation'); $sql .= ' OR server_properties.name = ? AND server_properties.value = ?'; $args[] = GCE_SERVER_PROPERTIES::CLOUD_LOCATION; $args[] = $this->getParam('cloudServerLocation'); $sql .= ' OR server_properties.name = ? AND server_properties.value = ?'; $args[] = OPENSTACK_SERVER_PROPERTIES::CLOUD_LOCATION; $args[] = $this->getParam('cloudServerLocation'); $sql .= ' OR server_properties.name = ? AND server_properties.value = ?'; $args[] = RACKSPACE_SERVER_PROPERTIES::DATACENTER; $args[] = $this->getParam('cloudServerLocation'); $sql .= ')'; } if ($this->getParam('farmId')) { $sql .= " AND farm_id=?"; $args[] = $this->getParam('farmId'); } if ($this->request->isAllowed(Acl::RESOURCE_FARMS_SERVERS)) { if (!$this->request->isAllowed(Acl::RESOURCE_FARMS, Acl::PERM_FARMS_NOT_OWNED_FARMS)) { $sql .= " AND (farms.created_by_id = ? OR servers.status IN (?, ?) AND farms.id IS NULL)"; $args[] = $this->user->getId(); $args[] = SERVER_STATUS::IMPORTING; $args[] = SERVER_STATUS::TEMPORARY; } } else { //show servers related to role creation process only $sql .= ' AND servers.status IN (?, ?)'; $args[] = SERVER_STATUS::IMPORTING; $args[] = SERVER_STATUS::TEMPORARY; } if ($this->getParam('farmRoleId')) { $sql .= " AND farm_roleid=?"; $args[] = $this->getParam('farmRoleId'); } if ($this->getParam('roleId')) { $sql .= " AND role_id=?"; $args[] = $this->getParam('roleId'); } if ($this->getParam('serverId')) { $sql .= " AND server_id=?"; $args[] = $this->getParam('serverId'); } if ($this->getParam('hideTerminated')) { $sql .= ' AND servers.status != ?'; $args[] = SERVER_STATUS::TERMINATED; } $response = $this->buildResponseFromSql2($sql, array('platform', 'farm_name', 'role_name', 'role_alias', 'index', 'server_id', 'remote_ip', 'local_ip', 'uptime', 'status'), array('servers.server_id', 'farm_id', 'farms.name', 'remote_ip', 'local_ip', 'servers.status', 'farm_roles.alias'), $args); foreach ($response["data"] as &$row) { try { $dbServer = DBServer::LoadByID($row['server_id']); $row['cloud_server_id'] = $dbServer->GetCloudServerID(); if (in_array($dbServer->status, array(SERVER_STATUS::RUNNING, SERVER_STATUS::INIT))) { $row['cluster_role'] = ""; if ($dbServer->GetFarmRoleObject()->GetRoleObject()->getDbMsrBehavior() || $dbServer->GetFarmRoleObject()->GetRoleObject()->hasBehavior(ROLE_BEHAVIORS::MYSQL)) { $isMaster = $dbServer->GetProperty(SERVER_PROPERTIES::DB_MYSQL_MASTER) || $dbServer->GetProperty(Scalr_Db_Msr::REPLICATION_MASTER); $row['cluster_role'] = $isMaster ? 'Master' : 'Slave'; if ($isMaster && $dbServer->GetFarmRoleObject()->GetSetting(Scalr_Db_Msr::SLAVE_TO_MASTER) || $dbServer->GetFarmRoleObject()->GetSetting(DBFarmRole::SETTING_MYSQL_SLAVE_TO_MASTER)) { $row['cluster_role'] = 'Promoting'; } } } $row['cloud_location'] = $dbServer->GetCloudLocation(); if ($dbServer->platform == SERVER_PLATFORMS::EC2) { $loc = $dbServer->GetProperty(EC2_SERVER_PROPERTIES::AVAIL_ZONE); if ($loc && $loc != 'x-scalr-diff') { $row['cloud_location'] .= "/" . substr($loc, -1, 1); } } if ($dbServer->platform == SERVER_PLATFORMS::EC2) { $row['has_eip'] = $this->db->GetOne("SELECT id FROM elastic_ips WHERE server_id = ?", array($dbServer->serverId)); } if ($dbServer->GetFarmRoleObject()->GetRoleObject()->hasBehavior(ROLE_BEHAVIORS::MONGODB)) { $shardIndex = $dbServer->GetProperty(Scalr_Role_Behavior_MongoDB::SERVER_SHARD_INDEX); $replicaSetIndex = $dbServer->GetProperty(Scalr_Role_Behavior_MongoDB::SERVER_REPLICA_SET_INDEX); $row['cluster_position'] = "{$shardIndex}-{$replicaSetIndex}"; } } catch (Exception $e) { } $rebooting = $this->db->GetOne("SELECT value FROM server_properties WHERE server_id=? AND `name`=? LIMIT 1", array($row['server_id'], SERVER_PROPERTIES::REBOOTING)); if ($dbServer->status == SERVER_STATUS::RUNNING) { if ($rebooting) { $row['status'] = "Rebooting"; } $subStatus = $dbServer->GetProperty(SERVER_PROPERTIES::SUB_STATUS); if ($subStatus) { $row['status'] = ucfirst($subStatus); } } $row['is_locked'] = $dbServer->GetProperty(EC2_SERVER_PROPERTIES::IS_LOCKED) ? 1 : 0; $row['is_szr'] = $dbServer->IsSupported("0.5"); $row['initDetailsSupported'] = $dbServer->IsSupported("0.7.181"); if ($dbServer->GetProperty(SERVER_PROPERTIES::SZR_IS_INIT_FAILED) && in_array($dbServer->status, array(SERVER_STATUS::INIT, SERVER_STATUS::PENDING))) { $row['isInitFailed'] = 1; } $launchError = $dbServer->GetProperty(SERVER_PROPERTIES::LAUNCH_ERROR); if ($launchError) { $row['launch_error'] = "1"; } $serverAlerts = new Alerts($dbServer); $row['agent_version'] = $dbServer->GetProperty(SERVER_PROPERTIES::SZR_VESION); $row['agent_update_needed'] = $dbServer->IsSupported("0.7") && !$dbServer->IsSupported("0.7.189"); $row['agent_update_manual'] = !$dbServer->IsSupported("0.5"); $row['os_family'] = $dbServer->GetOsFamily(); $row['flavor'] = $dbServer->GetFlavor(); $row['alerts'] = $serverAlerts->getActiveAlertsCount(); if (!$row['flavor']) { $row['flavor'] = ''; } if ($dbServer->status == SERVER_STATUS::RUNNING) { $tm = (int) $dbServer->GetProperty(SERVER_PROPERTIES::INITIALIZED_TIME); if (!$tm) { $tm = (int) strtotime($row['dtadded']); } if ($tm > 0) { $row['uptime'] = Scalr_Util_DateTime::getHumanReadableTimeout(time() - $tm, false); } } else { $row['uptime'] = ''; } $r_dns = $this->db->GetOne("SELECT value FROM farm_role_settings WHERE farm_roleid=? AND `name`=? LIMIT 1", array($row['farm_roleid'], DBFarmRole::SETTING_EXCLUDE_FROM_DNS)); $row['excluded_from_dns'] = !$dbServer->GetProperty(SERVER_PROPERTIES::EXCLUDE_FROM_DNS) && !$r_dns ? false : true; } $this->response->data($response); }