Exemplo n.º 1
0
def read_lv_sizes(lv_sizes_patch_json):
    """ Read user defined values for LV sizes, validate them and store in a json file """
    if not get_config_value('UPDATE_LV_SIZES'):
        # LV sizes are not overridden
        return

    modifiable_size_lvs = {'appdata', 'config', 'log', 'shared', 'var'}
    lv_sizes_dict = get_dict_from_config_json('UPDATE_LV_SIZES')
    filtered_dict = {}
    for lv_name in lv_sizes_dict:
        lv_size = lv_sizes_dict[lv_name]
        lv_name = lv_name.lower()
        if not isinstance(lv_size, int):
            raise RuntimeError(
                'LV size for \'{}\' must be an integer, '.format(lv_name) +
                'denoting the size in MiBs (without quotation marks).')
        if lv_name not in modifiable_size_lvs:
            raise RuntimeError(
                '\'{}\' is not a member '.format(lv_name) +
                'of modifiable size LVs: {}. '.format(modifiable_size_lvs) +
                '\'{}\' is not an LV or '.format(lv_name) +
                'its size cannot be changed!')
        filtered_dict[lv_name] = lv_size

    with open(lv_sizes_patch_json, 'w') as patch_file:
        json.dump(filtered_dict, patch_file, indent=2)
    def init_bucket(self):
        """
        Populate the bucket object based on GCE credential and GCE_BUCKET.
        """
        try:
            # start storage client
            creds_dict = get_dict_from_config_json(
                'GOOGLE_APPLICATION_CREDENTIALS')
            credentials = service_account.Credentials.from_service_account_info(
                creds_dict)
            project = ensure_value_from_dict(creds_dict, "project_id")
        except ValueError as value_exc:
            LOGGER.exception(value_exc)
            raise RuntimeError(
                "Failed to initialize GOOGLE_APPLICATION_CREDENTIALS credentials."
            )

        try:
            storage_client = storage.Client(credentials=credentials,
                                            project=project)
        except google.auth.exceptions.DefaultCredentialsError as exception:
            LOGGER.exception(exception)
            raise RuntimeError(
                "storage.Client failed with DefaultCredentialsError.")

        # ensure bucket exists
        bucket_name = get_config_value('GCE_BUCKET')
        if not bucket_name:
            raise RuntimeError("GCE_BUCKET is missing.")

        try:
            self.bucket = storage_client.lookup_bucket(bucket_name)
        except google.api_core.exceptions.BadRequest as exception:
            LOGGER.exception(exception)
            raise RuntimeError(
                "storage_client.lookup_bucket failed with BadRequest.")

        if self.bucket is None:
            LOGGER.info('Creating bucket [%s]', bucket_name)
            try:
                self.bucket = storage_client.create_bucket(bucket_name)
            except GoogleAPIError as exception:
                LOGGER.exception(exception)
                raise RuntimeError("storage_client.create_bucket failed.")

        LOGGER.debug("init_bucket completed successfully.")
Exemplo n.º 3
0
    def __init__(self, working_dir, input_disk_path):
        super().__init__(working_dir, input_disk_path)
        self.disk = GoogleDisk(input_disk_path)

        # Retrieve credentials dictionary
        creds_dict = get_dict_from_config_json(
            "GOOGLE_APPLICATION_CREDENTIALS")

        # Obtain project ID from the credentials dictionary
        self.gce_project_id = ensure_value_from_dict(creds_dict, "project_id")
        LOGGER.info("Using project_id: '%s'", self.gce_project_id)

        # Record project ID in metadata
        self.metadata = CloudImageMetadata()
        self.metadata.set(self.__class__.__name__, 'gce_project',
                          self.gce_project_id)

        # Create a service object from the credentials dictionary
        self.gce_credentials = service_account.Credentials.from_service_account_info(
            creds_dict)
        self.gce_service = discovery.build('compute',
                                           'v1',
                                           credentials=self.gce_credentials)