Added logging.
This commit is contained in:
36
src/App.php
Normal file
36
src/App.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
namespace App;
|
||||
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
use Symfony\Component\Yaml\Exception\ParseException;
|
||||
|
||||
use Monolog\Logger;
|
||||
use Monolog\Handler\StreamHandler;
|
||||
|
||||
|
||||
class App {
|
||||
protected Logger $logger;
|
||||
protected array $config;
|
||||
|
||||
function __construct(string $configFile)
|
||||
{
|
||||
$this->config = Yaml::parseFile($configFile);
|
||||
|
||||
$logger = new Logger('app');
|
||||
$logger->pushHandler(new StreamHandler($this->getConfig()['logging']['stream']));
|
||||
$logger->info("Initialization complete");
|
||||
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
|
||||
function getConfig() : array
|
||||
{
|
||||
return $this->config;
|
||||
}
|
||||
|
||||
function getLogger() : Logger
|
||||
{
|
||||
return $this->logger;
|
||||
}
|
||||
}
|
@ -6,8 +6,7 @@ use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
use Symfony\Component\Yaml\Exception\ParseException;
|
||||
|
||||
|
||||
use Twig\Environment;
|
||||
use Twig\Loader\ArrayLoader;
|
||||
@ -30,22 +29,27 @@ class CommandBackup extends Command
|
||||
$io->title('Start backup process');
|
||||
|
||||
$io->info('Opening: '.$input->getArgument('config'));
|
||||
|
||||
try {
|
||||
$config = Yaml::parseFile($input->getArgument('config'));
|
||||
} catch (ParseException $e) {
|
||||
$app = new App($input->getArgument('config'));
|
||||
}
|
||||
catch (\Throwable $e) {
|
||||
$io->error('Unable to parse the YAML string: '. $e->getMessage());
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
|
||||
$rclone = new Rclone\Rclone();
|
||||
$io->info("Rclone version: ". $rclone->getVersion());
|
||||
$ntfy = new Ntfy\Ntfy($config['notification']['domain']);
|
||||
$rclone->setLogger($app->getLogger()->withName('rclone'));
|
||||
|
||||
$loader = new ArrayLoader($config['templates']);
|
||||
$ntfy = new Ntfy\Ntfy($app->getConfig()['notification']['domain']);
|
||||
$ntfy->setLogger( $app->getLogger()->withName('notification'));
|
||||
|
||||
$loader = new ArrayLoader($app->getConfig()['templates']);
|
||||
$twig = new Environment($loader);
|
||||
$twig->addExtension(new Twig\AppExtension());
|
||||
|
||||
foreach($io->progressIterate( $config['backup']) as $conf) {
|
||||
foreach ($io->progressIterate($app->getConfig()['backup']) as $conf) {
|
||||
try {
|
||||
$template = array();
|
||||
$template['config'] = $conf;
|
||||
@ -63,7 +67,7 @@ class CommandBackup extends Command
|
||||
} catch (\Throwable $e) {
|
||||
$message = $e->getMessage();
|
||||
}
|
||||
$ntfy->send($config['notification']['topic'], $conf['title'], $message);
|
||||
$ntfy->send($app->getConfig()['notification']['topic'], $conf['title'], $message);
|
||||
}
|
||||
|
||||
$io->success("Complete");
|
||||
|
@ -25,15 +25,15 @@ class CommandShow extends Command
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
$io->title('List backup entities');
|
||||
|
||||
$io->note('Reading from: '.$input->getArgument('config'));
|
||||
try {
|
||||
$config = Yaml::parseFile($input->getArgument('config'));
|
||||
} catch (ParseException $e) {
|
||||
$app = new App($input->getArgument('config'));
|
||||
}
|
||||
catch (\Throwable $e) {
|
||||
$io->error('Unable to parse the YAML string: '. $e->getMessage());
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
$io->table(['Description', 'Source', 'Destination'], $config['backup']);
|
||||
$io->table(['Description', 'Source', 'Destination'], $app->getConfig()['backup']);
|
||||
|
||||
$io->success("Done");
|
||||
return Command::SUCCESS;
|
||||
|
@ -1,8 +1,11 @@
|
||||
<?php
|
||||
namespace App\Ntfy;
|
||||
|
||||
use Psr\Log\LoggerAwareTrait;
|
||||
|
||||
class Ntfy
|
||||
{
|
||||
use LoggerAwareTrait;
|
||||
protected string $domain;
|
||||
function __construct(string $domain)
|
||||
{
|
||||
@ -11,7 +14,8 @@ class Ntfy
|
||||
|
||||
function send(string $topic, string $title, string $message): void
|
||||
{
|
||||
file_get_contents(
|
||||
$this->logger->debug("Sending ntfy notification", ["topic"=>$topic, "title"=>$title, "message"=>$message]);
|
||||
$result = file_get_contents(
|
||||
'https://'.$this->domain.'/'.$topic, false, stream_context_create(
|
||||
['http' => [
|
||||
'method' => 'POST',
|
||||
@ -22,6 +26,7 @@ class Ntfy
|
||||
]
|
||||
)
|
||||
);
|
||||
$this->logger->debug("Result of ntfy notification", [$result]);
|
||||
}
|
||||
|
||||
}
|
@ -1,13 +1,19 @@
|
||||
<?php
|
||||
namespace App\Rclone;
|
||||
|
||||
use Psr\Log\LoggerAwareTrait;
|
||||
use Psr\Log\NullLogger;
|
||||
|
||||
use Symfony\Component\Process\Process;
|
||||
use Symfony\Component\Process\Exception\ProcessFailedException;
|
||||
|
||||
|
||||
use Exception;
|
||||
|
||||
class Rclone
|
||||
class Rclone
|
||||
{
|
||||
use LoggerAwareTrait;
|
||||
|
||||
protected string $rclonePath;
|
||||
/**
|
||||
* Global options
|
||||
@ -21,6 +27,7 @@ class Rclone
|
||||
function __construct(string $rclonePath = "rclone")
|
||||
{
|
||||
$this->rclonePath = $rclonePath;
|
||||
$this->setLogger(new NullLogger);
|
||||
try
|
||||
{
|
||||
$version = $this->exec('--version');
|
||||
@ -28,7 +35,6 @@ class Rclone
|
||||
catch(ProcessFailedException $e)
|
||||
{
|
||||
throw new Exception("Check installation of rclone");
|
||||
return;
|
||||
}
|
||||
|
||||
$this->version = explode("\n", $version)[0];
|
||||
@ -68,7 +74,7 @@ class Rclone
|
||||
* @param array<String> $options
|
||||
*/
|
||||
protected function exec(string $command, array $options = array()) : string
|
||||
{
|
||||
{
|
||||
$process = new Process(
|
||||
array_merge(
|
||||
[$this->rclonePath],
|
||||
@ -77,14 +83,16 @@ class Rclone
|
||||
$options
|
||||
)
|
||||
);
|
||||
$this->logger->info("Execute command", [$process->getCommandLine()]);
|
||||
$process->setTimeout(4*3600);
|
||||
$process->run();
|
||||
|
||||
// executes after the command finishes
|
||||
if (!$process->isSuccessful()) {
|
||||
$this->logger->error("Failed execution");
|
||||
throw new ProcessFailedException($process);
|
||||
}
|
||||
|
||||
$this->logger->info("Return code", [$process->getExitCode()]);
|
||||
return $process->getOutput();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user