mirror of
https://github.com/hyperknot/openfreemap.git
synced 2026-05-21 14:02:15 +00:00
refactor
This commit is contained in:
16
debug.py
Normal file
16
debug.py
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
import click
|
||||||
|
|
||||||
|
from ssh_lib.cli_helpers import common_options, get_connection
|
||||||
|
from ssh_lib.tasks_http_host import upload_config_and_certs
|
||||||
|
|
||||||
|
|
||||||
|
@click.group()
|
||||||
|
def cli():
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
@cli.command()
|
||||||
|
@common_options
|
||||||
|
def debug(hostname, user, port, noninteractive):
|
||||||
|
c = get_connection(hostname, user, port)
|
||||||
|
upload_config_and_certs(c)
|
||||||
65
http-host.py
Executable file
65
http-host.py
Executable file
@@ -0,0 +1,65 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import click
|
||||||
|
import json5
|
||||||
|
|
||||||
|
from ssh_lib.cli_helpers import common_options, get_connection
|
||||||
|
from ssh_lib.config import config
|
||||||
|
from ssh_lib.tasks_http_host import prepare_http_host, run_http_host_sync
|
||||||
|
from ssh_lib.tasks_shared import prepare_shared
|
||||||
|
from ssh_lib.utils import (
|
||||||
|
put,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@click.group()
|
||||||
|
def cli():
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
@cli.command()
|
||||||
|
@common_options
|
||||||
|
def http_host_static(hostname, user, port, noninteractive):
|
||||||
|
if not noninteractive and not click.confirm(f'Run script on {hostname}?'):
|
||||||
|
return
|
||||||
|
|
||||||
|
c = get_connection(hostname, user, port)
|
||||||
|
|
||||||
|
prepare_shared(c)
|
||||||
|
prepare_http_host(c)
|
||||||
|
|
||||||
|
run_http_host_sync(c)
|
||||||
|
|
||||||
|
|
||||||
|
@cli.command()
|
||||||
|
@common_options
|
||||||
|
def http_host_autoupdate(hostname, user, port, noninteractive):
|
||||||
|
if not noninteractive and not click.confirm(f'Run script on {hostname}?'):
|
||||||
|
return
|
||||||
|
|
||||||
|
c = get_connection(hostname, user, port)
|
||||||
|
|
||||||
|
c.sudo('rm -f /etc/cron.d/ofm_http_host')
|
||||||
|
|
||||||
|
prepare_shared(c)
|
||||||
|
prepare_http_host(c)
|
||||||
|
|
||||||
|
# for the monaco run, wait for the sync to complete
|
||||||
|
if json5.loads(config.local_config_jsonc.read_text()).get('skip_planet'):
|
||||||
|
run_http_host_sync(c)
|
||||||
|
|
||||||
|
put(c, config.local_modules_dir / 'http_host' / 'cron.d' / 'ofm_http_host', '/etc/cron.d/')
|
||||||
|
|
||||||
|
|
||||||
|
@cli.command()
|
||||||
|
@common_options
|
||||||
|
def http_host_sync(hostname, user, port, noninteractive):
|
||||||
|
if not noninteractive and not click.confirm(f'Run script on {hostname}?'):
|
||||||
|
return
|
||||||
|
|
||||||
|
c = get_connection(hostname, user, port)
|
||||||
|
run_http_host_sync(c)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
cli()
|
||||||
134
init-server.py
134
init-server.py
@@ -1,134 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
import os
|
|
||||||
|
|
||||||
import click
|
|
||||||
import json5
|
|
||||||
from fabric import Config, Connection
|
|
||||||
|
|
||||||
from ssh_lib.config import config
|
|
||||||
from ssh_lib.tasks_http_host import prepare_http_host, run_http_host_sync, upload_config_and_certs
|
|
||||||
from ssh_lib.tasks_shared import prepare_shared
|
|
||||||
from ssh_lib.tasks_tile_gen import prepare_tile_gen
|
|
||||||
from ssh_lib.utils import (
|
|
||||||
put,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def get_connection(hostname, user, port):
|
|
||||||
ssh_passwd = os.getenv('SSH_PASSWD')
|
|
||||||
|
|
||||||
if ssh_passwd:
|
|
||||||
print('Using SSH password')
|
|
||||||
|
|
||||||
c = Connection(
|
|
||||||
host=hostname,
|
|
||||||
user=user,
|
|
||||||
port=port,
|
|
||||||
connect_kwargs={'password': ssh_passwd},
|
|
||||||
config=Config(overrides={'sudo': {'password': ssh_passwd}}),
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
c = Connection(
|
|
||||||
host=hostname,
|
|
||||||
user=user,
|
|
||||||
port=port,
|
|
||||||
)
|
|
||||||
|
|
||||||
return c
|
|
||||||
|
|
||||||
|
|
||||||
def common_options(func):
|
|
||||||
"""Decorator to define common options."""
|
|
||||||
func = click.argument('hostname')(func)
|
|
||||||
func = click.option('--port', type=int, help='SSH port (if not in .ssh/config)')(func)
|
|
||||||
func = click.option('--user', help='SSH user (if not in .ssh/config)')(func)
|
|
||||||
func = click.option('-y', '--noninteractive', is_flag=True, help='Skip confirmation questions')(
|
|
||||||
func
|
|
||||||
)
|
|
||||||
return func
|
|
||||||
|
|
||||||
|
|
||||||
@click.group()
|
|
||||||
def cli():
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
@cli.command()
|
|
||||||
@common_options
|
|
||||||
def http_host_static(hostname, user, port, noninteractive):
|
|
||||||
if not noninteractive and not click.confirm(f'Run script on {hostname}?'):
|
|
||||||
return
|
|
||||||
|
|
||||||
c = get_connection(hostname, user, port)
|
|
||||||
|
|
||||||
prepare_shared(c)
|
|
||||||
prepare_http_host(c)
|
|
||||||
|
|
||||||
run_http_host_sync(c)
|
|
||||||
|
|
||||||
|
|
||||||
@cli.command()
|
|
||||||
@common_options
|
|
||||||
def http_host_autoupdate(hostname, user, port, noninteractive):
|
|
||||||
if not noninteractive and not click.confirm(f'Run script on {hostname}?'):
|
|
||||||
return
|
|
||||||
|
|
||||||
c = get_connection(hostname, user, port)
|
|
||||||
|
|
||||||
c.sudo('rm -f /etc/cron.d/ofm_http_host')
|
|
||||||
|
|
||||||
prepare_shared(c)
|
|
||||||
prepare_http_host(c)
|
|
||||||
|
|
||||||
# for the monaco run, wait for the sync to complete
|
|
||||||
if json5.loads(config.local_config_jsonc.read_text()).get('skip_planet'):
|
|
||||||
run_http_host_sync(c)
|
|
||||||
|
|
||||||
put(c, config.local_modules_dir / 'http_host' / 'cron.d' / 'ofm_http_host', '/etc/cron.d/')
|
|
||||||
|
|
||||||
|
|
||||||
@cli.command()
|
|
||||||
@common_options
|
|
||||||
def http_host_sync(hostname, user, port, noninteractive):
|
|
||||||
if not noninteractive and not click.confirm(f'Run script on {hostname}?'):
|
|
||||||
return
|
|
||||||
|
|
||||||
c = get_connection(hostname, user, port)
|
|
||||||
run_http_host_sync(c)
|
|
||||||
|
|
||||||
|
|
||||||
@cli.command()
|
|
||||||
@common_options
|
|
||||||
@click.option('--cron', is_flag=True, help='Enable cron task')
|
|
||||||
@click.option('--reinstall', is_flag=True, help='Reinstall everything in /data/ofm folder')
|
|
||||||
def tile_gen(
|
|
||||||
hostname,
|
|
||||||
user,
|
|
||||||
port,
|
|
||||||
noninteractive,
|
|
||||||
#
|
|
||||||
cron,
|
|
||||||
reinstall,
|
|
||||||
):
|
|
||||||
if not noninteractive and not click.confirm(f'Run script on {hostname}?'):
|
|
||||||
return
|
|
||||||
|
|
||||||
c = get_connection(hostname, user, port)
|
|
||||||
|
|
||||||
if reinstall:
|
|
||||||
c.sudo('rm -rf /data/ofm')
|
|
||||||
|
|
||||||
prepare_shared(c)
|
|
||||||
prepare_tile_gen(c, enable_cron=cron)
|
|
||||||
|
|
||||||
|
|
||||||
#
|
|
||||||
@cli.command()
|
|
||||||
@common_options
|
|
||||||
def debug(hostname, user, port, noninteractive):
|
|
||||||
c = get_connection(hostname, user, port)
|
|
||||||
upload_config_and_certs(c)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
cli()
|
|
||||||
38
ssh_lib/cli_helpers.py
Normal file
38
ssh_lib/cli_helpers.py
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
|
import click
|
||||||
|
from fabric import Config, Connection
|
||||||
|
|
||||||
|
|
||||||
|
def get_connection(hostname, user, port):
|
||||||
|
ssh_passwd = os.getenv('SSH_PASSWD')
|
||||||
|
|
||||||
|
if ssh_passwd:
|
||||||
|
print('Using SSH password')
|
||||||
|
|
||||||
|
c = Connection(
|
||||||
|
host=hostname,
|
||||||
|
user=user,
|
||||||
|
port=port,
|
||||||
|
connect_kwargs={'password': ssh_passwd},
|
||||||
|
config=Config(overrides={'sudo': {'password': ssh_passwd}}),
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
c = Connection(
|
||||||
|
host=hostname,
|
||||||
|
user=user,
|
||||||
|
port=port,
|
||||||
|
)
|
||||||
|
|
||||||
|
return c
|
||||||
|
|
||||||
|
|
||||||
|
def common_options(func):
|
||||||
|
"""Decorator to define common options."""
|
||||||
|
func = click.argument('hostname')(func)
|
||||||
|
func = click.option('--port', type=int, help='SSH port (if not in .ssh/config)')(func)
|
||||||
|
func = click.option('--user', help='SSH user (if not in .ssh/config)')(func)
|
||||||
|
func = click.option('-y', '--noninteractive', is_flag=True, help='Skip confirmation questions')(
|
||||||
|
func
|
||||||
|
)
|
||||||
|
return func
|
||||||
35
tile-gen.py
Normal file
35
tile-gen.py
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
import click
|
||||||
|
|
||||||
|
from ssh_lib.cli_helpers import common_options, get_connection
|
||||||
|
from ssh_lib.tasks_shared import prepare_shared
|
||||||
|
from ssh_lib.tasks_tile_gen import prepare_tile_gen
|
||||||
|
|
||||||
|
|
||||||
|
@click.group()
|
||||||
|
def cli():
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
@cli.command()
|
||||||
|
@common_options
|
||||||
|
@click.option('--cron', is_flag=True, help='Enable cron task')
|
||||||
|
@click.option('--reinstall', is_flag=True, help='Reinstall everything in /data/ofm folder')
|
||||||
|
def tile_gen(
|
||||||
|
hostname,
|
||||||
|
user,
|
||||||
|
port,
|
||||||
|
noninteractive,
|
||||||
|
#
|
||||||
|
cron,
|
||||||
|
reinstall,
|
||||||
|
):
|
||||||
|
if not noninteractive and not click.confirm(f'Run script on {hostname}?'):
|
||||||
|
return
|
||||||
|
|
||||||
|
c = get_connection(hostname, user, port)
|
||||||
|
|
||||||
|
if reinstall:
|
||||||
|
c.sudo('rm -rf /data/ofm')
|
||||||
|
|
||||||
|
prepare_shared(c)
|
||||||
|
prepare_tile_gen(c, enable_cron=cron)
|
||||||
Reference in New Issue
Block a user