2015-08-28 18:19:13 +00:00
|
|
|
# Copyright 2015 Canonical, Ltd.
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2023-04-21 08:45:26 +00:00
|
|
|
import asyncio
|
2015-08-28 18:19:13 +00:00
|
|
|
import logging
|
2023-07-25 21:26:25 +00:00
|
|
|
|
2015-10-23 15:03:04 +00:00
|
|
|
import yaml
|
2019-11-12 21:06:14 +00:00
|
|
|
from probert.network import StoredDataObserver, UdevObserver
|
|
|
|
|
2023-04-21 08:45:26 +00:00
|
|
|
from subiquitycore.async_helpers import run_in_thread
|
|
|
|
|
2016-06-30 18:17:01 +00:00
|
|
|
log = logging.getLogger("subiquitycore.prober")
|
2015-08-28 18:19:13 +00:00
|
|
|
|
2015-11-02 22:48:37 +00:00
|
|
|
|
2015-08-28 18:19:13 +00:00
|
|
|
class Prober:
|
2019-12-14 20:06:13 +00:00
|
|
|
def __init__(self, machine_config, debug_flags):
|
2017-11-15 01:27:24 +00:00
|
|
|
self.saved_config = None
|
2019-12-14 20:06:13 +00:00
|
|
|
if machine_config:
|
2021-07-14 22:40:22 +00:00
|
|
|
self.saved_config = yaml.safe_load(machine_config)
|
2019-12-14 20:06:13 +00:00
|
|
|
self.debug_flags = debug_flags
|
2017-11-15 01:27:24 +00:00
|
|
|
log.debug("Prober() init finished, data:{}".format(self.saved_config))
|
2015-08-28 18:19:13 +00:00
|
|
|
|
2017-11-15 01:27:24 +00:00
|
|
|
def probe_network(self, receiver):
|
2019-11-12 21:06:14 +00:00
|
|
|
if self.saved_config is not None:
|
2017-11-15 01:27:24 +00:00
|
|
|
observer = StoredDataObserver(self.saved_config["network"], receiver)
|
|
|
|
else:
|
|
|
|
observer = UdevObserver(receiver)
|
|
|
|
return observer, observer.start()
|
|
|
|
|
2023-04-21 08:45:26 +00:00
|
|
|
async def get_storage(self, probe_types=None):
|
2019-11-12 21:06:14 +00:00
|
|
|
if self.saved_config is not None:
|
2019-12-14 20:06:13 +00:00
|
|
|
flag = "bpfail-full"
|
|
|
|
if probe_types is not None:
|
|
|
|
flag = "bpfail-restricted"
|
|
|
|
if flag in self.debug_flags:
|
2023-04-21 08:45:26 +00:00
|
|
|
await asyncio.sleep(2)
|
2019-12-14 20:06:13 +00:00
|
|
|
1 / 0
|
2019-10-31 00:39:01 +00:00
|
|
|
r = self.saved_config["storage"].copy()
|
2022-03-21 20:16:54 +00:00
|
|
|
if probe_types is not None and "defaults" not in probe_types:
|
2019-11-12 21:06:14 +00:00
|
|
|
for k in self.saved_config["storage"]:
|
|
|
|
if k not in probe_types:
|
|
|
|
r[k] = {}
|
2019-10-31 00:39:01 +00:00
|
|
|
return r
|
2023-04-21 08:45:26 +00:00
|
|
|
|
2019-12-14 03:00:17 +00:00
|
|
|
from probert.storage import Storage
|
2023-04-21 08:45:26 +00:00
|
|
|
|
|
|
|
# Until probert is completely free of blocking IO, we should continue
|
|
|
|
# running it in a separate thread.
|
|
|
|
def run_probert(probe_types):
|
2023-04-21 09:01:32 +00:00
|
|
|
return asyncio.run(
|
|
|
|
Storage().probe(probe_types=probe_types, parallelize=True)
|
|
|
|
)
|
2023-04-21 08:45:26 +00:00
|
|
|
|
|
|
|
return await run_in_thread(run_probert, probe_types)
|