Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release candidate 1.0.0 #1

Merged
merged 21 commits into from
Nov 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
83a0258
added vendor:publish --tag="remotisan-config" to publish config into …
xbugster Nov 14, 2022
1ada8d5
Altered readme, added real description fo the package, etc.
xbugster Nov 14, 2022
2fbaba2
added to readme about uninterrupting http(not running into max_execut…
xbugster Nov 14, 2022
504ae86
added Configuration paragraph with temporary todo to explain regardin…
xbugster Nov 14, 2022
72346ce
added publishable and customizable views for developers to use.
xbugster Nov 14, 2022
9976f74
we added authentication, significantly improved documentation, extend…
xbugster Nov 14, 2022
8af1502
adjusted views to show command details on select of the command and u…
xbugster Nov 15, 2022
51c47d0
all the code now covered with docblock comments on each method.
xbugster Nov 15, 2022
c6e80b2
textarea is servicing for options and arguments options in additional…
xbugster Nov 15, 2022
9c49795
added the placeholder to textarea indicating that it is intended for …
xbugster Nov 15, 2022
67500e4
added todo to adjust group check later when we implement group.
xbugster Nov 15, 2022
7fd1590
adjusted readme md with auth examples.
xbugster Nov 15, 2022
efb4253
updated config to fix merge of allowed commands.
xbugster Nov 15, 2022
fb605fa
updated readme with explanation and example of using .env for environ…
xbugster Nov 15, 2022
a1b4e10
fixes to command usage authentication and the asterisk.
xbugster Nov 15, 2022
43740f0
added the command uuid to the URL when executing command + the URL ch…
xbugster Nov 16, 2022
42aefbf
added checkAuth on index, only allowed by auth will have access.
xbugster Nov 17, 2022
38a7e8b
changed the way of auth on index + added auth on execute(), directed …
xbugster Nov 17, 2022
8bcaf2d
changed the name of method checkAuth to routeGuardAuth().
xbugster Nov 17, 2022
51e04fd
returned config pull url key in routes definition.
xbugster Nov 17, 2022
049c62a
break down of views into particles, so they could be included when ne…
xbugster Nov 17, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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