Blog Detail

05

Dec
Execute and Manage Artisan Commands Remotely in Laravel cover image

arrow_back Execute and Manage Artisan Commands Remotely in Laravel

Remotisan is an excellent package that allows you to execute artisan commands remotely, using HTTP, and receiving propagating output on the page. Your command execution won’t run into the server’s MAX_EXECUTION_TIME, allowing you to preserve the original server configuration.

In general, the package could assist transitioning your project to CI/CD with auto-scaling, when supporters have no direct access to the server terminal.

Installation

Use composer to install Remotisan to your Laravel project. php7.4+ is required.

composer require paymeservice/remotisan

You can publish the config file with:

php artisan vendor:publish --tag="remotisan-config"

Optionally, you can publish the views using. The views will be published into /resources/views/vendor/remotisan/directory for your further adjustments.

php artisan vendor:publish --tag="remotisan-views"

Configuration

  • Remotisan allows you to customize default routes prefix, by adjusting base_url_prefix setting, do not forget to clear cached routes afterward.
  • Add any command you wish to be exposed to Remotisan in the config, by adjusting the following part.

Note: UserRoles class is NOT provided, for demonstration purposes only!

[
    "commands" =>   [
        "allowed" => [ // command level ACL.
            "COMMAND_NAME"            => ["roles" => [UserRoles::TECH_SUPPORT]],
            "COMMAND_FOR_DEVOPS_ONLY" => ["roles" => [UserRoles::DEV_OPS]],
            "COMMAND_SHARED"          => ["roles" => [UserRoles::TECH_SUPPORT, UserRoles::DEV_OPS]]
        ]
    ]
]

Use roles to define who is allowed to execute the command.

Setting ENV specific commands

You are able to configure environment-specific commands by simply static json string in your .env file with the name REMOTISAN_ALLOWED_COMMANDS.

REMOTISAN_ALLOWED_COMMANDS='{"artisanCommandName":{"roles":[]}, "artisanSecondCommand":{"roles":[]}}'

Authentication

Inside your AppServiceProvider::boot() add calls to \Remotisan::authWith($role, $callable).

Callable receives a \Illuminate\Http\Request instance and should return true if the request (probably by the user) matches the given role.

The roles MUST be matching to the roles you’ve defined in Remotisan config.

\Remotisan::authWith(UserRoles::TECH_SUPPORT, function(\Illuminate\Http\Request $request) {
    $user = $request->user('web');
    return $user && $user->isAllowed(UserPermissions::TECH_SUPPORT);
});

\Remotisan::authWith(UserRoles::DEV_OPS, function(\Illuminate\Http\Request $request) {
    $user = $request->user('web');
    return $user && $user->isAllowed(UserPermissions::DEV_OPS);
});

For more details, please visit Github.

Published at : 05-12-2022

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project