Skip to content

Commit

Permalink
Merge pull request #102 from SynoCommunity/diaoul/refactor
Browse files Browse the repository at this point in the history
ref!: various upgrades and fixes
  • Loading branch information
publicarray authored Dec 31, 2023
2 parents b3d6268 + c7e67e9 commit 8a5521e
Show file tree
Hide file tree
Showing 29 changed files with 1,886 additions and 2,047 deletions.
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[flake8]
max-line-length = 88
extend-ignore = E203
per-file-ignores = __init__.py:F401
per-file-ignores = __init__.py:F401 spkrepo/app.py:F841
exclude =
docs/*
migrations/*
24 changes: 11 additions & 13 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,32 @@ jobs:
pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
fetch-depth: 1
- uses: actions/setup-python@v2
with:
python-version: 3.8
- uses: pre-commit/[email protected]
python-version: 3.11
- uses: pre-commit/[email protected]

test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: 3.9
- uses: snok/install-poetry@v1.2.0
python-version: 3.11
- uses: snok/install-poetry@v1
with:
virtualenvs-in-project: true
- name: Load cached venv
id: cached-poetry-dependencies
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: .venv
key: venv-${{ runner.os }}-${{ hashFiles('pyproject.toml') }}
key: venv-${{ runner.os }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('**/pyproject.toml') }}
- name: Install dependencies
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
run: poetry install --no-interaction --no-root
- name: Install application
- name: Install project
run: poetry install --no-interaction
- name: Run tests
run: poetry run pytest -v
30 changes: 15 additions & 15 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@ jobs:
pypi:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: 3.9
- uses: snok/install-poetry@v1.2.0
python-version: 3.11
- uses: snok/install-poetry@v1
with:
virtualenvs-in-project: true
- name: Load cached venv
id: cached-poetry-dependencies
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: .venv
key: venv-${{ runner.os }}-${{ hashFiles('pyproject.toml') }}
key: venv-${{ runner.os }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('**/pyproject.toml') }}
- name: Install dependencies
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
run: poetry install --no-interaction --no-root
- name: Install library
- name: Install project
run: poetry install --no-interaction
- name: Publish
env:
Expand All @@ -36,20 +36,20 @@ jobs:
contents: read
packages: write
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v4
with:
images: ghcr.io/${{ github.repository }}
- name: Log in to the Container registry
uses: docker/login-action@v1
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v3
with:
images: ghcr.io/${{ github.repository }}
- name: Build and push Docker image
uses: docker/build-push-action@v2
uses: docker/build-push-action@v4
with:
context: .
push: true
Expand Down
15 changes: 9 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
---
default_language_version:
python: python3.11
repos:
- repo: https://github.com/python-poetry/poetry
rev: master
rev: 1.4.2
hooks:
- id: poetry-export
- repo: https://github.com/psf/black
rev: 22.1.0
rev: 23.3.0
hooks:
- id: black
- repo: https://gitlab.com/PyCQA/flake8
rev: 3.9.2
- repo: https://github.com/PyCQA/flake8
rev: 6.0.0
hooks:
- id: flake8
- repo: https://github.com/PyCQA/isort
rev: 5.10.1
rev: 5.12.0
hooks:
- id: isort
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.1.0
rev: v4.4.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
Expand Down
1 change: 0 additions & 1 deletion .python-version

This file was deleted.

4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3.9.7-buster
FROM python:3.11

WORKDIR /usr/src/app

Expand All @@ -14,7 +14,7 @@ RUN pip install --no-cache-dir -r requirements.txt

COPY spkrepo ./spkrepo
COPY migrations ./migrations
COPY manage.py wsgi.py ./
COPY wsgi.py ./

HEALTHCHECK --interval=1m --timeout=5s \
CMD curl -f http://localhost:8000/ || exit 1
Expand Down
56 changes: 26 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,41 @@ Synology Package Repository
![Build](https://img.shields.io/github/actions/workflow/status/SynoCommunity/spkrepo/build.yml?branch=main&style=for-the-badge)
[![Discord](https://img.shields.io/discord/732558169863225384?color=7289DA&label=Discord&logo=Discord&logoColor=white&style=for-the-badge)](https://discord.gg/nnN9fgE7EF)

## Development

## Development
### Requirements
* ImageMagic: `apt install imagemagic -y`
* Python 3.10: `pyenv install 3.10 && poetry env use 3.10`
* Recommended: `pyenv` and `pipx`: `apt install pyenv python-pipx -y`
1. Install docker and docker-compose
2. Install python and poetry
3. Optionally install direnv and a working poetry layout as described [here](https://github.com/direnv/direnv/issues/592#issuecomment-856227234) in your `~/.config/direnv/direnvrc`

### Installation
1. Install dependencies with `poetry install`
2. Run the next commands in the virtual environment `poetry shell`
3. Create the tables with `python manage.py create`
4. Populate the database with some fake packages with `python manage.py populate`
5. Add an user with `python manage.py user create -u Admin -e [email protected] -p adminadmin`
6. Grant the created user with Administrator permissions `python manage.py roles add [email protected] admin`
7. Grant the created user with Package Administrator permissions `python manage.py roles add [email protected] package_admin`
8. Grant the created user with Developer permissions `python manage.py roles add [email protected] developer`

To reset the environment, clean up with `python manage.py clean`.
1. Run postgres, e.g. using docker with `docker compose up db`
2. Install dependencies with `poetry install`
3. Run the next commands in the virtual environment `poetry shell`
4. Create the tables with `flask db upgrade`
5. Populate the database with some fake packages with `flask spkrepo populate_db`
6. Add a user with `flask spkrepo create_user -u admin -e [email protected] -p adminadmin`
7. Grant the created user with Administrator permissions `flask roles add [email protected] admin`
8. Grant the created user with Package Administrator permissions `flask roles add [email protected] package_admin`
9. Grant the created user with Developer permissions `flask roles add [email protected] developer`

To clean data created by fake packages, run `flask spkrepo depopulate_db`

### Run
1. Start the development server with `python manage.py run`
2. Website is available at http://localhost:5000
3. Admin interface is available at http://localhost:5000/admin
4. NAS interface is available at http://localhost:5000/nas
5. API is available at http://localhost:5000/api
6. Run the test suite with `poetry run pytest -v`
1. Start postgres with `docker compose up db`
2. Start the development server with `flask run`
3. Website is available at http://localhost:5000
4. Admin interface is available at http://localhost:5000/admin
5. NAS interface is available at http://localhost:5000/nas
6. API is available at http://localhost:5000/api
7. Run the test suite with `pytest -v`

## Docker Compose Run
It is also possible to start a development environment with postgres database
using docker compose:
1. Build and run `docker-compose up --build`
2. On first run you can apply database migrations with `docker exec spkrepo_spkrepo_1 python manage.py db upgrade`.
Also run any other command that you need (populate the databse, create user) as mentioned above but by prefixing
with `docker exec {container_id} [...]`.
3. Browse to http://localhost:5000
4. To tear down the environment, run `docker-compose down --remove`
- If you also want to run the app in docker you can with `docker compose up app`
- You can run both postgres and the app with `docker compose up`

## Deployment

## Deployment
### Configuration
Create a config file `./config.py` to disable debug logs, connect to a database, set a secure key and optionally set a cache:

Expand All @@ -55,7 +51,7 @@ SECRET_KEY = "Please-change-me-to-some-random-string"
SQLALCHEMY_ECHO = False
SQLALCHEMY_DATABASE_URI = "postgresql://user:pass@localhost/dbname"
# https://pythonhosted.org/Flask-Caching/#configuring-flask-caching
CACHE_TYPE= "simple"
CACHE_TYPE= "SimpleCache"
# For signing packages
GNUPG_PATH= "/usr/local/bin/gpg"
```
Expand Down
31 changes: 18 additions & 13 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
version: "3.9"
---
version: '3.8'

services:
db:
image: postgres:13
image: postgres:15
environment:
POSTGRES_DB: spkrepo
POSTGRES_USER: spkrepo
POSTGRES_PASSWORD: spkrepo
spkrepo:
build: .
command: python manage.py runserver -h 0.0.0.0
ports:
- "5000:5000"
environment:
SPKREPO_SQLALCHEMY_DATABASE_URI: postgresql://spkrepo:spkrepo@db/spkrepo
volumes:
- .:/usr/src/app/
depends_on:
- db
ports:
- 5432:5432
app:
build: .
image: ghcr.io/synocommunity/spkrepo:v0.2.4
command: flask run -h 0.0.0.0
ports:
- 5000:5000
environment:
SPKREPO_SQLALCHEMY_DATABASE_URI: postgresql://spkrepo:spkrepo@db/spkrepo
volumes:
- .:/usr/src/app/
depends_on:
- db
28 changes: 28 additions & 0 deletions migrations/versions/76d559b4e873_add_fs_uniquifier.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""Add fs_uniquifier
Revision ID: 76d559b4e873
Revises: d429595e8362
Create Date: 2022-10-24 09:31:01.814928
"""
revision = "76d559b4e873"
down_revision = "d429595e8362"

import sqlalchemy as sa
from alembic import op


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column(
"user", sa.Column("fs_uniquifier", sa.String(length=255), nullable=False)
)
op.create_unique_constraint(None, "user", ["fs_uniquifier"])
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint(None, "user", type_="unique")
op.drop_column("user", "fs_uniquifier")
# ### end Alembic commands ###
6 changes: 2 additions & 4 deletions migrations/versions/d429595e8362_update_build_path_length.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@
Create Date: 2022-10-22 21:31:03.050850
"""
revision = 'd429595e8362'
down_revision = 'dc7687894ba7'
revision = "d429595e8362"
down_revision = "dc7687894ba7"

import sqlalchemy as sa
from alembic import op




def upgrade():
op.alter_column(
"build",
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""empty message
"""Increase field sizes
Revision ID: dc7687894ba7
Revises: d785f1fb2307
Expand Down
Loading

0 comments on commit 8a5521e

Please sign in to comment.