Skip to content

Commit

Permalink
Merge pull request #25 from hacklabkyiv/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
VovaStelmashchuk authored Dec 2, 2023
2 parents 4e4c4c2 + b3cc161 commit 8ff5c1d
Show file tree
Hide file tree
Showing 51 changed files with 2,319 additions and 861 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/pylint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Pylint

on: [ push ]

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [ "3.10" ]
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Analysing the code with pylint
run: |
pylint app --disable=C0116,C0114,C0115,C0411,E0401,W0611,W0622,W0719,C0103,W1514,R0903,R1732,W0718
- name: Analysing the code with pycodestyle
run: |
pycodestyle app
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Project-specific files
config.cfg
external/
log.txt

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down Expand Up @@ -151,4 +153,6 @@ out/
.vscode/

.fleet
.DS_Store
**/.DS_Store

11 changes: 11 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM python:3.10-slim

WORKDIR /app

COPY requirements.txt /app
RUN --mount=type=cache,target=/root/.cache/pip \
pip3 install -r requirements.txt

COPY . /app

CMD ["gunicorn", "--bind", "0.0.0.0:5000", "application:app"]
189 changes: 96 additions & 93 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,122 +3,125 @@
PRISMO Admin Panel
===================

The goal of this webtool is provide basic management capabilities for hackerspaces, like:
Prismo is fully open source and easy to install access system for control access of tools and equipment for maker
spaces.

1. Presence management basing on MAC address monitoring
2. RFID access system management
3. Payments monitoring
4. Internal information storage(wiki based)
The gold for the project to create a system which any maker space in the world can setup for own use. The system fully
open source, include the backend, readers firmware and PCB schema.

## Prepare database
## Installation by docker

Install docker on your system.
- Install docker on the host machine.
Check [the tutorial for Raspberry Pi 4](https://github.com/codingforentrepreneurs/Pi-Awesome/blob/main/how-tos/Docker%20%26%20Docker%20Compose%20on%20Raspberry%20Pi.md)
- Create a folder `data` - the folder use for keep all persistent data, like a database.
- Run docker container:

1. Pull PostgreSQL docker image
```bash
docker run --name=prismo-app -p 80:5000 --restart always --detach -v "$(pwd)/data/:/app/external/" hacklabkyiv/prismo-app:0.1.7
```

Add docker to autostart:

```bash
$ docker pull postgres
```
```bash
sudo systemctl enable docker
```

2. Add user to group docker, user this instructions https://docs.docker.com/install/linux/linux-postinstall/. This will allow to use docker without sudo. TODO: update step for MacOS users
The application ready to work and available on `http://localhost:5000`

#### Optional steps
### The reader firmware

By default, this should be run by Prismo admin process, but for debugging purpose you should run this commands by yourself.
The reader is a device which connected to the network and read RFID cards. The reader firmware is stored in
the `prismo-reader` [repository](https://github.com/hacklabkyiv/prismo-reader/tree/micropython_pn532).

1. Run docker with. Here we will create database with name `prismo-db` inside docker container.
### Configuration

```bash
$ docker run -d --name prismo-db -e POSTGRES_PASSWORD=12345678 -e POSTGRES_DB=visitors -e POSTGRES_USER=prismo -p 5432:5432 -v $(pwd)/data:/var/lib/postgresql/data postgres
```
Config file name is `config.cfg`, the file located in the root directory of the project. Configs stored in YAML format.

2. Let's connect to database
```
logging:
logfile: log.txt
logsize_kb: 1000
rolldepth: 3
```

```bash
$ $ docker exec -it prismo-db psql -h localhost -U prismo -d visitors
psql (12.2 (Debian 12.2-2.pgdg100+1))
Type "help" for help.

visitors=#
```
## Development

3. Now you are in SQL console, basic commands are
### Preconditions

```
\? # Get help
\d # Describe table
\q # Quit psql
```
- Python 3.10+ with pip
- git
- supervisor(optional)

4. Let's create table with users. Also we will create two columns with access to door and lathe.
### Step-by-step installation

```bash
visitors=# CREATE TABLE users ( id serial primary key, name text, key text, last_enter timestamp, door boolean, lathe boolean);
```
```bash
visitors=# CREATE TABLE logs(device_name text, key text, time integer);
```
Alternatively you can add column to already existed table by command: `ALTER TABLE users ADD COLUMN last_enter TIMESTAMP;`
1. Clone the repository:

5. Show contents of table:
```sh
git clone [email protected]:hacklabkyiv/prismo.git
```
or by https:
```sh
git clone https://github.com/hacklabkyiv/prismo.git
```

```bash
# SELECT * FROM users;
id | name | key | door | lathe
----+------+-----+------+-------
(0 rows)
```
2. Install virtualenv in project's directory:
6. Quit database with `\q`
```sh
$ python3 -m venv ./virtualenv
```
If you want to stop docker container just run `docker stop prismo-db`, to start it again use `docker start prismo-db`
3. Activate virtual environment
## Installation
```
source ./virtualenv/bin/activate
```
1. Install virtualenv in project's directory:
```sh
$ python3 -m venv ./virtualenv
```
2. Activate virtual environment
```
source ./virtualenv/bin/activate
```
3. Install required packages:
```sh
$ pip3 install -r requirements.txt
```

4. Run app:
```sh
$ export FLASK_APP=application.py
$ flask run
```
table.sql contains create statements for database tables

Configuration
=============

Currently config is stored in YAML file. Example of config:
4. Install required packages:
```sh
pip3 install -r requirements.txt
```
5. Run for debugging and development: (it will reload app on code changes and enable debug mode)
```sh
export FLASK_APP=application.py
flask run --debug
```
# Example config file
data:
user: prismo
password: 12345678
host: localhost
port: 5432
name: visitors
latest-key-file: ./key.txt
logging:
debug: Yes
logfile: log.txt
logsize_kb: 1000
rolldepth: 3
```
path to config file is set in `applicaiton.py`. By default, config file name is `config.cfg`
By default, this should be run by Prismo admin process, but for debugging purpose you should run this commands by
yourself.
## Database
All information about the database is stored in [doc/database.md](docs/database.md) file.
### Logging
All logs are stored in `log.txt` file.
## API
The docs for API is stored in [docs/api.md](docs/api.md) file.
## Slack
Slack integration works with slack bot. You need to create slack bot in your slack workspace and get token for it.
Scope:
- chat:write
- files:write
- incoming-webhook
## Build docker image
The main target platform is `linux/arm64/v8` (Raspberry Pi 4). To build docker image for this platform you should use
buildx.
Execute `docker login` with hacklabkyiv credentials.
Execute this commands in the root directory of the project:
```
docker buildx create --use
docker buildx build --platform linux/arm64/v8 -t hacklabkyiv/prismo-app:<version> --push .
```
65 changes: 65 additions & 0 deletions app/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import json
import logging
import os
import sys
import secrets
from pathlib import Path

import yaml

try:
from yaml import CLoader as Loader, CDumper
except ImportError:
from yaml import Loader

# Configuration file
CONFIG_FILE = os.path.join(os.path.dirname(os.path.realpath(__file__)), '../config.cfg')

# Initial setup
try:
cfg = yaml.load(open(CONFIG_FILE, 'r'), Loader=Loader)
except IOError as e:
logging.error("Config file not found!")
logging.error("Exception: %s", str(e))
sys.exit(1)

os.makedirs("external", exist_ok=True)
database_file = Path("external/database.db")

UPLOAD_FOLDER = '/uploads'

internal_config_file = Path("external/internal_config.json")
slat_key = 'slat'
key_slack_token = 'key_slack_token'
key_slack_backup_channel = 'key_slack_backup_channel'
key_secret_key = 'key_secret_key'
key_database_version = 'key_database_version'


def get_setting(key: str):
with open(internal_config_file, 'r') as config_file:
config = json.load(config_file)
config_file.close()
return config.get(key, None)


def set_setting(key: str, value: str):
with open(internal_config_file, 'r') as config_file:
config = json.load(config_file)
config_file.close()

config[key] = value

with open(internal_config_file, 'w') as config_file:
json.dump(config, config_file, indent=4)
config_file.close()


def create_internal_config_file():
if not internal_config_file.is_file():
with open(internal_config_file, 'w') as config_file:
json.dump({
key_database_version: 1,
key_secret_key: secrets.token_hex(32),
}, config_file, indent=4)
config_file.close()
11 changes: 11 additions & 0 deletions app/data/device_dto.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from dataclasses import dataclass


@dataclass
class DeviceDto:
id: str
name: str

def __init__(self, id: str, name: str):
self.id = id
self.name = name
21 changes: 21 additions & 0 deletions app/data/dtos.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from dataclasses import dataclass


@dataclass
class UserDto:
key: str
name: str

def __init__(self, user_key, user_name):
self.key = user_key
self.name = user_name


@dataclass
class OperationDto:
time: int
type: str

def __init__(self, operation_time, operation_type):
self.time = operation_time
self.type = operation_type
Loading

0 comments on commit 8ff5c1d

Please sign in to comment.