diff --git a/app/Console/Commands/GitAutoDeployNotificationSlack.php b/app/Console/Commands/GitAutoDeployNotificationSlack.php index 91ae834..01ebbea 100644 --- a/app/Console/Commands/GitAutoDeployNotificationSlack.php +++ b/app/Console/Commands/GitAutoDeployNotificationSlack.php @@ -5,6 +5,7 @@ use Illuminate\Console\Command; use App\Console\ModCommand; use App\Console\Commands\Tasks\GitAutoDeployNotificationSlackTaskManager; +use App\Setting; class GitAutoDeployNotificationSlack extends ModCommand { @@ -41,13 +42,19 @@ public function handle() { parent::handle(); - $this->line('Visit ' . config('services.stool.base') . '/deploy/login/slack and come back with a token'); - $sToken = $this->ask('Token?'); + $this->line('Visit'); + $this->info(self::generateOauthUrl()); + $this->line('and give permission to send Slack messages!'); + $sChannel = $this->ask('Channel?'); (new GitAutoDeployNotificationSlackTaskManager([ - 'public_id' => $sToken, 'channel' => $sChannel, ]))->work(); } + + public static function generateOauthUrl() { + $sServerId = Setting::where('key', 'server_id')->value('value'); + return config('services.stool.base') . '/deploy/login/slack/' . $sServerId; + } } diff --git a/app/Console/Commands/InstallationFinishCommand.php b/app/Console/Commands/InstallationFinishCommand.php index 3ce0b6d..c59f6f5 100644 --- a/app/Console/Commands/InstallationFinishCommand.php +++ b/app/Console/Commands/InstallationFinishCommand.php @@ -42,6 +42,13 @@ public function handle() { parent::handle(); + // Init + $oBody = (new ApiRequestService())->request('server/init'); + Setting::create([ + 'key' => 'server_id', + 'value' => $oBody->public_id, + ]); + // Deploy Job (new CreateDeamonTaskManager([ 'name' => 'stool-deploy', diff --git a/app/Console/Commands/Tasks/GitAutoDeployNotificationSlack/GitAutoDeployNotificationSlackTask.php b/app/Console/Commands/Tasks/GitAutoDeployNotificationSlack/GitAutoDeployNotificationSlackTask.php index 872a0ff..ead4768 100644 --- a/app/Console/Commands/Tasks/GitAutoDeployNotificationSlack/GitAutoDeployNotificationSlackTask.php +++ b/app/Console/Commands/Tasks/GitAutoDeployNotificationSlack/GitAutoDeployNotificationSlackTask.php @@ -5,6 +5,7 @@ use App\Console\Commands\Tasks\Task; use App\Services\ApiRequestService; use App\Setting; +use App\Console\Commands\GitAutoDeployNotificationSlack; class GitAutoDeployNotificationSlackTask extends Task { @@ -22,14 +23,15 @@ public function localRequirements() public function handle() { - (new ApiRequestService())->request('verifySlack', [ - 'public_id' => $this->oOptions->public_id, - 'channel' => $this->oOptions->channel, - ]); // TODO: validate response + $oBody = (new ApiRequestService())->request('slack/isTokenSet'); + if ($oBody->ok === false) { + return $this->shell->saveError('No write Permission! Please visit ' . GitAutoDeployNotificationSlack::generateOauthUrl() . ' and give permission to send Slack messages!'); + } - Setting::create([ - 'key' => 'deploy_slack_token', - 'value' => $this->oOptions->public_id, + Setting::updateOrCreate([ + 'key' => 'slack_channel', + ], [ + 'value' => $this->oOptions->channel, ]); } } diff --git a/app/Console/Commands/Tasks/GitAutoDeployNotificationSlackTaskManager.php b/app/Console/Commands/Tasks/GitAutoDeployNotificationSlackTaskManager.php index d500c08..b2811d3 100644 --- a/app/Console/Commands/Tasks/GitAutoDeployNotificationSlackTaskManager.php +++ b/app/Console/Commands/Tasks/GitAutoDeployNotificationSlackTaskManager.php @@ -14,7 +14,6 @@ class GitAutoDeployNotificationSlackTaskManager extends Taskmanager public function validate() { return [ - 'public_id' => 'required', 'channel' => 'required', ]; } diff --git a/app/Jobs/Deploy.php b/app/Jobs/Deploy.php index 558d0b4..f0054ba 100644 --- a/app/Jobs/Deploy.php +++ b/app/Jobs/Deploy.php @@ -57,7 +57,7 @@ public function handle() exec($sCommand, $aOutput, $iExit); if ($iExit != 0) { - (new ApiRequestService())->request('sendEmail', [ + (new ApiRequestService())->request('email/send', [ 'type' => 'DeployFailed', 'email' => Setting::where('key', 'admin_email')->value('value'), 'repository' => $this->repository->dir, diff --git a/app/Services/ApiRequestService.php b/app/Services/ApiRequestService.php index 0ef6c5f..4f1abff 100644 --- a/app/Services/ApiRequestService.php +++ b/app/Services/ApiRequestService.php @@ -15,10 +15,12 @@ public function __construct() ], ]); } - public function request($sRoute, $aParams) + public function request($sRoute, $aParams = []) { - return $this->client->request('POST', $sRoute, [ + $oResponse = $this->client->request('POST', $sRoute, [ 'form_params' => $aParams, - ])->getBody(); + ]); + + return json_decode($oResponse->getBody()); } } diff --git a/app/Services/Slack.php b/app/Services/Slack.php index e630e20..e0753ad 100644 --- a/app/Services/Slack.php +++ b/app/Services/Slack.php @@ -7,21 +7,21 @@ class Slack { - public $token; + public $channel; public function __construct() { - $this->token = Setting::where('key', 'deploy_slack_token')->value('value'); + $this->channel = Setting::where('key', 'slack_channel')->value('value'); } public function send($sText, $sFormat = null) { - if (! $this->token) { + if (! $this->channel) { return false; } - return (new ApiRequestService())->request('sendSlack', [ - 'public_id' => $this->token, + return (new ApiRequestService())->request('slack/send', [ 'text' => $sText, + 'channel' => $this->channel, 'format' => $sFormat, ]); } diff --git a/scripts/install.sh b/scripts/install.sh index 8b48481..90c7044 100644 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -1,12 +1,12 @@ #!/bin/bash -# http://patorjk.com/software/taag/#p=display&v=0&f=Slant&t=stool%20v1.2.6 +# http://patorjk.com/software/taag/#p=display&v=0&f=Slant&t=stool%20v1.2.8 cat << "EOF" - __ __ ___ ___ _____ - _____/ /_____ ____ / / _ _< /|__ \/__ / - / ___/ __/ __ \/ __ \/ / | | / / / __/ / / / - (__ ) /_/ /_/ / /_/ / / | |/ / / / __/_ / / -/____/\__/\____/\____/_/ |___/_(_)____(_)_/ + __ __ ___ ___ ____ + _____/ /_____ ____ / / _ _< /|__ \ ( __ ) + / ___/ __/ __ \/ __ \/ / | | / / / __/ / / __ | + (__ ) /_/ /_/ / /_/ / / | |/ / / / __/_/ /_/ / +/____/\__/\____/\____/_/ |___/_(_)____(_)____/ EOF diff --git a/scripts/partials/finish.sh b/scripts/partials/finish.sh index dbf63d5..0948550 100644 --- a/scripts/partials/finish.sh +++ b/scripts/partials/finish.sh @@ -17,6 +17,9 @@ finish () { # Fix for laravel mix sudo apt-get install libpng-dev -y + # unzip + sudo apt-get install unzip -y + # APACHE PERMISSIONS apache_permissions service apache2 reload