-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
87 lines (71 loc) · 3.02 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# Copyright 2022 TIER IV, INC. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
import asyncio
import logging
import os
import sys
from pathlib import Path
import grpc.aio
# NOTE: as otaclient_api and ota_metadata are using dynamic module import,
# we need to import them before any other otaclient modules.
import ota_metadata.legacy # noqa: F401
from otaclient import __version__
from otaclient.app.configs import config as cfg
from otaclient.app.configs import ecu_info, server_cfg
from otaclient.app.ota_client_stub import OTAClientServiceStub
from otaclient.log_setting import configure_logging
from otaclient_api.v2 import otaclient_v2_pb2_grpc as v2_grpc
from otaclient_api.v2.api_stub import OtaClientServiceV2
from otaclient_common.common import read_str_from_file, write_str_to_file_sync
# configure logging before any code being executed
configure_logging()
logger = logging.getLogger(__name__)
def _check_other_otaclient():
"""Check if there is another otaclient instance running."""
# create a lock file to prevent multiple ota-client instances start
if pid := read_str_from_file(cfg.OTACLIENT_PID_FILE):
# running process will have a folder under /proc
if Path(f"/proc/{pid}").is_dir():
logger.error(f"another instance of ota-client({pid=}) is running, abort")
sys.exit()
else:
logger.warning(f"dangling otaclient lock file({pid=}) detected, cleanup")
Path(cfg.OTACLIENT_PID_FILE).unlink(missing_ok=True)
# create run dir
_run_dir = Path(cfg.RUN_DIR)
_run_dir.mkdir(parents=True, exist_ok=True)
os.chmod(_run_dir, 0o550)
# write our pid to the lock file
write_str_to_file_sync(cfg.OTACLIENT_PID_FILE, f"{os.getpid()}")
def create_otaclient_grpc_server():
service_stub = OTAClientServiceStub()
ota_client_service_v2 = OtaClientServiceV2(service_stub)
server = grpc.aio.server()
v2_grpc.add_OtaClientServiceServicer_to_server(
server=server, servicer=ota_client_service_v2
)
server.add_insecure_port(f"{ecu_info.ip_addr}:{server_cfg.SERVER_PORT}")
return server
async def launch_otaclient_grpc_server():
server = create_otaclient_grpc_server()
await server.start()
await server.wait_for_termination()
def main():
logger.info("started")
logger.info(f"otaclient version: {__version__}")
logger.info(f"ecu_info.yaml: \n{ecu_info}")
# start the otaclient grpc server
_check_other_otaclient()
asyncio.run(launch_otaclient_grpc_server())