Upgrade to PHP8.2
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Jens True 2023-06-07 09:56:23 +00:00
parent 79dfcd6495
commit afc4a1e3ce
8 changed files with 1058 additions and 794 deletions

@ -4,11 +4,12 @@
"homepage": "https://jcktrue.dk", "homepage": "https://jcktrue.dk",
"version": "0.1.0", "version": "0.1.0",
"require": { "require": {
"symfony/console": "^5.4", "symfony/console": "^6.3",
"symfony/yaml": "^5.4", "symfony/yaml": "^6.3",
"twig/twig": "^3.6", "twig/twig": "^3.6",
"symfony/process": "^5.4", "symfony/process": "^6.3",
"monolog/monolog": "^2.9" "monolog/monolog": "^3.3",
"verifiedjoseph/ntfy-php-library": "^4.2"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {
@ -20,6 +21,6 @@
"phpstan/phpstan": "^1.10", "phpstan/phpstan": "^1.10",
"vimeo/psalm": "^5.12", "vimeo/psalm": "^5.12",
"phpmd/phpmd": "^2.13", "phpmd/phpmd": "^2.13",
"phpunit/phpunit": "^9.6" "phpunit/phpunit": "^10.2"
} }
} }

1750
composer.lock generated

File diff suppressed because it is too large Load Diff

@ -1,5 +1,5 @@
notification: notification:
domain: ntfy.jcktrue.dk domain: https://ntfy.jcktrue.dk
topic: backup topic: backup
log: output.log log: output.log
rclone: rclone:

@ -1,6 +1,7 @@
<?php <?php
namespace App; namespace App;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputArgument;
@ -13,6 +14,15 @@ use Twig\Loader\ArrayLoader;
use DateTime; use DateTime;
use Ntfy\Server;
use Ntfy\Message;
use Ntfy\Client;
#[AsCommand(
name: 'backup',
description: 'Start backup to assigned buckets.',
hidden: false,
)]
class CommandBackup extends Command class CommandBackup extends Command
{ {
static $defaultName = "backup"; static $defaultName = "backup";
@ -40,14 +50,17 @@ class CommandBackup extends Command
$rclone = new Rclone\Rclone(); $rclone = new Rclone\Rclone();
$rclone->setLogger($app->getLogger()->withName('rclone')); $rclone->setLogger($app->getLogger()->withName('rclone'));
$ntfy = new Ntfy\Ntfy($app->getConfig()['notification']['domain']); $server = new Server($app->getConfig()['notification']['domain']);
$ntfy->setLogger($app->getLogger()->withName('notification')); $client = new Client($server);
$loader = new ArrayLoader($app->getConfig()['templates']); $loader = new ArrayLoader($app->getConfig()['templates']);
$twig = new Environment($loader); $twig = new Environment($loader);
$twig->addExtension(new Twig\AppExtension()); $twig->addExtension(new Twig\AppExtension());
foreach ($io->progressIterate($app->getConfig()['backup']) as $conf) { foreach ($io->progressIterate($app->getConfig()['backup']) as $conf) {
$message = new Message();
$message->topic($app->getConfig()['notification']['topic']);
$message->title($conf['title']);
try { try {
$template = array(); $template = array();
$template['config'] = $conf; $template['config'] = $conf;
@ -60,12 +73,13 @@ class CommandBackup extends Command
$template['destination_size_after'] = $rclone->getSize($conf['destination']); $template['destination_size_after'] = $rclone->getSize($conf['destination']);
$template['end'] = new DateTime(); $template['end'] = new DateTime();
$message = $twig->render('notify', $template); $message->body($twig->render('notify', $template));
} catch (\Throwable $e) { } catch (\Throwable $e) {
$message = $e->getMessage(); $message->body($e->getMessage());
$message->tags(["warning"]);
} }
$ntfy->send($app->getConfig()['notification']['topic'], $conf['title'], $message);
$client->send($message);
} }
$io->success("Complete"); $io->success("Complete");

@ -1,15 +1,19 @@
<?php <?php
namespace App; namespace App;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Yaml\Exception\ParseException;
#[AsCommand(
name: 'show',
description: 'Show all backup entries.',
hidden: false,
)]
class CommandShow extends Command class CommandShow extends Command
{ {
static $defaultName = "show"; static $defaultName = "show";

@ -1,32 +0,0 @@
<?php
namespace App\Ntfy;
use Psr\Log\LoggerAwareTrait;
class Ntfy
{
use LoggerAwareTrait;
protected string $domain;
function __construct(string $domain)
{
$this->domain = $domain;
}
function send(string $topic, string $title, string $message): void
{
$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',
'header' =>
"Content-Type: text/plain\r\n" .
"Title: $title\r\n",
'content' => $message]
]
)
);
$this->logger->debug("Result of ntfy notification", [$result]);
}
}

@ -3,6 +3,7 @@ namespace App\Rclone;
use Psr\Log\LoggerAwareTrait; use Psr\Log\LoggerAwareTrait;
use Psr\Log\NullLogger; use Psr\Log\NullLogger;
use Psr\Log\LoggerInterface;
use Symfony\Component\Process\Process; use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException; use Symfony\Component\Process\Exception\ProcessFailedException;
@ -45,13 +46,14 @@ class Rclone
try try
{ {
$version = $this->exec('--version'); $version = $this->exec('--version');
$this->version = explode("\n", $version)[0];
} }
catch(ProcessFailedException $e) catch(ProcessFailedException $e)
{ {
throw new Exception("Check installation of rclone"); throw new Exception("Check installation of rclone");
} }
$this->version = explode("\n", $version)[0];
if (!\str_contains($this->version, 'rclone')) { if (!\str_contains($this->version, 'rclone')) {
throw new Exception("Rclone not recognized"); throw new Exception("Rclone not recognized");
@ -122,16 +124,23 @@ class Rclone
$options $options
) )
); );
if ($this->logger instanceof LoggerInterface) {
$this->logger->info("Execute command", [$process->getCommandLine()]); $this->logger->info("Execute command", [$process->getCommandLine()]);
}
$process->setTimeout(4*3600); $process->setTimeout(4*3600);
$process->run(); $process->run();
// executes after the command finishes // executes after the command finishes
if (!$process->isSuccessful()) { if (!$process->isSuccessful()) {
if ($this->logger instanceof LoggerInterface) {
$this->logger->error("Failed execution"); $this->logger->error("Failed execution");
}
throw new ProcessFailedException($process); throw new ProcessFailedException($process);
} }
if ($this->logger instanceof LoggerInterface) {
$this->logger->info("Return code", [$process->getExitCode()]); $this->logger->info("Return code", [$process->getExitCode()]);
}
return $process->getOutput(); return $process->getOutput();
} }
} }

@ -19,8 +19,10 @@ class AppExtension extends AbstractExtension
{ {
/** /**
* Extend the filters * Extend the filters
*
*
*/ */
public function getFilters() public function getFilters() : array
{ {
return array( return array(
new TwigFilter('formatBytes', array($this, 'formatBytes')), new TwigFilter('formatBytes', array($this, 'formatBytes')),