Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat[DO NOT MERGE]: spike PR with scratch implementation #1515

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions splunk_add_on_ucc_framework/commands/dev_env.py
Original file line number Diff line number Diff line change
@@ -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()
46 changes: 46 additions & 0 deletions splunk_add_on_ucc_framework/commands/run_splunk.sh
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions splunk_add_on_ucc_framework/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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


Expand Down
102 changes: 102 additions & 0 deletions splunk_add_on_ucc_framework/watchdog_handler.py
Original file line number Diff line number Diff line change
@@ -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/<addon-name>?refresh=true
pass


def run_watchdog():
watch = OnMyWatch()
watch.run()
Loading