Skip to content

Commit

Permalink
new 5
Browse files Browse the repository at this point in the history
  • Loading branch information
lee-cq committed Aug 23, 2024
1 parent a8e6c0e commit 29cb913
Show file tree
Hide file tree
Showing 13 changed files with 364 additions and 160 deletions.
103 changes: 103 additions & 0 deletions .github/workflows/alist-server.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
name: Alist Sync
run-name: "action-${{github.actor}}-${{github.run_id}}-${{github.run_number}}"
on:
workflow_dispatch:
inputs:
reload_storage:
description: 删除现有存储库,并从配置文件或远程配置中重新创建存储器
type: boolean
required: false
default: false


debug:
description: 开启调试模式
type: boolean
required: false
default: false

env:
_ALIST_SYNC_NAME: "${{github.run-name}}"
_ALIST_SYNC_DEBUG: ${{ github.event.inputs.debug }}
_ALIST_ADMIN_PASSWORD: ${{ secrets.ALIST_ADMIN_PASSWORD }}

jobs:
run:
name: "sync-actions"
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup Python v4
uses: actions/setup-python@v4
with:
python-version: 3.11

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install -e .

- name: Create Tunnel for Cloudflare
run: |
echo RUNNER = ${_ALIST_SYNC_NAME}
test ! -n "${{secrets.CLOUDFLARE_TUNNEL_TOKEN}}" && {
echo "CLOUDFLARE_TUNNEL_TOKEN is not set. Skip Cloudflare Tunnel Installation."
exit 0
}
echo "Installing Cloudflare Tunnel ..."
curl -L --output cloudflared.deb https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb &&
sudo dpkg -i cloudflared.deb &&
sudo cloudflared service install ${{ secrets.CLOUDFLARE_TUNNEL_TOKEN }}
echo "CLOUDFLARE_INSTALLED=true" >> "$GITHUB_ENV"
echo "Cloudflare Tunnel Installed."
- name: Install Alist
run: |
ALIST_VERSION=$(curl -s https://api.github.com/repos/alist-org/alist/releases/latest | grep tag_name | cut -d '"' -f 4)
export ALIST_VERSION
download_url="https://github.com/alist-org/alist/releases/download/${version}/alist-linux-amd64.tar.gz"
wget -O alist.zip $download_url
tar -xf alist.zip
./alist admin password --set "$_ALIST_ADMIN_PASSWORD"
sed -i"" s'"workers": 5/"workers": 10/'g data/config.json
- name: Install and Init Alist Server
env:
_ALIST_ADMIN_PASSWORD: ${{ secrets.ALIST_ADMIN_PASSWORD }}
run: |
sudo ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
ALIST_VERSION=$(curl -s https://api.github.com/repos/alist-org/alist/releases/latest | grep tag_name | cut -d '"' -f 4)
export ALIST_VERSION
bash -ex bootstrap.sh install
bash -x bootstrap.sh alist-init
bash -x bootstrap.sh alist start
- name: Create Storage for Alist
env:
_ALIST_ADMIN_PASSWORD: ${{ secrets.ALIST_ADMIN_PASSWORD }}

_ALIST_BACKUP: ${{secrets.ALIST_BACKUP}}
_ALIST_BACKUP_URL: ${{secrets.ALIST_BACKUP_URL}}
_ALIST_BACKUP_USERNAME: ${{secrets.ALIST_BACKUP_USERNAME}}
_ALIST_BACKUP_PASSWORD: ${{secrets.ALIST_BACKUP_PASSWORD}}

_RELOAD_STORAGE: ${{ github.event.inputs.reload_storage }}
run: |
echo RUNNER = ${_ALIST_SYNC_NAME}
# 这将会导入全部的内容包括:设置,元数据,用户,存储器。
echo $(pwd)
python3 tools/create_storage.py
- name: Run Alist Server
run: |
./bootstrap.sh alist stop
./bootstrap.sh alist sever
23 changes: 17 additions & 6 deletions alist_sync/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"""
import logging
import os
import time
from pathlib import Path
from typing import Optional
from functools import cached_property
Expand All @@ -17,12 +18,21 @@
from pydantic import BaseModel
from alist_sdk import AlistPath, AlistPathType, login_server

__all__ = ["load_env", "Database", "Config", "config"]
__all__ = [
"load_env",
"Database",
"Config",
"AlistServer",
"SyncGroup",
"config",
"sync_trace",
]

from alist_sync.common import sha1
from alist_sync.const import Env

logger = logging.getLogger("alist-sync.config")
sync_trace = logging.getLogger("sync_trace")


def load_env():
Expand Down Expand Up @@ -130,21 +140,22 @@ def _get_base_dir(_t: AlistPath) -> AlistPath:

class Config(BaseModel):
version: str = "1" #
runner_id: str = f"alist-sync-{int(time.time())}" # 运行器ID
database: Database = Database() # 数据库存储,默认SQLite
alist_servers: list[AlistServer] # Alist 服务器配置信息
sync_groups: list[SyncGroup] # 同步组配置信息
logs: dict
logs: dict | None = None # 日志配置信息

@classmethod
def load_from_yaml(cls, file: str) -> "Config":
dc = safe_load(Path(file).read_text())
dc = safe_load(Path(file).read_text(encoding="utf-8"))
return cls.model_validate(dc)

def login(self):
for server in self.alist_servers:
server.login()

def loda_logger(self):
def load_logger(self):
if self.logs:
from logging import config

Expand All @@ -154,5 +165,5 @@ def loda_logger(self):
load_env()
config = Config.load_from_yaml(os.getenv(Env.config, "config.yaml"))
config.login()
config.loda_logger()
logger.info("COnfig Load Success.")
config.load_logger()
logger.info("Config Load Success.")
1 change: 1 addition & 0 deletions alist_sync/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Env:
exit_flag = "ALIST_SYNC_EXIT_FLAG"
config = "ALIST_SYNC_CONFIG"
debug = "ALIST_SYNC_DEBUG"
runner_id = "ALIST_SYNC_RUNNER_ID"


class _Base:
Expand Down
2 changes: 0 additions & 2 deletions alist_sync/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ def copy():
from alist_sync.config import config
from alist_sync.worker_manager import WorkerManager

atexit.register(lambda: os.environ.setdefault(Env.exit_flag, "true"))

for sg in config.sync_groups:
logger.info(f"Start Sync Group: {sg.name}")
WorkerManager(sg).start() # TODO 线程
24 changes: 12 additions & 12 deletions alist_sync/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ class BaseModel(Model):
class Meta:
database = config.database.db

def save(self, force_insert=None, only=None):
if force_insert is not None:
return super(BaseModel, self).save(force_insert, only)
try:
if only is not None:
return super(BaseModel, self).save(force_insert=False, only=only)
return super(BaseModel, self).save(force_insert=True, only=only)
except IntegrityError as _e:
return self.update()
# def save(self, force_insert=None, only=None):
# if force_insert is not None:
# return super(BaseModel, self).save(force_insert, only)
# try:
# if only is not None:
# return super(BaseModel, self).save(force_insert=False, only=only)
# return super(BaseModel, self).save(force_insert=True, only=only)
# except IntegrityError as _e:
# return self.update()

@classmethod
def create_no_error(cls, **query):
Expand Down Expand Up @@ -98,11 +98,11 @@ class TransferLog(BaseModel):
transfer_type: str = CharField()
source_path: str = CharField()
target_path: str = CharField()
backed_up: str = CharField(null=True)
backup_path: str = CharField(null=True)
status: str = CharField()
message: str = TextField(null=True)
start_time: datetime = DateTimeField()
end_time: datetime = DateTimeField()
start_time: datetime = DateTimeField(default=datetime.now)
end_time: datetime | None = DateTimeField(null=True)

def __repr__(self):
tar = (
Expand Down
7 changes: 7 additions & 0 deletions alist_sync/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@File Name : server.py
@Author : LeeCQ
@Date-Time : 2024/8/23 20:57
"""
3 changes: 2 additions & 1 deletion alist_sync/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def __init__(self):
self.thread = threading.Thread(
target=self.thread_update_task,
name="task-status-updater",
daemon=True,
)
self.thread.start()

Expand Down Expand Up @@ -70,7 +71,7 @@ def update_task(self):
self.tasks[i.id] = i

def get_task(self, _id) -> Task:
return self.tasks[_id]
return self.tasks.get(_id)

def status(self, _id: str) -> str:
try:
Expand Down
Loading

0 comments on commit 29cb913

Please sign in to comment.