def test_jcds_create_and_upload(self, cloud_j, pkg_path): # type: (JSS) -> None """Assert that we can create a package object, then upload an associated package via JCDS.""" jcds = JCDS(jss=cloud_j) jcds._scrape_tokens() assert 'jcds_base_url' in jcds.connection print(jcds.connection['jcds_base_url']) assert 'jcds_upload_token' in jcds.connection print(jcds.connection['jcds_upload_token']) jcds.copy_pkg(pkg_path) pkg = jss.Package(cloud_j, os.path.basename(pkg_path)) pkg.save()
def handle_package(self): """Creates or updates, and copies a package object. This will only upload a package if a file with the same name does not already exist on a DP. If you need to force a re-upload, you must delete the package on the DP first. Further, if you are using a JDS, it will only upload a package if a package object with a filename matching the AutoPkg filename does not exist. If you need to force a re-upload to a JDS, please delete the package object through the web interface first. """ # Skip package handling if there is no package or repos. if self.env["JSS_REPOS"] and self.env["pkg_path"] != "": os_requirements = self.env.get("os_requirements") package_info = self.env.get("package_info") package_notes = self.env.get("package_notes") # See if the package is non-flat (requires zipping prior to # upload). if os.path.isdir(self.env["pkg_path"]): shutil.make_archive(self.env["pkg_path"], "zip", os.path.dirname(self.env["pkg_path"]), self.pkg_name) self.env["pkg_path"] += ".zip" self.pkg_name += ".zip" try: package = self.jss.Package(self.pkg_name) self.output("Pkg-object already exists according to JSS, " "moving on...") except jss.JSSGetError: # Package doesn't exist package = jss.Package(self.jss, self.pkg_name) pkg_update = ( self.env["jss_changed_objects"]["jss_package_updated"]) if self.category is not None: cat_name = self.category.name else: cat_name = "" self.update_object(cat_name, package, "category", pkg_update) self.update_object(os_requirements, package, "os_requirements", pkg_update) self.update_object(package_info, package, "info", pkg_update) self.update_object(package_notes, package, "notes", pkg_update) # Ensure packages are on distribution point(s) # If we had to make a new package object, we know we need to # copy the package file, regardless of DP type. This solves # the issue regarding the JDS.exists() method: See # python-jss docs for info. The problem with this method is # that if you cancel an AutoPkg run and the package object # has been created, but not uploaded, you will need to # delete the package object from the JSS before running a # recipe again or it won't upload the package file. # # Passes the id of the newly created package object so JDS' # will upload to the correct package object. Ignored by # AFP/SMB. if self.env["jss_changed_objects"]["jss_package_added"]: self.copy(self.env["pkg_path"], id_=package.id) # For AFP/SMB shares, we still want to see if the package # exists. If it's missing, copy it! elif not self.jss.distribution_points.exists( os.path.basename(self.env["pkg_path"])): self.copy(self.env["pkg_path"]) else: self.output("Package upload not needed.") else: package = None self.output("Package upload and object update skipped. If this is " "a mistake, ensure you have JSS_REPOS configured.") return package
def handle_package(self): """Creates or updates a package object on the jss, then uploads a package file to the configured distribution points. This will only upload a package if a file with the same name does not already exist on a DP. If you need to force a re-upload, you must delete the package on the DP first. Further, if you are using a JDS, it will only upload a package if a package object with a filename matching the AutoPkg filename does not exist. If you need to force a re-upload to a JDS, please delete the package object through the web interface first. """ # Skip package handling if there is no package or repos. if self.env["JSS_REPOS"] and self.env["pkg_path"] != '': os_requirements = self.env.get("os_requirements") package_info = self.env.get("package_info") package_notes = self.env.get("package_notes") # See if the package is non-flat (requires zipping prior to # upload). if os.path.isdir(self.env['pkg_path']): shutil.make_archive(self.env['pkg_path'], 'zip', os.path.dirname(self.env['pkg_path']), self.pkg_name) self.env['pkg_path'] += '.zip' self.pkg_name += '.zip' try: package = self.j.Package(self.pkg_name) self.output("Pkg-object already exists according to JSS, " "moving on...") # TODO: 4x of basically the same block code smell! # Set os_requirements if they don't match. if os_requirements != package.findtext("os_requirements"): package.set_os_requirements(os_requirements) package.save() self.output("Package os_requirements updated.") self.env["jss_changed_objects"][ "jss_package_updated"].append(package.name) # Set package_info if they don't match. if package_info != package.findtext("info"): package.find("info").text = package_info package.save() self.output("Package info field updated.") self.env["jss_changed_objects"][ "jss_package_updated"].append(package.name) # Set package_notes if they don't match. if package_notes != package.findtext("notes"): package.find("notes").text = package_notes package.save() self.output("Package notes field updated.") self.env["jss_changed_objects"][ "jss_package_updated"].append(package.name) # Update category if necessary. if self.category is not None: recipe_name = self.category.name else: recipe_name = 'Unknown' if package.find('category').text != recipe_name: package.find('category').text = recipe_name package.save() self.output("Package category updated.") self.env["jss_changed_objects"][ "jss_package_updated"].append(package.name) except jss.JSSGetError: # Package doesn't exist if self.category is not None: package = jss.Package(self.j, self.pkg_name, cat_name=self.category.name) else: package = jss.Package(self.j, self.pkg_name) package.set_os_requirements(os_requirements) package.find("info").text = package_info package.find("notes").text = package_notes package.save() self.env["jss_changed_objects"]["jss_package_added"].append( package.name) # Ensure packages are on distribution point(s) # If we had to make a new package object, we know we need to # copy the package file, regardless of DP type. This solves # the issue regarding the JDS.exists() method: See # python-jss docs for info. The problem with this method is # that if you cancel an AutoPkg run and the package object # has been created, but not uploaded, you will need to # delete the package object from the JSS before running a # recipe again or it won't upload the package file. # # Passes the id of the newly created package object so JDS' # will upload to the correct package object. Ignored by # AFP/SMB. if self.env["jss_changed_objects"]["jss_package_added"]: self._copy(self.env["pkg_path"], id_=package.id) # For AFP/SMB shares, we still want to see if the package # exists. If it's missing, copy it! elif not self.j.distribution_points.exists( os.path.basename(self.env["pkg_path"])): self._copy(self.env["pkg_path"]) else: self.output("Package upload not needed.") else: package = None self.output("Package upload and object update skipped. If this is " "a mistake, ensure you have JSS_REPOS configured.") return package
def handle_package(self): """Creates or updates, and copies a package object. This will only upload a package if a file with the same name does not already exist on a DP. If you need to force a re-upload, you must delete the package on the DP first. Further, if you are using a JDS, it will only upload a package if a package object with a filename matching the AutoPkg filename does not exist. If you need to force a re-upload to a JDS, please delete the package object through the web interface first. """ # Skip package handling if there is no package or repos. pkg_path = self.env["pkg_path"] if self.env["JSS_REPOS"] and pkg_path != "": # Ensure that `pkg_path` is valid. if not os.path.exists(pkg_path): raise ProcessorError( "JSSImporter can't find a package at '%s'!" % pkg_path) # See if the package is non-flat (requires zipping prior to # upload). if os.path.isdir(pkg_path): pkg_path = self.zip_pkg_path(pkg_path) self.env["pkg_path"] = pkg_path # Make sure our change gets added back into the env for # visibility. self.pkg_name += ".zip" try: package = self.jss.Package(self.pkg_name) self.output("Pkg-object already exists according to JSS, " "moving on...") pkg_update = (self.env["jss_changed_objects"]["jss_package_updated"]) except jss.GetError: # Package doesn't exist package = jss.Package(self.jss, self.pkg_name) pkg_update = (self.env["jss_changed_objects"]["jss_package_added"]) # Ensure packages are on distribution point(s) # If we had to make a new package object, we know we need to # copy the package file, regardless of DP type. This solves # the issue regarding the JDS.exists() method: See # python-jss docs for info. The problem with this method is # that if you cancel an AutoPkg run and the package object # has been created, but not uploaded, you will need to # delete the package object from the JSS before running a # recipe again or it won't upload the package file. # # Passes the id of the newly created package object so JDS' # will upload to the correct package object. Ignored by # AFP/SMB. if self.env["jss_changed_objects"]["jss_package_added"]: self.copy(pkg_path, id_=package.id) self.upload_needed = True # For AFP/SMB shares, we still want to see if the package # exists. If it's missing, copy it! elif not self.jss.distribution_points.exists( os.path.basename(pkg_path)): self.copy(pkg_path) self.upload_needed = True else: self.output("Package upload not needed.") self.upload_needed = False # only update the package object if an upload was carried out if (self.env["STOP_IF_NO_JSS_UPLOAD"] == True and not self.upload_needed): self.output("Not overwriting policy as STOP_IF_NO_JSS_UPLOAD " "is set to True.") self.env["stop_processing_recipe"] = True return # wait for feedback that the package is there (only for cloud repos) try: self.env["JSS_REPOS"][0]["type"] timeout = time.time() + 60 while time.time() < timeout: try: package = self.jss.Package(self.pkg_name) break except: self.output("Waiting for package id from server...") time.sleep(5) self.output("Uploaded package id: {}".format(package.id)) except: pass pkg_update = ( self.env["jss_changed_objects"]["jss_package_updated"]) os_requirements = self.env.get("os_requirements") package_info = self.env.get("package_info") package_notes = self.env.get("package_notes") package_priority = self.env.get("package_priority") package_reboot = self.env.get("package_reboot") package_boot_volume_required = self.env.get( "package_boot_volume_required") if self.category is not None: cat_name = self.category.name else: cat_name = "" self.update_object(cat_name, package, "category", pkg_update) self.update_object(os_requirements, package, "os_requirements", pkg_update) self.update_object(package_info, package, "info", pkg_update) self.update_object(package_notes, package, "notes", pkg_update) self.update_object(package_priority, package, "priority", pkg_update) self.update_object(package_reboot, package, "reboot_required", pkg_update) self.update_object(package_boot_volume_required, package, "boot_volume_required", pkg_update) else: package = None self.output("Package upload and object update skipped. If this is " "a mistake, ensure you have JSS_REPOS configured.") return package