1 # Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs")
2 # and others. All rights reserved.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
17 class ImageConfig(object):
18 def __init__(self, **kwargs):
21 :param name: the image's name (required)
22 :param image_user: the image's default sudo user (required)
23 :param format or img_format: the image format type (required)
24 :param url or download_url: the image download location (requires url
26 :param image_file: the image file location (requires url or img_file)
27 :param extra_properties: dict() object containing extra parameters to
28 pass when loading the image;
29 can be ids of kernel and initramfs images for
31 :param nic_config_pb_loc: the file location to the Ansible Playbook
32 that can configure multiple NICs
33 :param kernel_image_settings: the settings for a kernel image
34 :param ramdisk_image_settings: the settings for a ramdisk image
35 :param exists: When True, an image with the given name must exist
36 :param public: When True, an image will be created with public
40 self.name = kwargs.get('name')
41 self.image_user = kwargs.get('image_user')
42 self.format = kwargs.get('format')
44 self.format = kwargs.get('img_format')
46 self.url = kwargs.get('url')
48 self.url = kwargs.get('download_url')
49 if self.url == 'None':
52 self.image_file = kwargs.get('image_file')
53 if self.image_file == 'None':
54 self.image_file = None
56 self.extra_properties = kwargs.get('extra_properties')
57 self.nic_config_pb_loc = kwargs.get('nic_config_pb_loc')
59 kernel_image_settings = kwargs.get('kernel_image_settings')
60 if kernel_image_settings:
61 if isinstance(kernel_image_settings, dict):
62 self.kernel_image_settings = ImageConfig(
63 **kernel_image_settings)
65 self.kernel_image_settings = kernel_image_settings
67 self.kernel_image_settings = None
69 ramdisk_image_settings = kwargs.get('ramdisk_image_settings')
70 if ramdisk_image_settings:
71 if isinstance(ramdisk_image_settings, dict):
72 self.ramdisk_image_settings = ImageConfig(
73 **ramdisk_image_settings)
75 self.ramdisk_image_settings = ramdisk_image_settings
77 self.ramdisk_image_settings = None
79 if 'exists' in kwargs and kwargs['exists'] is True:
84 if 'public' in kwargs and kwargs['public'] is True:
90 raise ImageConfigError("The attribute name is required")
92 if not (self.url or self.image_file) and not self.exists:
93 raise ImageConfigError(
94 'URL or image file must be set or image must already exist')
96 if not self.image_user:
97 raise ImageConfigError('Image user is required')
99 if not self.format and not self.exists:
100 raise ImageConfigError(
101 'Format is required when the image should not already exist')
104 class ImageConfigError(Exception):
106 Exception to be thrown when an image settings are incorrect
109 def __init__(self, message):
110 Exception.__init__(self, message)