2022-08-20 03:20:49 +00:00
< ? php
namespace S3DB\Sync ;
use Bramus\Monolog\Formatter\ColoredLineFormatter ;
use Garden\Cli\Args ;
use Garden\Cli\Cli ;
use Monolog\Handler\StreamHandler ;
use Monolog\Logger ;
2022-08-23 12:19:05 +00:00
use S3DB\Sync\Filesystems\LocalFilesystem ;
use S3DB\Sync\Filesystems\StorageFilesystem ;
2022-08-20 03:20:49 +00:00
use Spatie\Emoji\Emoji ;
class Sync
{
protected Logger $logger ;
protected Cli $cli ;
protected Args $args ;
protected AbstractSyncer $syncer ;
2022-08-23 12:19:05 +00:00
protected StorageFilesystem $storageFilesystem ;
protected LocalFilesystem $localFilesystem ;
2022-08-20 03:20:49 +00:00
public function __construct (
) {
$environment = array_merge ( $_ENV , $_SERVER );
ksort ( $environment );
$this -> cli = new Cli ();
$this -> cli -> opt ( 'postgres' , 'postgres mode' )
-> opt ( 'mysql' , 'mysql mode' )
-> opt ( 'push' , 'push to s3' )
-> opt ( 'pull' , 'pull from s3' )
2022-09-01 15:10:40 +00:00
-> opt ( 'prune' , 'comb and prune the s3 bucket backups to reduce storage mass' )
-> opt ( 'dry-run' , 'do not actually delete things' )
2022-08-20 03:20:49 +00:00
;
$this -> args = $this -> cli -> parse ( $environment [ 'argv' ], true );
$this -> logger = new Logger ( 'syncer' );
$this -> logger -> pushHandler ( new StreamHandler ( '/var/log/syncer.log' , Logger :: DEBUG ));
$stdout = new StreamHandler ( 'php://stdout' , Logger :: DEBUG );
$stdout -> setFormatter ( new ColoredLineFormatter ( null , " %level_name%: %message% \n " ));
$this -> logger -> pushHandler ( $stdout );
2022-08-23 12:19:05 +00:00
$this -> storageFilesystem = new StorageFilesystem ();
$this -> localFilesystem = new LocalFilesystem ();
2023-03-22 07:12:05 +00:00
if ( ! isset ( $environment [ 'S3_API_KEY' ]) || ! isset ( $environment [ 'S3_API_SECRET' ])){
$this -> logger -> warning ( sprintf ( '%s S3_API_KEY/S3_API_SECRET missing, so running in non-storing mode like a normal database.' , Emoji :: CHARACTER_NERD_FACE ));
sleep ( 60 );
exit ;
}
2022-08-23 12:19:05 +00:00
if ( $this -> args -> hasOpt ( 'postgres' ) || isset ( $environment [ 'PG_VERSION' ])) {
// Postgres mode is enabled if --postgres is set, or PG_VERSION envvar is set,
// which it is when we're built ontop of the postgres docker container
$this -> logger -> debug ( sprintf ( '%s Starting in postgres mode' , Emoji :: CHARACTER_HOURGLASS_NOT_DONE ));
2022-08-28 09:38:02 +00:00
$this -> syncer = new PostgresSyncer ( $this -> logger , $this -> storageFilesystem , $this -> localFilesystem );
} elseif ( $this -> args -> hasOpt ( 'mysql' ) || isset ( $environment [ 'MARIADB_VERSION' ])) {
2022-08-23 12:19:05 +00:00
$this -> logger -> debug ( sprintf ( '%s Starting in mysql mode' , Emoji :: CHARACTER_HOURGLASS_NOT_DONE ));
2022-08-28 09:38:02 +00:00
$this -> syncer = new MysqlSyncer ( $this -> logger , $this -> storageFilesystem , $this -> localFilesystem );
2022-08-20 03:20:49 +00:00
} else {
$this -> logger -> critical ( sprintf ( '%s Must be started in either --mysql or --postgres mode!' , Emoji :: CHARACTER_NERD_FACE ));
exit ;
}
}
public function run () : void
{
if ( $this -> args -> hasOpt ( 'push' )) {
2022-09-01 15:10:40 +00:00
$this -> logger -> info ( sprintf ( ' %s Running push' , Emoji :: upArrow ()));
2022-08-20 03:20:49 +00:00
$this -> syncer -> push ();
} elseif ( $this -> args -> hasOpt ( 'pull' )) {
2022-09-01 15:10:40 +00:00
$this -> logger -> info ( sprintf ( ' %s Running pull' , Emoji :: downArrow ()));
2022-08-20 03:20:49 +00:00
$this -> syncer -> pull ();
2022-09-01 15:10:40 +00:00
} elseif ( $this -> args -> hasOpt ( 'prune' )) {
$this -> logger -> info ( sprintf ( ' %s Running pruner' , Emoji :: recyclingSymbol ()));
$this -> syncer -> prune ( $this -> args -> hasOpt ( 'dry-run' ));
2022-08-20 03:20:49 +00:00
} else {
$this -> logger -> critical ( sprintf ( '%s Must be run in either --push or --pull mode!' , Emoji :: CHARACTER_NERD_FACE ));
exit ;
}
}
}