Add Servers and PrivateKeys

Add new testhost
Remove privatekey injection from Dockerfile
This commit is contained in:
Andras Bacsai
2023-03-24 22:15:36 +01:00
parent 9e326d15b9
commit f57684b024
14 changed files with 116 additions and 38 deletions

View File

@@ -3,11 +3,12 @@
use App\Actions\RemoteProcess\DispatchRemoteProcess;
use App\Data\RemoteProcessArgs;
use App\Models\Server;
use Illuminate\Support\Facades\Storage;
use Spatie\Activitylog\Contracts\Activity;
if (!function_exists('remoteProcess')) {
/**
* Run a Coolify Process, which SSH's asynchronously into a machine to run the command(s).
* Run a Remote Process, which SSH's asynchronously into a machine to run the command(s).
* @TODO Change 'root' to 'coolify' when it's able to run Docker commands without sudo
*
*/
@@ -15,21 +16,36 @@ if (!function_exists('remoteProcess')) {
string $command,
string $destination
): Activity {
$found_server = Server::where('name', $destination)->first();
if (!$found_server) {
throw new \RuntimeException('Server not found.');
}
$found_team = auth()->user()->teams->pluck('id')->contains($found_server->team_id);
if (!$found_team) {
throw new \RuntimeException('You do not have access to this server.');
}
$found_server = checkServer($destination);
checkTeam($found_server->team_id);
$temp_file = 'id.rsa_'.'root'.'@'.$found_server->ip;
Storage::disk('local')->put($temp_file, $found_server->privateKeys->first()->private_key, 'private');
$private_key_location = '/var/www/html/storage/app/'.$temp_file;
return resolve(DispatchRemoteProcess::class, [
'remoteProcessArgs' => new RemoteProcessArgs(
destination: $found_server->ip,
private_key_location: $private_key_location,
command: $command,
port: $found_server->port,
user: $found_server->user,
),
])();
}
function checkServer(string $destination){
// @TODO: Use UUID instead of name
$found_server = Server::where('name', $destination)->first();
if (!$found_server) {
throw new \RuntimeException('Server not found.');
};
return $found_server;
}
function checkTeam(string $team_id){
$found_team = auth()->user()->teams->pluck('id')->contains($team_id);
if (!$found_team) {
throw new \RuntimeException('You do not have access to this server.');
}
}
}