214 lines
7.3 KiB
PHP
214 lines
7.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Jobs\Hosting\CreateHostingJobs;
|
|
use App\Jobs\VirtualMachine\Action\RemoveVirtualMachine;
|
|
use App\Jobs\VirtualMachine\Action\VMJobs;
|
|
use App\Jobs\VirtualMachine\Create\CreateVMJobs;
|
|
use App\Models\Log;
|
|
use App\Repositories\Module\HostingServer\HostingServerRepositoryInterface;
|
|
use App\Repositories\Module\IpAddress\IpAddressRepositoryInterface;
|
|
use App\Repositories\Module\Vmware\VmwareRepositoryInterface;
|
|
use Exception;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
|
|
class TaskController extends Controller
|
|
{
|
|
public function __construct(
|
|
public HostingServerRepositoryInterface $moduleHostingServerRepository,
|
|
public IpAddressRepositoryInterface $ipAddressRepository,
|
|
public VmwareRepositoryInterface $vmwareRepository,
|
|
)
|
|
{
|
|
|
|
}
|
|
|
|
public function Task(Request $request)
|
|
{
|
|
|
|
$validator = Validator::make($request->all(), [
|
|
'userid' => 'numeric|required',
|
|
'type' => 'numeric|required',
|
|
'product' => 'numeric|required',
|
|
'action' => 'required'
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return back()->withErrors($validator);
|
|
}
|
|
$jobs = null;
|
|
|
|
try {
|
|
DB::beginTransaction();
|
|
switch ($request->type) {
|
|
case 3:
|
|
$validate = Validator::make(
|
|
$request->all(),
|
|
[
|
|
'domain' => 'required'
|
|
],
|
|
[
|
|
'domain' => ['required' => 'Please enter your domain name before creating Hosting!']
|
|
]
|
|
);
|
|
if ($validate->fails()) {
|
|
return back()->withErrors($validate);
|
|
}
|
|
$regexFilterDomain = '/(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]/m';
|
|
|
|
$filter = filter_var(
|
|
$request->domain,
|
|
FILTER_VALIDATE_REGEXP,
|
|
[
|
|
'options' => [
|
|
'regexp' => $regexFilterDomain
|
|
]
|
|
]
|
|
);
|
|
|
|
if ($filter === false) {
|
|
return back()->withErrors("The domain name is not in the correct format");
|
|
}
|
|
|
|
$server = $this->moduleHostingServerRepository->get_active();
|
|
|
|
if (!$server) {
|
|
return back()->withErrors('Invalid request.');
|
|
}
|
|
|
|
$this->moduleHostingServerRepository->update($request->product,['name' => $request->domain, 'module_server_id' => $server->first()->id]);
|
|
|
|
$jobs = new CreateHostingJobs($request->product);
|
|
|
|
break;
|
|
case 4:
|
|
$validate = Validator::make($request->all(), [
|
|
'os_group_id' => 'numeric|required|not__in:0',
|
|
'os_version' => 'numeric|required|not__in:0'
|
|
], [
|
|
'os_group_id.required' => 'Please choose Os',
|
|
'os_version.required' => 'Please choose Os Version'
|
|
]);
|
|
if ($validate->fails()) {
|
|
return back()->withErrors($validate);
|
|
}
|
|
$data = [];
|
|
|
|
if ($request->os_version) {
|
|
$data['os_id'] = $request->os_version;
|
|
}
|
|
$ipaddress = $this->ipAddressRepository->findBy('module_host_id',$request->module_host_id);
|
|
if ($ipaddress) {
|
|
$data['module_ipaddress_id'] = $ipaddress->id;
|
|
|
|
$this->ipAddressRepository->update($ipaddress->id,[
|
|
'status' => 1
|
|
]);
|
|
}
|
|
$this->vmwareRepository->update($request->product,$data);
|
|
$jobs = new CreateVMJobs($request->product);
|
|
}
|
|
DB::commit();
|
|
} catch (Exception $error) {
|
|
DB::rollBack();
|
|
return back()->withErrors($error->getMessage());
|
|
}
|
|
|
|
if ($jobs) {
|
|
|
|
dispatch($jobs)->delay(now()->addSeconds(5));
|
|
|
|
return back()->with('success', 'The system has received the information. Please wait a second');
|
|
}
|
|
return back()->withErrors('Invalid request.');
|
|
}
|
|
|
|
public function Console(Request $request)
|
|
{
|
|
$validator = Validator::make(
|
|
$request->all(),
|
|
[
|
|
'product' => 'numeric|required|not__in:0',
|
|
'vmware-id' => 'string|required'
|
|
],
|
|
[
|
|
'product' => [
|
|
"required" => "Product can't empty"
|
|
],
|
|
'vmware-id' => [
|
|
"required" => "Product can't empty"
|
|
]
|
|
]
|
|
);
|
|
if ($validator->fails()) {
|
|
return back()->withErrors($validator);
|
|
}
|
|
$product = $this->vmwareRepository->findWithRelationships($request->product,['ipaddress.host.datacenter']);
|
|
|
|
if (! $product) {
|
|
return back()->withErrors("Can't find Product for ID #" . $request->product);
|
|
}
|
|
$title = 'View Console for VirtualMachine #' . $request->{'vmware-id'};
|
|
|
|
return view('admin.customer.console', compact('title', 'product'));
|
|
}
|
|
|
|
public function Action(Request $request)
|
|
{
|
|
$validator = Validator::make($request->all(), [
|
|
'product' => 'numeric|required',
|
|
'userid' => 'numeric|required',
|
|
'action' => 'required'
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return back()->withErrors($validator);
|
|
}
|
|
|
|
$jobs = new VMJobs($request->product, $request->action);
|
|
|
|
dispatch($jobs)->delay(now()->addSeconds(5));
|
|
|
|
Log::create([
|
|
'agent' => $request->userAgent(),
|
|
'client_ip' => $request->getClientIp(),
|
|
'username' => auth()->guard('admins')->user()->username,
|
|
'description' => auth()->guard('admins')->user()->username . ": $request->action for VirtualMachine $request->product",
|
|
'customer_id' => $request->userid
|
|
]);
|
|
|
|
return back()->with('success', 'The system has received the processing request, please wait a moment.');
|
|
}
|
|
|
|
public function Reinstall(Request $request)
|
|
{
|
|
|
|
$validator = Validator::make($request->all(), [
|
|
'userid' => 'numeric|required',
|
|
'type' => 'numeric|required',
|
|
'product' => 'numeric|required',
|
|
'action' => 'required'
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return back()->withErrors($validator);
|
|
}
|
|
|
|
$product = $this->vmwareRepository->find($request->product);
|
|
|
|
if (! $product) {
|
|
return back()->withErrors("Can't find Product");
|
|
}
|
|
$jobs = new RemoveVirtualMachine($product->id);
|
|
|
|
dispatch($jobs)->delay(now()->addSeconds(5));
|
|
|
|
return back()->with('success', 'The system has received the information. Please wait a second');
|
|
}
|
|
}
|