public function getView(Request $request, $id) { $server = Models\Server::select('servers.*', 'nodes.name as a_nodeName', 'users.email as a_ownerEmail', 'locations.long as a_locationName', 'services.name as a_serviceName', DB::raw('IFNULL(service_options.executable, services.executable) as a_serviceExecutable'), 'service_options.docker_image', 'service_options.name as a_servceOptionName', 'allocations.ip', 'allocations.port', 'allocations.ip_alias')->join('nodes', 'servers.node', '=', 'nodes.id')->join('users', 'servers.owner', '=', 'users.id')->join('locations', 'nodes.location', '=', 'locations.id')->join('services', 'servers.service', '=', 'services.id')->join('service_options', 'servers.option', '=', 'service_options.id')->join('allocations', 'servers.allocation', '=', 'allocations.id')->where('servers.id', $id)->first(); if (!$server) { return abort(404); } return view('admin.servers.view', ['server' => $server, 'assigned' => Models\Allocation::where('assigned_to', $id)->orderBy('ip', 'asc')->orderBy('port', 'asc')->get(), 'unassigned' => Models\Allocation::where('node', $server->node)->whereNull('assigned_to')->orderBy('ip', 'asc')->orderBy('port', 'asc')->get(), 'startup' => Models\ServiceVariables::select('service_variables.*', 'server_variables.variable_value as a_serverValue')->join('server_variables', 'server_variables.variable_id', '=', 'service_variables.id')->where('service_variables.option_id', $server->option)->where('server_variables.server_id', $server->id)->get(), 'databases' => Models\Database::select('databases.*', 'database_servers.host as a_host', 'database_servers.port as a_port')->where('server_id', $server->id)->join('database_servers', 'database_servers.id', '=', 'databases.db_server')->get(), 'db_servers' => Models\DatabaseServer::all()]); }
public function getIndex(Request $request) { return view('admin.databases.index', ['databases' => Models\Database::select('databases.*', 'database_servers.host as a_host', 'database_servers.port as a_port', 'servers.id as a_serverId', 'servers.name as a_serverName')->join('database_servers', 'database_servers.id', '=', 'databases.db_server')->join('servers', 'databases.server_id', '=', 'servers.id')->paginate(20), 'dbh' => Models\DatabaseServer::select('database_servers.*', 'nodes.name as a_linkedNode', DB::raw('(SELECT COUNT(*) FROM `databases` WHERE `databases`.`db_server` = database_servers.id) as c_databases'))->join('nodes', 'nodes.id', '=', 'database_servers.linked_node')->paginate(20)]); }
/** * Adds a new Database Server to the system. * @param array $data */ public function add(array $data) { $validator = Validator::make($data, ['name' => 'required|string|max:255', 'host' => 'required|ip|unique:database_servers,host', 'port' => 'required|numeric|between:1,65535', 'username' => 'required|string|max:32', 'password' => 'required|string', 'linked_node' => 'sometimes']); if ($validator->fails()) { throw new DisplayValidationException($validator->errors()); } DB::beginTransaction(); try { $capsule = new Capsule(); $capsule->addConnection(['driver' => 'mysql', 'host' => $data['host'], 'port' => $data['port'], 'database' => 'mysql', 'username' => $data['username'], 'password' => $data['password'], 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'options' => [\PDO::ATTR_TIMEOUT => 3]]); $capsule->setAsGlobal(); // Allows us to check that we can connect to things. Capsule::select('SELECT 1 FROM dual'); $dbh = new Models\DatabaseServer(); $dbh->fill(['name' => $data['name'], 'host' => $data['host'], 'port' => $data['port'], 'username' => $data['username'], 'password' => Crypt::encrypt($data['password']), 'max_databases' => NULL, 'linked_node' => !empty($data['linked_node']) && $data['linked_node'] > 0 ? $data['linked_node'] : NULL]); $dbh->save(); DB::commit(); } catch (\Exception $ex) { DB::rollBack(); throw $ex; } }