Refactor code to add sudo prefix for certain commands in multiple files
This commit is contained in:
@@ -582,7 +582,7 @@ function validateComposeFile(string $compose, int $server_id): string|Throwable
|
||||
$server = Server::findOrFail($server_id);
|
||||
$base64_compose = base64_encode($compose);
|
||||
$output = instant_remote_process([
|
||||
"echo {$base64_compose} | base64 -d > /tmp/{$uuid}.yml",
|
||||
"echo {$base64_compose} | base64 -d | tee /tmp/{$uuid}.yml > /dev/null",
|
||||
"docker compose -f /tmp/{$uuid}.yml config",
|
||||
], $server);
|
||||
ray($output);
|
||||
|
||||
@@ -35,6 +35,7 @@ function remote_process(
|
||||
if ($server->isNonRoot()) {
|
||||
$command = parseCommandsByLineForSudo(collect($command), $server);
|
||||
}
|
||||
ray($command);
|
||||
$command_string = implode("\n", $command);
|
||||
if (auth()->user()) {
|
||||
$teams = auth()->user()->teams->pluck('id');
|
||||
|
||||
@@ -71,7 +71,7 @@ function getFilesystemVolumesFromServer(ServiceApplication|ServiceDatabase|Appli
|
||||
$dir = Str::of($fileLocation)->dirname();
|
||||
instant_remote_process([
|
||||
"mkdir -p $dir",
|
||||
"echo '$content' | base64 -d > $fileLocation"
|
||||
"echo '$content' | base64 -d | tee $fileLocation"
|
||||
], $server);
|
||||
} else if ($isFile == 'NOK' && $isDir == 'NOK' && $fileVolume->is_directory && $isInit) {
|
||||
$fileVolume->content = null;
|
||||
|
||||
@@ -1954,7 +1954,7 @@ function check_domain_usage(ServiceApplication|Application|null $resource = null
|
||||
function parseCommandsByLineForSudo(Collection $commands, Server $server): array
|
||||
{
|
||||
$commands = $commands->map(function ($line) {
|
||||
if (!str($line)->startSwith('cd') && !str($line)->startSwith('command')) {
|
||||
if (!str($line)->startsWith('cd') && !str($line)->startsWith('command') && !str($line)->startsWith('echo') && !str($line)->startsWith('true')) {
|
||||
return "sudo $line";
|
||||
}
|
||||
return $line;
|
||||
@@ -1966,16 +1966,20 @@ function parseCommandsByLineForSudo(Collection $commands, Server $server): array
|
||||
return $line;
|
||||
});
|
||||
$commands = $commands->map(function ($line) {
|
||||
if (str($line)->contains('$(') || str($line)->contains('`')) {
|
||||
return str($line)->replace('$(', '$(sudo ')->replace('`', '`sudo ')->value();
|
||||
$line = str($line);
|
||||
if (str($line)->contains('$(')) {
|
||||
$line = $line->replace('$(', '$(sudo ');
|
||||
}
|
||||
if (str($line)->contains('||')) {
|
||||
return str($line)->replace('||', '|| sudo ')->value();
|
||||
$line = $line->replace('||', '|| sudo');
|
||||
}
|
||||
if (str($line)->contains('&&')) {
|
||||
return str($line)->replace('&&', '&& sudo ')->value();
|
||||
$line = $line->replace('&&', '&& sudo');
|
||||
}
|
||||
return $line;
|
||||
if (str($line)->contains(' | ')) {
|
||||
$line = $line->replace(' | ', ' | sudo ');
|
||||
}
|
||||
return $line->value();
|
||||
});
|
||||
|
||||
return $commands->toArray();
|
||||
|
||||
Reference in New Issue
Block a user