Ejemplo n.º 1
0
    def do_storage(self, args, arguments):
        """
        ::

          Usage:
                storage [--storage=SERVICE] create dir DIRECTORY
                storage [--storage=SERVICE] get SOURCE DESTINATION [--recursive]
                storage [--storage=SERVICE] put SOURCE DESTINATION [--recursive]
                storage [--storage=SERVICE] list SOURCE [--recursive]
                storage [--storage=SERVICE] delete SOURCE
                storage [--storage=SERVICE] search  DIRECTORY FILENAME [--recursive]


          This command does some useful things.

          Arguments:
              SOURCE        SOURCE can be a directory or file
              DESTINATION   DESTINATION can be a directory or file
              DIRECTORY     DIRECTORY refers to a folder on the cloud service


          Options:
              --storage=SERVICE  specify the cloud service name like aws or azure or box or google
          Description:
                commands used to upload, download, list files on different cloud storage services.

                storage put [options..]
                    Uploads the file specified in the filename to specified cloud from the SOURCEDIR.

                storage get [options..]
                    Downloads the file specified in the filename from the specified cloud to the DESTDIR.

                storage delete [options..]
                    Deletes the file specified in the filename from the specified cloud.

                storage list [options..]
                    lists all the files from the container name specified on the specified cloud.

                storage create dir [options..]
                    creates a folder with the directory name specified on the specified cloud.

                storage search [options..]
                    searches for the source in all the folders on the specified cloud.

          Example:
            set storage=azureblob
            storage put SOURCE DESTINATION --recursive

            is the same as
            storage --storage=azureblob put SOURCE DESTINATION --recursive

        """
        # arguments.CONTAINER = arguments["--container"]

        map_parameters(arguments,
                       "recursive",
                       "storage")
        arguments.storage = arguments["--storage"]
        pprint(arguments)

        m = Provider()

        service = None

        #
        # BUG
        # services = Parameter.expand(arguments.storage)
        # service = services[0]
        # if services is None:
        #  ... do second try

        ##### BUG
        try:
            service = arguments["--storage"][0]
        except Exception as e:
            try:
                v = Variables()
                service = v['storage']
            except Exception as e:
                service = None

        if service is None:
            Console.error("storage service not defined")
            return

        # bug this is now done twice ....
        if arguments.storage is None:
            variables = Variables()
            arguments.storage = variables['storage']

        ##### Prvious code needs to be modified

        if arguments.get:
            m.get(arguments.storage, arguments.SOURCE, arguments.DESTINATION,
                  arguments.recursive)

        elif arguments.put:
            m.put(arguments.storage, arguments.SOURCE, arguments.DESTINATION,
                  arguments.recursive)

        elif arguments.list:
            print('in List')
            m.list(arguments.storage, arguments.SOURCE, arguments.recursive)

        elif arguments.create and arguments.dir.:
            m.createdir(arguments.storage, arguments.DIRECTORY)

        elif arguments.delete.:
            m.delete(arguments.storage, arguments.SOURCE)

        elif arguments['search']:
            m.search(arguments.storage, arguments.DIRECTORY, arguments.FILENAME,
                     arguments.recursive)
Ejemplo n.º 2
0
class TestStorageBox(object):
    def create_file(self, location, content):
        Shell.mkdir(os.dirname(path_expand(location)))
        writefile(location, content)

    def setup(self):
        variables = Variables()
        self.service = Parameter.expand(variables['storage'])[0]
        self.p = Provider(service=self.service)
        self.sourcedir = path_expand("~/.cloudmesh/storage/test")
        print()

    def test_create_source(self):
        HEADING()
        home = self.sourcedir
        # Setup a dir
        self.content = []
        self.files = [
            "a/a1.txt", "a/a2.txt", "a/a3.txt", "a/b/b1.txt", "a/b/b2.txt",
            "a/b/b3.txt", "a/b/c/c1.txt", "a/b/c/c2.txt", "a/b/c/c3.txt",
            "a/b/d/d1.txt", "a/b/d/d2.txt", "a/b/d/d3.txt", "a/b/d/a1.txt"
        ]

        for f in self.files:
            location = f"{home}/{f}"
            self.create_file(location, f"content of {f}")
            self.content.append(location)

        # setup empty dir in a
        d1 = Path(path_expand(f"{home}/a/empty"))
        d1.mkdir(parents=True, exist_ok=True)

        for f in self.files:
            assert os.path.isfile(f"{home}/{f}")

        assert os.path.isdir(f"{home}/a/empty")

    def test_put_and_get(self):
        HEADING()
        home = self.sourcedir
        StopWatch.start("PUT file")
        test_file = self.p.put(self.p.service, f"{home}/a/a1.txt", "/")
        StopWatch.stop("PUT file")
        assert test_file is not None

        StopWatch.start("GET file")
        test_file = self.p.get(self.p.service, f"/a1.txt", f"{home}/hello.txt")
        StopWatch.stop("GET file")
        assert test_file is not None

        content = readfile(f"{home}/hello.txt")
        assert "a1.txt" in content

    def test_list(self):
        HEADING()
        StopWatch.start("LIST Directory")
        contents = self.p.list(self.p.service, "/")
        StopWatch.stop("LIST Directory")
        for c in contents:
            pprint(c)

        assert len(contents) > 0
        found = False
        for entry in contents:
            if entry["cm"]["name"] == "a1.txt":
                found = True
        assert found

    def test_create_dir(self):
        HEADING()
        src = '/a/created_dir'
        StopWatch.start("CREATE DIR")
        directory = self.p.createdir(self.p.service, src)
        StopWatch.stop("CREATE DIR")
        pprint(directory)

        assert dir is not None
        assert "a/created_dir" in directory[0]["name"]

    def test_search(self):
        HEADING()
        src = '/'
        filename = "a1.txt"
        StopWatch.start("SEARCH file")
        search_files = self.p.search(self.p.service, src, filename, True)
        StopWatch.stop("SEARCH file")
        pprint(search_files)
        assert len(search_files) > 0
        assert search_files[0]["name"] == filename

    def test_delete(self):
        HEADING()
        src = "/a/created_dir"
        StopWatch.start("DELETE Directory")
        contents = self.p.delete(self.p.service, src)
        StopWatch.stop("DELETE Directory")
        deleted = False
        for entry in contents:
            if "created_dir" in entry["cm"]["name"]:
                if entry["cm"]["status"] == "deleted":
                    deleted = True
        assert deleted

    def test_recursive_put(self):
        # must be implemented by student from ~/.cloudmesh/storage/test
        # make sure all files are in the list see self.content which contains
        # all files
        home = self.sourcedir
        StopWatch.start("PUT Directory --recursive")
        upl_files = self.p.put(self.p.service, f"{home}", "/a", True)
        StopWatch.stop("PUT Directory --recursive")
        pprint(upl_files)

        assert upl_files is not None

    def test_recursive_get(self):
        # must be implemented by student into ~/.cloudmesh/storage/test/get
        # see self.content which contains all files but you must add get/
        home = self.sourcedir
        d2 = Path(path_expand(f"{home}/get"))
        d2.mkdir(parents=True, exist_ok=True)
        StopWatch.start("GET Directory --recursive")
        dnld_files = self.p.get(self.p.service, "/a", f"{home}/get", True)
        StopWatch.stop("GET Directory --recursive")
        pprint(dnld_files)

        assert dnld_files is not None

    def test_recursive_delete(self):
        # must be implemented by student into ~/.cloudmesh/storage/test/get
        # see self.content which contains all files but you must add get/
        src = "/a/a/b/c"
        StopWatch.start("DELETE Sub-directory")
        del_files = self.p.delete(self.p.service, src)
        StopWatch.stop("DELETE Sub-directory")

        assert len(del_files) > 0

    def test_exhaustive_list(self):
        # must be implemented by student into ~/.cloudmesh/storage/test/
        # see self.content which contains all files that you can test against
        # in the list return. all of them must be in there
        StopWatch.start("LIST Directory --recursive")
        contents = self.p.list(self.p.service, "/a", True)
        StopWatch.stop("LIST Directory --recursive")

        assert len(contents) > 0

    def test_selective_list(self):
        # must be implemented by student into ~/.cloudmesh/storage/test/a/b
        # see self.content which contains all files that you can test against
        # in the list return. all of them must be in there but not more?
        # I am unsure if we implemented a secive list. If not let us know
        # full list for now is fine
        StopWatch.start("LIST Sub-directory --recursive")
        contents = self.p.list(self.p.service, "/a/a/b", True)
        StopWatch.stop("LIST Sub-directory --recursive")

        assert len(contents) > 0

    def test_search_b1(self):
        # search for b1.txt
        src = '/a'
        filename = 'b1.txt'
        StopWatch.start("SEARCH file --recursive")
        search_files = self.p.search(self.p.service, src, filename, True)
        StopWatch.stop("SEARCH file --recursive")

        assert search_files is not None

    def test_search_b1_dir(self):
        # search for b/b2.txt see that this one has even the dir in the search
        src = '/a'
        filename = '/b/b1.txt'
        StopWatch.start("SEARCH file under a sub-dir --r")
        search_files = self.p.search(self.p.service, src, filename, True)
        StopWatch.stop("SEARCH file under a sub-dir --r")
        assert search_files is not None

    def test_search_a1(self):
        # search for a1.txt which shold return 2 entries
        src = '/a'
        filename = 'a1.txt'
        StopWatch.start("SEARCH file under root dir --r")
        search_files = self.p.search(self.p.service, src, filename, True)
        StopWatch.stop("SEARCH file under root dir --r")

        assert len(search_files) == 2

    def test_results(self):
        HEADING()
        # storage = self.p.service
        service = self.service
        banner(f"Benchmark results for {service} Storage")
        StopWatch.benchmark()
Ejemplo n.º 3
0
    def do_storage(self, args, arguments):
        """
        ::

          Usage:
                storage [--storage=SERVICE] create dir DIRECTORY
                storage [--storage=SERVICE] get SOURCE DESTINATION [--recursive]
                storage [--storage=SERVICE] put SOURCE DESTINATION [--recursive]
                storage [--storage=SERVICE] list SOURCE [--recursive] [--output=OUTPUT]
                storage [--storage=SERVICE] delete SOURCE
                storage [--storage=SERVICE] search  DIRECTORY FILENAME [--recursive] [--output=OUTPUT]
                storage [--storage=SERVICE] sync SOURCE DESTINATION [--name=NAME] [--async]
                storage [--storage=SERVICE] sync status [--name=NAME]
                storage config list [--output=OUTPUT]

          This command does some useful things.

          Arguments:
              SOURCE        SOURCE can be a directory or file
              DESTINATION   DESTINATION can be a directory or file
              DIRECTORY     DIRECTORY refers to a folder on the cloud service


          Options:
              --storage=SERVICE  specify the cloud service name like aws or
                                 azure or box or google

          Description:
                commands used to upload, download, list files on different
                cloud storage services.

                storage put [options..]
                    Uploads the file specified in the filename to specified
                    cloud from the SOURCEDIR.

                storage get [options..]
                    Downloads the file specified in the filename from the
                    specified cloud to the DESTDIR.

                storage delete [options..]
                    Deletes the file specified in the filename from the
                    specified cloud.

                storage list [options..]
                    lists all the files from the container name specified on
                    the specified cloud.

                storage create dir [options..]
                    creates a folder with the directory name specified on the
                    specified cloud.

                storage search [options..]
                    searches for the source in all the folders on the specified
                    cloud.

                sync SOURCE DESTINATION
                    puts the content of source to the destination.
                    If --recursive is specified this is done recursively from
                       the source
                    If --async is specified, this is done asynchronously
                    If a name is specified, the process can also be monitored
                       with the status command by name.
                    If the name is not specified all date is monitored.

                sync status
                    The status for the asynchronous sync can be seen with this
                    command

                config list
                    Lists the configures storage services in the yaml file

          Example:
            set storage=azureblob
            storage put SOURCE DESTINATION --recursive

            is the same as
            storage --storage=azureblob put SOURCE DESTINATION --recursive

        """
        # arguments.CONTAINER = arguments["--container"]

        map_parameters(arguments, "recursive", "storage")
        VERBOSE(arguments)

        if arguments.storage is None:
            try:
                v = Variables()
                arguments.storage = v['storage']
            except Exception as e:
                arguments.storage = None
                raise ValueError("Storage provider is not defined")

        arguments.storage = Parameter.expand(arguments.storage)

        #
        # BUG: some commands could be run on more than the first provider,
        # such as list
        # thus the if condition needs to be reorganized

        if arguments["get"]:
            provider = Provider(arguments.storage[0])

            result = provider.get(arguments.storage, arguments.SOURCE,
                                  arguments.DESTINATION, arguments.recursive)

        elif arguments.put:
            provider = Provider(arguments.storage[0])

            result = provider.put(arguments.storage, arguments.SOURCE,
                                  arguments.DESTINATION, arguments.recursive)

        elif arguments.create and arguments.dir:
            provider = Provider(arguments.storage[0])

            result = provider.createdir(arguments.storage, arguments.DIRECTORY)

        elif arguments.list:

            #
            # BUG: this command is much more complicated
            #

            for storage in arguments.storage:
                provider = Provider(storage)

                result = provider.list(arguments.storage, arguments.SOURCE,
                                       arguments.recursive)

        elif arguments.delete:

            #
            # BUG:: this command could be much more complicated
            #
            for storage in arguments.storage:
                provider = Provider(storage)

                provider.delete(arguments.storage, arguments.SOURCE)

        elif arguments.search:
            #
            # BUG: this command is much more complicated
            #

            for storage in arguments.storage:
                provider = Provider(storage)

                provider.search(arguments.storage, arguments.DIRECTORY,
                                arguments.FILENAME, arguments.recursive)

        elif arguments.rsync:
            # TODO: implement
            raise NotImplementedError