Skip to content

Commit

Permalink
Set pretty hostname on service start
Browse files Browse the repository at this point in the history
  • Loading branch information
jcapona committed Apr 1, 2024
1 parent 77459ed commit b4308a6
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 23 deletions.
1 change: 1 addition & 0 deletions debian/further-link.lintian-overrides
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ further-link: no-manual-page usr/bin/further-link
further-link: no-manual-page usr/bin/start-further
further-link: no-manual-page usr/bin/further-link-bluetooth-pairing
further-link: no-manual-page usr/bin/further-link-bluetooth-encryption
further-link: no-manual-page usr/bin/further-link-set-pretty-hostname
1 change: 1 addition & 0 deletions debian/further-link.service
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ After=network.target
Type=simple
Environment="PYTHONUNBUFFERED=1"
Environment="PYTHONDONTWRITEBYTECODE=1"
ExecStartPre=/usr/bin/further-link-set-pretty-hostname
ExecStart=/usr/bin/further-link

[Install]
Expand Down
25 changes: 2 additions & 23 deletions further_link/endpoint/apt_version.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import json
import re
from os import environ
from shlex import split
from subprocess import run

from aiohttp import web

from ..util.sdk import run_command


def _apt_version_dict(package):
return {"version": apt_cache_installed(package)}
Expand All @@ -29,23 +28,3 @@ def apt_cache_installed(pkg_name):
return match.group(1) if match else None
except Exception:
return None


def run_command(command_str):
def __get_env():
env = environ.copy()
# Print output of commands in english
env["LANG"] = "en_US.UTF-8"
return env

try:
resp = run(
split(command_str),
check=False,
capture_output=True,
timeout=5,
env=__get_env(),
)
return str(resp.stdout, "utf8")
except Exception:
return None
15 changes: 15 additions & 0 deletions further_link/util/hostname.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import logging

Check warning on line 1 in further_link/util/hostname.py

View check run for this annotation

Codecov / codecov/patch

further_link/util/hostname.py#L1

Added line #L1 was not covered by tests

import click

Check warning on line 3 in further_link/util/hostname.py

View check run for this annotation

Codecov / codecov/patch

further_link/util/hostname.py#L3

Added line #L3 was not covered by tests

from .bluetooth.utils import get_bluetooth_server_name
from .sdk import run_command

Check warning on line 6 in further_link/util/hostname.py

View check run for this annotation

Codecov / codecov/patch

further_link/util/hostname.py#L5-L6

Added lines #L5 - L6 were not covered by tests


@click.command()
def set_pretty_hostname() -> None:
try:
name = get_bluetooth_server_name()
run_command(f"hostnamectl set-hostname --pretty {name}")
except Exception as e:
logging.error(f"Error setting pretty hostname: {e}")

Check warning on line 15 in further_link/util/hostname.py

View check run for this annotation

Codecov / codecov/patch

further_link/util/hostname.py#L9-L15

Added lines #L9 - L15 were not covered by tests
21 changes: 21 additions & 0 deletions further_link/util/sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from glob import glob
from os import environ
from shlex import split
from subprocess import run
from typing import List, Optional

Expand Down Expand Up @@ -80,3 +81,23 @@ def __call__(cls, *args, **kwargs):
if cls.instance is None:
cls.instance = super(Singleton, cls).__call__(*args, **kwargs)
return cls.instance


def run_command(command_str):
def __get_env():
env = environ.copy()
# Print output of commands in english
env["LANG"] = "en_US.UTF-8"
return env

try:
resp = run(
split(command_str),
check=False,
capture_output=True,
timeout=5,
env=__get_env(),
)
return str(resp.stdout, "utf8")
except Exception:
return None

Check warning on line 103 in further_link/util/sdk.py

View check run for this annotation

Codecov / codecov/patch

further_link/util/sdk.py#L102-L103

Added lines #L102 - L103 were not covered by tests
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ console_scripts =
start-further=further_link.start_further:start_further
further-link-bluetooth-pairing=further_link.util.bluetooth.pairing:main
further-link-bluetooth-encryption=further_link.util.bluetooth.encryption:main
further-link-set-pretty-hostname=further_link.util.hostname:set_pretty_hostname

[bdist_wheel]
universal = 1
Expand Down

0 comments on commit b4308a6

Please sign in to comment.