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

feature: add new ssh command #9

Merged
merged 1 commit into from
Aug 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/.vscode
/.vagrant
.phpunit.result.cache
ploi.yml
39 changes: 39 additions & 0 deletions app/Commands/SshCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace App\Commands;

use App\Concerns\EnsureHasPloiConfiguration;
use App\Concerns\EnsureHasToken;
use App\Support\Configuration;
use App\Support\Ploi;
use Illuminate\Console\Scheduling\Schedule;
use LaravelZero\Framework\Commands\Command;

class SshCommand extends Command
{
use EnsureHasToken;
use EnsureHasPloiConfiguration;

protected $signature = 'ssh {user?}';

protected $description = 'Start a new SSH session.';

public function handle(Ploi $ploi, Configuration $configuration)
{
$this->ensureHasToken();
$this->ensureHasPloiConfiguration();

$server = $ploi->getServer($configuration->get('server'));
$site = $ploi->getSite($configuration->get('server'), $configuration->get('site'));
$user = $this->argument('user') ?? 'ploi';

$this->info('Establishing SSH connection...');

passthru($this->buildCommand($server, $site, $user));
}

protected function buildCommand($server, $site, $user)
{
return sprintf('ssh -t %s@%s "cd %s; bash --login"', $user, $server['ip_address'], $site['domain'] . $site['project_root']);
}
}
10 changes: 10 additions & 0 deletions app/Support/Ploi.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public function getServers()
return $response['data'];
}

public function getServer($server)
{
return Http::withToken($this->token)->get(self::$BASE_URL . "servers/{$server}")['data'];
}

public function getServerProviders()
{
return Http::withToken($this->token)->get(self::$BASE_URL . "user/server-providers")['data'];
Expand All @@ -37,6 +42,11 @@ public function getSites($server)
return Http::withToken($this->token)->get(self::$BASE_URL . "servers/{$server}/sites?per_page=50")['data'];
}

public function getSite($server, $site)
{
return Http::withToken($this->token)->get(self::$BASE_URL . "servers/{$server}/sites/{$site}")['data'];
}

public function createSite($server, $rootDomain, $webDir, $systemUser): int
{
$response = Http::withToken($this->token)->post(self::$BASE_URL . "servers/{$server}/sites", [
Expand Down