Esempio n. 1
0
def download_snapshot_with_progress_bar(url, directory=os.path.curdir):
    """downloads file from specific 'url' with progress bar and save it
    to some 'directory'.
    """
    if not os.path.exists(directory):
        exit_with_error("Folder {0} doesn't exist.".format(directory))
    file_name = os.path.join(os.path.abspath(directory), url.split('/')[-1])
    download_handle = urllib2.urlopen(url)
    with open(file_name, 'wb') as file_handle:
        meta = download_handle.info()
        file_size = int(meta.getheaders("Content-Length")[0])
        print("Downloading: {0} Bytes: {1}".format(url, file_size))
        file_size_dl = 0
        block_size = 8192
        bar = partial(get_bar_for_progress, 80)
        while True:
            data_buffer = download_handle.read(block_size)
            if not data_buffer:
                break
            file_size_dl += len(data_buffer)
            file_handle.write(data_buffer)
            progress = int(100 * float(file_size_dl) / file_size)
            sys.stdout.write("\r{0}".format(bar(progress)))
            sys.stdout.flush()
            sleep(1 / 10)
        print()
Esempio n. 2
0
def download_snapshot_with_progress_bar(url, directory=os.path.curdir):
    """downloads file from specific 'url' with progress bar and save it
    to some 'directory'.
    """
    if not os.path.exists(directory):
        exit_with_error("Folder {0} doesn't exist.".format(directory))
    file_name = os.path.join(
        os.path.abspath(directory),
        url.split('/')[-1]
    )
    download_handle = urllib2.urlopen(url)
    with open(file_name, 'wb') as file_handle:
        meta = download_handle.info()
        file_size = int(meta.getheaders("Content-Length")[0])
        print("Downloading: {0} Bytes: {1}".format(url, file_size))
        file_size_dl = 0
        block_size = 8192
        bar = partial(get_bar_for_progress, 80)
        while True:
            data_buffer = download_handle.read(block_size)
            if not data_buffer:
                break
            file_size_dl += len(data_buffer)
            file_handle.write(data_buffer)
            progress = int(100 * float(file_size_dl) / file_size)
            sys.stdout.write("\r{0}".format(
                bar(progress)
            ))
            sys.stdout.flush()
            sleep(1 / 10)
        print()
Esempio n. 3
0
    def update_or_create(cls, metadata, force=False):
        """Try to update existent plugin or create new one.

        :param dict metadata: plugin information
        :param bool force: updates existent plugin even if
                           it is not updatable
        """
        # Try to update plugin
        plugin_for_update = cls.get_plugin_for_update(metadata)
        if plugin_for_update:
            url = cls.class_instance_path.format(id=plugin_for_update['id'])
            resp = cls.connection.put_request(url, metadata)
            return resp

        # If plugin is not updatable it means that we should
        # create new instance in Nailgun
        resp_raw = cls.connection.post_request_raw(
            cls.class_api_path, metadata)
        resp = resp_raw.json()

        if resp_raw.status_code == 409 and force:
            # Replace plugin information
            message = json.loads(resp['message'])
            url = cls.class_instance_path.format(id=message['id'])
            resp = cls.connection.put_request(url, metadata)
        elif resp_raw.status_code == 409:
            error.exit_with_error(
                "Nothing to do: %(title)s, version "
                "%(package_version)s, does not update "
                "installed plugin." % metadata)
        else:
            resp_raw.raise_for_status()

        return resp
Esempio n. 4
0
    def check(self, params):
        """To run some health checks:
                fuel --env 1 health --check smoke,sanity
        """
        env = Environment(params.env)

        if env.status not in self._allowed_statuses and not params.force:
            exit_with_error(
                "Environment is not ready to run health check "
                "because it is in {0} state. "
                "Health check is likely to fail because of "
                "this. Use --force flag to proceed anyway.". format(env.status)
            )

        if env.is_customized and not params.force:
            exit_with_error(
                "Environment deployment facts were updated. "
                "Health check is likely to fail because of "
                "that. Use --force flag to proceed anyway."
            )
        test_sets_to_check = params.check or set(
            ts["id"] for ts in env.get_testsets())
        env.run_test_sets(test_sets_to_check)
        tests_state = env.get_state_of_tests()
        self.serializer.print_to_output(
            tests_state,
            env,
            print_method=print_health_check
        )
Esempio n. 5
0
 def read_attribute(self, attributes_type, directory, serializer=None):
     attributes_directory = self.get_attributes_path(directory)
     if not os.path.exists(attributes_directory):
         exit_with_error(
             "Folder {0} doesn't contain node folder '{1}'".format(
                 directory, "node_{0}".format(self.id)))
     return (serializer or self.serializer).read_from_file(
         os.path.join(attributes_directory, attributes_type))
Esempio n. 6
0
 def read_attribute(self, attributes_type, directory):
     attributes_directory = self.get_attributes_path(directory)
     if not os.path.exists(attributes_directory):
         exit_with_error(
             "Folder {0} doesn't contain node folder '{1}'"
             .format(directory, "node_{0}".format(self.id))
         )
     return self.serializer.read_from_file(
         os.path.join(
             attributes_directory,
             attributes_type
         )
     )
Esempio n. 7
0
 def check(self, params):
     """To run some health checks:
             fuel --env 1 health --check smoke,sanity
     """
     env = Environment(params.env)
     if env.is_customized and not params.force:
         exit_with_error("Environment deployment facts were updated. "
                         "Health check is likely to fail because of "
                         "that. Use --force flag to proceed anyway.")
     test_sets_to_check = params.check or set(ts["id"]
                                              for ts in env.get_testsets())
     env.run_test_sets(test_sets_to_check)
     tests_state = env.get_state_of_tests()
     self.serializer.print_to_output(tests_state,
                                     env,
                                     print_method=print_health_check)