Skip to content

Commit

Permalink
Merge pull request #1 from xbugster/config-publish-ability
Browse files Browse the repository at this point in the history
Release candidate 1.0.0
  • Loading branch information
o-kima authored Nov 20, 2022
2 parents 05400db + 049c62a commit cb421cb
Show file tree
Hide file tree
Showing 12 changed files with 415 additions and 149 deletions.
85 changes: 71 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
# Executing Artisan commands interactively
# Remote Execution of Artisan commands

[![Latest Version on Packagist](https://img.shields.io/packagist/v/paymeservice/remotisan.svg?style=flat-square)](https://packagist.org/packages/paymeservice/remotisan)
[![GitHub Tests Action Status](https://img.shields.io/github/workflow/status/paymeservice/remotisan/run-tests?label=tests)](https://github.com/paymeservice/remotisan/actions?query=workflow%3Arun-tests+branch%3Amain)
[![GitHub Code Style Action Status](https://img.shields.io/github/workflow/status/paymeservice/remotisan/Fix%20PHP%20code%20style%20issues?label=code%20style)](https://github.com/paymeservice/remotisan/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain)
[![Total Downloads](https://img.shields.io/packagist/dt/paymeservice/remotisan.svg?style=flat-square)](https://packagist.org/packages/paymeservice/remotisan)

This is where your description should go. Limit it to a paragraph or two. Consider adding a small example.
The package allows you to execute artisan commands remotely, using HTTP, and receiving propagating output on the page.

## Support us
**Your command execution won't run into server's MAX_EXECUTION_TIME**, allowing you to preserve original server configuration.

[<img src="https://github-ads.s3.eu-central-1.amazonaws.com/remotisan.jpg?t=1" width="419px" />](https://spatie.be/github-ad-click/remotisan)
The basic view is included in the package, as well as the basic config (make sure you publish config).

We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).
In general, the package could very well assist transitioning your project to CI/CD with auto-scaling, whenever you don't really know which boxes you have to connect to in order to perform specific command you want.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).
As well, if you disconnected in process of command execution, but you got the URL. When you get back online, the page refreshes and will STILL bring you the log of the execution of your command. This way, you are not losing track.

## Installation

Expand All @@ -23,25 +23,82 @@ You can install the package via composer:
composer require paymeservice/remotisan
```

You can publish and run the migrations with:

```bash
php artisan vendor:publish --tag="remotisan-migrations"
php artisan migrate
```

You can publish the config file with:

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

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

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

## Configuration

After publishing config, find your remotisan's configuration file in <PROJECT_DIR>/config/remotisan.php

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

Note: UserRoles class is NOT provided, for demonstration purpose only!
```php
[
"allowance_rules" => [
"roles" => [
UserRoles::TECH_SUPPORT,
UserRoles::DEV_OPS
]
],
"commands" =>
"allowed" => [ // command level ACL.
"COMMAND_NAME" => [
UserRoles::TECH_SUPPORT,
],
"COMMAND_FOR_DEVOPS_ONLY" => [
UserRoles::DEV_OPS
],
"COMMAND_SHARED" => [
UserRoles::TECH_SUPPORT,
UserRoles::DEV_OPS
]
]
]
```

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

## Authentication
In the AppServiceProvider::register() add calls to authWith(ROLE, callable).

Callable is expected to identify and return the User Role.

The roles **MUST** be matching to the roles you've defined in _Remotisan_ config.

We are adding the auth callable to identify whether user is of specific role, so package could restrict or allow actions.
```php
$remotisan = app()->make(PayMe\Remotisan\Remotisan::class);
$remotisan->authWith(UserRoles::TECH_SUPPORT, function(\Illuminate\Http\Request $request) {
/** @var User|null $user */
$user = $request->user('web');
return $user && $user->isAllowed(UserPermissions::TECH_SUPPORT);
});

$remotisan->authWith(UserRoles::DEV_OPS, function(\Illuminate\Http\Request $request) {
/** @var User|null $user */
$user = $request->user('web');
return $user && $user->isAllowed(UserPermissions::TECH_SUPPORT);
});
```

## Setting ENV specific commands
You are able to configure environment specific commands by simply static json string in your .env file with name `REMOTISAN_ALLOWED_COMMANDS`.
```dotenv
REMOTISAN_ALLOWED_COMMANDS='{"artisanCommandName":{"roles":[]}, "artisanSecondCommand":{"roles":[]}}'
```
Now you are good to go.

## Testing

```bash
Expand Down
10 changes: 7 additions & 3 deletions config/remotisan.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

return [
"url" => env('REMOTISAN_URL', 'remotisan'),
"allowance_rules" => [
"roles" => ["superadmin"]
],
"commands" => [
"allowed" => [
"migrate:status" => ["super", "semi"]
],json_decode(env('REMOTISAN_ALLOWED_COMMANDS', '{"*"}'), true)
"allowed" => array_merge([
"migrate:status" => ["roles" => []],
"migrate" => ["roles" => []],
], json_decode(env('REMOTISAN_ALLOWED_COMMANDS', '{"*"}'), true)),
],
"logger" => [
"path" => env('REMOTISAN_LOG_PATH', storage_path('temp/')),
Expand Down
35 changes: 35 additions & 0 deletions resources/views/html.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

<div class="container" id="container" ng-app="RemotisanApp" ng-controller="RemotisanController">
<h2>Commands</h2>
<form class="form-inline" ng-submit="execute()" ng-init='init("{{ config('remotisan.url') }}")'>
<label class="my-1 mr-2" for="inlineFormCustomSelectPref">Preference</label>
<select required class="custom-select my-1 mr-sm-2" ng-model="command" name="command"
ng-options='c.name as (c.name + " - " + c.description) for c in commands' ng-change="onChangeDropdownValue()">
</select>

<textarea placeholder="input options & arguments (if required)..." name="command_arguments" ng-model="command_arguments" style="width:70%"></textarea>

<input type="button" class="btn btn-primary" ng-click="execute()" value="Execute" />

<hr style="opacity:0; display:block; width:100%;"/>

<div id="command_details_wrapper" ng-show="command !== null" ng-model="command_details">
<div class="abc" style="background-color: #f9fdf0">
<div><strong>Command name:</strong> @{{command_details.name}}</div>
<div><strong>Description:</strong> @{{command_details.description}}</div>
<div><strong>Help:</strong> @{{command_details.help}}</div>
<div><strong>Arguments:</strong></div>
<div style="margin-left:20px;" ng-repeat="(field_name, field_details) in command_details['definition']['args']">
<div><strong>@{{field_name}}:</strong> @{{field_details}}</div>
</div>
<div><strong>Options:</strong></div>
<div style="margin-left:20px;" ng-repeat="(field_name, field_details) in command_details['definition']['ops']">
<div><strong>@{{field_name}}:</strong> @{{field_details}}</div>
</div>
</div>
</div>
</form>

<h2>Logger</h2>
<pre style="width: 90%; background-color: black; color: darkcyan;font-family: 'Space Mono', sans-serif;">@{{ log.content }}</pre>
</div>
64 changes: 2 additions & 62 deletions resources/views/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,70 +17,10 @@
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>
</head>
<body ng-app="RemotisanApp">

<div class="container" id="container" ng-controller="RemotisanController">
<h2>Commands</h2>
<form class="form-inline" ng-submit="execute()" ng-init='init()'>
<label class="my-1 mr-2" for="inlineFormCustomSelectPref">Preference</label>
<select required class="custom-select my-1 mr-sm-2" ng-model="command" name="command"
ng-options='c.name as (c.name + " - " + c.description) for c in commands'>
</select>
<input type="button" class="btn btn-primary" ng-click="execute()" value="Execute" />
</form>

<h2>Logger</h2>
<pre style="width: 90%; background-color: black; color: darkcyan;font-family: 'Space Mono', sans-serif;">@{{ log.content }}</pre>
</div>
@include("remotisan::html")
</body>
<script>
angular.module('RemotisanApp', [])
.controller('RemotisanController', ["$scope", "$http", "$timeout", "$sce", function($scope, $http, $timeout, $sce) {
$scope.commands = [];
$scope.command = null;
$scope.params = null;
$scope.log = {
uuid: null,
content: "",
}
$scope.init = function() {
$scope.fetchCommands();
}
$scope.execute = function () {
$http.post("/remotisan/execute", {
command: $scope.command,
params: $scope.params
}).then(function (response) {
$scope.uuid = response.data.id;
$timeout( function(){ $scope.readLog(); }, 5000);
}, function (response) {
console.log(response);
});
}
$scope.fetchCommands = function () {
$http.get("/remotisan/commands")
.then(function (response) {
$scope.commands = response.data.commands;
}, function (response) {
console.log(response);
});
}
$scope.readLog = function () {
$http.get("/remotisan/execute/" + $scope.uuid)
.then(function (response) {
console.log(response.data);
$scope.log.content = response.data.content.join("\n");
if (!response.data.isEnded) {
$timeout( function(){ $scope.readLog(); }, 1000);
}
}, function (response) {
console.log(response);
});
}
}]);
@include("remotisan::scripts")
</script>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.12.9/dist/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
Expand Down
72 changes: 72 additions & 0 deletions resources/views/scripts.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
@if(!($ngApp ?? null))
var RemotisanApp = angular.module('RemotisanApp', []);
@endif

{{ $ngApp ?? "RemotisanApp" }}.controller('RemotisanController', ["$scope", "$http", "$timeout", "$sce", "$location", function($scope, $http, $timeout, $sce, $location) {
$scope.baseUrl = '';
$scope.commands = [];
$scope.command = null;
$scope.command_arguments = null;
$scope.command_details = [];
$scope.params = null;
$scope.$location = {};
$scope.log = {
uuid: null,
content: "",
}
$scope.init = function(baseUrl) {
$scope.baseUrl = baseUrl;
$scope.fetchCommands();
if($location.path() != '') {
$scope.uuid = $location.path().replace('/', '');
$scope.readLog();
}
}

$scope.locationPath = function (newPath)
{
return $location.path(newPath);
}

$scope.onChangeDropdownValue = function () {
$scope.command_arguments = '';
$scope.command_details = $scope.commands[$scope.command];
}

$scope.execute = function () {
$http.post($scope.baseUrl + "/execute", {
command: $scope.command,
command_arguments: $scope.command_arguments,
params: $scope.params
}).then(function (response) {
$scope.uuid = response.data.id;

$timeout( function(){ $scope.readLog(); }, 5000);
}, function (response) {
console.log(response);
});
}

$scope.fetchCommands = function () {
$http.get($scope.baseUrl + "/commands")
.then(function (response) {
$scope.commands = response.data.commands;
}, function (response) {
console.log(response);
});
}

$scope.readLog = function () {
$http.get($scope.baseUrl + "/execute/" + $scope.uuid)
.then(function (response) {
$scope.locationPath($scope.uuid);
console.log(response.data);
$scope.log.content = response.data.content.join("\n");
if (!response.data.isEnded) {
$timeout( function(){ $scope.readLog(); }, 1000);
}
}, function (response) {
console.log(response);
});
}
}]);
11 changes: 2 additions & 9 deletions routes/web.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
<?php

/**
* Created by PhpStorm.
* User: omer
* Date: 04/11/2022
* Time: 20:35
*/

use Illuminate\Support\Facades\Route;
use PayMe\Remotisan\Http\Controllers\RemotisanController;

Route::prefix(config("remotisan.url"))->group(function () {
Route::middleware('web')
->prefix(config("remotisan.url"))->group(function () {
Route::get('/', [RemotisanController::class, "index"]);
Route::get('/commands', [RemotisanController::class, "commands"]);
Route::post('/execute', [RemotisanController::class, "execute"]);
Expand Down
Loading

0 comments on commit cb421cb

Please sign in to comment.