/**
* Helper for the server lister
*
* @param array $response Reference to a response array
*/
private function listServersResponseHelper(&$response)
{
if (empty($response["data"])) {
return;
}
$serverIds = [];
$farmRoles = [];
$userBelongsToTeam = [];
foreach ($response["data"] as $idx => $row) {
$serverIds[$row["server_id"]][$idx] = [];
$farmRoles[$row["farm_roleid"]][$idx] = [];
}
$neededServerProperties = [GCE_SERVER_PROPERTIES::SERVER_NAME => "cloud_server_id", OPENSTACK_SERVER_PROPERTIES::SERVER_ID => "cloud_server_id", CLOUDSTACK_SERVER_PROPERTIES::SERVER_ID => "cloud_server_id", EC2_SERVER_PROPERTIES::INSTANCE_ID => "cloud_server_id", AZURE_SERVER_PROPERTIES::SERVER_NAME => "cloud_server_id", Scalr_Role_Behavior::SERVER_BASE_HOSTNAME => "hostname", Entity\Server::DB_MYSQL_MASTER => "cluster_role", Scalr_Db_Msr::REPLICATION_MASTER => "cluster_role", EC2_SERVER_PROPERTIES::AVAIL_ZONE => "avail_zone", EC2_SERVER_PROPERTIES::REGION => "cloud_location", OPENSTACK_SERVER_PROPERTIES::CLOUD_LOCATION => "cloud_location", CLOUDSTACK_SERVER_PROPERTIES::CLOUD_LOCATION => "cloud_location", GCE_SERVER_PROPERTIES::CLOUD_LOCATION => "cloud_location", AZURE_SERVER_PROPERTIES::CLOUD_LOCATION => "cloud_location", Scalr_Role_Behavior_MongoDB::SERVER_SHARD_INDEX => "cluster_position", Scalr_Role_Behavior_MongoDB::SERVER_REPLICA_SET_INDEX => "cluster_position", Entity\Server::REBOOTING => "status", Entity\Server::MISSING => "status", EC2_SERVER_PROPERTIES::IS_LOCKED => "is_locked", Entity\Server::SZR_VESION => "szr_version", Entity\Server::SZR_IS_INIT_FAILED => "isInitFailed", Entity\Server::LAUNCH_ERROR => "launch_error", Entity\Server::EXCLUDE_FROM_DNS => "excluded_from_dns"];
$neededFarmRoleSettings = [Scalr_Db_Msr::SLAVE_TO_MASTER, Entity\FarmRoleSetting::MYSQL_SLAVE_TO_MASTER, Entity\FarmRoleSetting::EXCLUDE_FROM_DNS, Entity\FarmRoleSetting::SCALING_ENABLED];
// get necessary properties
foreach (Entity\Server\Property::fetch(array_keys($serverIds), array_keys($neededServerProperties)) as $prop) {
foreach ($serverIds[$prop->serverId] as $idx => &$upd) {
$upd[$prop->name] = $prop->value;
}
}
// get farm role settings
foreach (Entity\FarmRoleSetting::fetch(array_keys($farmRoles), $neededFarmRoleSettings) as $prop) {
foreach ($farmRoles[$prop->farmRoleId] as $idx => &$upd) {
$upd[$prop->name] = $prop->value;
}
}
// check elastic IP existence
foreach (Entity\Server\ElasticIp::checkPresenceOfPublicIP(array_keys($serverIds)) as $resRow) {
foreach ($serverIds[$resRow["server_id"]] as $idx => &$upd) {
$upd["extIp"] = $resRow["ipc"];
}
}
// check alerts
foreach (Entity\Server\Alert::checkPresenceOfAlerts(array_keys($serverIds)) as $resRow) {
foreach ($serverIds[$resRow["server_id"]] as $idx => &$upd) {
$upd["alerts"] = $resRow["alerts"];
}
}
foreach ($response["data"] as $idx => &$row) {
$status = $row["status"];
$behaviors = explode(",", $row["behaviors"]);
$row["hostname"] = $row['cluster_role'] = $row["alerts"] = "";
$cloudServerIds = [];
foreach ($this->propertyFilter($neededServerProperties, "cloud_server_id") as $prop => $key) {
if (array_key_exists($prop, $serverIds[$row["server_id"]][$idx])) {
$cloudServerIds[] = $serverIds[$row["server_id"]][$idx][$prop];
}
}
$row['cloud_server_id'] = empty($cloudServerIds) ? "" : $cloudServerIds[0];
if (array_key_exists(Scalr_Role_Behavior::SERVER_BASE_HOSTNAME, $serverIds[$row["server_id"]][$idx])) {
$row["hostname"] = $serverIds[$row["server_id"]][$idx][Scalr_Role_Behavior::SERVER_BASE_HOSTNAME];
}
$row['flavor'] = $row['type'];
if (in_array($status, [Entity\Server::STATUS_RUNNING, Entity\Server::STATUS_INIT])) {
$hasDbBehavior = array_intersect([ROLE_BEHAVIORS::REDIS, ROLE_BEHAVIORS::POSTGRESQL, ROLE_BEHAVIORS::MYSQL, ROLE_BEHAVIORS::MYSQL2, ROLE_BEHAVIORS::PERCONA, ROLE_BEHAVIORS::MARIADB], $behaviors);
if (!empty($hasDbBehavior)) {
$isMaster = false;
foreach ($this->propertyFilter($neededServerProperties, "cluster_role") as $prop => $key) {
if (array_key_exists($prop, $serverIds[$row["server_id"]][$idx]) && $serverIds[$row["server_id"]][$idx][$prop] != 0) {
$isMaster = true;
break;
}
}
$row['cluster_role'] = $isMaster ? 'Master' : 'Slave';
if ($isMaster && (array_key_exists(Scalr_Db_Msr::SLAVE_TO_MASTER, $farmRoles[$row["farm_roleid"]][$idx]) && $farmRoles[$row["farm_roleid"]][$idx][Scalr_Db_Msr::SLAVE_TO_MASTER] == 1) || array_key_exists(Entity\FarmRoleSetting::MYSQL_SLAVE_TO_MASTER, $farmRoles[$row["farm_roleid"]][$idx]) && $farmRoles[$row["farm_roleid"]][$idx][Entity\FarmRoleSetting::MYSQL_SLAVE_TO_MASTER] == 1) {
$row['cluster_role'] = 'Promoting';
}
}
$row['suspendHidden'] = $this->hasDatabaseBehavior($behaviors) || in_array(ROLE_BEHAVIORS::RABBITMQ, $behaviors);
/* @var $image Image */
$row['suspendEc2Locked'] = $row['platform'] == SERVER_PLATFORMS::EC2 && ($image = Image::findOne([['platform' => $row['platform']], ['cloudLocation' => $row['cloud_location']], ['id' => $row['image_id']], ['$or' => [['accountId' => null], ['accountId' => $row['client_id']]]], ['$or' => [['envId' => null], ['envId' => $row['env_id']]]]])) && $image->isEc2InstanceStoreImage();
}
$cloudLocations = [];
foreach ($this->propertyFilter($neededServerProperties, "cloud_location") as $prop => $key) {
if (array_key_exists($prop, $serverIds[$row["server_id"]][$idx])) {
$cloudLocations[] = $serverIds[$row["server_id"]][$idx][$prop];
}
}
$row['cloud_location'] = empty($cloudLocations) ? null : $cloudLocations[0];
if ($row["platform"] === SERVER_PLATFORMS::EC2 && array_key_exists(EC2_SERVER_PROPERTIES::AVAIL_ZONE, $serverIds[$row["server_id"]][$idx])) {
$loc = $serverIds[$row["server_id"]][$idx][EC2_SERVER_PROPERTIES::AVAIL_ZONE];
if ($loc && $loc != 'x-scalr-diff') {
$row['cloud_location'] .= "/" . substr($loc, -1, 1);
}
$row['has_eip'] = array_key_exists("extIp", $serverIds[$row["server_id"]][$idx]) && $serverIds[$row["server_id"]][$idx]["extIp"] > 0;
}
if (in_array(ROLE_BEHAVIORS::MONGODB, $behaviors)) {
$shardIndex = $serverIds[$row["server_id"]][$idx][Scalr_Role_Behavior_MongoDB::SERVER_SHARD_INDEX];
$replicaSetIndex = $serverIds[$row["server_id"]][$idx][Scalr_Role_Behavior_MongoDB::SERVER_REPLICA_SET_INDEX];
$row['cluster_position'] = $shardIndex . "-" . $replicaSetIndex;
}
if (in_array($status, [Entity\Server::STATUS_RUNNING, Entity\Server::STATUS_SUSPENDED])) {
if (array_key_exists(Entity\Server::REBOOTING, $serverIds[$row["server_id"]][$idx]) && $serverIds[$row["server_id"]][$idx][Entity\Server::REBOOTING] != 0) {
$row["status"] = "Rebooting";
}
if (array_key_exists(Entity\Server::MISSING, $serverIds[$row["server_id"]][$idx]) && $serverIds[$row["server_id"]][$idx][Entity\Server::MISSING] != 0) {
$row["status"] = "Missing";
}
}
$row['agent_version'] = $serverIds[$row["server_id"]][$idx][Entity\Server::SZR_VESION];
$agentVersion = Entity\Server::versionInfo($row['agent_version']);
$row['is_locked'] = array_key_exists(EC2_SERVER_PROPERTIES::IS_LOCKED, $serverIds[$row["server_id"]][$idx]) && $serverIds[$row["server_id"]][$idx][EC2_SERVER_PROPERTIES::IS_LOCKED] != 0 ? 1 : 0;
$row['is_szr'] = $agentVersion >= Entity\Server::versionInfo("0.5");
if (array_key_exists(Entity\Server::SZR_IS_INIT_FAILED, $serverIds[$row["server_id"]][$idx]) && $serverIds[$row["server_id"]][$idx][Entity\Server::SZR_IS_INIT_FAILED] == 1 && in_array($status, [Entity\Server::STATUS_INIT, Entity\Server::STATUS_PENDING])) {
$row['isInitFailed'] = 1;
}
if (array_key_exists(Entity\Server::LAUNCH_ERROR, $serverIds[$row["server_id"]][$idx]) && strlen($serverIds[$row["server_id"]][$idx][Entity\Server::LAUNCH_ERROR]) > 0) {
$row['launch_error'] = "1";
}
$row['isScalarized'] = $row["is_scalarized"];
$row['agent_update_needed'] = $agentVersion >= Entity\Server::versionInfo("0.7") && $agentVersion < Entity\Server::versionInfo("0.7.189");
$row['agent_update_manual'] = $agentVersion < Entity\Server::versionInfo("0.5");
$row['os_family'] = $row["os_type"];
$flavors = [];
foreach ($this->propertyFilter($neededServerProperties, "flavor") as $prop => $key) {
if (array_key_exists($prop, $serverIds[$row["server_id"]][$idx])) {
$flavors[] = $serverIds[$row["server_id"]][$idx][$prop];
}
}
$row["flavor"] = empty($flavors) ? "" : $flavors[0];
if (array_key_exists("alerts", $serverIds[$row["server_id"]][$idx])) {
$row["alerts"] = $serverIds[$row["server_id"]][$idx]["alerts"];
}
if ($status === Entity\Server::STATUS_RUNNING) {
$row['uptime'] = \Scalr_Util_DateTime::getHumanReadableTimeout(time() - strtotime($row['uptime']), false);
} else {
$row['uptime'] = '';
}
$row['excluded_from_dns'] = !(!(array_key_exists(Entity\Server::EXCLUDE_FROM_DNS, $serverIds[$row["server_id"]][$idx]) && $serverIds[$row["server_id"]][$idx][Entity\Server::EXCLUDE_FROM_DNS] == 1) && !(array_key_exists(Entity\FarmRoleSetting::EXCLUDE_FROM_DNS, $farmRoles[$row["farm_roleid"]][$idx]) && $farmRoles[$row["farm_roleid"]][$idx][Entity\FarmRoleSetting::EXCLUDE_FROM_DNS] == 1));
$row['scalingEnabled'] = array_key_exists(Entity\FarmRoleSetting::SCALING_ENABLED, $farmRoles[$row["farm_roleid"]][$idx]) && $farmRoles[$row["farm_roleid"]][$idx][Entity\FarmRoleSetting::SCALING_ENABLED] == 1;
$row['farmOwnerIdPerm'] = $row['farmOwnerId'] && $this->user->getId() == $row['farmOwnerId'];
$row['farmTeamIdPerm'] = !!$row['farmTeamIdPerm'];
}
}