Stubbing out cli & container.

This commit is contained in:
Greyscale 2022-08-20 05:20:49 +02:00
commit 9738cc182c
No known key found for this signature in database
GPG key ID: 74BAFF55434DA4B2
13 changed files with 3717 additions and 0 deletions

23
.github/workflows/build.yml vendored Normal file
View file

@ -0,0 +1,23 @@
name: Build
on:
push:
workflow_dispatch:
jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- name: Git Checkout
uses: actions/checkout@v3
- uses: docker/login-action@v1
name: Login to Docker Hub
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_PAT }}
- name: Bake
uses: docker/bake-action@v2.1.0
with:
#files: bake.yml
push: true

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
.idea
/vendor/

22
.php-cs-fixer.php Normal file
View file

@ -0,0 +1,22 @@
<?php
$finder = PhpCsFixer\Finder::create();
$finder->in(__DIR__);
return (new PhpCsFixer\Config)
->setRiskyAllowed(true)
->setHideProgress(false)
->setRules([
'@PSR2' => true,
'strict_param' => true,
'array_syntax' => ['syntax' => 'short'],
'@PhpCsFixer' => true,
'@PHP73Migration' => true,
'no_php4_constructor' => true,
'no_unused_imports' => true,
'no_useless_else' => true,
'no_superfluous_phpdoc_tags' => true,
'void_return' => true,
'yoda_style' => false,
])
->setFinder($finder)
;

47
Dockerfile Normal file
View file

@ -0,0 +1,47 @@
ARG PGSQL_VERSION
FROM postgres:$PGSQL_VERSION-alpine AS postgres
RUN apk add --no-cache runit
RUN apk --no-cache --repository https://dl-cdn.alpinelinux.org/alpine/edge/main add \
icu-libs \
&&\
apk --no-cache --repository https://dl-cdn.alpinelinux.org/alpine/edge/community add \
# Current packages don't exist in other repositories
libavif \
&& \
apk add --no-cache --repository http://dl-cdn.alpinelinux.org/alpine/edge/testing/ --allow-untrusted gnu-libiconv \
# Packages
tini \
php81 \
php81-dev \
php81-common \
php81-gd \
php81-xmlreader \
php81-bcmath \
php81-ctype \
php81-curl \
php81-exif \
php81-iconv \
php81-intl \
php81-mbstring \
php81-opcache \
php81-openssl \
php81-pcntl \
php81-phar \
php81-session \
php81-xml \
php81-xsl \
php81-zip \
php81-zlib \
php81-dom \
php81-fpm \
php81-sodium \
php81-tokenizer \
# Iconv Fix
php81-pecl-apcu \
&& ln -s /usr/bin/php81 /usr/bin/php
COPY start.sh /usr/local/bin/start.sh
COPY postgres.runit /etc/service/postgres/run
WORKDIR /sync
COPY . /sync
RUN chmod +x /sync/sync
CMD ["start.sh"]

32
composer.json Normal file
View file

@ -0,0 +1,32 @@
{
"name": "matthewbaggett/s3db",
"type": "project",
"require": {
"php": ">8.1",
"ext-json": "*",
"ext-curl": "*",
"kint-php/kint": "^3",
"league/flysystem-aws-s3-v3": "^3.2",
"league/flysystem": "^3.2",
"vanilla/garden-cli": "~2.0",
"monolog/monolog": "^2.2",
"bramus/monolog-colored-line-formatter": "~3.0",
"adambrett/shell-wrapper": "dev-master",
"spatie/emoji": "^2.3"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.0"
},
"license": "GPL",
"autoload": {
"psr-4": {
"S3DB\\Sync\\": "syncer/"
}
},
"authors": [
{
"name": "Matthew Baggett",
"email": "matthew@baggett.me"
}
]
}

3429
composer.lock generated Normal file

File diff suppressed because it is too large Load diff

35
docker-compose.yml Normal file
View file

@ -0,0 +1,35 @@
version: "3.7"
services:
minio:
image: quay.io/minio/minio:RELEASE.2022-08-13T21-54-44Z
command: server --console-address ":9001" http://minio{1...4}/data{1...2}
expose:
- "9000"
- "9001"
environment:
MINIO_ROOT_USER: &s3_key minio
MINIO_ROOT_PASSWORD: &s3_secret changeme
healthcheck:
test: [ "CMD", "curl", "-f", "http://localhost:9000/minio/health/live" ]
interval: 30s
timeout: 20s
retries: 3
postgres-14:
image: benzine/postgres:14
build:
context: .
target: postgres
args:
PGSQL_VERSION: 14
environment:
POSTGRES_USER: example
POSTGRES_PASSWORD: changeme
S3_ENDPOINT: http://minio:9000/
S3_API_KEY: *s3_key
S3_API_SECRET: *s3_secret
ports:
- "127.0.0.127:5432:5432"
depends_on:
- minio

6
postgres.runit Normal file
View file

@ -0,0 +1,6 @@
#!/bin/bash
echo "Running docker-entrypoint"
/usr/local/bin/docker-entrypoint.sh postgres
sleep 60

11
start.sh Executable file
View file

@ -0,0 +1,11 @@
#!/usr/bin/env bash
# Fix for windows hosts manging run files
dos2unix /etc/service/*/run
# Fix permissions on run files
chmod +x /etc/service/*/run
# Start Runit.
echo "Starting Runit."
runsvdir -P /etc/service

8
sync Normal file
View file

@ -0,0 +1,8 @@
#!/usr/bin/env php
<?php
use S3DB\Sync\Sync;
require_once 'vendor/autoload.php';
(new Sync())->run();

21
syncer/AbstractSyncer.php Normal file
View file

@ -0,0 +1,21 @@
<?php
namespace S3DB\Sync;
use Monolog\Logger;
abstract class AbstractSyncer
{
public function __construct(
protected Logger $logger
) {
}
abstract public function push();
abstract public function pull();
public function uploadToS3(): void
{
}
}

View file

@ -0,0 +1,14 @@
<?php
namespace S3DB\Sync;
class PostgresAbstractSyncer extends AbstractSyncer
{
public function push(): void
{
}
public function pull(): void
{
}
}

66
syncer/Sync.php Normal file
View file

@ -0,0 +1,66 @@
<?php
namespace S3DB\Sync;
use Bramus\Monolog\Formatter\ColoredLineFormatter;
use Garden\Cli\Args;
use Garden\Cli\Cli;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Spatie\Emoji\Emoji;
class Sync
{
protected Logger $logger;
protected Cli $cli;
protected Args $args;
protected AbstractSyncer $syncer;
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')
;
$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);
if ($this->args->hasOpt('postgres')) {
$this->logger->debug(sprintf('%s Starting in postgres mode', Emoji::CHARACTER_HOURGLASS_NOT_DONE));
$this->syncer = new PostgresAbstractSyncer($this->logger);
} elseif ($this->args->hasOpt('mysql')) {
$this->logger->debug(sprintf('%s Starting in mysql mode', Emoji::CHARACTER_HOURGLASS_NOT_DONE));
exit('Not implemented yet');
} 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')) {
$this->logger->debug(sprintf('%s Running push', Emoji::upArrow()));
$this->syncer->push();
} elseif ($this->args->hasOpt('pull')) {
$this->logger->debug(sprintf('%s Running pull', Emoji::downArrow()));
$this->syncer->pull();
} else {
$this->logger->critical(sprintf('%s Must be run in either --push or --pull mode!', Emoji::CHARACTER_NERD_FACE));
exit;
}
}
}