Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 724166b4a4 | |||
| 681b11bbc1 | |||
| 310eb9028c | |||
| 4dcac70520 | |||
| 6eadca4041 | |||
| 8a0b1abfe7 | |||
| a7ef878490 |
11
.github/workflows/docker-publish.yml
vendored
11
.github/workflows/docker-publish.yml
vendored
@@ -29,7 +29,7 @@ jobs:
|
|||||||
needs: test
|
needs: test
|
||||||
|
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
if: github.event_name == 'push'
|
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v2
|
||||||
@@ -42,3 +42,12 @@ jobs:
|
|||||||
tag_with_ref: true
|
tag_with_ref: true
|
||||||
add_git_labels: true
|
add_git_labels: true
|
||||||
push: ${{ startsWith(github.ref, 'refs/tags/') }}
|
push: ${{ startsWith(github.ref, 'refs/tags/') }}
|
||||||
|
|
||||||
|
- uses: docker/build-push-action@v1
|
||||||
|
with:
|
||||||
|
username: ${{ secrets.DOCKER_USERNAME }}
|
||||||
|
password: ${{ secrets.DOCKER_PASSWORD }}
|
||||||
|
repository: snegov/seafile-client
|
||||||
|
tags: latest
|
||||||
|
add_git_labels: true
|
||||||
|
push: ${{ startsWith(github.ref, 'refs/tags/') }}
|
||||||
|
|||||||
10
Dockerfile
10
Dockerfile
@@ -1,8 +1,8 @@
|
|||||||
FROM python:3-slim
|
FROM python:3.8.12-slim-buster
|
||||||
|
|
||||||
RUN apt-get update && apt-get install gnupg curl -y && rm -rf /var/lib/apt/lists/*
|
RUN apt-get update && apt-get install gnupg curl -y && rm -rf /var/lib/apt/lists/*
|
||||||
RUN curl https://linux-clients.seafile.com/seafile.key | apt-key add - && \
|
RUN curl https://linux-clients.seafile.com/seafile.asc | apt-key add - && \
|
||||||
echo 'deb [arch=amd64] http://linux-clients.seafile.com/seafile-deb/buster/ stable main' > /etc/apt/sources.list.d/seafile.list && \
|
echo 'deb [arch=amd64] https://linux-clients.seafile.com/seafile-deb/buster/ stable main' > /etc/apt/sources.list.d/seafile.list && \
|
||||||
apt-get update -y && \
|
apt-get update -y && \
|
||||||
apt-get install -y seafile-cli procps grep && \
|
apt-get install -y seafile-cli procps grep && \
|
||||||
rm -rf /var/lib/apt/lists/*
|
rm -rf /var/lib/apt/lists/*
|
||||||
@@ -11,7 +11,7 @@ WORKDIR /seafile-client
|
|||||||
COPY requirements.txt ./
|
COPY requirements.txt ./
|
||||||
RUN pip install --no-cache-dir -r requirements.txt
|
RUN pip install --no-cache-dir -r requirements.txt
|
||||||
|
|
||||||
COPY seafile_client ./seafile_client/
|
COPY dsc ./dsc/
|
||||||
COPY start.py ./start.py
|
COPY start.py ./start.py
|
||||||
|
|
||||||
RUN chmod +x /seafile-client/start.py && \
|
RUN chmod +x /seafile-client/start.py && \
|
||||||
@@ -20,4 +20,6 @@ RUN chmod +x /seafile-client/start.py && \
|
|||||||
chown seafile:seafile -R /seafile-client && \
|
chown seafile:seafile -R /seafile-client && \
|
||||||
su - seafile -c "seaf-cli init -d /seafile-client"
|
su - seafile -c "seaf-cli init -d /seafile-client"
|
||||||
|
|
||||||
|
VOLUME /seafile-client/seafile-data
|
||||||
|
|
||||||
CMD ["./start.py"]
|
CMD ["./start.py"]
|
||||||
|
|||||||
35
README.md
35
README.md
@@ -1,33 +1,42 @@
|
|||||||
# docker-seafile-client
|
# docker-seafile-client
|
||||||
Run a seafile client inside docker witch can sync files from seafile repositories
|
Runs a seafile client in docker with possibility to sync seafile repositories.
|
||||||
|
|
||||||
See docker-compose how to use.
|
|
||||||
|
|
||||||
## Docker-compose example:
|
## Docker-compose example:
|
||||||
```yaml
|
```yaml
|
||||||
version: '3'
|
version: '3'
|
||||||
|
|
||||||
services:
|
services:
|
||||||
seafile-client:
|
seafile-client:
|
||||||
restart: always
|
restart: always
|
||||||
image: snegov/seafile-client
|
image: snegov/seafile-client
|
||||||
container_name: seafile-client
|
|
||||||
environment:
|
environment:
|
||||||
- LIBRARY_ID=<your-library-id-here>
|
- LIBRARY_ID=<your-library-id-here>
|
||||||
- SERVER_HOST=<server-host>
|
- SERVER_HOST=<server-host>
|
||||||
- USERNAME=<username>
|
- USERNAME=<username>
|
||||||
- PASSWORD=<password>
|
- PASSWORD=<password>
|
||||||
- DATA_DIR=<directory-path-to-sync>
|
|
||||||
- SEAFILE_UID=<your_uid>
|
- SEAFILE_UID=<your_uid>
|
||||||
- SEAFILE_GID=<your_gid>
|
- SEAFILE_GID=<your_gid>
|
||||||
|
hostname: docker-seafile-client
|
||||||
volumes:
|
volumes:
|
||||||
- <host-volume-path>:<directory-path-to-sync>
|
- seafile-data:/seafile-client/seafile-data
|
||||||
|
- <host-volume-path>:/data
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
seafile-data:
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Library id could be found from "My Libraries" page in Seafile webUI - link to each library contains library ID in it.
|
||||||
|
|
||||||
|
Inside container libraries' content will be put in `/data` directory, so map your host directory to it.
|
||||||
|
|
||||||
|
`hostname` parameter in docker-compose will set client name in Seafile's "Linked devices" admin page. Resulting name will be prefixed by "terminal-".
|
||||||
|
|
||||||
|
Also you could check [docker-compose example](docker-compose.example.yml).
|
||||||
|
|
||||||
## Environment variables:
|
## Environment variables:
|
||||||
- `LIBRARY_ID=<your-library-id-here>` ID of library to sync; multiple libraries could be separated by colon `:`
|
- `LIBRARY_ID=<your-library-id-here>` Library to sync, ID or name; multiple libraries could be separated by colon `:`.
|
||||||
- `SERVER_HOST=<server-host>` Hostname of your seafile server, eg: seafile.example.com. If you're using non-standart port, specify it here, eg: seafile.example.com:8080
|
- `SERVER_HOST=<server-host>` Hostname of your seafile server, eg: `seafile.example.com`. If you're using non-standart port, specify it here, eg: `seafile.example.com:8080`.
|
||||||
- `USERNAME=<username>` Seafile account username
|
- `USERNAME=<username>` Seafile account username.
|
||||||
- `PASSWORD=<password>` Seafile account password
|
- `PASSWORD=<password>` Seafile account password.
|
||||||
- `DATA_DIR=<directory-path-to-sync>` The path where to put the files
|
- `SEAFILE_UID=<uid>` Downloaded files will have this uid.
|
||||||
- `SEAFILE_UID=<uid>` Downloaded files will have this uid
|
- `SEAFILE_GID=<gid>` Downloaded files will have this gid.
|
||||||
- `SEAFILE_GID=<gid>` Downloaded files will have this gid
|
|
||||||
|
|||||||
@@ -1,16 +1,21 @@
|
|||||||
version: '3'
|
version: '3'
|
||||||
|
|
||||||
services:
|
services:
|
||||||
seafile-client:
|
seafile-client:
|
||||||
restart: always
|
restart: always
|
||||||
image: snegov/seafile-client
|
image: snegov/seafile-client
|
||||||
container_name: seafile-client
|
|
||||||
environment:
|
environment:
|
||||||
- LIBRARY_ID=<your-library-id-here>
|
- LIBRARY_ID="79867cbf-2944-488d-9105-859463ecdf9e:test_library"
|
||||||
- SERVER_HOST=<server-host>
|
- SERVER_HOST=seafile.example.com
|
||||||
- USERNAME=<username>
|
- USERNAME=user
|
||||||
- PASSWORD=<password>
|
- PASSWORD=password
|
||||||
- DATA_DIR=<directory-path-to-sync>
|
- SEAFILE_UID=1000
|
||||||
- SEAFILE_UID=<your_uid>
|
- SEAFILE_GID=100
|
||||||
- SEAFILE_GID=<your_gid>
|
hostname: docker-seafcli
|
||||||
volumes:
|
volumes:
|
||||||
- <host-volume-path>:<directory-path-to-sync>
|
- seafile-data:/seafile-client/seafile-data
|
||||||
|
- /home/johndow/seafile:/data
|
||||||
|
container_name: seafile-client
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
seafile-data:
|
||||||
|
|||||||
@@ -2,16 +2,16 @@ import logging
|
|||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
import time
|
import time
|
||||||
|
from typing import Optional
|
||||||
from urllib.parse import urlparse
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
|
from cached_property import cached_property_with_ttl
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
from .consts import DEFAULT_USERNAME
|
from dsc import consts
|
||||||
from .misc import create_dir
|
from dsc.misc import create_dir
|
||||||
|
|
||||||
|
_lg = logging.getLogger(__name__)
|
||||||
logging.basicConfig(format="%(asctime)s %(message)s",
|
|
||||||
level=logging.INFO)
|
|
||||||
|
|
||||||
|
|
||||||
class SeafileClient:
|
class SeafileClient:
|
||||||
@@ -36,16 +36,24 @@ class SeafileClient:
|
|||||||
self.__token = r.json()['token']
|
self.__token = r.json()['token']
|
||||||
return self.__token
|
return self.__token
|
||||||
|
|
||||||
def get_lib_name(self, lib_id: str) -> str:
|
@cached_property_with_ttl(ttl=60)
|
||||||
url = f"{self.url}/api2/repos/{lib_id}"
|
def remote_libraries(self) -> dict:
|
||||||
|
url = f"{self.url}/api2/repos/"
|
||||||
auth_header = {"Authorization": f"Token {self.token}"}
|
auth_header = {"Authorization": f"Token {self.token}"}
|
||||||
r = requests.get(url, headers=auth_header)
|
r = requests.get(url, headers=auth_header)
|
||||||
if r.status_code != 200:
|
if r.status_code != 200:
|
||||||
raise RuntimeError(r.text)
|
raise RuntimeError(r.text)
|
||||||
return r.json()['name']
|
r_libs = {lib["id"]: lib["name"] for lib in r.json()}
|
||||||
|
return r_libs
|
||||||
|
|
||||||
|
def get_library_id(self, library) -> Optional[str]:
|
||||||
|
for lib_id, lib_name in self.remote_libraries.items():
|
||||||
|
if library in (lib_id, lib_name):
|
||||||
|
return lib_id
|
||||||
|
return None
|
||||||
|
|
||||||
def sync_lib(self, lib_id: str, data_dir: str):
|
def sync_lib(self, lib_id: str, data_dir: str):
|
||||||
lib_name = self.get_lib_name(lib_id)
|
lib_name = self.remote_libraries[lib_id]
|
||||||
lib_dir = os.path.join(data_dir, lib_name.replace(' ', '_'))
|
lib_dir = os.path.join(data_dir, lib_name.replace(' ', '_'))
|
||||||
create_dir(lib_dir)
|
create_dir(lib_dir)
|
||||||
cmd = ['seaf-cli', 'sync',
|
cmd = ['seaf-cli', 'sync',
|
||||||
@@ -55,11 +63,11 @@ class SeafileClient:
|
|||||||
'-u', self.user,
|
'-u', self.user,
|
||||||
'-p', self.password]
|
'-p', self.password]
|
||||||
cmd = ' '.join(cmd)
|
cmd = ' '.join(cmd)
|
||||||
subprocess.run(['su', '-', DEFAULT_USERNAME, '-c', cmd])
|
subprocess.run(['su', '-', consts.DEFAULT_USERNAME, '-c', cmd])
|
||||||
|
|
||||||
def get_status(self):
|
def get_status(self):
|
||||||
cmd = 'seaf-cli status'
|
cmd = 'seaf-cli status'
|
||||||
out = subprocess.check_output(['su', '-', DEFAULT_USERNAME, '-c', cmd])
|
out = subprocess.check_output(['su', '-', consts.DEFAULT_USERNAME, '-c', cmd])
|
||||||
out = out.decode().splitlines()
|
out = out.decode().splitlines()
|
||||||
|
|
||||||
statuses = dict()
|
statuses = dict()
|
||||||
@@ -75,15 +83,25 @@ class SeafileClient:
|
|||||||
def watch_status(self):
|
def watch_status(self):
|
||||||
prev_status = dict()
|
prev_status = dict()
|
||||||
while True:
|
while True:
|
||||||
time.sleep(5)
|
time.sleep(consts.STATUS_POLL_PERIOD)
|
||||||
cur_status = self.get_status()
|
cur_status = self.get_status()
|
||||||
for folder, state in cur_status.items():
|
for folder, state in cur_status.items():
|
||||||
if state != prev_status.get(folder):
|
if state != prev_status.get(folder):
|
||||||
logging.info(f"Library {folder}:\t{state}")
|
logging.info("Library %s:\t%s", folder, state)
|
||||||
prev_status[folder] = cur_status[folder]
|
prev_status[folder] = cur_status[folder]
|
||||||
|
|
||||||
|
def get_local_libraries(self) -> set:
|
||||||
|
cmd = 'seaf-cli list'
|
||||||
|
out = subprocess.check_output(['su', '-', consts.DEFAULT_USERNAME, '-c', cmd])
|
||||||
|
out = out.decode().splitlines()[1:] # first line is a header
|
||||||
|
|
||||||
|
local_libs = set()
|
||||||
|
for line in out:
|
||||||
|
lib_name, lib_id, lib_path = line.rsplit(maxsplit=3)
|
||||||
|
local_libs.add(lib_id)
|
||||||
|
return local_libs
|
||||||
|
|
||||||
|
|
||||||
def start_seaf_daemon():
|
def start_seaf_daemon():
|
||||||
cmd = 'seaf-cli start'
|
cmd = 'seaf-cli start'
|
||||||
subprocess.run(['su', '-', DEFAULT_USERNAME, '-c', cmd])
|
subprocess.run(['su', '-', consts.DEFAULT_USERNAME, '-c', cmd])
|
||||||
time.sleep(5)
|
|
||||||
@@ -1 +1,2 @@
|
|||||||
DEFAULT_USERNAME = "seafile"
|
DEFAULT_USERNAME = "seafile"
|
||||||
|
STATUS_POLL_PERIOD = 1
|
||||||
@@ -2,7 +2,7 @@ import os
|
|||||||
import pwd
|
import pwd
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
from .consts import DEFAULT_USERNAME
|
from dsc.consts import DEFAULT_USERNAME
|
||||||
|
|
||||||
|
|
||||||
def setup_uid(uid: int, gid: int):
|
def setup_uid(uid: int, gid: int):
|
||||||
@@ -19,7 +19,3 @@ def create_dir(dir_path: str):
|
|||||||
else:
|
else:
|
||||||
if not os.path.isdir(dir_path):
|
if not os.path.isdir(dir_path):
|
||||||
raise RuntimeError(f"Data dir {dir_path} is not a directory")
|
raise RuntimeError(f"Data dir {dir_path} is not a directory")
|
||||||
|
|
||||||
|
|
||||||
def tail_f(fpath):
|
|
||||||
os.system(f"tail -f {fpath}")
|
|
||||||
@@ -1 +1,2 @@
|
|||||||
requests
|
cached_property==1.5.2
|
||||||
|
requests==2.27.1
|
||||||
|
|||||||
32
start.py
32
start.py
@@ -1,19 +1,24 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
|
import logging
|
||||||
import os
|
import os
|
||||||
import os.path
|
import os.path
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from seafile_client import SeafileClient, start_seaf_daemon
|
from dsc import SeafileClient, start_seaf_daemon
|
||||||
from seafile_client.misc import setup_uid, create_dir
|
from dsc.misc import setup_uid, create_dir
|
||||||
|
|
||||||
|
_lg = logging.getLogger('dsc')
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
logging.basicConfig(format="%(asctime)s %(message)s", level=logging.INFO)
|
||||||
|
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument("--uid", default=os.getenv("SEAFILE_UID"), type=int)
|
parser.add_argument("--uid", default=os.getenv("SEAFILE_UID", default=1000), type=int)
|
||||||
parser.add_argument("--gid", default=os.getenv("SEAFILE_GID"), type=int)
|
parser.add_argument("--gid", default=os.getenv("SEAFILE_GID", default=100), type=int)
|
||||||
parser.add_argument("--data-dir", default=os.getenv("DATA_DIR"))
|
parser.add_argument("--data-dir", default=os.getenv("DATA_DIR", default="/data"))
|
||||||
parser.add_argument("--host", default=os.getenv("SERVER_HOST"))
|
parser.add_argument("--host", default=os.getenv("SERVER_HOST"))
|
||||||
parser.add_argument("--username", default=os.getenv("USERNAME"))
|
parser.add_argument("--username", default=os.getenv("USERNAME"))
|
||||||
parser.add_argument("--password", default=os.getenv("PASSWORD"))
|
parser.add_argument("--password", default=os.getenv("PASSWORD"))
|
||||||
@@ -21,10 +26,23 @@ def main():
|
|||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
setup_uid(args.uid, args.gid)
|
setup_uid(args.uid, args.gid)
|
||||||
start_seaf_daemon()
|
|
||||||
create_dir(args.data_dir)
|
create_dir(args.data_dir)
|
||||||
|
start_seaf_daemon()
|
||||||
|
|
||||||
|
libs_to_sync = set()
|
||||||
|
|
||||||
client = SeafileClient(args.host, args.username, args.password)
|
client = SeafileClient(args.host, args.username, args.password)
|
||||||
for lib_id in args.libs.split(sep=":"):
|
for arg_lib in args.libs.split(sep=":"):
|
||||||
|
lib_id = client.get_library_id(arg_lib)
|
||||||
|
if lib_id:
|
||||||
|
libs_to_sync.add(lib_id)
|
||||||
|
else:
|
||||||
|
_lg.warning("Library %s is not found on server %s", arg_lib, args.host)
|
||||||
|
|
||||||
|
# don't start to sync libraries already in sync
|
||||||
|
libs_to_sync -= client.get_local_libraries()
|
||||||
|
|
||||||
|
for lib_id in libs_to_sync:
|
||||||
client.sync_lib(lib_id, args.data_dir)
|
client.sync_lib(lib_id, args.data_dir)
|
||||||
client.watch_status()
|
client.watch_status()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user