Exemplo n.º 1
0
 def test_jcds_scrape_info(self, cloud_j):  # type: (JSS) -> None
     """Test that the JCDS distribution server object can scrape all needed information from the jamfcloud packages
     page."""
     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'])
Exemplo n.º 2
0
    def __init__(
        self,
        source: str,
        destination: str,
        delete: bool = False,
        username: Optional[str] = None,
        password: Optional[str] = None,
        logger=None,
    ):
        if logger is not None:
            self.logger = logger
        else:
            self.logger = logging.getLogger("SyncOperation")

        source_url = urlparse(source)
        if source_url.scheme == 'https' or source_url.scheme == 'http':
            # Assume JAMF Cloud if given a URL
            self.jss = jss.JSS(
                url=source,
                user=username,
                password=password,
                ssl_verify=False,
            )
            self.source = JCDS(jss=self.jss)
        elif source_url.scheme == '' or source_url.scheme == 'file':
            # Assume source is a local directory
            self.source = source

        dest_url = urlparse(destination)
        if dest_url.scheme == 'https' or dest_url.scheme == 'http':
            # Assume JAMF Cloud if given a URL
            self.jss = jss.JSS(
                url=destination,
                user=username,
                password=password,
                ssl_verify=False,
            )
            self.source = JCDS(jss=self.jss)
        elif dest_url.scheme == '' or dest_url.scheme == 'file':
            # Assume dest is a local directory
            self.destination = destination
Exemplo n.º 3
0
    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()
Exemplo n.º 4
0
    def test_jcds_upload_chunks(self, cloud_j,
                                pkg_path):  # type: (JSS) -> None
        """Assert that we can POST a chunked file as multipart form data, with form field name = ``file`` and filename
        = ``blob`. eg.

        POST https://regioncode-jcds.jamfcloud.com//api/file/v1/<tenant code>/package.pkg/part?chunk=0&chunks=12
        """
        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)
Exemplo n.º 5
0
class SyncOperation:
    def __init__(
        self,
        source: str,
        destination: str,
        delete: bool = False,
        username: Optional[str] = None,
        password: Optional[str] = None,
        logger=None,
    ):
        if logger is not None:
            self.logger = logger
        else:
            self.logger = logging.getLogger("SyncOperation")

        source_url = urlparse(source)
        if source_url.scheme == 'https' or source_url.scheme == 'http':
            # Assume JAMF Cloud if given a URL
            self.jss = jss.JSS(
                url=source,
                user=username,
                password=password,
                ssl_verify=False,
            )
            self.source = JCDS(jss=self.jss)
        elif source_url.scheme == '' or source_url.scheme == 'file':
            # Assume source is a local directory
            self.source = source

        dest_url = urlparse(destination)
        if dest_url.scheme == 'https' or dest_url.scheme == 'http':
            # Assume JAMF Cloud if given a URL
            self.jss = jss.JSS(
                url=destination,
                user=username,
                password=password,
                ssl_verify=False,
            )
            self.source = JCDS(jss=self.jss)
        elif dest_url.scheme == '' or dest_url.scheme == 'file':
            # Assume dest is a local directory
            self.destination = destination

    def scan(self):  # type: () -> CopyOperations
        """Scan for a list of files to copy by pruning from the source list files which have the same
        name and checksum.

        The result is a list of tuples which is aliased as `CopyOperations`."""
        operations = list()

        src_pkgs = self.source.package_index_using_casper()

        for p in src_pkgs:
            destination_filename = os.path.join(self.destination,
                                                p['filename'])

            if needs_sync(p, destination_filename):
                operations.append((
                    p['fileURL'],
                    destination_filename,
                ))

        return operations

    def run(self, operations):  # type: (CopyOperations) -> None
        """Execute download/upload operations."""
        for (source, dest) in operations:
            self.logger.debug("Fetching from source: %s", source)
            response = requests.get(source)
            self.logger.debug("Writing to destination filename: %s", dest)
            with open(dest, 'wb') as fd:
                fd.write(response.content)