Implement more of the cases where we should be looking in the same directory as managedsoftwareupdate indtead of hard-coding /usr/local/munki

This commit is contained in:
Greg Neagle
2024-10-28 13:00:45 -07:00
parent 9142b382c5
commit e1e59d5bdb
5 changed files with 13 additions and 18 deletions

View File

@@ -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

View File

@@ -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
}

View File

@@ -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)

View File

@@ -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"
}

View File

@@ -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
}