Skip to content

Commit

Permalink
Changed recipe variable handling. (Issue #1 & 2)
Browse files Browse the repository at this point in the history
  • Loading branch information
Honestpuck committed Sep 13, 2020
1 parent 84d4b54 commit 42d1af1
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 46 deletions.
11 changes: 4 additions & 7 deletions JPCImporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
# ARW 2020-03-12 Version 2 with changes for new workflow
# ARW 2020-06-09 Some changes to log levels and cleaning up code
# ARW 2020-06-24 Final tidy before publication
# ARW 2020-09-13 Modified package category handling (see issues #3 & #4)

"""See docstring for JPCImporter class"""

Expand All @@ -28,9 +27,6 @@
LOGLEVEL = logging.DEBUG
LOGFILE = "/usr/local/var/log/%s.log" % APPNAME

# default category for packages
CATEGORY = 'Applications'

__all__ = [APPNAME]


Expand All @@ -41,12 +37,13 @@ class JPCImporter(Processor):

input_variables = {
"pkg_path": {
"required": True,
"required": False,
"description": "Path to the package to be imported into Jamf Pro ",
},
}
output_variables = {
"jpc_importer_summary_result": {"description": "Summary of action"}
"pkg_path": {"description": "The created package.",},
"jpc_importer_summary_result": {"description": "Summary of action"},
}

def setup_logging(self):
Expand Down Expand Up @@ -130,7 +127,7 @@ def upload(self, pkg_path):
# build the package record XML
today = datetime.datetime.now().strftime("(%Y-%m-%d)")
data = "<package><id>{}</id>".format(packid)
data += "<category>{}</category>".format(CATEGORY)
data += "<category>Applications</category>"
data += "<notes>Built by Autopkg. {}</notes></package>".format(today)

# we use requests for all the other API calls as it codes nicer
Expand Down
24 changes: 11 additions & 13 deletions PatchBot.recipe
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
<?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">
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Description</key>
<string>This is an example of a recipe that can be used by another
recipe outside of this directory or repo, to refer to
a processor in this directory.
<string>This is an example of a recipe that can be used
by another recipe outside of this directory or repo, to refer to
a processor in this directory.

Instead of setting the 'Processor' key to a processor name
only, we separate the recipe identifier and the processor
name with a slash:
Instead of setting the 'Processor' key to a processor name
only, we separate the recipe identifier and the processor
name with a slash:

com.honestpuck.PatchBot/JPCImporter.py

&lt;dict&gt;
&lt;key&gt;Processor&lt;/key&gt;
&lt;string&gt;com.honestpuck.PatchBot/JPCImporter.py&lt;/string&gt;
&lt;/dict&gt;

..assuming that this recipe is in one of AutoPkg's search dirs.
..assuming that this recipe is in one of AutoPkg's search dirs.
</string>
<key>Identifier</key>
<string>com.honestpuck.PatchBot</string>
Expand Down
28 changes: 16 additions & 12 deletions PatchManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@
class Package:
"""A package. This exists merely to carry the variables"""

title = "" # the application title matching the test policy
# the application part of the package name matching the test policy
package = ""
patch = "" # name of the patch definition
name = "" # full name of the package '<title>-<version>.pkg'
name = "" # full name of the package '<package>-<version>.pkg'
version = "" # the version of our package
idn = "" # id of the package in our JP server

Expand All @@ -40,8 +41,8 @@ class PatchManager(Processor):
description = __doc__

input_variables = {
"title": {"required": True, "description": "Application title"},
"patch": {"required": True, "description": "Patch name"},
"package": {"required": True, "description": "Application part of package name"},
"patch": {"required": False, "description": "Patch name"},
}
output_variables = {
"patch_manager_summary_result": {"description": "Summary of action"}
Expand Down Expand Up @@ -73,7 +74,7 @@ def setup_logging(self):
def policy(self):
"""Download the TEST policy for the app and return version string"""
self.logger.warning(
"******** Starting policy %s *******" % self.pkg.title
"******** Starting policy %s *******" % self.pkg.package
)
# Which pref format to use, autopkg or jss_importer
autopkg = False
Expand All @@ -91,7 +92,7 @@ def policy(self):
prefs = plistlib.load(fp)
self.base = prefs["url"] + "/JSSResource/"
self.auth = (prefs["user"], prefs["password"])
policy_name = "TEST-{}".format(self.pkg.title)
policy_name = "TEST-{}".format(self.pkg.package)
url = self.base + "policies/name/{}".format(policy_name)
self.logger.debug(
"About to make request URL %s, auth %s" % (url, self.auth)
Expand Down Expand Up @@ -242,7 +243,7 @@ def patch(self):
root.find("general/enabled").text = "true"
# create a description with date
now = datetime.datetime.now().strftime(" (%Y-%m-%d)")
desc = "Update " + self.pkg.title + now
desc = "Update " + self.pkg.package + now
root.find(
"user_interaction/self_service_description"
).text = desc
Expand All @@ -267,24 +268,27 @@ def main(self):
if "patch_manager_summary_result" in self.env:
del self.env["patch_manager_summary_result"]
self.logger.debug("About to update package")
self.pkg.patch = self.env.get("patch")
self.pkg.title = self.env.get("title")
self.pkg.package = self.env.get("package")
try:
self.pkg.patch = self.env.get("patch")
except KeyError:
self.pkg.patch = self.pkg.package

self.pkg.version = self.policy()
pol_id = self.patch()
if pol_id != 0:
self.env["patch_manager_summary_result"] = {
"summary_text": "The following packages were sent to test:",
"report_fields": ["patch_id", "title", "version"],
"report_fields": ["patch_id", "package", "version"],
"data": {
"patch_id": pol_id,
"title": self.pkg.title,
"package": self.pkg.package,
"version": self.pkg.version,
},
}
print(
"%s version %s sent to test"
% (self.pkg.title, self.pkg.version)
% (self.pkg.package, self.pkg.version)
)
else:
self.logger.debug("Zero policy id %s" % self.pkg.patch)
Expand Down
27 changes: 13 additions & 14 deletions Production.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@
class Package:
"""A package. This exists merely to carry the variables"""

title = "" # the application title matching the test policy
# the application title from package name matching the test policy
package = ""
patch = "" # name of the patch definition
name = "" # full name of the package '<title>-<version>.pkg'
name = "" # full name of the package '<package>-<version>.pkg'
version = "" # the version of our package
idn = "" # id of the package in our JP server

Expand Down Expand Up @@ -97,7 +98,7 @@ def setup_logging(self):
def lookup(self):
"""look up test policy to find package name, id and version """
self.logger.debug("Starting")
url = self.base + "/policies/name/Test-" + self.pkg.title
url = self.base + "/policies/name/Test-" + self.pkg.package
pack_base = "package_configuration/packages/package"
self.logger.debug("About to request %s", url)
ret = requests.get(url, auth=self.auth)
Expand All @@ -116,7 +117,7 @@ def lookup(self):

def production(self):
"""change the package in the production policy"""
url = self.base + "/policies/name/Install " + self.pkg.title
url = self.base + "/policies/name/Install " + self.pkg.package
pack_base = "package_configuration/packages/package"
self.logger.debug("About to request %s", url)
ret = requests.get(url, auth=self.auth)
Expand Down Expand Up @@ -161,7 +162,7 @@ def patch(self):
break
if pst_id == 0:
raise ProcessorError(
"Patch list did not contain title: {}".format(self.pkg.title)
"Patch list did not contain title: {}".format(self.pkg.package)
)
# get patch list for our title
url = self.base + "/patchsoftwaretitles/id/" + str(pst_id)
Expand Down Expand Up @@ -234,7 +235,7 @@ def patch(self):
root.find("general/release_date").text = ""
# create a description with date
now = datetime.datetime.now().strftime(" (%Y-%m-%d)")
desc = "Update " + self.pkg.title + now
desc = "Update " + self.pkg.package + now
root.find(
"user_interaction/self_service_description"
).text = desc
Expand Down Expand Up @@ -278,22 +279,20 @@ def main(self):
if "production_summary_result" in self.env:
self.logger.debug("Clearing prev summary")
del self.env["prod_summary_result"]
self.pkg.title = self.env.get("package")
if self.env.get("patch"):
self.pkg.package = self.env.get("package")
try:
self.pkg.patch = self.env.get("patch")
else:
self.pkg.patch = self.pkg.title
self.logger.debug("Set self.pkg.patch: %s", self.pkg.patch)
self.logger.debug("About to call lookup for %s", self.pkg.title)
except KeyError:
self.pkg.patch = self.pkg.package
self.lookup()
self.production()
self.logger.debug("Post production self.pkg.patch: %s", self.pkg.patch)
self.patch()
self.logger.debug("Done patch")
self.env["production_summary_result"] = {
"summary_text": "The following updates were productionized:",
"report_fields": ["title", "version"],
"data": {"title": self.pkg.title, "version": self.pkg.version,},
"report_fields": ["package", "version"],
"data": {"package": self.pkg.package, "version": self.pkg.version,},
}
self.logger.debug(
"Summary done: %s" % self.env["production_summary_result"]
Expand Down

0 comments on commit 42d1af1

Please sign in to comment.