/** * Returns all servers registered on $clusterId * * @param Cluster|int $clusterId * * @return array */ protected static function _lookupClusterServers($clusterId) { $_cluster = $clusterId instanceof Cluster ? $clusterId : static::_lookupCluster($clusterId); /** @noinspection PhpUndefinedMethodInspection */ $_rows = DB::select(<<<MYSQL SELECT s.id, s.server_id_text, s.server_type_id, csa.cluster_id FROM cluster_server_asgn_t csa JOIN server_t s ON s.id = csa.server_id WHERE csa.cluster_id = :cluster_id MYSQL , [':cluster_id' => $_cluster->id]); // Organize by type $_servers = ['cluster' => $_cluster]; foreach (ServerTypes::getDefinedConstants() as $_name => $_value) { $_servers[$_value] = ['.id' => null, '.ids' => [], '.name' => $_name, 'data' => []]; } /** * @type Server $_server */ foreach ($_rows as $_server) { if (!isset($_servers[$_server->server_type_id])) { continue; } $_servers[$_server->server_type_id]['data'][$_server->server_id_text] = (array) $_server; $_servers[$_server->server_type_id]['.ids'][] = $_server->id; } // Set the single id for quick lookups foreach ($_servers as $_type => $_group) { if (null !== array_get($_group, '.id')) { continue; } if (null !== ($_list = array_get($_group, '.ids'))) { if (!empty($_list) && is_array($_list)) { $_servers[$_type]['.id'] = $_list[0]; continue; } } if (null !== ($_list = array_get($_group, 'data'))) { if (!empty($_list) && is_array($_list)) { foreach ($_list as $_item) { if (isset($_item['id'])) { $_servers[$_type['.id']] = $_item['id']; continue; } } } } } return $_servers; }
/** * @param array $servers * @param string $name * * @return mixed */ protected function extractServerIds(array $servers, $name = 'id') { $_list = ServerTypes::getDefinedConstants(true); $_types = array_flip($_list); foreach ($_list as $_typeId => $_typeName) { $_types[$_typeId] = false; if (null === ($_server = array_get($servers, $_typeId))) { continue; } if (null !== ($_id = array_get($_server, '.' . $name))) { $_types[$_typeId] = $_id; continue; } if (null !== ($_ids = array_get($_server, '.' . $name . 's'))) { if (is_array($_ids) && !empty($_ids)) { $_types[$_typeId] = $_ids[0]; continue; } } } return $_types; }
/** @inheritdoc */ protected function getOptions() { return array_merge(parent::getOptions(), [['server-type', 't', InputOption::VALUE_REQUIRED, 'The type of server: ' . implode(', ', ServerTypes::getDefinedConstants(true))], ['mount-id', 'm', InputOption::VALUE_REQUIRED, 'The id of the storage mount for this server'], ['host-name', 'a', InputOption::VALUE_REQUIRED, 'The host name of this server'], ['config', 'c', InputOption::VALUE_REQUIRED, 'JSON-encoded array of configuration data for this server']]); }