-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from deliciousbrains/14-reboot-server-command
Reboot server command
- Loading branch information
Showing
2 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
namespace App\Commands\Servers; | ||
|
||
use App\Commands\BaseCommand; | ||
|
||
class RebootCommand extends BaseCommand | ||
{ | ||
protected $signature = 'servers:reboot | ||
{server_id? : The server to reboot} | ||
{--all : Reboot all servers} | ||
{--f|force : Reboot the server without prompting for confirmation} | ||
{--profile=}'; | ||
|
||
protected $description = 'Reboot a server'; | ||
|
||
public function action(): int | ||
{ | ||
if ((bool) $this->option('all')) { | ||
$this->rebootAll(); | ||
return self::SUCCESS; | ||
} | ||
|
||
$serverId = $this->argument('server_id'); | ||
|
||
if (empty($serverId)) { | ||
$serverId = $this->askToSelectServer('Which server would you like to reboot'); | ||
} | ||
|
||
$server = $this->spinupwp->getServer((int) $serverId); | ||
|
||
if ((bool) $this->option('force') || $this->confirm("Are you sure you want to reboot \"{$server->name}\"?", true)) { | ||
$this->rebootServers([$server]); | ||
} | ||
|
||
return self::SUCCESS; | ||
} | ||
|
||
protected function rebootAll(): void | ||
{ | ||
if ((bool) $this->option('force') || $this->confirm('Are you sure you want to reboot all servers?', true)) { | ||
$this->rebootServers($this->spinupwp->listServers()->toArray()); | ||
} | ||
} | ||
|
||
protected function rebootServers(array $servers): void | ||
{ | ||
if (empty($servers)) { | ||
return; | ||
} | ||
|
||
$events = []; | ||
|
||
foreach ($servers as $server) { | ||
try { | ||
$eventId = $server->reboot(); | ||
$events[] = ["{$eventId}", $server->name]; | ||
} catch (\Exception $e) { | ||
if (count($servers) === 1) { | ||
$this->error("{$server->name} could not be rebooted."); | ||
return; | ||
} | ||
} | ||
} | ||
|
||
if (empty($events)) { | ||
$this->error('No servers could be rebooted.'); | ||
return; | ||
} | ||
|
||
$this->successfulStep((count($events) === 1 ? 'Server' : 'Servers') . ' queued for reboot.'); | ||
|
||
$this->stepTable([ | ||
'Event ID', | ||
'Server', | ||
], $events); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
use GuzzleHttp\Psr7\Response; | ||
|
||
beforeEach(function () { | ||
setTestConfigFile(); | ||
|
||
$this->clientMock->shouldReceive('request')->with('GET', 'servers/1', [])->andReturn( | ||
new Response(200, [], json_encode(['data' => ['id' => 1, 'name' => 'hellfish-media']])) | ||
); | ||
|
||
$this->clientMock->shouldReceive('request')->with('POST', 'servers/1/reboot', [])->andReturn( | ||
new Response(200, [], json_encode(['event_id' => '100'])) | ||
); | ||
}); | ||
|
||
afterEach(function () { | ||
deleteTestConfigFile(); | ||
}); | ||
|
||
test('reboot a server', function () { | ||
$this->artisan('servers:reboot 1') | ||
->expectsConfirmation('Are you sure you want to reboot "hellfish-media"?', 'yes') | ||
->expectsOutput('==> Server queued for reboot.'); | ||
}); | ||
|
||
test('reboot a server with force option', function () { | ||
$this->artisan('servers:reboot 1 --force') | ||
->expectsOutput('==> Server queued for reboot.'); | ||
}); | ||
|
||
test('reboot all servers', function () { | ||
$this->clientMock->shouldReceive('request')->once()->with('GET', 'servers?page=1&limit=100', [])->andReturn( | ||
new Response(200, [], listResponseJson([ | ||
['id' => 1, 'name' => 'hellfish-media'], | ||
['id' => 2, 'name' => 'staging.hellfish-media'], | ||
])) | ||
); | ||
$this->clientMock->shouldReceive('request')->with('POST', 'servers/2/reboot', [])->andReturn( | ||
new Response(200, [], json_encode(['event_id' => '101'])) | ||
); | ||
$this->artisan('servers:reboot --all') | ||
->expectsConfirmation('Are you sure you want to reboot all servers?', 'yes') | ||
->expectsOutput('==> Servers queued for reboot.'); | ||
}); |