Skip to content
Open
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
151 changes: 95 additions & 56 deletions blade_runner/controllers/main_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,27 +57,71 @@


class MainController(Controller):
"""Blade-Runner's main controller."""
"""Blade Runner main controller."""

def __init__(self, root, server, slack_data, verify_params, search_params, doc_settings):
def __init__(self, root, config_dir):
"""Initialize the main controller.

Args:
root: The root Tk window.
server (JssServer): The JSS server to connect to.
slack_data (dict): Slack configuration data.
verify_params (dict): Verification parameters configuration data.
search_params (dict): Search parameters configuration data.
config_dir (str): Path to the configuration directory. See Notes below.

Notes:

config_dir must contain the following directory structure with the accompanying configuration files,
though the configuration file(s) shown in offboard_configs may have a different name.

+---config_dir
| +---jamf_pro_configs
| | /---jamf_pro.plist
| +---offboard_configs
| | /---an_offboard_config
| | /---another_offboard_config
| +---print_config
| | /---print.plist
| +---python_bin_config
| | /---python_bin.plist
| +---search_params_configs
| | /---search_params.plist
| +---slack_configs
| | /---slack.plist
| +---verify_params_configs
| | /---verify_params.plist
-------------

"""
# <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
# Get logger.
self.logger = logging.getLogger(__name__)
# <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
# Initialize config paths
self._config_dir = config_dir
self._jamf_pro_config_path = os.path.join(config_dir, "jamf_pro_configs/jamf_pro.plist")
self._slack_config_path = os.path.join(config_dir, "slack_configs/slack.plist")
self._verify_config_path = os.path.join(config_dir, "verify_params_configs/verify_params.plist")
self._search_config_path = os.path.join(config_dir, "search_params_configs/search_params.plist")
self._print_config_path = os.path.join(config_dir, "print_config/print.plist")
self._python_bin_config_path = os.path.join(config_dir, "python_bin_config/python_bin.plist")
self._offboard_configs_dir = os.path.join(config_dir, "offboard_configs")
# <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
# Check arg type. If it's a string, process as path and create dictionary, otherwise just save it.
jss_server_data = plistlib.readPlist(self._jamf_pro_config_path)
self._jss_server = JssServer(**jss_server_data)

self._slack_data = plistlib.readPlist(self._slack_config_path)

self._verify_params_data = plistlib.readPlist(self._verify_config_path)
self.verify_params = VerifyParams(self._verify_params_data)

self._search_params_data = plistlib.readPlist(self._search_config_path)
self.search_params = SearchParams(self._search_params_data)

self._doc_settings = plistlib.readPlist(self._print_config_path)
# <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
# Set report callback so that a Tk window will appear with the exception message when an exception occurs.
self._root = root
self._root.report_callback_exception = self._exception_messagebox
# <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
# JSS server
self._jss_server = server
# <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
# Stores information about the computer
self._computer = Computer()
self._computer.serial_number = self._computer.get_serial()
Expand All @@ -93,32 +137,45 @@ def __init__(self, root, server, slack_data, verify_params, search_params, doc_s
self._search_controller = None
self._verify_controller = None
# <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
# Set whether or not a JSSDoc will be printed to a printer.
self._doc_settings = doc_settings
# <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
# Set configuration data
# Initialize offboard config.
self._offboard_config = None
self._slack_data = slack_data
self.verify_params = VerifyParams(verify_params)
self.search_params = SearchParams(search_params)
# <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
# Save Verify/SearchParams original input
self.verify_params_input = verify_params
self.search_params_input = search_params
# <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
# Set path to config directory that contains the configuration files.

app_root_dir = os.path.abspath(__file__)
for i in range(3):
app_root_dir = os.path.dirname(app_root_dir)

self.app_root_dir = app_root_dir
self._config_dir = os.path.join(self.app_root_dir, "config")
self._blade_runner_dir = os.path.join(self.app_root_dir, "blade_runner")
self._slack_dir = os.path.join(self._blade_runner_dir, "slack")
self._offboard_configs_dir = os.path.join(self._config_dir, "offboard_configs")
self._secure_erase_dir = os.path.join(self._blade_runner_dir, "secure_erase")

def reload_configs(self):
"""Reloads configuration files if the path to the config has been specified.

Returns:
void
"""
if self._jamf_pro_config_path:
jss_server_data = plistlib.readPlist(self._jamf_pro_config_path)
self._jss_server = JssServer(**jss_server_data)

if self._slack_config_path:
self._slack_data = plistlib.readPlist(self._slack_config_path)

if self._verify_config_path:
self._verify_params_data = plistlib.readPlist(self._verify_config_path)
self.verify_params = VerifyParams(self._verify_params_data)

if self._search_config_path:
self._search_params_data = plistlib.readPlist(self._search_config_path)
self.search_params = SearchParams(self._search_params_data)

if self._print_config_path:
self._doc_settings = plistlib.readPlist(self._print_config_path)

self.logger.info("Reloaded configuration files.")

def _exception_messagebox(self, exc, value, traceback):
"""Displays a message box with the accompanying exception message whenver an exception occurs.

Expand Down Expand Up @@ -191,10 +248,10 @@ def restart(self):
self._computer = Computer()
# <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
# Create a new VerifyParams object.
self.verify_params = VerifyParams(self.verify_params_input)
self.verify_params = VerifyParams(self._verify_params_data)
# <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
# Create a new SearchParams object.
self.search_params = SearchParams(self.search_params_input)
self.search_params = SearchParams(self._search_params_data)
# <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
self.logger.info("Blade-Runner reset.")

Expand Down Expand Up @@ -583,27 +640,28 @@ def open_config(self, config_id):
# <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
# Determine the path to the configuration file.
if config_id == "slack":
path = "slack_configs/slack.plist"
path = self._slack_config_path
elif config_id == "offboard":
path = "offboard_configs/"
path = os.path.join(self._config_dir, "offboard_configs")
elif config_id == "verify":
path = "verify_params_configs/verify_params.plist"
path = self._verify_config_path
elif config_id == "search":
path = "search_params_configs/search_params.plist"
path = self._search_config_path
elif config_id == "config":
path = ""
path = self._config_dir
elif config_id == "print":
path = "print_config/print.plist"
path = self._print_config_path
elif config_id == "python_bin":
path = "python_bin_config/python_bin.plist"
path = self._python_bin_config_path
elif config_id == "jamf_pro":
path = "jamf_pro_configs/jamf_pro.plist"
path = self._jamf_pro_config_path
else:
self.logger.warn("No configuration ID matches \"{}\"".format(config_id))
raise SystemError("No configuration ID matches \"{}\"".format(config_id))
# <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
# Open the file in its default application
subprocess.check_output(["open", os.path.join(self._config_dir, path)])
if path:
subprocess.check_output(["open", path])

def cat_readme(self):
"""Outputs the text of README.md.
Expand Down Expand Up @@ -656,30 +714,11 @@ def main():
for i in range(3):
blade_runner_dir = os.path.dirname(blade_runner_dir)
# <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
# Read from jss config plist and set up the Jamf Pro server
jss_server_plist = os.path.join(blade_runner_dir, "config/jamf_pro_configs/jamf_pro.plist")
jss_server_data = plistlib.readPlist(jss_server_plist)
jss_server = JssServer(**jss_server_data)
logger.debug(jss_server._jss_url)

# Read from Slack config plist to set up Slack notifications
slack_plist = os.path.join(blade_runner_dir, "config/slack_configs/slack.plist")
slack_data = plistlib.readPlist(slack_plist)

# Read from verify params plist to set up verification parameters
verify_config = os.path.join(blade_runner_dir, "config/verify_params_configs/verify_params.plist")
verify_params = plistlib.readPlist(verify_config)

# Read from search params plist to set up search parameters.
search_params_config = os.path.join(blade_runner_dir, "config/search_params_configs/search_params.plist")
search_params = plistlib.readPlist(search_params_config)

# Read from print config to enable or disable printing.
print_config = os.path.join(blade_runner_dir, "config/print_config/print.plist")
print_settings = plistlib.readPlist(print_config)
# Path to configuration directory.
config_dir = os.path.join(blade_runner_dir, "config")
# <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
# Run the application
app = MainController(root, jss_server, slack_data, verify_params, search_params, print_settings)
app = MainController(root, config_dir)
app.run()
logger.info("Blade Runner exiting.")

Expand Down
6 changes: 6 additions & 0 deletions blade_runner/views/main_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,8 @@ def _back_btn_clicked(self):
# Settins scene logic
elif self.curr_scene == "settings_scene":
self._grid_forget_settings_scene()
# Reload configs before switching scenes.
self._controller.reload_configs()
if self.prev_scene == "selection_scene":
self._selection_scene()
elif self.prev_scene == "offboard_scene":
Expand All @@ -418,6 +420,10 @@ def _help_btn_clicked(self, curr_scene):
void
"""
# <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
# If the current scene is the settings scene, reload the configs before switching to the help scene.
if curr_scene == "settings_scene":
self._controller.reload_configs()
# <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
# Set previous scene.
if curr_scene != "help_scene":
self.prev_scene = curr_scene
Expand Down
8 changes: 8 additions & 0 deletions test/config/python_bin_config/python_bin.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>python_binary</key>
<string>/usr/bin/python</string>
</dict>
</plist>
25 changes: 2 additions & 23 deletions test/test_blade_runner_manual.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import os
import sys
import logging
import plistlib
import unittest
import subprocess
import Tkinter as tk
Expand All @@ -41,7 +40,6 @@
sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(__file__)), "blade_runner/dependencies"))
sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(__file__)), "blade_runner/slack"))

from blade_runner.jamf_pro.jss_server import JssServer
from blade_runner.controllers.main_controller import MainController

logging.getLogger(__name__).addHandler(logging.NullHandler())
Expand All @@ -60,29 +58,10 @@ def setUp(self):
abs_file_path = os.path.abspath(__file__)
self.blade_runner_dir = os.path.dirname(abs_file_path)
# <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
# Get Jamf Pro settings.
jss_server_plist = os.path.join(self.blade_runner_dir, "config/jamf_pro_configs/jamf_pro.plist")
jss_server_data = plistlib.readPlist(jss_server_plist)
jss_server = JssServer(**jss_server_data)

# Get Slack settings.
self.slack_plist = os.path.join(self.blade_runner_dir, "config/slack_configs/slack.plist")
self.slack_data = plistlib.readPlist(self.slack_plist)

# Get verification parameter settings
self.verify_config = os.path.join(self.blade_runner_dir, "config/verify_params_configs/verify_params.plist")
self.verify_data = plistlib.readPlist(self.verify_config)

# Get search parameter settings.
self.search_params_config = os.path.join(self.blade_runner_dir, "config/search_params_configs/search_params.plist")
self.search_params = plistlib.readPlist(self.search_params_config)

# Get document settings.
self.print_config = os.path.join(self.blade_runner_dir, "config/print_config/print.plist")
self.print_settings = plistlib.readPlist(self.print_config)
self.config_dir = os.path.join(self.blade_runner_dir, "config")

# Set up main controller.
self.br = MainController(root, jss_server, self.slack_data, self.verify_data, self.search_params, self.print_settings)
self.br = MainController(root, self.config_dir)

def test_manual(self):
"""Manually test Blade Runner."""
Expand Down