Skip to content
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
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
# easyengine/dash-command

This package provides the `ee dash` command, allowing you to integrate your EasyEngine server and sites with the web dashboard.
This package provides the `ee dash` command, allowing you to integrate your EasyEngine server and sites with the EasyEngine Dashboard.

## Description

The `ee dash` command facilitates the connection between your server and web dashboard. It securely sends server and site-specific data to your web dashboard organization, enabling you to manage and monitor your EasyEngine sites from a centralized dashboard.
The `ee dash` command facilitates the connection between your server and the EasyEngine Dashboard. It securely sends server and site-specific data to your EasyEngine Dashboard organization, enabling you to manage and monitor your EasyEngine sites from a centralized dashboard.

The primary command is `ee dash init`, which performs the following actions:
- Verifies server compatibility (Ubuntu 22.04 or later).
- Adds the web dashboard SSH key for secure communication.
- Registers the server with your specified web dashboard organization.
- Syncs all existing EasyEngine sites to web dashboard, sending relevant details like PHP version, SSL status, and more.
- Adds the EasyEngine Dashboard SSH key for secure communication.
- Registers the server with your specified EasyEngine Dashboard organization.
- Syncs all existing EasyEngine sites to the EasyEngine Dashboard, sending relevant details like PHP version, SSL status, and more.

## Installation

This command is bundled with EasyEngine. If you have a standard EasyEngine installation, no additional installation steps are required.

## Usage

To integrate your server with web dashboard, run the following command:
To integrate your server with the EasyEngine Dashboard, run the following command:

```bash
ee dash init --api=<your-api-key> --org=<your-org-name>
```

This step is no longer be necessary manually. Once you register your server, the web dashboard will automatically fire this command to sync your sites.
This step is no longer necessary manually. Once you register your server, the EasyEngine Dashboard will automatically fire this command to sync your sites.

## Contributing

Expand Down
70 changes: 35 additions & 35 deletions src/Dash_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
use function EE\Utils\http_request;

/**
* Manages EasyDash integration.
* Manages EasyEngine Dashboard integration.
*
* @package ee-cli
*/
class Dash_Command extends EE_Command {

/**
* The base URL for the EasyDash API.
* The base URL for the EasyEngine Dashboard API.
*
* @var string
*/
Expand All @@ -23,15 +23,15 @@ class Dash_Command extends EE_Command {
private $ed_base_url = '';

/**
* Integrates server on EasyDash dashboard.
* Integrates the server with the EasyEngine Dashboard.
*
* ## OPTIONS
*
* [--api=<api-key>]
* : ED API Key.
* : EasyEngine Dashboard API key.
*
* [--org=<org-name>]
* : ED Organization name in which server needs to be added.
* : EasyEngine Dashboard organization name in which the server needs to be added.
*
* [--ip=<ip-address>]
* : Public IP address of the server.
Expand All @@ -41,8 +41,8 @@ class Dash_Command extends EE_Command {
*
* ## EXAMPLES
*
* # Integrate server on EasyDash dashboard
* $ ee dash init --api=xxx
* # Integrate the server with the EasyEngine Dashboard
* $ ee dash init --api=xxx --org=my-org
*
*/
public function init( $args, $assoc_args ) {
Expand All @@ -59,11 +59,11 @@ public function init( $args, $assoc_args ) {
$organization = trim( $organization );

if ( empty( $api_key ) ) {
EE::error( 'Please provide an EasyDash API key using the --api=<token:key> flag.' );
EE::error( 'Please provide an EasyEngine Dashboard API key using the --api=<token:key> flag.' );
}

if ( empty( $organization ) ) {
EE::error( 'Please provide an EasyDash organization name using the --org=<org-name> flag.' );
EE::error( 'Please provide an EasyEngine Dashboard organization name using the --org=<org-name> flag.' );
}

$this->pre_checks();
Expand Down Expand Up @@ -117,12 +117,12 @@ public function init( $args, $assoc_args ) {
EE::launch( 'hostnamectl set-hostname ' . $hostname );
}

// Check if the server already exists on EasyDash
// Check if the server already exists on EasyEngine Dashboard
if ( $this->server_exists_on_easydash( $api_key, $public_ipv4, $organization ) ) {
EE::log( 'Server with IP ' . $public_ipv4 . ' already exists on EasyDash. Skipping server addition.' );
EE::log( 'Server with IP ' . $public_ipv4 . ' already exists on EasyEngine Dashboard. Skipping server addition.' );
} else {

// @todo: Check if the server is already integrated with EasyDash.
// @todo: Check if the server is already integrated with EasyEngine Dashboard.

$ssh_key_exists = EE::launch( 'grep -q "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILbESQqRcGdwnn/u1BkDCD9rDiFqgDhTHBHIIasaDpWV EasyEngine" /root/.ssh/authorized_keys' );
if ( 0 !== $ssh_key_exists->return_code ) {
Expand All @@ -137,7 +137,7 @@ public function init( $args, $assoc_args ) {

$server_data_json = json_encode( $server_data );

// Make the API call to add server to EasyDash
// Make the API call to add server to EasyEngine Dashboard
try {
$server_response = http_request(
'POST',
Expand All @@ -147,14 +147,14 @@ public function init( $args, $assoc_args ) {
);

if ( $server_response->success ) {
EE::success( 'Server integrated with EasyDash successfully. Please wait for sometime for site data to populate on EasyDash.' );
EE::success( 'Server integrated with EasyEngine Dashboard successfully. Please wait for sometime for site data to populate on EasyEngine Dashboard.' );
exit( 0 );
} else {
EE::warning( 'Failed to integrate server with EasyDash. Response: ' . $server_response->body );
EE::warning( 'Failed to integrate server with EasyEngine Dashboard. Response: ' . $server_response->body );
EE::warning( 'Status code: ' . $server_response->status_code );
}
} catch ( \RuntimeException $e ) {
EE::error( 'Error connecting to EasyDash API: ' . $e->getMessage() );
EE::error( 'Error connecting to EasyEngine Dashboard API: ' . $e->getMessage() );
return;
}
}
Expand Down Expand Up @@ -184,9 +184,9 @@ public function init( $args, $assoc_args ) {
$public_dir = str_replace( '/var/www/htdocs', '', $site->site_container_fs_path );
$public_dir = ltrim( $public_dir, '/' );

// Check if the site already exists on EasyDash
// Check if the site already exists on EasyEngine Dashboard
if ( $this->site_exists_on_easydash( $api_key, $site->site_url, $organization ) ) {
EE::log( 'Site ' . $site->site_url . ' already exists on EasyDash. Skipping site addition.' );
EE::log( 'Site ' . $site->site_url . ' already exists on EasyEngine Dashboard. Skipping site addition.' );
if ( ! $site->site_enabled ) {
EE::launch( "ee site disable $site->site_url" );
}
Expand Down Expand Up @@ -274,7 +274,7 @@ public function init( $args, $assoc_args ) {

// If php version is < 8.1, then skip adding the site.
if ( $site->site_type !== 'html' && version_compare( $site->php_version, '8.1', '<' ) ) {
EE::warning( "Skipping site {$site->site_url} integration with EasyDash as PHP version is less than 8.1." );
EE::warning( "Skipping site {$site->site_url} integration with EasyEngine Dashboard as PHP version is less than 8.1." );
continue;
}

Expand All @@ -289,22 +289,22 @@ public function init( $args, $assoc_args ) {
);

if ( $site_response->success ) {
EE::success( "Site {$site->site_url} integrated with EasyDash successfully." );
EE::success( "Site {$site->site_url} integrated with EasyEngine Dashboard successfully." );
} else {
EE::error( "Failed to integrate site {$site->site_url} with EasyDash. Response: " . $site_response->body );
EE::error( "Failed to integrate site {$site->site_url} with EasyEngine Dashboard. Response: " . $site_response->body );
}
} catch ( \RuntimeException $e ) {
EE::error( "Error connecting to EasyDash API for site {$site->site_url}: " . $e->getMessage() );
EE::error( "Error connecting to EasyEngine Dashboard API for site {$site->site_url}: " . $e->getMessage() );
}
}

EE\Utils\delem_log( 'dash ' . __FUNCTION__ . ' end' );
}

/**
* Checks if a server already exists on EasyDash based on its IP address.
* Checks if a server already exists on EasyEngine Dashboard based on its IP address.
*
* @param string $api_key The EasyDash API key.
* @param string $api_key The EasyEngine Dashboard API key.
* @param string $public_ipv4 The public IPv4 address of the server.
*
* @return bool True if the server exists, false otherwise.
Expand All @@ -325,8 +325,8 @@ private function server_exists_on_easydash( $api_key, $public_ipv4, $organizatio
EE::debug( 'Request URL: ' . $server_api_url );
EE::debug( 'Request Headers: ' . json_encode( $headers ) );
EE::debug( 'Request data: ' . json_encode( $data ) );
EE::debug( 'Checking if server with IP ' . $public_ipv4 . ' exists on EasyDash...' );
EE::debug( 'Response from EasyDash: ' . $server_response->body );
EE::debug( 'Checking if server with IP ' . $public_ipv4 . ' exists on EasyEngine Dashboard...' );
EE::debug( 'Response from EasyEngine Dashboard: ' . $server_response->body );

if ( $server_response->success ) {
$server_data = json_decode( $server_response->body, true );
Expand All @@ -346,7 +346,7 @@ private function server_exists_on_easydash( $api_key, $public_ipv4, $organizatio
return false; // Assume server doesn't exist to avoid potential issues
}
} catch ( \RuntimeException $e ) {
EE::warning( 'Error connecting to EasyDash API to get server list.' );
EE::warning( 'Error connecting to EasyEngine Dashboard API to get server list.' );
EE::debug( 'Error message: ' . $e->getMessage() );
EE::debug( 'Error code: ' . $e->getCode() );
return false; // Assume server doesn't exist to avoid potential issues
Expand All @@ -355,9 +355,9 @@ private function server_exists_on_easydash( $api_key, $public_ipv4, $organizatio


/**
* Checks if a site already exists on EasyDash.
* Checks if a site already exists on EasyEngine Dashboard.
*
* @param string $api_key The EasyDash API key.
* @param string $api_key The EasyEngine Dashboard API key.
* @param string $site_url The URL of the site.
* @param string $hostname The hostname of the server.
*
Expand Down Expand Up @@ -394,36 +394,36 @@ private function site_exists_on_easydash( $api_key, $site_url, $organization ) {
return false; // Assume site doesn't exist to avoid potential issues
}
} catch ( \RuntimeException $e ) {
EE::warning( 'Error connecting to EasyDash API to get site list: ' . $e->getMessage() );
EE::warning( 'Error connecting to EasyEngine Dashboard API to get site list: ' . $e->getMessage() );
return false; // Assume site doesn't exist to avoid potential issues
}
}

/**
* Do initial checks if server is eligible for EasyDash integration.
* Do initial checks if server is eligible for EasyEngine Dashboard integration.
*/
private function pre_checks() {

// If the OS is not Ubuntu or Debian, exit with error that unsupported OS.
$os = trim( EE::launch( 'lsb_release -i' )->stdout );
// Check if the server is running Ubuntu or Debian.
if ( false === strpos( $os, 'Ubuntu' ) && false === strpos( $os, 'Debian' ) ) {
EE::error( 'EasyDash integration is only supported on Ubuntu or Debian.' );
EE::error( 'EasyEngine Dashboard integration is only supported on Ubuntu or Debian.' );
}

// Check if the server is running Ubuntu 22.04 or later.
$ubuntu_version = trim( EE::launch( 'lsb_release -r' )->stdout );
$version_string = str_replace('Release:', '', $ubuntu_version);
$version = trim($version_string);
if (version_compare($version, '22.04', '<')) {
EE::error( 'EasyDash integration is only supported on Ubuntu 22.04 or later.' );
EE::error( 'EasyEngine Dashboard integration is only supported on Ubuntu 22.04 or later.' );
}

// Check if rclone config is present. If there, show confirm msg to continue.
// if ( file_exists( '~/.config/rclone/rclone.conf' ) ) {
// $confirm = EE::confirm( 'Rclone config file found. Do you want to continue with EasyDash integration?', [], false );
// $confirm = EE::confirm( 'Rclone config file found. Do you want to continue with EasyEngine Dashboard integration?', [], false );
// if ( ! $confirm ) {
// EE::error( 'EasyDash integration aborted by user.' );
// EE::error( 'EasyEngine Dashboard integration aborted by user.' );
// }
// }
}
Expand Down