Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert shell scripts to single Python script #48

Merged
merged 20 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/publishBranchesPRs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ name: PR - Create and publish a Docker image
on:
pull_request:
#push:
# branches:
# branches:
# - '*'
# - '!master'

Expand All @@ -22,10 +22,10 @@ jobs:
permissions:
contents: read
packages: write
#
#
steps:
- name: Checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4
# Uses the `docker/login-action` action to log in to the Container registry registry using the account and password that will publish the packages. Once published, the packages are scoped to the account defined here.
- name: Log in to the Container registry
uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1
Expand All @@ -36,7 +36,7 @@ jobs:
# This step uses [docker/metadata-action](https://github.com/docker/metadata-action#about) to extract tags and labels that will be applied to the specified image. The `id` "meta" allows the output of this step to be referenced in a subsequent step. The `images` value provides the base name for the tags and labels.
- name: Extract branch name
shell: bash
run: echo "branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> $GITHUB_OUTPUT
run: echo "branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> "$GITHUB_OUTPUT"
id: extract_branch
- name: Extract metadata (tags, labels) for Docker
id: meta
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/publishMaster.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ name: Master - Create and publish a Docker image
# Configures this workflow to run every time a change is pushed to the branch called `master`.
on:
push:
branches: ['master']
branches: ["master"]

# Defines two custom environment variables for the workflow. These are used for the Container registry domain, and a name for the Docker image that this workflow builds.
env:
Expand All @@ -19,10 +19,10 @@ jobs:
permissions:
contents: read
packages: write
#
#
steps:
- name: Checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4
# Uses the `docker/login-action` action to log in to the Container registry registry using the account and password that will publish the packages. Once published, the packages are scoped to the account defined here.
- name: Log in to the Container registry
uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/publishTags.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ name: Tags - Create and publish a Docker image
# Build when a tag is created
on:
push:
tags:
- '**'
tags:
- "**"

# Defines two custom environment variables for the workflow. These are used for the Container registry domain, and a name for the Docker image that this workflow builds.
env:
Expand All @@ -20,10 +20,10 @@ jobs:
permissions:
contents: read
packages: write
#
#
steps:
- name: Checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4
# Uses the `docker/login-action` action to log in to the Container registry registry using the account and password that will publish the packages. Once published, the packages are scoped to the account defined here.
- name: Log in to the Container registry
uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1
Expand Down
47 changes: 47 additions & 0 deletions .github/workflows/superLinter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
# This workflow executes several linters on changed files based on languages used in your code base whenever
# you push a code or open a pull request.
#
# You can adjust the behavior by modifying this file.
# For more information, see:
# https://github.com/github/super-linter
name: Lint Code Base

on: # yamllint disable-line rule:truthy
push:
branches: ["master"]
pull_request:
branches: ["master"]

permissions:
contents: read

jobs:
run-lint:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
# Full git history is needed to get a proper list of changed files within `super-linter`
fetch-depth: 0

- name: Lint Code Base
uses: github/super-linter/slim@v7
env:
DISABLE_ERRORS: true
VALIDATE_ALL_CODEBASE: false
VALIDATE_CHECKOV: false
VALIDATE_EDITORCONFIG: false
VALIDATE_JSCPD: false
VALIDATE_MARKDOWN: false
VALIDATE_PYTHON: true
VALIDATE_PYTHON_PYLINT: false
VALIDATE_YAML: false
VALIDATE_YAML_PRETTIER: false
DEFAULT_BRANCH: master
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
LINTER_RULES_PATH: /
DOCKERFILE_HADOLINT_FILE_NAME: .hadolint.yml
MARKDOWN_CONFIG_FILE: .markdown-lint.yml
YAML_CONFIG_FILE: .yamllint.yml
3 changes: 3 additions & 0 deletions .hadolint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ignored:
- DL3008 # Pin versions in apt get install. Instead of `apt-get install <package>` use `apt-get install <package>=<version>`
- DL3018 # Pin versions in apk add. Instead of `apk add <package>` use `apk add <package>=<version>`
11 changes: 11 additions & 0 deletions .yamllint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
extends: default

rules:
document-start: disable
line-length:
max: 180
level: warning
comments:
# Changed this to stop a mess between linters from Prettier (vscode) to yamllint
# - https://github.com/prettier/prettier/pull/10926
min-spaces-from-content: 1
95 changes: 44 additions & 51 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,82 +1,76 @@
#FROM ubuntu:16.04
FROM ubuntu:22.04
FROM python:slim-bullseye

RUN DEBIAN_FRONTEND=noninteractive apt-get update && apt-get -y install tzdata && apt-get -y clean

RUN apt-get update && apt-get install -y --no-install-recommends apt-utils && apt-get -y clean

RUN apt-get -y update && apt-get -y upgrade && apt-get -y clean
RUN apt-get -y install \
sane \
sane-utils \
RUN <<EOF
apt-get update && \
apt-get -y --no-install-recommends install \
curl \
ghostscript \
netpbm \
x11-common \
wget \
graphicsmagick \
curl \
ssh \
sshpass \
iproute2 \
lighttpd \
netbase \
netpbm \
pdftk \
php-cgi \
php-curl \
sudo \
iproute2 \
jq \
bc \
pdftk \
poppler-utils \
&& apt-get -y clean

RUN cd /tmp && \
wget https://download.brother.com/welcome/dlf105200/brscan4-0.4.11-1.amd64.deb && \
dpkg -i /tmp/brscan4-0.4.11-1.amd64.deb && \
rm /tmp/brscan4-0.4.11-1.amd64.deb

RUN cd /tmp && \
wget https://download.brother.com/welcome/dlf006652/brscan-skey-0.3.1-2.amd64.deb && \
dpkg -i /tmp/brscan-skey-0.3.1-2.amd64.deb && \
rm /tmp/brscan-skey-0.3.1-2.amd64.deb

ADD files/runScanner.sh /opt/brother/runScanner.sh
python3 \
sane \
sane-utils \
ssh \
sshpass \
sudo \
tzdata \
wget \
x11-common && \
apt-get -y clean && \
rm -rf /var/lib/apt/lists/* && \
pip install --no-cache-dir requests==2.32.3 && \
wget https://download.brother.com/welcome/dlf105200/brscan4-0.4.11-1.amd64.deb --progress=dot:giga -O /tmp/brscan4.deb && \
wget https://download.brother.com/welcome/dlf006652/brscan-skey-0.3.2-0.amd64.deb --progress=dot:giga -O /tmp/brscan-skey.deb && \
dpkg -i --force-all /tmp/brscan4.deb && \
dpkg -i --force-all /tmp/brscan-skey.deb && \
rm -f /tmp/brscan4.deb /tmp/brscan-skey.deb
EOF

COPY files/runScanner.sh /opt/brother/runScanner.sh
COPY files/brscan-skey.config /opt/brother/scanner/brscan-skey/brscan-skey.config
COPY script /opt/brother/scanner/brscan-skey/script

RUN cp /etc/lighttpd/conf-available/05-auth.conf /etc/lighttpd/conf-enabled/
RUN cp /etc/lighttpd/conf-available/15-fastcgi-php.conf /etc/lighttpd/conf-enabled/
RUN cp /etc/lighttpd/conf-available/10-fastcgi.conf /etc/lighttpd/conf-enabled/
RUN mkdir -p /var/run/lighttpd
RUN touch /var/run/lighttpd/php-fastcgi.socket
RUN chown -R www-data /var/run/lighttpd
RUN echo 'www-data ALL=(NAS) NOPASSWD:ALL' >> /etc/sudoers
RUN <<EOF
cp /etc/lighttpd/conf-available/05-auth.conf /etc/lighttpd/conf-enabled/ && \
cp /etc/lighttpd/conf-available/15-fastcgi-php.conf /etc/lighttpd/conf-enabled/ && \
cp /etc/lighttpd/conf-available/10-fastcgi.conf /etc/lighttpd/conf-enabled/ && \
mkdir -p /var/run/lighttpd && \
touch /var/run/lighttpd/php-fastcgi.socket && \
chown -R www-data /var/run/lighttpd && \
echo 'www-data ALL=(NAS) NOPASSWD:ALL' >> /etc/sudoers
EOF

ENV NAME="Scanner"
ENV MODEL="MFC-L2700DW"
ENV IPADDRESS="192.168.1.123"
ENV USERNAME="NAS"
ENV REMOVE_BLANK_THRESHOLD="0.3"

#only set these variables in the compose file, if inotify needs to be triggered (e.g., for Synology Drive):
# Only set these variables in the compose file, if inotify needs to be triggered (e.g., for Synology Drive):
ENV SSH_USER=""
ENV SSH_PASSWORD=""
ENV SSH_HOST=""
ENV SSH_PATH=""

#only set these variables in the compose file, if you need FTP upload:
# Only set these variables in the compose file, if you need FTP upload:
ENV FTP_USER=""
ENV FTP_PASSWORD=""
ENV FTP_HOST=""

#only set these variables in the compose file, if you need Telegram notifications:
# Only set these variables in the compose file, if you need Telegram notifications:
ENV TELEGRAM_TOKEN=""
ENV TELEGRAM_CHATID=""

# Make sure this ends in a slash.
ENV FTP_PATH="/scans/"

EXPOSE 54925
EXPOSE 54921
EXPOSE 80

#ADD files/gui/index.php /var/www/html
#ADD files/gui/main.css /var/www/html
#ADD files/api/scan.php /var/www/html
Expand All @@ -85,9 +79,8 @@ EXPOSE 80
#ADD files/api/download.php /var/www/html
COPY html /var/www/html
RUN chown -R www-data /var/www/

#directory for scans:
VOLUME /scans

CMD /opt/brother/runScanner.sh


CMD ["bash", "-c", "/opt/brother/runScanner.sh"]
8 changes: 8 additions & 0 deletions files/brscan-skey.config
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, just now I said in #53 that 1) there is not an issue, but here it is. If we remove the shell files altogether, we will also need to make sure the web interface is directly calling the python scripts...

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
password=
IMAGE="python3 /opt/brother/scanner/brscan-skey/script/scantoimage.py"
OCR="python3 /opt/brother/scanner/brscan-skey/script/scantoocr.py"
EMAIL="python3 /opt/brother/scanner/brscan-skey/script/scantoemail.py"
FILE="python3 /opt/brother/scanner/brscan-skey/script/scantofile.py"
SEMID=b
eth=
ip_address=
4 changes: 2 additions & 2 deletions files/runScanner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ echo "setting up user & logfile:"

if [[ $NAME == *" "* ]]; then
echo "Do not use spaces in NAME!"
exit -1
exit 1
fi

if [[ -z ${UID} ]]; then
Expand All @@ -16,7 +16,7 @@ groupadd --gid "$GID" NAS
adduser "$NAME" --uid $UID --gid "$GID" --disabled-password --force-badname --gecos ""
mkdir -p /scans
chmod 777 /scans
touch /var/log/scanner.log
echo -n "" >/var/log/scanner.log
chown "$NAME" /var/log/scanner.log
env >/opt/brother/scanner/env.txt
chmod -R 777 /opt/brother
Expand Down
34 changes: 17 additions & 17 deletions html/active.php
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably don't need any of the php cleanups, once #32 is in...

Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
<?php

function isProcessRunning($processName) {
// Execute the pgrep command
$command = "pgrep $processName";
exec($command, $output, $status);
// Check if pgrep returned a status of 0, which means the process was found
if ($status === 0) {
// Process is running
return true;
} else {
// Process is not running
return false;
}
// Execute the pgrep command
$command = "pgrep $processName";
exec($command, $output, $status);

// Check if pgrep returned a status of 0, which means the process was found
if ($status === 0) {
// Process is running
return true;
} else {
// Process is not running
return false;
}
}

// Check if the scanimage, sleep, and curl processes are running
$result = array(
'scan' => isProcessRunning('scanimage'),
'waiting' => isProcessRunning('sleep'),
'ocr' => isProcessRunning('curl')
'scan' => isProcessRunning('scanimage'),
'waiting' => isProcessRunning('sleep'),
'ocr' => isProcessRunning('curl')
);


// Output the result as JSON
header('Content-Type: application/json; charset=utf-8');
echo json_encode($result);

?>
?>
38 changes: 20 additions & 18 deletions html/download.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
<?php

if ($_SERVER['REQUEST_METHOD'] == 'GET') {
if(array_key_exists("file", $_GET)) {
$file = $_GET["file"];
if(str_contains($file, "..") || str_contains($file, "/")) {
header($_SERVER["SERVER_PROTOCOL"] . " 400 OK");
die("Error: Dont't be evil!");
}
$filename="/scans/".$file;
if(file_exists($filename)) {
header("Content-type:application/pdf");
header("Content-Disposition:attachment;filename=\"$file\"");
readfile($filename);
} else {
header($_SERVER["SERVER_PROTOCOL"] . " 400 OK");
die("Error: File does not exist!");
}
if(array_key_exists("file", $_GET)) {
$file = $_GET["file"];
if(str_contains($file, "..") || str_contains($file, "/")) {
header($_SERVER["SERVER_PROTOCOL"] . " 400 OK");
die("Error: Dont't be evil!");
}
$filename="/scans/".$file;
if(file_exists($filename)) {
header("Content-type:application/pdf");
header("Content-Disposition:attachment;filename=\"$file\"");
readfile($filename);
} else {
header($_SERVER["SERVER_PROTOCOL"] . " 400 OK");
die("Error: No file provided!");
header($_SERVER["SERVER_PROTOCOL"] . " 400 OK");
die("Error: File does not exist!");
}
} else {
header($_SERVER["SERVER_PROTOCOL"] . " 400 OK");
die("Error: No file provided!");
}
}
?>

?>
Loading
Loading