Skip to content

Commit 171189d

Browse files
committed
downloader start
1 parent 1f5f08b commit 171189d

File tree

8 files changed

+122
-24
lines changed

8 files changed

+122
-24
lines changed

.gitignore

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ rclone.conf
1010
.DS_Store
1111

1212
node_modules
13+
tmp
14+
temp
15+
1316

14-
/venv
1517
/.idea
16-
/temp
17-
/rclone-ofm.conf
18-
/scripts/tile_gen/extract_mbtiles/out
18+
venv
19+
1920

config/rclone.conf.sample

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ provider = Cloudflare
44
access_key_id = xxx
55
secret_access_key = xxx
66
endpoint = https://xxx.r2.cloudflarestorage.com
7-
7+
no_check_bucket = true

init-server.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@
66
from fabric import Config, Connection
77

88
from ssh_lib.benchmark import c1000k
9-
from ssh_lib.config import CONFIG_DIR, OFM_DIR, REMOTE_CONFIG, SCRIPTS_DIR, TILE_GEN_BIN
9+
from ssh_lib.config import (
10+
CONFIG_DIR,
11+
HTTP_HOST_BIN,
12+
OFM_DIR,
13+
REMOTE_CONFIG,
14+
SCRIPTS_DIR,
15+
TILE_GEN_BIN,
16+
)
1017
from ssh_lib.kernel import set_cpu_governor, setup_kernel_settings
1118
from ssh_lib.nginx import certbot, nginx
1219
from ssh_lib.pkg_base import pkg_base, pkg_upgrade
@@ -91,9 +98,23 @@ def prepare_tile_gen(c):
9198

9299

93100
def prepare_http_host(c):
94-
nginx(c)
95-
certbot(c)
96-
c1000k(c)
101+
# nginx(c)
102+
# certbot(c)
103+
# c1000k(c)
104+
105+
prepare_venv(c)
106+
107+
c.sudo(f'mkdir -p {HTTP_HOST_BIN}')
108+
109+
for file in [
110+
'downloader.py',
111+
]:
112+
put(
113+
c,
114+
SCRIPTS_DIR / 'http_host' / file,
115+
HTTP_HOST_BIN,
116+
permissions='755',
117+
)
97118

98119

99120
def debug_tmp(c):

scripts/http_host/downloader.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/usr/bin/env python3
2+
import shutil
3+
import subprocess
4+
import sys
5+
from pathlib import Path
6+
7+
import click
8+
import requests
9+
10+
11+
DEFAULT_RUNS_DIR = '/data/ofm/http_host/runs'
12+
13+
14+
@click.command()
15+
@click.option('--area', default='planet', help='The area to process')
16+
@click.option('--version', default='latest', help='Version string, like "20231227_043106_pt"')
17+
@click.option(
18+
'--runs-dir',
19+
help='Specify /runs directory',
20+
type=click.Path(dir_okay=True, file_okay=False, path_type=Path),
21+
)
22+
@click.option('--list-versions', is_flag=True, help='List all versions in an area and terminate')
23+
def cli(area: str, version: str, list_versions: bool, runs_dir: Path):
24+
if area not in {'planet', 'monaco'}:
25+
sys.exit('Area must be planet or monaco')
26+
27+
r = requests.get(f'https://{area}.openfreemap.com/dirs.txt')
28+
r.raise_for_status()
29+
30+
versions = sorted(r.text.splitlines())
31+
32+
all_versions_str = '\n'.join(versions)
33+
if list_versions:
34+
print(all_versions_str)
35+
return
36+
37+
if version == 'latest':
38+
selected_version = versions[-1]
39+
else:
40+
if version not in versions:
41+
sys.exit(f'Requested version is not available. Available versions:\n{all_versions_str}')
42+
selected_version = version
43+
44+
download(area, selected_version, runs_dir or DEFAULT_RUNS_DIR)
45+
46+
47+
def download(area: str, version: str, runs_dir: Path):
48+
click.echo(f'Downloading: area: {area}, version: {version}')
49+
50+
version_dir = runs_dir / version
51+
btrfs_file = version_dir / 'tiles.btrfs'
52+
if btrfs_file.exists():
53+
print('File exists, skipping download')
54+
return
55+
56+
temp_dir = runs_dir / '_tmp'
57+
shutil.rmtree(temp_dir, ignore_errors=True)
58+
temp_dir.mkdir(parents=True)
59+
60+
gzip_file = temp_dir / 'tiles.btrfs.gz'
61+
62+
url = f'https://{area}.openfreemap.com/{version}/tiles.btrfs.gz'
63+
print(url)
64+
65+
subprocess.run(
66+
[
67+
'aria2c',
68+
'--split=8',
69+
'--max-connection-per-server=8',
70+
'--file-allocation=none',
71+
'-o',
72+
gzip_file,
73+
url,
74+
],
75+
check=True,
76+
)
77+
78+
subprocess.run(['unpigz', gzip_file])
79+
btrfs_src = temp_dir / 'tiles.btrfs'
80+
81+
version_dir.mkdir()
82+
btrfs_src.rename(btrfs_file)
83+
84+
shutil.rmtree(temp_dir)
85+
86+
87+
if __name__ == '__main__':
88+
cli()

scripts/http_host/downloader/downloader.py

Lines changed: 0 additions & 14 deletions
This file was deleted.

scripts/prepare-virtualenv.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ venv/bin/pip -V
1111

1212
venv/bin/pip install -U pip wheel setuptools
1313

14-
venv/bin/pip install click
14+
venv/bin/pip install click requests
1515

1616

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
'python-dotenv',
88
'click',
99
'nginxfmt',
10+
'requests',
1011
]
1112

1213
setup(

ssh_lib/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@
1111
OFM_DIR = '/data/ofm'
1212
REMOTE_CONFIG = '/data/ofm/config'
1313
TILE_GEN_BIN = '/data/ofm/tile_gen/bin'
14+
HTTP_HOST_BIN = '/data/ofm/http_host/bin'

0 commit comments

Comments
 (0)