first commit

master
Dirk Heilig 2024-04-04 16:15:42 +02:00
commit 7497efbf9b
6 changed files with 199 additions and 0 deletions

21
Dockerfile 100644
View File

@ -0,0 +1,21 @@
FROM debian:12
RUN apt-get update && apt-get upgrade -y && apt-get clean
RUN apt-get install -y supervisor apache2 php libapache2-mod-php php-mbstring php-zip curl git openscad zip
RUN apt-get clean
RUN chown -R www-data:www-data /var/www
RUN curl https://files.openscad.org/snapshots/OpenSCAD-2024.03.28.ai18952-x86_64.AppImage > /tmp/openscad
RUN chmod +x /tmp/openscad
WORKDIR /opt
RUN /tmp/openscad --appimage-extract
RUN ln -s /opt/squashfs-root/usr/bin/openscad /usr/local/bin/openscad
USER 33:33
WORKDIR /var/www
RUN mkdir -p /var/www/html
RUN rm -rf /var/www/html/*
RUN ln -s /var/www/src/index.php /var/www/html/index.php
RUN mkdir queue
RUN git clone https://git.c3re.de/dirk/AuerNameTag.git
ADD supervisord.conf /etc/supervisor/conf.d/supervisord.conf
ADD src /var/www/src
USER root
CMD ["/usr/bin/supervisord", "-c" , "/etc/supervisor/conf.d/supervisord.conf"]

4
devrun 100755
View File

@ -0,0 +1,4 @@
#!/usr/bin/env bash
set -e
docker build -t taggor .
docker run -p 8888:80 -v "$PWD/src:/var/www/src" --rm -it taggor "$@"

112
src/index.php 100644
View File

@ -0,0 +1,112 @@
<?php
if($_SERVER['REQUEST_METHOD'] === 'POST') {
$tags = explode("\n", $_POST['tags']);
$tags = array_map('trim', $tags);
$tags = array_filter($tags, function($tag) {
return !empty($tag);
});
if(empty($tags)){
header("Location: /?err=notags");
exit;
}
$jobId=microtime(1).'_'.uniqid();
#file_put_contents()
$queueDir=__DIR__.'/../queue';
$queueDir=realpath($queueDir);
$queueFile=$queueDir.'/'.$jobId.'.json';
file_put_contents($queueFile, json_encode($tags));
header('Location: /?jobid='.$jobId);
exit;
}
if(isset($_GET['jobid'])){
$jobId=$_GET['jobid'];
$queueDir=__DIR__.'/../queue';
$queueDir=realpath($queueDir);
$queueFile=$queueDir.'/'.$jobId.'.json';
$allJobs=glob($queueDir.'/*.json');
sort($allJobs);
$myPositionInQueue=array_search($queueFile, $allJobs);
if(false === $myPositionInQueue) {
if(isset($_GET['download']) && $_GET['download'] == '1'){
$dlDir=__DIR__.'/../html/dl';
$dlDir=realpath($dlDir);
$zipFile=$dlDir.'/'.$jobId.'.zip';
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="'.basename($zipFile).'"');
readfile($zipFile);
exit;
}else{
echo <<<EOT
<html>
<head>
<title>c3RE AuerBoxTag Generator (Download starting)</title>
<meta http-equiv="refresh" content="1; url='/?jobid=$jobId&download=1'">
</head>
<body>
Your job $jobId is Finished.
<a href="/">Back</a>
</body>
</html>
EOT;
}
header('HTTP/1.1 404 Not Found');
exit;
}
$myPositionInQueue++;
echo <<<EOT
<html>
<head>
<title>c3RE AuerBoxTag Generator ($myPositionInQueue)</title>
<meta http-equiv="refresh" content="1">
</head>
<body>
Your job $jobId is $myPositionInQueue in the queue.
</body>
</html>
EOT;
exit;
}
$err='';
switch($_GET['err']){
case 'notags':
$err='Please provide at least one tag';
break;
}
?>
<html>
<head>
<title>c3RE AuerBoxTag Generator</title>
</head>
<body>
<h1>c3RE AuerBoxTag Generator</h1>
<div style="color:red"><?php echo $err; ?></div>
<form method="post" action="/">
<textarea name="tags" rows="10" cols="30" placeholder="one tag per line please"></textarea>
<br>
<br>
<input type="submit" value="Lets Go!">
</form>
</body>
</html>

7
src/queue 100755
View File

@ -0,0 +1,7 @@
#!/usr/bin/env bash
cd "$( dirname "${BASH_SOURCE[0]}" )"
while :; do
sleep 5 &
./queueWorker >> /var/www/html/log.txt
wait
done

45
src/queueWorker 100755
View File

@ -0,0 +1,45 @@
#!/usr/bin/env php
<?php
echo "starting queue worker\n";
$queueDir=__DIR__.'/../queue';
$queueDir=realpath($queueDir);
$queueItems=glob($queueDir.'/*.json');
$dlDir=__DIR__.'/../html/dl/';
if(!is_dir($dlDir)){
mkdir($dlDir);
}
if(empty($queueItems)){
//clean up old files from dldir older than 24h
$oldFiles=glob($dlDir.'/*.zip');
foreach ($oldFiles as $oldFile){
if(filemtime($oldFile) < time()-24*60*60){
unlink($oldFile);
}
}
sleep(5);
exit;
}
sort($queueItems);
$jobFile=array_shift($queueItems);
echo "working on $jobFile\n";
$jobId=basename($jobFile, '.json');
$tags=json_decode(file_get_contents($jobFile), true);
$zipFile=$dlDir.$jobId.'.zip';
chdir(__DIR__.'/../AuerNameTag');
$shellArgs=array_map('escapeshellarg', $tags);
system("rm -rf out");
system("git reset --hard");
system("./make.sh ". implode(' ', $shellArgs));
chdir("out");
system("zip -r $zipFile .");
chdir('..');
system("rm -rf out");
unlink($jobFile);

10
supervisord.conf 100644
View File

@ -0,0 +1,10 @@
[supervisord]
nodaemon=true
[program:Apache]
command=/usr/sbin/apache2ctl -DFOREGROUND
autostart=true
autorestart=true
[program:queue]
command=/var/www/src/queue
autostart=true
autorestart=true