diff --git a/code/cli/munki/managedsoftwareupdate/managedsoftwareupdate.swift b/code/cli/munki/managedsoftwareupdate/managedsoftwareupdate.swift index 56956491..0a2fe63f 100644 --- a/code/cli/munki/managedsoftwareupdate/managedsoftwareupdate.swift +++ b/code/cli/munki/managedsoftwareupdate/managedsoftwareupdate.swift @@ -509,7 +509,7 @@ struct ManagedSoftwareUpdate: AsyncParsableCommand { try processLaunchdOptions() try ensureMunkiDirsExist() configureDisplayOptions() - doCleanupTasks(runType: runtype) + await doCleanupTasks(runType: runtype) initializeReport() // TODO: support logging to syslog and unified logging diff --git a/code/cli/munki/managedsoftwareupdate/msuutils.swift b/code/cli/munki/managedsoftwareupdate/msuutils.swift index d5ba5e76..d0493db0 100644 --- a/code/cli/munki/managedsoftwareupdate/msuutils.swift +++ b/code/cli/munki/managedsoftwareupdate/msuutils.swift @@ -83,9 +83,11 @@ func runMunkiDirScript(_ scriptPath: String, taskName: String, runType: String) /// Helper to specifically run Munki preflight or postflight scripts func runPreOrPostScript(name: String, runType: String) async -> Int { - // TODO: make this path relative to managedsoftwareupdate binary - let scriptdir = "/usr/local/munki" as NSString - let scriptPath = scriptdir.appendingPathComponent(name) + // first check the same directory where managedsoftwareupdate lives + var scriptPath = currentExecutableDir(appendingPathComponent: name) + if !pathIsExecutableFile(scriptPath) { + return 0 + } return await runMunkiDirScript(scriptPath, taskName: name, runType: runType) } @@ -93,9 +95,8 @@ func runPreOrPostScript(name: String, runType: String) async -> Int { /// run them and remove them if successful /// NOTE: historically, this has been used to clean up the Python framework when updating /// Python versions. This may no longer be needed. -func doCleanupTasks(_ runType: String) async { - // TODO: make this relative to managedsoftwareupdate binary - let cleanupdir = "/usr/local/munki/cleanup" +func doCleanupTasks(runType: String) async { + let cleanupdir = currentExecutableDir(appendingPathComponent: "cleanup") if !pathIsDirectory(cleanupdir) { return } diff --git a/code/cli/munki/shared/facts.swift b/code/cli/munki/shared/facts.swift index 9bab47eb..9742f8ce 100644 --- a/code/cli/munki/shared/facts.swift +++ b/code/cli/munki/shared/facts.swift @@ -88,16 +88,10 @@ func getMachineFacts() async -> PlistDict { return await MachineFacts.shared.get() } -/// Returns the path to the conditional scripts dir -private func conditionalScriptsDir() -> String { - // TODO: make this relative to the managedsoftwareupdate binary - return "/usr/local/munki/conditions" -} - /// Fetches key/value pairs from condition scripts /// which can be placed into /usr/local/munki/conditions func getConditions() async -> PlistDict { - let conditionalScriptDir = conditionalScriptsDir() + let conditionalScriptDir = currentExecutableDir(appendingPathComponent: "conditions") let conditionalItemsPath = managedInstallsDir(subpath: "ConditionalItems.plist") let filemanager = FileManager.default try? filemanager.removeItem(atPath: conditionalItemsPath) diff --git a/code/cli/munki/shared/updatecheck/download.swift b/code/cli/munki/shared/updatecheck/download.swift index c1ee6c6d..b605e878 100644 --- a/code/cli/munki/shared/updatecheck/download.swift +++ b/code/cli/munki/shared/updatecheck/download.swift @@ -439,7 +439,7 @@ func startPrecachingAgent() { return } // first look in same dir as the current executable - var precacheAgentPath = (currentExecutableDir() as NSString).appendingPathComponent("precache_agent") + var precacheAgentPath = currentExecutableDir(appendingPathComponent: "precache_agent") if !pathExists(precacheAgentPath) { precacheAgentPath = "/usr/local/munki/precache_agent" } diff --git a/code/cli/munki/shared/utils/fileutils.swift b/code/cli/munki/shared/utils/fileutils.swift index 9fd77e34..f81bd19a 100644 --- a/code/cli/munki/shared/utils/fileutils.swift +++ b/code/cli/munki/shared/utils/fileutils.swift @@ -219,9 +219,9 @@ func baseName(_ str: String) -> String { } /// Return the path to the current executable's directory -func currentExecutableDir() -> String { +func currentExecutableDir(appendingPathComponent: String = "") -> String { if let executablePath = Bundle.main.executablePath { - return (executablePath as NSString).deletingLastPathComponent + return ((executablePath as NSString).deletingLastPathComponent as NSString).appendingPathComponent(appendingPathComponent) } - return "" + return appendingPathComponent }