Skip to content

Commit

Permalink
Merge pull request #85 from openstreetmap-polska/dev
Browse files Browse the repository at this point in the history
Release to main
  • Loading branch information
Zaczero authored Oct 29, 2024
2 parents 52bb62b + afd1954 commit fb205ba
Show file tree
Hide file tree
Showing 8 changed files with 541 additions and 366 deletions.
14 changes: 5 additions & 9 deletions .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
uses: actions/checkout@v4

- name: Install Nix
uses: cachix/install-nix-action@v27
uses: cachix/install-nix-action@v30
with:
nix_path: nixpkgs=channel:nixpkgs-24.05-darwin

Expand Down Expand Up @@ -51,30 +51,26 @@ jobs:
~/.cache/uv
.venv
- name: Install dependencies
- name: Build container image
run: |
nix-shell --pure --run true
nix-shell --pure --arg isDevelopment false --run true
echo "IMAGE_PATH=$(nix-build --no-out-link)" >> $GITHUB_ENV
- name: Export Nix store cache
if: steps.nix-cache.outputs.cache-hit != 'true'
run: |
nix-store --export $(find /nix/store -maxdepth 1 -name '*-*') > /tmp/nix-cache
- name: Build container image
run: |
echo "IMAGE_PATH=$(nix-build --no-out-link)" >> $GITHUB_ENV
- name: Configure SSH
run: |
mkdir -p ~/.ssh
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa && chmod 600 ~/.ssh/id_rsa
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id && chmod 600 ~/.ssh/id
echo "${{ secrets.SSH_KNOWN_HOSTS }}" > ~/.ssh/known_hosts
echo "Host remote
HostName ${{ secrets.SSH_HOST }}
User ${{ secrets.SSH_USER }}
Port ${{ secrets.SSH_PORT }}
IdentityFile ~/.ssh/id_rsa
IdentityFile ~/.ssh/id
" > ~/.ssh/config
- name: Upload container image
Expand Down
19 changes: 16 additions & 3 deletions api/v1/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,28 @@ async def _get_image_data(tags: dict[str, str]) -> dict:
'@photo_source': image_url,
}

wikimedia_commons: str = tags.get('wikimedia_commons', '').partition(';')[0]

if wikimedia_commons:
if (
(wikimedia_commons := tags.get('wikimedia_commons', '').partition(';')[0]) #
and wikimedia_commons[:4].casefold() != 'http'
):
return {
'@photo_id': None,
'@photo_url': f'/api/v1/photos/proxy/wikimedia-commons/{quote_plus(wikimedia_commons)}',
'@photo_source': get_wikimedia_commons_url(wikimedia_commons),
}

if (
(panoramax := tags.get('panoramax', '').partition(';')[0]) #
and panoramax[:4].casefold() != 'http'
):
photo_url = f'https://api.panoramax.xyz/api/pictures/{panoramax}/sd.jpg'
photo_source = f'https://api.panoramax.xyz/#focus=pic&pic={panoramax}'
return {
'@photo_id': None,
'@photo_url': f'/api/v1/photos/proxy/direct/{quote_plus(photo_url)}',
'@photo_source': photo_source,
}

return {
'@photo_id': None,
'@photo_url': None,
Expand Down
4 changes: 2 additions & 2 deletions config.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import os
from datetime import timedelta
from logging.config import dictConfig
from pathlib import Path

import sentry_sdk
from anyio import Path
from pyproj import Transformer

NAME = 'openaedmap-backend'
VERSION = '2.11.1'
VERSION = '2.12.0'
CREATED_BY = f'{NAME} {VERSION}'
WEBSITE = 'https://openaedmap.org'

Expand Down
2 changes: 1 addition & 1 deletion models/db/photo.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import secrets
from pathlib import Path

from anyio import Path
from sqlalchemy import BigInteger, Unicode
from sqlalchemy.orm import Mapped, mapped_column

Expand Down
4 changes: 2 additions & 2 deletions services/photo_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ async def get_by_id(id: str, *, check_file: bool = True) -> Photo | None:

if photo is None:
return None
if check_file and (not await photo.file_path.is_file()):
if check_file and (not photo.file_path.is_file()):
return None

return photo
Expand All @@ -39,7 +39,7 @@ async def upload(node_id: int, user_id: int, file: UploadFile) -> Photo:
)
session.add(photo)

await photo.file_path.write_bytes(img_bytes)
photo.file_path.write_bytes(img_bytes)
return photo


Expand Down
24 changes: 12 additions & 12 deletions services/worker_service.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import fcntl
import os
from typing import Literal
from pathlib import Path
from typing import Literal, cast

import anyio
from anyio import AsyncFile, Path

from config import DATA_DIR
from utils import retry_exponential
Expand All @@ -17,26 +17,26 @@

class WorkerService:
is_primary: bool
_lock_file: AsyncFile[str]
_lock_fd: int

@retry_exponential(10)
@staticmethod
async def init() -> 'WorkerService':
self = WorkerService()
self._lock_file = await anyio.open_file(_LOCK_PATH, 'w')
self._lock_fd = os.open(_LOCK_PATH, os.O_RDONLY | os.O_CREAT)

try:
fcntl.flock(self._lock_file.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
fcntl.flock(self._lock_fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
self.is_primary = True
await _STATE_PATH.write_text('startup')
await _PID_PATH.write_text(str(os.getpid()))
_STATE_PATH.write_text('startup')
_PID_PATH.write_text(str(os.getpid()))
except BlockingIOError:
self.is_primary = False

while True:
if await _PID_PATH.is_file() and await _STATE_PATH.is_file():
pid = await _PID_PATH.read_text()
if pid and await Path(f'/proc/{pid}').is_dir():
if _PID_PATH.is_file() and _STATE_PATH.is_file():
pid = _PID_PATH.read_text()
if pid and Path(f'/proc/{pid}').is_dir():
break
await anyio.sleep(0.1)

Expand All @@ -45,11 +45,11 @@ async def init() -> 'WorkerService':
async def set_state(self, state: WorkerState) -> None:
if not self.is_primary:
raise AssertionError('Only the primary worker can set the state')
await _STATE_PATH.write_text(state)
_STATE_PATH.write_text(state)

@retry_exponential(10)
async def get_state(self) -> WorkerState:
return await _STATE_PATH.read_text() # pyright: ignore[reportReturnType]
return cast(WorkerState, _STATE_PATH.read_text())

async def wait_for_state(self, state: WorkerState) -> None:
while await self.get_state() != state: # noqa: ASYNC110
Expand Down
14 changes: 7 additions & 7 deletions shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

let
# Update packages with `nixpkgs-update` command
pkgs = import (fetchTarball "https://github.com/NixOS/nixpkgs/archive/658e7223191d2598641d50ee4e898126768fe847.tar.gz") { };
pkgs = import (fetchTarball "https://github.com/NixOS/nixpkgs/archive/0fcb98acb6633445764dafe180e6833eb0f95208.tar.gz") { };

pythonLibs = with pkgs; [
stdenv.cc.cc.lib
Expand Down Expand Up @@ -129,21 +129,21 @@ let
];

shell' = with pkgs; lib.optionalString isDevelopment ''
export PYTHONNOUSERSITE=1
export TZ=UTC
current_python=$(readlink -e .venv/bin/python || echo "")
current_python=''${current_python%/bin/*}
[ "$current_python" != "${python'}" ] && rm -rf .venv/
echo "Installing Python dependencies"
echo "${python'}/bin/python" > .python-version
export UV_COMPILE_BYTECODE=1
export UV_PYTHON="${python'}/bin/python"
uv sync --frozen
echo "Activating Python virtual environment"
source .venv/bin/activate
# Development environment variables
export PYTHONNOUSERSITE=1
export TZ=UTC
if [ -f .env ]; then
echo "Loading .env file"
set -o allexport
Expand All @@ -156,7 +156,7 @@ let
make-version
'';
in
pkgs.mkShellNoCC {
pkgs.mkShell {
buildInputs = packages';
shellHook = shell';
}
Loading

0 comments on commit fb205ba

Please sign in to comment.