/**
* @param int $roleId
* @param string $catId
* @param string $osFamily
* @param string $keyword
* @param string $platform
*
* Get list of roles for roles library
*/
public function xGetListAction($roleId = null, $catId = null, $osFamily = null, $keyword = null, $platform = null)
{
$this->request->restrictFarmDesignerAccess();
$total = 0;
$roles = array();
$ec2Locations = array_keys(PlatformFactory::NewPlatform(SERVER_PLATFORMS::EC2)->getLocations($this->environment));
$enabledPlatforms = $this->getEnvironment()->getEnabledPlatforms();
$enabledServerPlatforms = array();
$serverPlatforms = SERVER_PLATFORMS::GetList();
foreach ($enabledPlatforms as $enabledPlatform) {
$enabledServerPlatforms[$enabledPlatform] = $serverPlatforms[$enabledPlatform];
}
if ($platform && empty($enabledServerPlatforms[$platform])) {
throw new Exception("Selected cloud not enabled in current environment");
}
$globalVars = new Scalr_Scripting_GlobalVariables($this->user->getAccountId(), $this->getEnvironmentId(), ScopeInterface::SCOPE_FARMROLE);
$rolesSql = "\n SELECT DISTINCT r.*, rc.name cat_name\n FROM roles r\n INNER JOIN role_images ri ON ri.role_id = r.id\n INNER JOIN os ON os.id = r.os_id\n LEFT JOIN role_environments re ON re.role_id = r.id\n LEFT JOIN role_categories rc ON rc.id = r.cat_id\n WHERE r.is_deprecated = '0'\n AND os.status = ?\n AND (r.client_id IS NULL OR r.env_id = ? OR r.client_id = ? AND r.env_id IS NULL AND (re.env_id IS NULL OR re.env_id = ?))\n ";
$args = [Os::STATUS_ACTIVE, $this->getEnvironmentId(), $this->user->getAccountId(), $this->getEnvironmentId()];
if ($platform) {
$rolesSql .= " AND ri.platform = ?";
$args[] = $platform;
} else {
$rolesSql .= " AND ri.platform IN ('" . implode("','", array_keys($enabledServerPlatforms)) . "')";
}
if ($osFamily) {
$rolesSql .= " AND os.family = ?";
$args[] = $osFamily;
}
if (in_array(SERVER_PLATFORMS::EC2, $enabledPlatforms)) {
if (empty($ec2Locations)) {
throw new Exception('EC2 locations list is empty');
}
$rolesSql .= " AND (ri.platform != ? OR ri.cloud_location IN (" . rtrim(str_repeat("?,", count($ec2Locations)), ',') . "))";
$args[] = SERVER_PLATFORMS::EC2;
$args = array_merge($args, $ec2Locations);
}
if ($catId === 'shared') {
$rolesSql .= " AND r.is_quick_start = '1'";
$software = [];
$softwareOrdering = ['base' => 0, 'mysql' => 10, 'percona' => 20, 'mariadb' => 10, 'postgresql' => 30, 'mongodb' => 40, 'redis' => 50, 'apache' => 60, 'lamp' => 70, 'tomcat' => 80, 'haproxy' => 90, 'nginx' => 100, 'memcached' => 110, 'rabbitmq' => 120, 'vpcrouter' => 130];
} else {
$rolesSql .= " AND r.generation = '2'";
if ($catId === 'search') {
$rolesSql .= ' AND r.name LIKE ' . $this->db->qstr('%' . trim($keyword) . '%');
if ($roleId) {
$rolesSql .= ' AND r.id = ?';
$args[] = $roleId;
}
} elseif ($catId === 'recent') {
} elseif ($catId) {
$rolesSql .= ' AND r.cat_id = ?';
$args[] = $catId;
}
$rolesSql .= ' GROUP BY r.id';
if ($catId === 'recent') {
$rolesSql .= ' ORDER BY r.id DESC LIMIT 10';
}
}
$rolesData = $this->db->Execute($rolesSql, $args);
foreach ($rolesData as $roleData) {
$role = new Role();
$role->load($roleData);
$images = [];
foreach ($role->fetchImagesArray() as $imagePlatform => $image) {
if ($this->getEnvironment()->isPlatformEnabled($imagePlatform)) {
if ($imagePlatform == SERVER_PLATFORMS::EC2) {
foreach ($image as $cloudlocation => $img) {
if (in_array($cloudlocation, $ec2Locations)) {
$images[$imagePlatform][$cloudlocation] = $img;
}
}
} else {
$images[$imagePlatform] = $image;
}
}
}
if (!empty($images)) {
$item = ['role_id' => $role->id, 'name' => $role->name, 'behaviors' => $role->getBehaviors(), 'origin' => $role->origin, 'cat_id' => $role->catId, 'osId' => $role->osId, 'description' => $role->description, 'images' => $images, 'scope' => $role->accountId ? $role->envId ? ScopeInterface::SCOPE_ENVIRONMENT : ScopeInterface::SCOPE_ACCOUNT : ScopeInterface::SCOPE_SCALR, 'variables' => $globalVars->getValues($role->id), 'cat_name' => $roleData['cat_name']];
if ($catId === 'shared') {
// Get type
if ($role->hasBehavior(ROLE_BEHAVIORS::VPC_ROUTER)) {
$type = 'vpcrouter';
} elseif ($role->hasBehavior(ROLE_BEHAVIORS::APACHE) && ($role->hasBehavior(ROLE_BEHAVIORS::MYSQL2) || $role->hasBehavior(ROLE_BEHAVIORS::MYSQL) || $role->hasBehavior(ROLE_BEHAVIORS::PERCONA) || $role->hasBehavior(ROLE_BEHAVIORS::MARIADB))) {
$type = 'lamp';
} elseif ($role->hasBehavior(ROLE_BEHAVIORS::MYSQL2)) {
$type = 'mysql';
} elseif ($role->hasBehavior(ROLE_BEHAVIORS::APACHE)) {
$type = 'apache';
} elseif ($role->hasBehavior(ROLE_BEHAVIORS::TOMCAT)) {
$type = ROLE_BEHAVIORS::TOMCAT;
} elseif ($role->hasBehavior(ROLE_BEHAVIORS::NGINX)) {
$type = 'nginx';
} elseif ($role->hasBehavior(ROLE_BEHAVIORS::HAPROXY)) {
$type = 'haproxy';
} elseif ($role->getDbMsrBehavior()) {
$type = $role->getDbMsrBehavior();
} elseif ($role->hasBehavior(ROLE_BEHAVIORS::MONGODB)) {
$type = ROLE_BEHAVIORS::MONGODB;
} elseif ($role->hasBehavior(ROLE_BEHAVIORS::RABBITMQ)) {
$type = ROLE_BEHAVIORS::RABBITMQ;
} elseif ($role->hasBehavior(ROLE_BEHAVIORS::MEMCACHED)) {
$type = ROLE_BEHAVIORS::MEMCACHED;
} elseif ($role->hasBehavior(ROLE_BEHAVIORS::BASE)) {
$type = ROLE_BEHAVIORS::BASE;
} elseif ($role->hasBehavior(ROLE_BEHAVIORS::MYSQL)) {
continue;
}
$item['isScalarized'] = 1;
$software[$type]['roles'][] = $item;
$software[$type]['name'] = $type;
$software[$type]['ordering'] = isset($softwareOrdering[$type]) ? $softwareOrdering[$type] : 1000;
} else {
$item['isQuickStart'] = $role->isQuickStart ? '1' : '0';
$item['isDeprecated'] = $role->isDeprecated ? '1' : '0';
$item['isScalarized'] = $role->isScalarized ? '1' : '0';
$roles[] = $item;
}
$total++;
}
}
$this->response->data(['roles' => $roles, 'software' => isset($software) ? array_values($software) : [], 'total' => $total]);
}