Skip to content

Commit

Permalink
Add input protection scripts for SEA sensors, update SCOS plugins (#256)
Browse files Browse the repository at this point in the history
* Add input protection scripts

* Update scos plugin branches for testing

* Make input protection script executable

* Update requirements files

* Update scos-tekrsa and scos-actions

* Update pre-commit hooks

* Run pre-commit hooks

* Modify log levels

* Update pre-commit autoformatter

* Run autoformatter

* Update requirements

---------

Co-authored-by: root <[email protected]>
  • Loading branch information
aromanielloNTIA and root authored Oct 27, 2023
1 parent d8dabea commit d652dcb
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 27 deletions.
8 changes: 4 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ default_language_version:
python: python3.8
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v4.5.0
hooks:
- id: check-ast
types: [file, python]
Expand All @@ -19,7 +19,7 @@ repos:
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/asottile/pyupgrade
rev: v3.4.0
rev: v3.15.0
hooks:
- id: pyupgrade
args: ["--py38-plus"]
Expand All @@ -31,12 +31,12 @@ repos:
types: [file, python]
args: ["--profile", "black", "--filter-files", "--gitignore"]
- repo: https://github.com/psf/black
rev: 23.3.0
rev: 23.10.1
hooks:
- id: black
types: [file, python]
- repo: https://github.com/igorshubovych/markdownlint-cli
rev: v0.35.0
rev: v0.37.0
hooks:
- id: markdownlint
types: [file, markdown]
Expand Down
26 changes: 26 additions & 0 deletions scripts/_input_protection_disable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import json

from its_preselector.controlbyweb_web_relay import ControlByWebWebRelay
from its_preselector.web_relay_preselector import WebRelayPreselector

sensor_definition_file = "/opt/scos-sensor/configs/sensor_definition.json"
preselector_json_file = "/opt/scos-sensor/configs/preselector_config.json"
spu_relay_json_file = "/opt/scos-sensor/configs/switches/x410_config.json"

# Load Preselector
with open(sensor_definition_file) as f:
sensor_definition_json = json.load(f)
with open(preselector_json_file) as f:
preselector_json = json.load(f)
preselector = WebRelayPreselector(sensor_definition_json, preselector_json)

# Load SPU Relay
with open(spu_relay_json_file) as f:
spu_json = json.load(f)
spu_relay = ControlByWebWebRelay(spu_json)

# Enable Preselector Power
spu_relay.set_state("power_on_preselector")

# Set Preselector to Antenna State
preselector.set_state("antenna")
26 changes: 26 additions & 0 deletions scripts/_input_protection_enable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import json

from its_preselector.controlbyweb_web_relay import ControlByWebWebRelay
from its_preselector.web_relay_preselector import WebRelayPreselector

sensor_definition_file = "/opt/scos-sensor/configs/sensor_definition.json"
preselector_json_file = "/opt/scos-sensor/configs/preselector_config.json"
spu_relay_json_file = "/opt/scos-sensor/configs/switches/x410_config.json"

# Load Preselector
with open(sensor_definition_file) as f:
sensor_definition_json = json.load(f)
with open(preselector_json_file) as f:
preselector_json = json.load(f)
preselector = WebRelayPreselector(sensor_definition_json, preselector_json)

# Load SPU Relay
with open(spu_relay_json_file) as f:
spu_json = json.load(f)
spu_relay = ControlByWebWebRelay(spu_json)

# Set Preselector to Noise Diode Off State
preselector.set_state("noise_diode_off")

# Disable Preselector Power
spu_relay.set_state("power_off_preselector")
29 changes: 29 additions & 0 deletions scripts/input_protection.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash

# Usage:
# - Activate a Python environment with the its_preselector
# package installed
# - Run this script with start and stop times for input
# protection specified as command line arguments.

# Example: Enable input protection from 10:00 to 12:00
# > sudo bash
# > cd /opt/scos-sensor
# > venv/bin/activate
# > pip install git+https://github.com/NTIA/Preselector
# > bash scripts/input_protection.sh 10:00 12:00

# This script sets the preselector state to "noise_diode_off"
# then powers off the preselector, at START_TIME
# Then, at STOP_TIME, the preselector is powered on and set to
# the "antenna" state.


START_TIME=$1
STOP_TIME=$2

SCRIPT_DIR=$(dirname "$0")

echo "python3 $SCRIPT_DIR/_input_protection_enable.py" | at $START_TIME

echo "python3 $SCRIPT_DIR/_input_protection_disable.py" | at $STOP_TIME
2 changes: 1 addition & 1 deletion src/requirements-dev.in
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ tox>=4.0,<5.0
# The following are sub-dependencies for which SCOS Sensor enforces a
# higher minimum patch version than the dependencies which require them.
# This is done to ensure the inclusion of specific security patches.
aiohttp>=3.8.5 # CVE-2023-37276
aiohttp>=3.8.5 # CVE-2023-37276
4 changes: 2 additions & 2 deletions src/requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -321,11 +321,11 @@ scipy==1.10.1
# via
# -r requirements.txt
# scos-actions
scos-actions @ git+https://github.com/NTIA/scos-actions@6.3.3
scos-actions @ git+https://github.com/NTIA/scos-actions@6.4.2
# via
# -r requirements.txt
# scos-tekrsa
scos-tekrsa @ git+https://github.com/NTIA/[email protected].3
scos-tekrsa @ git+https://github.com/NTIA/[email protected].5
# via -r requirements.txt
sigmf @ git+https://github.com/NTIA/SigMF@multi-recording-archive
# via
Expand Down
3 changes: 1 addition & 2 deletions src/requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ psycopg2-binary>=2.0, <3.0
pyjwt>=2.4.0, <3.0
requests-mock>=1.0, <2.0
requests_oauthlib>=1.0, <2.0
scos_actions @ git+https://github.com/NTIA/[email protected]
scos_tekrsa @ git+https://github.com/NTIA/[email protected]
scos_tekrsa @ git+https://github.com/NTIA/[email protected]

# The following are sub-dependencies for which SCOS Sensor enforces a
# higher minimum patch version than the dependencies which require them.
Expand Down
8 changes: 3 additions & 5 deletions src/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,9 @@ ruamel-yaml-clib==0.2.8
# via ruamel-yaml
scipy==1.10.1
# via scos-actions
scos-actions @ git+https://github.com/NTIA/[email protected]
# via
# -r requirements.in
# scos-tekrsa
scos-tekrsa @ git+https://github.com/NTIA/[email protected]
scos-actions @ git+https://github.com/NTIA/[email protected]
# via scos-tekrsa
scos-tekrsa @ git+https://github.com/NTIA/[email protected]
# via -r requirements.in
sigmf @ git+https://github.com/NTIA/SigMF@multi-recording-archive
# via scos-actions
Expand Down
22 changes: 13 additions & 9 deletions src/scheduler/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,16 @@ def _consume_task_queue(self, pending_task_queue):
status, detail = self._call_task_action()
finished = timezone.now()
if settings.ASYNC_CALLBACK:
finalize_task_thread = threading.Thread(target=self._finalize_task_result, args=(task_result, started,finished,status,detail), daemon=True)
finalize_task_thread = threading.Thread(
target=self._finalize_task_result,
args=(task_result, started, finished, status, detail),
daemon=True,
)
finalize_task_thread.start()
else:
self._finalize_task_result(task_result, started, finished, status, detail)
self._finalize_task_result(
task_result, started, finished, status, detail
)

def _initialize_task_result(self) -> TaskResult:
"""Initalize an 'in-progress' result so it exists when action runs."""
Expand Down Expand Up @@ -177,10 +183,9 @@ def _finalize_task_result(self, task_result, started, finished, status, detail):
task_result.detail = detail
task_result.save()


if self.entry.callback_url:
try:
logger.info("Trying callback to URL: " + self.entry.callback_url)
logger.debug("Trying callback to URL: " + self.entry.callback_url)
context = {"request": self.entry.request}
result_json = TaskResultSerializer(task_result, context=context).data
verify_ssl = settings.CALLBACK_SSL_VERIFICATION
Expand All @@ -200,7 +205,7 @@ def _finalize_task_result(self, task_result, started, finished, status, detail):
)
self._callback_response_handler(response, task_result)
else:
logger.info("Posting with token")
logger.debug("Posting callback with token")
token = self.entry.owner.auth_token
headers = {"Authorization": "Token " + str(token)}
response = requests.post(
Expand All @@ -210,7 +215,7 @@ def _finalize_task_result(self, task_result, started, finished, status, detail):
verify=verify_ssl,
timeout=settings.CALLBACK_TIMEOUT,
)
logger.info("posted")
logger.debug("posted callback")
self._callback_response_handler(response, task_result)
except Exception as err:
logger.error(str(err))
Expand All @@ -231,11 +236,10 @@ def _finalize_task_result(self, task_result, started, finished, status, detail):

self.last_status = status


@staticmethod
def _callback_response_handler(resp, task_result):
if resp.ok:
logger.info(f"POSTed to {resp.url}")
logger.debug(f"POSTed to {resp.url}")
else:
msg = "Failed to POST to {}: {}"
logger.warning(msg.format(resp.url, resp.reason))
Expand Down Expand Up @@ -308,7 +312,7 @@ def _get_min_interval(self, schedule_snapshot):
def _cancel_if_completed(self, entry):
if not entry.has_remaining_times():
msg = f"no times remaining in {entry.name}, removing"
logger.debug(msg)
logger.info(msg)
self.cancel(entry)

@property
Expand Down
1 change: 0 additions & 1 deletion src/sensor/runtime_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
SCOS_SENSOR_GIT_TAG = env("SCOS_SENSOR_GIT_TAG", default="Unknown")

if not DOCKER_TAG or DOCKER_TAG == "latest":

VERSION_STRING = GIT_BRANCH
else:
VERSION_STRING = DOCKER_TAG
Expand Down
2 changes: 1 addition & 1 deletion src/sensor/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,4 +426,4 @@
)
SIGAN_POWER_CYCLE_STATES = env("SIGAN_POWER_CYCLE_STATES", default=None)
SIGAN_POWER_SWITCH = env("SIGAN_POWER_SWITCH", default=None)
MAX_FAILURES = env("MAX_FAILURES", default=2)
MAX_FAILURES = env("MAX_FAILURES", default=2)
6 changes: 4 additions & 2 deletions src/status/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from .utils import get_location

logger = logging.getLogger(__name__)
logger.info("Loading status/views")
logger.debug("Loading status/views")


def serialize_location():
Expand All @@ -33,13 +33,15 @@ def serialize_location():


def disk_usage():
"""Return the total disk usage as a percentage."""
usage = shutil.disk_usage("/")
percent_used = round(100 * usage.used / usage.total)
logger.info(str(percent_used) + " disk used")
logger.debug(str(percent_used) + " disk used")
return round(percent_used, 2)


def get_days_up():
"""Return the number of days SCOS has been running."""
elapsed = datetime.datetime.utcnow() - start_time
days = elapsed.days
fractional_day = elapsed.seconds / (60 * 60 * 24)
Expand Down

0 comments on commit d652dcb

Please sign in to comment.