Skip to content

Commit

Permalink
Merge pull request #3 from squ1rr3lly/fix_archiving
Browse files Browse the repository at this point in the history
Fix archiving/backup, update pdf library, clean up Dockerfile
  • Loading branch information
squ1rr3lly authored Aug 24, 2020
2 parents 4625c19 + 79515bc commit 96f5034
Show file tree
Hide file tree
Showing 12 changed files with 192 additions and 114 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ Homestead.yaml
npm-debug.log
package-lock.json
yarn-error.log
docker-compose.override.yml
35 changes: 0 additions & 35 deletions Dockerfile

This file was deleted.

93 changes: 93 additions & 0 deletions Dockerfile.shaark
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
FROM php:alpine
MAINTAINER Shaark contributors <https://github.com/MarceauKa/shaark>

WORKDIR /app
COPY . /app

# Install packages needed for shaark
RUN apk add --no-cache \
bash \
openssl \
zip \
unzip \
oniguruma-dev \
zlib-dev \
libpng-dev \
libzip-dev \
postgresql-dev \
gmp \
gmp-dev \
python3 \
git \
libcap \
mariadb-client \
nodejs \
npm \
busybox-suid

# Set inheritied capabilities on entrypoint
RUN setcap cap_net_raw+eip /app/app/entrypoint-shaark.sh && \
setcap cap_sys_admin+eip /app/app/entrypoint-shaark.sh && \
setcap cap_net_bind_service=+ep `which php`

# Installs latest Chromium (83) package.
RUN apk add --no-cache \
chromium \
nss \
freetype \
freetype-dev \
harfbuzz \
ca-certificates \
ttf-freefont

# Set environment variables
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true \
PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium-browser \
DB_HOST="mariadb" \
REDIS_HOST="redis" \
APP_ENV="production" \
APP_DEBUG="false" \
APP_MIGRATE_DB="true" \
CACHE_DRIVER="redis" \
QUEUE_CONNECTION="redis" \
SESSION_DRIVER="redis" \
REDIS_HOST="redis"

# Puppeteer v3.1.0 works with Chromium 83.
RUN npm install puppeteer@3.1.0

# Add user so we don't have to run everything as root
RUN addgroup -S shaark && adduser -S -G shaark shaarkuser \
&& mkdir -p /home/shaarkuser/Downloads \
&& chown -R shaarkuser:shaark /home/shaarkuser \
&& chown -R shaarkuser:shaark /app

# Install youtube-dl binary
RUN curl -L https://yt-dl.org/downloads/latest/youtube-dl -o /usr/bin/youtube-dl && \
chmod a+rx /usr/bin/youtube-dl

# Make sure python binary is python3
RUN if [ ! -e /usr/bin/python ]; then ln -sf /usr/bin/python3 /usr/bin/python; fi

# Install composer and php extensions
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer && \
docker-php-ext-install pdo mbstring gd exif zip sockets pdo_mysql pgsql pdo_pgsql gmp bcmath

# Configure Backups cron
RUN crontab -u shaarkuser app/crontab

# Run everything after as non-privileged user.
USER shaarkuser

RUN composer install --no-dev -o

RUN cp .env.example .env && \
\
php artisan optimize && \
php artisan view:clear && \
\
php artisan key:generate && \
php artisan storage:link

EXPOSE 80
ENTRYPOINT [ "app/entrypoint-shaark.sh" ]
6 changes: 3 additions & 3 deletions app/Http/Controllers/Api/Manage/FeaturesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ protected function checkArchivePdf(Shaark $shaark)
return $this->sendError(__('Your node path is unreachable: :path', ['path' => $exec]));
}

$dir = base_path('node_modules/puppeteer/.local-chromium');
$dir = base_path('vendor/spatie/browsershot');

if (false === is_dir($dir)) {
return $this->sendError(__('Puppeteer dependencies not installed, run `npm install @nesk/puphpeteer --no-save`'));
return $this->sendError(__('Puppeteer dependencies not installed, run `composer require spatie/browsershot`'));
}

try {
$name = LinkArchive::archive(url()->route('home'), 'pdf');
$name = LinkArchive::archive('http://example.com', 'pdf');
} catch (\Exception $e) {
return $this->sendError(__('Unable to create archive, error is: :message', ['message' => $e->getMessage()]));
}
Expand Down
53 changes: 53 additions & 0 deletions app/Services/LinkArchive/BrowsershotProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace App\Services\LinkArchive;

use Spatie\Browsershot\Browsershot;

class BrowsershotProvider extends BaseProvider
{
public function makeArchive(): ?string
{
$name = md5($this->url) . '.pdf';
$filename = sprintf('app/archives/%s', $name);
$windowWidth = app('shaark')->getArchivePdfWidth();
$windowHeight = app('shaark')->getArchivePdfHeight();
$nodeBin = app('shaark')->getNodeBin();

try {
$browsershot = new Browsershot($this->url, true);
$browsershot
->windowSize($windowWidth, $windowHeight)
->margins(0,0,0,0)
->setNodeBinary($nodeBin)
->setNodeModulePath('node_modules/')
->setIncludePath('/usr/bin/')
->showBackground()
->addChromiumArguments([
'disable-dev-shm-usage'
])
->noSandbox()
->ignoreHttpsErrors()
->dismissDialogs()
->waitUntilNetworkIdle()
->emulateMedia('screen')
->save(storage_path($filename))
;
} catch (\Exception $e) {
throw new \RuntimeException("Unable to create link archive", 0, $e);
}

return $name;
}

public function isEnabled(): bool
{
return app('shaark')->getLinkArchivePdf() === true;
}

public function canArchive(): bool
{
return true;
}
}

2 changes: 1 addition & 1 deletion app/Services/LinkArchive/LinkArchive.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class LinkArchive
/** @var array $providers */
public static $providers = [
'media' => YoutubeDlProvider::class,
'pdf' => PuppeteerProvider::class,
'pdf' => BrowsershotProvider::class,
];

public static function availableFor(string $url): array
Expand Down
58 changes: 0 additions & 58 deletions app/Services/LinkArchive/PuppeteerProvider.php

This file was deleted.

10 changes: 10 additions & 0 deletions app/crontab
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# do daily/weekly/monthly maintenance
# min hour day month weekday command
*/15 * * * * run-parts /etc/periodic/15min
0 * * * * run-parts /etc/periodic/hourly
0 2 * * * run-parts /etc/periodic/daily
0 3 * * 6 run-parts /etc/periodic/weekly
0 5 1 * * run-parts /etc/periodic/monthly
# run backups configured by Shaark
* * * * * cd /app && php artisan schedule:run >> /dev/null 2>&1

26 changes: 17 additions & 9 deletions app/entrypoint-shaark.sh
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
#!/bin/bash
FILE=.app_initialized

cd /app
echo "Clearing any cached config."
php artisan config:clear
if [ ! -f $FILE ]; then
echo "Migrating database and creating default Admin user."
php artisan migrate --seed --force
echo "Admin Username: admin@example.com"
echo "Admin Password: secret"
touch $FILE
elif [ "${APP_MIGRATE_DB}" = 'true' ]; then
if [ "`php artisan migrate:status`" = "Migration table not found." ]; then
echo "Migrating database and creating default Admin user."
php artisan migrate --seed --force
echo "Admin Username: admin@example.com"
echo "Admin Password: "${APP_ADMIN_PASSWORD}
elif [ "${APP_MIGRATE_DB}" = 'true' ] && \
[ `php artisan migrate:status|cut -d'|' -f2 |grep -c "No"` -gt 0 ]; then
echo "Migrating database."
php artisan migrate --force
else
echo "Database migration skipped."
fi
php artisan serve --host=0.0.0.0 --port=80

if [ "${APP_DEBUG}" = 'true' ]; then
echo "Debugging enabled: creating verbose logs at /app/storage/logs/"
php artisan queue:work >> storage/logs/artisan_queue.log &
php artisan serve --host=0.0.0.0 --port=80 -vvv >> storage/logs/artisan_serve.log
else
echo "Starting Shaark!"
php artisan queue:work &
php artisan serve --host=0.0.0.0 --port=80
fi
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
"laravel/tinker": "^1.0",
"maatwebsite/excel": "^3.1",
"mews/captcha": "^3.0",
"nesk/puphpeteer": "^1.6",
"norkunas/youtube-dl-php": "^1.6",
"predis/predis": "^1.1",
"spatie/browsershot": "^3.37",
"spatie/laravel-backup": "^6.11",
"spatie/laravel-medialibrary": "^7.0.0",
"spatie/valuestore": "^1.2",
Expand Down
2 changes: 1 addition & 1 deletion database/seeds/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function run()
DB::table('users')->insert([
'name' => 'Admin',
'email' => 'admin@example.com',
'password' => Hash::make('secret'),
'password' => Hash::make(env('APP_ADMIN_PASSWORD', 'secret')),
'api_token' => 'api-token-secret',
'is_admin' => 1,
'created_at' => now()->toDateTimeString(),
Expand Down
18 changes: 12 additions & 6 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
version: '3.7'
services:
shaark:
image: shaark
build: .
image: shaark:${SHAARK_IMAGE_TAG:-latest}
build:
context: ./
dockerfile: Dockerfile.shaark
restart: unless-stopped
ports:
- target: 80
published: ${SHAARK_PORT:-80}
published: ${SHAARK_PORT:-8080}
protocol: tcp
mode: host
depends_on:
Expand All @@ -18,9 +20,12 @@ services:
APP_ENV: "production"
APP_DEBUG: ${SHAARK_DEBUG:-false}
APP_URL: ${SHAARK_URL:-http://localhost}
APP_ADMIN_PASSWORD: ${SHAARK_ADMIN_PASSWORD:-secret}
DB_PASSWORD: ${SHAARK_DATABASE_PASSWORD:-secret}
DB_USER: ${SHAARK_DATABASE_USER:-homestead}
DB_DATABASE: ${SHAARK_DATABASE_NAME:-homestead}
volumes:
- storage:/app/storage
logging:
driver: "json-file"
options:
Expand All @@ -33,7 +38,7 @@ services:
volumes:
- mariadb:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: ${SHAARK_MYSQL_ROOT_PASSWORD:-rootpassword9867ow3q459087w980}
MYSQL_RANDOM_ROOT_PASSWORD: 'true'
MYSQL_PASSWORD: ${SHAARK_DATABASE_PASSWORD:-secret}
MYSQL_USER: ${SHAARK_DATABASE_USER:-homestead}
MYSQL_DATABASE: ${SHAARK_DATABASE_NAME:-homestead}
Expand All @@ -55,5 +60,6 @@ services:
max-file: "5"

volumes:
redis: {}
mariadb: {}
redis:
mariadb:
storage:

0 comments on commit 96f5034

Please sign in to comment.