From 5cec81152394bca178047c24aa7223c8ccb685a8 Mon Sep 17 00:00:00 2001 From: sgoral Date: Mon, 23 Dec 2024 10:50:29 +0100 Subject: [PATCH] feat: spike PR with scratch implementation --- .../commands/dev_env.py | 37 +++++++ .../commands/run_splunk.sh | 46 ++++++++ splunk_add_on_ucc_framework/main.py | 14 +++ .../watchdog_handler.py | 102 ++++++++++++++++++ 4 files changed, 199 insertions(+) create mode 100644 splunk_add_on_ucc_framework/commands/dev_env.py create mode 100755 splunk_add_on_ucc_framework/commands/run_splunk.sh create mode 100644 splunk_add_on_ucc_framework/watchdog_handler.py diff --git a/splunk_add_on_ucc_framework/commands/dev_env.py b/splunk_add_on_ucc_framework/commands/dev_env.py new file mode 100644 index 000000000..f989a119a --- /dev/null +++ b/splunk_add_on_ucc_framework/commands/dev_env.py @@ -0,0 +1,37 @@ +# +# Copyright 2024 Splunk Inc. +# +# 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. +# +import logging +import subprocess +from splunk_add_on_ucc_framework import watchdog_handler +import os + + +logger = logging.getLogger("ucc_gen") +internal_root_dir = os.path.dirname(os.path.dirname(__file__)) + + +def set_up_env(splunk_version): + logger.info(f"Setting up env: {splunk_version}") + sv = splunk_version if splunk_version else "latest" + path_to_script = os.path.join(internal_root_dir, "commands", "run_splunk.sh") + process = subprocess.Popen([path_to_script, sv], text=True, stdout=subprocess.PIPE) + while True: + line = process.stdout.readline() + if not line: + break + print(line.rstrip(), flush=True) + print("Splunk is up! Code monitor is running:") + watchdog_handler.run_watchdog() diff --git a/splunk_add_on_ucc_framework/commands/run_splunk.sh b/splunk_add_on_ucc_framework/commands/run_splunk.sh new file mode 100755 index 000000000..3c668bd12 --- /dev/null +++ b/splunk_add_on_ucc_framework/commands/run_splunk.sh @@ -0,0 +1,46 @@ +#!/bin/bash + +# Copyright 2024 Splunk Inc. +# +# 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. + +SCRIPT_DIR=$(cd $(dirname ${BASH_SOURCE[0]}) && pwd) +REPO_ROOT_DIR=$(cd "$SCRIPT_DIR/../.." && pwd) + +# TODO parse app name +APP_NAME="Splunk_TA_UCCExample" + +echo "$REPO_ROOT_DIR/output/$APP_NAME:/opt/splunk/etc/apps/$APP_NAME" + +# running on ARM Mac +if [[ $(uname -m) == 'arm64' ]]; then + export DOCKER_DEFAULT_PLATFORM=linux/amd64 +fi + +docker run \ + -v "$REPO_ROOT_DIR/output/$APP_NAME:/opt/splunk/etc/apps/$APP_NAME" \ + -p 8000:8000 \ + -p 8088:8088 \ + -p 8089:8089 \ + -p 9997:9997 \ + -e "SPLUNK_START_ARGS=--accept-license" \ + -e "SPLUNK_PASSWORD=Chang3d!" \ + -e "SPLUNK_HEC_TOKEN=4a8a737d-5452-426c-a6f7-106dca4e813f" \ + -e "SPLUNK_DISABLE_POPUPS=true" \ + -d \ + --pull=always \ + --name splunk splunk/splunk:${1} + +echo "Waiting Splunk for run" + +until curl -Lsk "https://localhost:8088/services/collector/health" &>/dev/null ; do echo "." && sleep 5 ; done \ No newline at end of file diff --git a/splunk_add_on_ucc_framework/main.py b/splunk_add_on_ucc_framework/main.py index 6ea50c034..39fc19a67 100644 --- a/splunk_add_on_ucc_framework/main.py +++ b/splunk_add_on_ucc_framework/main.py @@ -23,6 +23,7 @@ from splunk_add_on_ucc_framework.commands import init from splunk_add_on_ucc_framework.commands import import_from_aob from splunk_add_on_ucc_framework.commands import package +from splunk_add_on_ucc_framework.commands import dev_env logger = logging.getLogger("ucc_gen") @@ -233,6 +234,15 @@ def main(argv: Optional[Sequence[str]] = None) -> int: required=True, ) + dev_env_parser = subparsers.add_parser("dev-env", description="Spin up development environment.") + dev_env_parser.add_argument( + "--splunk-version", + type=str, + help="splunk version", + required=False, + default=None, + ) + args = parser.parse_args(argv) if args.command == "build": build.generate( @@ -264,6 +274,10 @@ def main(argv: Optional[Sequence[str]] = None) -> int: import_from_aob.import_from_aob( addon_name=args.addon_name, ) + if args.command == "dev-env": + dev_env.set_up_env( + splunk_version=args.splunk_version + ) return 0 diff --git a/splunk_add_on_ucc_framework/watchdog_handler.py b/splunk_add_on_ucc_framework/watchdog_handler.py new file mode 100644 index 000000000..1997d8660 --- /dev/null +++ b/splunk_add_on_ucc_framework/watchdog_handler.py @@ -0,0 +1,102 @@ +# +# Copyright 2024 Splunk Inc. +# +# 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. +# +import time +import json +import subprocess +from pprint import pprint +from watchdog.observers import Observer +from watchdog.events import FileSystemEventHandler + + +def get_gc(path): + with open(path) as f: + file = f.read() + gc = json.loads(file) + return gc + +# TODO parse config path +old_gc = get_gc(r"/Users/sgoral/Splunk_repos/addonfactory-ucc-generator/tests/testdata/test_addons/package_global_config_everything/globalConfig.json") + + +class OnMyWatch: + # TODO parse directory path + watchDirectory = r"/Users/sgoral/Splunk_repos/addonfactory-ucc-generator/tests/testdata/test_addons/package_global_config_everything" + + def __init__(self): + self.observer = Observer() + + def run(self) -> None: + event_handler = Handler(self.observer) + self.observer.schedule(event_handler, self.watchDirectory, recursive=True) + self.observer.start() + try: + while True: + time.sleep(5) + except: + self.observer.stop() + print("Observer Stopped") + + self.observer.join() + + +class Handler(FileSystemEventHandler): + def __init__(self, observer): + self.observer = observer + self.last_trigger = time.time() + + def on_any_event(self, event) -> None: + if event.is_directory: + return None + if event.src_path.find('~') == -1: + global old_gc + print("Watchdog received created event - % s." % event.src_path) + # TODO handle different files than globalConfig.json + new_gc = get_gc(event.src_path) + if only_meta_changed(old_gc, new_gc): + pass + else: + # TODO parse package path from console + # TODO fix infinite build + pa = r"/Users/sgoral/Splunk_repos/addonfactory-ucc-generator/tests/testdata/test_addons/package_global_config_everything/package" + cmd = f"ucc-gen build --source {pa}" + subprocess.run(cmd, shell=True) + + refresh_addon() + + old_gc = new_gc + + +def only_meta_changed(old_config: dict, new_config: dict) -> bool: + changed = set() + for ok, ov in old_config.items(): + for nk, nv in new_config.items(): + if ok == nk: + if ov != nv: + changed.add(ok) + + if len(changed) == 1 and next(iter(changed)) == "meta": + return True + return False + + +def refresh_addon(): + # TODO call endpoint https://127.0.0.1:8089/services/apps/local/?refresh=true + pass + + +def run_watchdog(): + watch = OnMyWatch() + watch.run()