Moved printing into its own separate project called frank. It also has its own toml and deployement system.

It also works on a raspberry pi
This commit is contained in:
Amritanshu Agrawal 2021-03-23 18:18:33 +05:30
parent 0b30ce258c
commit f4caa19bb2
17 changed files with 184 additions and 67 deletions

View File

@ -21,6 +21,5 @@ docker build --tag barker:latest .
docker build --tag barker-worker:latest --file worker.Dockerfile .
cd "$parent_path/docker" || exit
docker save barker:latest | bzip2 | pv | ssh tanshu@knox.tanshu.com 'bunzip2 | sudo docker load'
docker save barker-worker:latest | bzip2 | pv | ssh tanshu@knox.tanshu.com 'bunzip2 | sudo docker load'
ansible-playbook playbook-chd.yml
ansible-playbook playbook-pkl.yml

View File

@ -0,0 +1,24 @@
FROM python:latest
RUN mkdir /app
WORKDIR /app
ADD https://git.tanshu.com/tanshu/barker/raw/branch/main/frank/pyproject.toml /app/pyproject.toml
# Install Poetry
RUN curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | POETRY_HOME=/opt/poetry python && \
cd /usr/local/bin && \
ln -s /opt/poetry/bin/poetry && \
poetry config virtualenvs.create false && \
cd /app && \
poetry install --no-root --no-dev
ADD https://git.tanshu.com/api/v1/repos/tanshu/barker/tags /tags.json
RUN git clone --single-branch --depth 1 https://git.tanshu.com/tanshu/barker.git /repo
RUN mv /repo/frank/* /app
ENV PYTHONPATH=/app
RUN chmod 777 /app/worker-start.sh \
&& ln -s /app/worker-start.sh /usr/local/bin/worker-start.sh \
&& ln -s /app/worker-start.sh /
CMD ["bash", "worker-start.sh"]

View File

@ -1,8 +0,0 @@
FROM barker:latest
LABEL maintainer="Amritanshu <docker@tanshu.com>"
RUN chmod 777 /app/worker-start.sh \
&& ln -s /app/worker-start.sh /usr/local/bin/worker-start.sh \
&& ln -s /app/worker-start.sh /
CMD ["bash", "worker-start.sh"]

View File

@ -1,3 +0,0 @@
WORKER_REDIS_HOST=redis
WORKER_REDIS_PORT=6379
WORKER_QUEUE_NAME="DJ Computer"

3
docker/files/frank.env Normal file
View File

@ -0,0 +1,3 @@
REDIS_HOST=localhost
REDIS_PORT=6379
QUEUE_NAME=DJ Computer

View File

@ -1,3 +0,0 @@
WORKER_REDIS_HOST=redis
WORKER_REDIS_PORT=6379
WORKER_QUEUE_NAME="Ground Floor Printer"

View File

@ -21,26 +21,11 @@
state: present
source: build
- name: Build barker worker image
docker_image:
name: barker-worker:latest
build:
path: /tmp/app/
dockerfile: /tmp/app/worker.Dockerfile
pull: yes
state: present
source: build
- name: Upload the .env file
template:
src: "files/chd.env"
dest: "/var/lib/{{ host_directory }}/.env"
- name: Upload the worker .env file
template:
src: "files/chd-worker.env"
dest: "/var/lib/{{ host_directory }}/worker.env"
- name: Create barker container
docker_container:
name: "{{ host_directory }}"
@ -56,17 +41,6 @@
volumes:
- "/var/lib/{{ host_directory }}/frontend:/frontend"
- name: Create barker worker container
docker_container:
name: "{{ host_directory }}-worker"
image: barker-worker:latest
state: started
restart_policy: "unless-stopped"
env_file: "/var/lib/{{ host_directory }}/worker.env"
links:
- "redis:redis"
- name: Check if Nginx conf file exists
stat: path="/etc/nginx/sites-available/{{ http_conf }}"
register: status

38
docker/playbook-frank.yml Executable file
View File

@ -0,0 +1,38 @@
#################################################
# DO Community Playbooks: Docker
#################################################
---
- hosts: all
become: true
vars_files:
- vars/frank.yml
tasks:
- name: Copy dockerfile
synchronize: src=app dest=/tmp
- name: Build frank image
docker_image:
name: frank:latest
build:
path: /tmp/app/
dockerfile: /tmp/app/frank.Dockerfile
pull: yes
state: present
source: build
- name: Upload the worker .env file
template:
src: "files/frank.env"
dest: "/home/pi/frank.env"
- name: Create frank worker container
docker_container:
name: "frank"
image: frank:latest
state: started
restart_policy: "unless-stopped"
env_file: "/home/pi/frank.env"
network_mode: "host"
devices:
- "{{ printer_port }}:/printer"

View File

@ -21,26 +21,11 @@
state: present
source: build
- name: Build barker worker image
docker_image:
name: barker-worker:latest
build:
path: /tmp/app/
dockerfile: /tmp/app/worker.Dockerfile
pull: yes
state: present
source: build
- name: Upload the .env file
template:
src: "files/pkl.env"
dest: "/var/lib/{{ host_directory }}/.env"
- name: Upload the worker .env file
template:
src: "files/pkl-worker.env"
dest: "/var/lib/{{ host_directory }}/worker.env"
- name: Create barker container
docker_container:
name: "{{ host_directory }}"
@ -56,17 +41,6 @@
volumes:
- "/var/lib/{{ host_directory }}/frontend:/frontend"
- name: Create barker worker container
docker_container:
name: "{{ host_directory }}-worker"
image: barker-worker:latest
state: started
restart_policy: "unless-stopped"
env_file: "/var/lib/{{ host_directory }}/worker.env"
links:
- "redis:redis"
- name: Check if Nginx conf file exists
stat: path="/etc/nginx/sites-available/{{ http_conf }}"
register: status

2
docker/vars/frank.yml Normal file
View File

@ -0,0 +1,2 @@
---
printer_port: "/dev/usb/lp0"

9
frank.sh Executable file
View File

@ -0,0 +1,9 @@
#!/usr/bin/env bash
parent_path=$( cd "$(dirname "${BASH_SOURCE[0]}")" || exit ; pwd -P )
git push
git tag --delete latest
git tag latest
git push --delete origin latest
git push --tags
cd "$parent_path/docker" || exit
ansible-playbook --limit moh1 playbook-frank.yml

16
frank/config.py Normal file
View File

@ -0,0 +1,16 @@
from dotenv import load_dotenv
from pydantic import BaseSettings
class Settings(BaseSettings):
REDIS_HOST: str = "127.0.0.1"
REDIS_PORT: int = 6379
QUEUE_NAME: str = "arq:queue"
class Config:
case_sensitive = True
env_file = ".env"
load_dotenv()
settings = Settings()

38
frank/printing.py Normal file
View File

@ -0,0 +1,38 @@
import re # noqa: F401
import socket # noqa: F401
import sys # noqa: F401
from arq import Retry
async def sent_to_printer(ctx: dict, data: str, address: str, cut_code: str):
address = "/printer"
try:
print("Printing to :", address, "\n", data, "\n")
with open(address, "w") as printer:
printer.write(data)
printer.write("\n")
print(cut_code)
printer.write(cut_code)
except LookupError as e:
print("Lookup error:", e)
raise Retry(defer=ctx["job_try"] * 30)
except FileNotFoundError as e:
print("File not found error:", e)
raise Retry(defer=ctx["job_try"] * 30)
except: # noqa: E722
print("Unexpected error:", sys.exc_info()[0])
# retry the job with increasing back-off
# delays will be 5s, 10s, 15s, 20s
# after max_tries (default 5) the job will permanently fail
raise Retry(defer=ctx["job_try"] * 30)
# GS = "\x1d"
# PAPER_CUT = GS + "V"
# # For the printer ESC/POS Reference
# # https://reference.epson-biz.com/modules/ref_escpos/index.php?content_id=87
# # The m codes are in decimal and can be converted using the following table
# # https://www.eso.org/~ndelmott/ascii.html
# # The \x03 in all the following is to feed 3 lines before cut it can be increased or reduced
# FUNCTION_B_FULL_CUT = "\x41\x03"
# FUNCTION_B_PARTIAL_CUT = "\x42\x03"

15
frank/pyproject.toml Normal file
View File

@ -0,0 +1,15 @@
[tool.poetry]
name = "frank"
version = "7.3.0"
description = "Point of Sale for a restaurant"
authors = ["tanshu <git@tanshu.com>"]
[tool.poetry.dependencies]
python = "^3.8"
pydantic = {extras = ["dotenv"], version = "^1.8.1"}
arq = "^0.19.1"
aiohttp = "^3.7.4"
[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

4
frank/settings.py Normal file
View File

@ -0,0 +1,4 @@
from arq.connections import RedisSettings
from config import settings as sett
settings = RedisSettings(host=sett.REDIS_HOST, port=sett.REDIS_PORT)

5
frank/worker-start.sh Executable file
View File

@ -0,0 +1,5 @@
#! /usr/bin/env bash
set -e
echo arq worker.WorkerSettings
arq worker.WorkerSettings

30
frank/worker.py Normal file
View File

@ -0,0 +1,30 @@
import sys
from aiohttp import ClientSession
from config import settings as sett
from printing import sent_to_printer
from settings import settings
sys.path.extend(["./"])
async def startup(ctx):
ctx["session"] = ClientSession()
print(f"Worker listening for: {sett.QUEUE_NAME}")
# print(f"Worker printing on: {sett.WORKER_PRINTER_ADDRESS}")
async def shutdown(ctx):
await ctx["session"].close()
class WorkerSettings:
"""
Settings for the ARQ worker.
"""
queue_name = f"barker:print:{sett.QUEUE_NAME}"
redis_settings = settings
functions: list = [sent_to_printer]
on_startup = startup
on_shutdown = shutdown