Skip to content

Commit

Permalink
new symphony basede cli interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitexus committed Jan 19, 2025
1 parent 9aa5d05 commit 01825c0
Show file tree
Hide file tree
Showing 13 changed files with 384 additions and 133 deletions.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"confirm-it-solutions/php-zabbix-api": "^3.0",
"vitexsoftware/ease-html-widgets": "^1.1",
"vitexsoftware/ease-html": "^1.40",
"vitexsoftware/multiflexi-api-server": "dev-main"
"vitexsoftware/multiflexi-api-server": "dev-main",
"symfony/console": "6.4.x-dev"
},
"license": "MIT",
"authors": [
Expand All @@ -52,6 +53,7 @@
"MultiFlexi\\Action\\": "src/MultiFlexi/Action",
"MultiFlexi\\Zabbix\\": "src/MultiFlexi/Zabbix",
"MultiFlexi\\Pohoda\\": "src/MultiFlexi/Pohoda",
"MultiFlexi\\Command\\": "src/MultiFlexi/Command",
"MultiFlexi\\Ui\\Form\\": "src/MultiFlexi/Ui/Form",
"MultiFlexi\\Executor\\": "src/MultiFlexi/Executor",
"MultiFlexi\\Api\\Auth\\": "src/MultiFlexi/Api/Auth",
Expand Down
1 change: 1 addition & 0 deletions debian/conf/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"MultiFlexi\\Pohoda\\": "MultiFlexi/Pohoda",
"MultiFlexi\\Zabbix\\": "MultiFlexi/Zabbix",
"MultiFlexi\\Action\\": "MultiFlexi/Action",
"MultiFlexi\\Command\\": "MultiFlexi/Command",
"MultiFlexi\\Ui\\Form\\": "MultiFlexi/Ui/Form",
"MultiFlexi\\Executor\\": "MultiFlexi/Executor",
"MultiFlexi\\AbraFlexi\\": "MultiFlexi/AbraFlexi",
Expand Down
13 changes: 12 additions & 1 deletion debian/control
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,18 @@ Homepage: https://github.com/VitexSoftware/MultiFlexi

Package: multiflexi
Multi-Arch: foreign
Depends: ${misc:Depends}, php-vitexsoftware-ease-core (>=1.45), php-vitexsoftware-ease-html-widgets (>=1.1.1), php-cli, locales-all,php-vitexsoftware-ease-bootstrap4-widgets, php-vitexsoftware-ease-fluentpdo (>=1.3), multiflexi-sqlite|multiflexi-mysql|multiflexi-pgsql, libjs-jquery-datatables, libjs-jquery-selectize.js, anacron, adduser (>= 3.11)
Depends: ${misc:Depends},
php-vitexsoftware-ease-core (>=1.45),
php-vitexsoftware-ease-html-widgets (>=1.1.1),
php-cli,
locales-all,
php-vitexsoftware-ease-bootstrap4-widgets,
php-vitexsoftware-ease-fluentpdo (>=1.3),
multiflexi-sqlite|multiflexi-mysql|multiflexi-pgsql,
libjs-jquery-datatables,
libjs-jquery-selectize.js,
anacron, adduser (>= 3.11),
php-symfony-console
Suggests: multiflexi-all
Pre-Depends: php-robmorgan-phinx|php-cakephp-phinx
Architecture: all
Expand Down
9 changes: 9 additions & 0 deletions debian/multiflexi-cli.1
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ Include job-related status information.
.TP
.B \-\-help
Display this help message.
.TP
.B \-\-format
Specify the output format. Can be 'plain' (default) or 'json'.

.SH EXAMPLES
.TP
Expand All @@ -63,6 +66,12 @@ Display the status of the application.
.TP
.B multiflexi-cli status jobs
Display the status of the application including job-related information.
.TP
.B multiflexi-cli status --format json
Display the status of the application in JSON format.
.TP
.B multiflexi-cli status jobs --format json
Display the status of the application including job-related information in JSON format.

.SH AUTHOR
MultiFlexi was written by Vítězslav Dvořák <[email protected]>.
Expand Down
1 change: 1 addition & 0 deletions debian/multiflexi.install
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ debian/tmp/composer.json usr/lib/multiflexi
debian/conf/phinx-adapter.php usr/lib/multiflexi
src/MultiFlexi/*.php usr/lib/multiflexi/MultiFlexi/
src/MultiFlexi/Env/*.php usr/lib/multiflexi/MultiFlexi/Env/
src/MultiFlexi/Command/*.php usr/lib/multiflexi/MultiFlexi/Command/
src/MultiFlexi/Pohoda/*.php usr/lib/multiflexi/MultiFlexi/Pohoda/
src/MultiFlexi/AbraFlexi/*.php usr/lib/multiflexi/MultiFlexi/AbraFlexi/
src/MultiFlexi/Executor/Native.php usr/lib/multiflexi/MultiFlexi/Executor
Expand Down
1 change: 1 addition & 0 deletions debian/rules
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ override_dh_install:
rm debian/multiflexi/usr/lib/multiflexi/zabbixlldactions.php
sed -i "/require_once __DIR__/crequire_once '/var/lib/composer/multiflexi/autoload.php'; " debian/multiflexi-api/usr/share/multiflexi/api/index.php
sed -i "/loadConfig/c\\\\\Ease\\\\Shared::singleton()->loadConfig('/etc/multiflexi/multiflexi.env', true);" debian/multiflexi-api/usr/share/multiflexi/api/index.php
sed -i "22i require_once '/usr/share/php/Symfony/Component/Console/autoload.php';" debian/multiflexi/usr/lib/multiflexi/cli.php
167 changes: 167 additions & 0 deletions lib/cli-old.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
<?php

declare(strict_types=1);

/**
* This file is part of the MultiFlexi package
*
* https://multiflexi.eu/
*
* (c) Vítězslav Dvořák <http://vitexsoftware.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace MultiFlexi;

use Ease\Anonym;
use Ease\Shared;

require_once '../vendor/autoload.php';
Shared::init(['DB_CONNECTION', 'DB_HOST', 'DB_PORT', 'DB_DATABASE', 'DB_USERNAME', 'DB_PASSWORD'], '../.env');
$loggers = ['syslog', '\MultiFlexi\LogToSQL', 'console'];

if (Shared::cfg('ZABBIX_SERVER') && Shared::cfg('ZABBIX_HOST') && class_exists('\MultiFlexi\LogToZabbix')) {
$loggers[] = '\MultiFlexi\LogToZabbix';
}

if (Shared::cfg('APP_DEBUG') === 'true') {
$loggers[] = 'console';
}

\define('EASE_LOGGER', implode('|', $loggers));
\define('APP_NAME', 'MultiFlexi cli');
Shared::user(new Anonym());

// Parse command line arguments
$command = $argv[1] ?? null;
$argument = $argv[2] ?? null;
$identifier = $argv[3] ?? null;
$property = $argv[4] ?? null;
$format = 'plain'; // Default format

// Parse options
for ($i = 1; $i < \count($argv); ++$i) {
if (strpos($argv[$i], '--') === 0) {
$probBegin = $i;

break;
}
}

if (isset($probBegin)) {
for ($i = $probBegin; $i < \count($argv); ++$i) {
if (strpos($argv[$i], '--') === 0) {
$key = substr($argv[$i], 2);
$value = \array_key_exists($i + 1, $argv) ? $argv[$i + 1] : null;

if ($key === 'format') {
$format = $value ?? 'plain';
} else {
$properties[$key] = $value;
}

++$i; // Skip the next argument as it is the value
}
}
}

switch ($command) {
case 'version':
echo Shared::appName().' '.Shared::appVersion().\PHP_EOL;

break;
case 'remove':
switch ($argument) {
case 'user':
$engine = new \MultiFlexi\User(is_numeric($identifier) ? (int) $identifier : $identifier);

break;
case 'app':
$engine = new \MultiFlexi\Application((int) $identifier);

break;
case 'company':
$engine = new \MultiFlexi\Company(is_numeric($identifier) ? (int) $identifier : ['code' => $identifier], ['autoload' => 'true']);

break;
case 'runtemplate':
$engine = new \MultiFlexi\RunTemplate((int) $identifier);

break;
case 'job':
$engine = new \MultiFlexi\Job((int) $identifier);

break;
}

break;
case 'status':
$engine = new \MultiFlexi\Engine();
$pdo = $engine->getPdo();
$database = $pdo->getAttribute(\PDO::ATTR_DRIVER_NAME).' '.
$pdo->getAttribute(\PDO::ATTR_CONNECTION_STATUS).' '.
$pdo->getAttribute(\PDO::ATTR_SERVER_INFO).' '.
$pdo->getAttribute(\PDO::ATTR_SERVER_VERSION);

$status = [
'version' => Shared::appVersion(),
'php' => \PHP_VERSION,
'os' => \PHP_OS,
'memory' => memory_get_usage(),
'companies' => $engine->getFluentPDO()->from('company')->count(),
'apps' => $engine->getFluentPDO()->from('apps')->count(),
'runtemplates' => $engine->getFluentPDO()->from('runtemplate')->count(),
'topics' => $engine->getFluentPDO()->from('topic')->count(),
'credentials' => $engine->getFluentPDO()->from('credentials')->count(),
'credential_types' => $engine->getFluentPDO()->from('credential_type')->count(),
'database' => $database,
'daemon' => \MultiFlexi\Runner::isServiceActive('multiflexi.service') ? 'running' : 'stopped',
'timestamp' => date('c'),
];

if ($argument === 'jobs') {
$queeLength = (new \MultiFlexi\Scheduler())->listingQuery()->count();

// Query to get job status information
$query = <<<'EOD'
SELECT
COUNT(*) AS total_jobs,
SUM(CASE WHEN exitcode = 0 THEN 1 ELSE 0 END) AS successful_jobs,
SUM(CASE WHEN exitcode != 0 THEN 1 ELSE 0 END) AS failed_jobs,
SUM(CASE WHEN exitcode IS NULL THEN 1 ELSE 0 END) AS incomplete_jobs,
COUNT(DISTINCT app_id) AS total_applications,
SUM(CASE WHEN schedule IS NOT NULL THEN 1 ELSE 0 END) AS repeated_jobs
FROM job
EOD;

$stmt = $pdo->query($query);
$result = $stmt->fetch(\PDO::FETCH_ASSOC);

$status = array_merge($status, [
'successful_jobs' => (int) $result['successful_jobs'],
'failed_jobs' => (int) $result['failed_jobs'],
'incomplete_jobs' => (int) $result['incomplete_jobs'],
'total_applications' => (int) $result['total_applications'],
'repeated_jobs' => (int) $result['repeated_jobs'],
'total_jobs' => (int) $result['total_jobs'],
'quee_length' => (int) $queeLength,
]);
}

if ($format === 'json') {
echo json_encode($status, \JSON_PRETTY_PRINT).\PHP_EOL;
} else {
foreach ($status as $key => $value) {
echo ucfirst(str_replace('_', ' ', $key)).': '.$value.\PHP_EOL;
}
}

break;

default:
echo "Unknown command: {$command}".\PHP_EOL;

break;
}
130 changes: 8 additions & 122 deletions lib/cli.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env php
<?php

declare(strict_types=1);
Expand All @@ -17,6 +18,7 @@

use Ease\Anonym;
use Ease\Shared;
use Symfony\Component\Console\Application;

require_once '../vendor/autoload.php';
Shared::init(['DB_CONNECTION', 'DB_HOST', 'DB_PORT', 'DB_DATABASE', 'DB_USERNAME', 'DB_PASSWORD'], '../.env');
Expand All @@ -31,127 +33,11 @@
}

\define('EASE_LOGGER', implode('|', $loggers));
\define('APP_NAME', 'MultiFlexi cli');
Shared::user(new Anonym());

// Parse command line arguments
$command = $argv[1] ?? null;
$argument = $argv[2] ?? null;
$identifier = $argv[3] ?? null;
$property = $argv[4] ?? null;
$format = 'plain'; // Default format

// Parse options
for ($i = 1; $i < \count($argv); ++$i) {
if (strpos($argv[$i], '--') === 0) {
$probBegin = $i;
break;
}
}

if (isset($probBegin)) {
for ($i = $probBegin; $i < \count($argv); ++$i) {
if (strpos($argv[$i], '--') === 0) {
$key = substr($argv[$i], 2);
$value = \array_key_exists($i + 1, $argv) ? $argv[$i + 1] : null;
if ($key === 'format') {
$format = $value ?? 'plain';
} else {
$properties[$key] = $value;
}
++$i; // Skip the next argument as it is the value
}
}
}

switch ($command) {
case 'version':
echo Shared::appName().' '.Shared::appVersion().\PHP_EOL;
break;

case 'remove':
switch ($argument) {
case 'user':
$engine = new \MultiFlexi\User(is_numeric($identifier) ? (int) $identifier : $identifier);
break;
case 'app':
$engine = new \MultiFlexi\Application((int) $identifier);
break;
case 'company':
$engine = new \MultiFlexi\Company(is_numeric($identifier) ? (int) $identifier : ['code' => $identifier], ['autoload' => 'true']);
break;
case 'runtemplate':
$engine = new \MultiFlexi\RunTemplate((int) $identifier);
break;
case 'job':
$engine = new \MultiFlexi\Job((int) $identifier);
break;
}
break;
\define('APP_NAME', 'MultiFlexiCLI');

case 'status':
$engine = new \MultiFlexi\Engine();
$pdo = $engine->getPdo();
$database = $pdo->getAttribute(\PDO::ATTR_DRIVER_NAME).' '.
$pdo->getAttribute(\PDO::ATTR_CONNECTION_STATUS).' '.
$pdo->getAttribute(\PDO::ATTR_SERVER_INFO).' '.
$pdo->getAttribute(\PDO::ATTR_SERVER_VERSION);

$status = [
'version' => Shared::appVersion(),
'php' => \PHP_VERSION,
'os' => \PHP_OS,
'memory' => memory_get_usage(),
'companies' => $engine->getFluentPDO()->from('company')->count(),
'apps' => $engine->getFluentPDO()->from('apps')->count(),
'runtemplates' => $engine->getFluentPDO()->from('runtemplate')->count(),
'topics' => $engine->getFluentPDO()->from('topic')->count(),
'credentials' => $engine->getFluentPDO()->from('credentials')->count(),
'credential_types' => $engine->getFluentPDO()->from('credential_type')->count(),
'database' => $database,
'daemon' => \MultiFlexi\Runner::isServiceActive('multiflexi.service') ? 'running' : 'stopped',
'timestamp' => date('c')
];

if ($argument === 'jobs') {
$queeLength = (new \MultiFlexi\Scheduler())->listingQuery()->count();

// Query to get job status information
$query = <<<'EOD'
SELECT
COUNT(*) AS total_jobs,
SUM(CASE WHEN exitcode = 0 THEN 1 ELSE 0 END) AS successful_jobs,
SUM(CASE WHEN exitcode != 0 THEN 1 ELSE 0 END) AS failed_jobs,
SUM(CASE WHEN exitcode IS NULL THEN 1 ELSE 0 END) AS incomplete_jobs,
COUNT(DISTINCT app_id) AS total_applications,
SUM(CASE WHEN schedule IS NOT NULL THEN 1 ELSE 0 END) AS repeated_jobs
FROM job
EOD;

$stmt = $pdo->query($query);
$result = $stmt->fetch(\PDO::FETCH_ASSOC);

$status = array_merge($status, [
'successful_jobs' => (int) $result['successful_jobs'],
'failed_jobs' => (int) $result['failed_jobs'],
'incomplete_jobs' => (int) $result['incomplete_jobs'],
'total_applications' => (int) $result['total_applications'],
'repeated_jobs' => (int) $result['repeated_jobs'],
'total_jobs' => (int) $result['total_jobs'],
'quee_length' => (int) $queeLength,
]);
}

if ($format === 'json') {
echo json_encode($status, JSON_PRETTY_PRINT) . \PHP_EOL;
} else {
foreach ($status as $key => $value) {
echo ucfirst(str_replace('_', ' ', $key)) . ': ' . $value . \PHP_EOL;
}
}
break;
Shared::user(new Anonym());

default:
echo "Unknown command: $command" . \PHP_EOL;
break;
}
$app = new Application(\Ease\Shared::appName(), \Ease\Shared::appVersion());
$app->add(new Command\AppStatus());
$app->add(new Command\JobStatus());
$app->run();
Loading

0 comments on commit 01825c0

Please sign in to comment.