laravel-multidomain icon indicating copy to clipboard operation
laravel-multidomain copied to clipboard

Feature wish: artisan --all-domains option

Open Scaenicus opened this issue 5 years ago • 11 comments

Maybe I haven't read the documentation enough, but is there a way to do some artisan task for all domains? Something like:

php artisan --all-domains cache:clear
php artisan --all-domains config:clear
php artisan --all-domains migrate --seed

If not I will probably develop a bash script which evaluates php artisan domain:list to repeat tasks for each domain.

Scaenicus avatar Dec 12 '19 10:12 Scaenicus

Hi,

to my knowledge (and due to the same reasons pointed out here ) there is no a safe and general way to use artisan for a command operating on all the domains. Maybe I'm wrong but I spent a lot of time on this topic.

I understand very well that it would be a useful addition, but the only way I know to do this is via a bash script. Indeed, in my head, it is a planned feature to add a quite general bash script for calling artisan on all domains or on a subset of them. I'm not very expert of shell scripting but If you have done such a script and you want to contribute, you are welcome!

Cheers,

Giacomo

gecche avatar Dec 12 '19 11:12 gecche

Thank you Giacomo! Good. I will develop and upload my bash-solution when finished. With kind regards, Philipp

Scaenicus avatar Dec 12 '19 11:12 Scaenicus

HI @Scaenicus, do you have any news about that script?

Epizefiri avatar Jan 23 '21 11:01 Epizefiri

Since this is technically impossible with laravel command, I coded this script to run through shell.

Usage

  • Place the script in projects root with name tenant
  • Run the script file like: php tenant config:clear The script cycle through all domains and run command. I could add this to package if @gecche wants.
#!/usr/bin/env php
<?php

$config = require __DIR__ . "/config/domain.php";
$domains = array_keys($config['domains']);

echo "\033[32m Run one command against all domains.\033[39m" . PHP_EOL;

if ($argv[1] ?? null) {
    unset($argv[0]);
    $command = implode(' ', $argv);
    echo "Your command is $command" . PHP_EOL;
    $input = confirm('How run command? a:All s:selective c:Cancel ');
    if ($input === 'c') exit(0);
    foreach ($domains as $index => $domain) {
        if ($input === 'a' or (confirm("\033[96m Run against $domain ? Y/N \033[39m ") === 'y')) {
            echo "\033[32m php artisan $command --domain=$domain \033[39m " . PHP_EOL;
            echo shell_exec("yes | php artisan $command --domain=$domain");
        }
        echo PHP_EOL;
    }
} else {
    echo "\033[31m No command found! usage: php tenant {command}" . PHP_EOL;
}

function confirm($question)
{
    echo $question . PHP_EOL;
    return strtolower(rtrim(fgets(STDIN)));
}

SadeghPM avatar Jan 23 '21 18:01 SadeghPM

@SadeghPM thanks, you made my day!

Epizefiri avatar Jan 24 '21 11:01 Epizefiri

Thank you very much @SadeghPM and sorry for my delay... I will check it and include it in next version of the package (very soon)

Thanks again

Giacomo

gecche avatar Feb 11 '21 16:02 gecche

HI @Scaenicus, do you have any news about that script?

I'm sorry for not responding. Was a hard year and we never finished a general-purpose script in a sharable quality like @SadeghPM (thank you too!)

Scaenicus avatar Feb 11 '21 17:02 Scaenicus

Hi @SadeghPM, I was including your script in my package: I think it is very good, thank you! :) Only one question: I've done some tests and it seems that if an artisan command asks something to the user before to run, the script ignores it. Is it so?

Cheers

Giacomo

gecche avatar Feb 13 '21 14:02 gecche

Hi @gecche Yeh, PHP has no native way of doing an interactive prompt for user.

SadeghPM avatar Feb 14 '21 06:02 SadeghPM

@SadeghPM i make some changes on your script

put still i need a way to run it from the master project to the subproject so I can clear the cache on run time

Usage

Place the script in projects root with name tenant
Run the script file like: php tenant config:clear
The script cycle through all domains and run command.
I could add this to package if @gecche wants.
#!/usr/bin/env php
<?php

$config = require __DIR__ . "/config/domain.php";

$domains = array_keys($config['domains']);

if ($argv[1] ?? null) {

    $command = $argv[1] ?? '';

    unset($argv[0]);
    unset($argv[1]);

    if (!$command || !commandExists($command)) {
        echo "invalid command!";
        echo PHP_EOL;
        exit(0);
    }

    foreach ($domains as $index => $domain) {

        $argv['domain'] = "--domain=$domain";

        $args = trim(implode(' ', $argv));

        echo "php artisan $command $args";
        echo PHP_EOL;
        echo shell_exec("php artisan $command $args");
    }
}

function commandExists($name): bool
{
    $output = shell_exec("php artisan list");

    $commands = [];
    foreach (explode(PHP_EOL, $output) as $line) {
        $command = explode(' ', trim($line))[0] ?? '';

        if($command && strrpos($command, "-") === false && !in_array($command, ['Laravel', 'Usage:', 'command', 'Options:', 'Available'])) {
            $commands[] = $command;
        }
    }

    return in_array($name, $commands);
}

Qanah avatar Mar 26 '21 13:03 Qanah