1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15- import base64
16- import gzip
1715import hashlib
1816import os
1917import requests
@@ -144,88 +142,6 @@ def _write_image(image_info, device):
144142 return uuids
145143
146144
147- def _configdrive_is_url (configdrive ):
148- """Determine if the configdrive location looks like an HTTP(S) URL.
149-
150- :param configdrive: Location of the configdrive as a string.
151- :returns: True if configdrive looks like an HTTP(S) URL, False otherwise.
152- """
153- return (configdrive .startswith ('http://' )
154- or configdrive .startswith ('https://' ))
155-
156-
157- def _download_configdrive_to_file (configdrive , filename ):
158- """Download the configdrive to a local file.
159-
160- :param configdrive: The URL of the configdrive.
161- :param filename: The filename of where to store the configdrive locally.
162- """
163- content = requests .get (configdrive ).content
164- _write_configdrive_to_file (content , filename )
165-
166-
167- def _write_configdrive_to_file (configdrive , filename ):
168- """Writes the configdrive to a file.
169-
170- Note that the contents of the configdrive are expected to be gzipped and
171- base64 encoded.
172-
173- :param configdrive: Contents of the configdrive file.
174- :param filename: The filename of where to write the configdrive.
175- """
176- LOG .debug ('Writing configdrive to {}' .format (filename ))
177- # configdrive data is base64'd, decode it first
178- data = six .StringIO (base64 .b64decode (configdrive ))
179- gunzipped = gzip .GzipFile ('configdrive' , 'rb' , 9 , data )
180- with open (filename , 'wb' ) as f :
181- f .write (gunzipped .read ())
182- gunzipped .close ()
183-
184-
185- def _write_configdrive_to_partition (configdrive , device ):
186- """Writes the configdrive to a partition on the given device.
187-
188- :param configdrive: A string containing the location of the config drive
189- as a URL OR the contents of the configdrive which
190- must be gzipped and base64 encoded.
191- :param device: The disk name, as a string, on which to store the image.
192- Example: '/dev/sda'
193-
194- :raises: ConfigDriveTooLargeError if the configdrive contents are too
195- large to store on the given device.
196- """
197- filename = _configdrive_location ()
198- if _configdrive_is_url (configdrive ):
199- _download_configdrive_to_file (configdrive , filename )
200- else :
201- _write_configdrive_to_file (configdrive , filename )
202-
203- # check configdrive size before writing it
204- filesize = os .stat (filename ).st_size
205- if filesize > (64 * 1024 * 1024 ):
206- raise errors .ConfigDriveTooLargeError (filename , filesize )
207-
208- starttime = time .time ()
209- script = _path_to_script ('shell/copy_configdrive_to_disk.sh' )
210- command = ['/bin/bash' , script , filename , device ]
211- LOG .info ('copying configdrive to disk with command {}' .format (
212- ' ' .join (command )))
213-
214- try :
215- stdout , stderr = utils .execute (* command , check_exit_code = [0 ])
216- except processutils .ProcessExecutionError as e :
217- raise errors .ConfigDriveWriteError (device ,
218- e .exit_code ,
219- e .stdout ,
220- e .stderr )
221-
222- totaltime = time .time () - starttime
223- LOG .info ('configdrive copied from {} to {} in {} seconds' .format (
224- filename ,
225- device ,
226- totaltime ))
227-
228-
229145def _message_format (msg , image_info , device , partition_uuids ):
230146 """Helper method to get and populate different messages."""
231147 message = None
@@ -527,7 +443,7 @@ def prepare_image(self,
527443 :raises: ImageChecksumError if the checksum of the local image does not
528444 match the checksum as reported by glance in image_info.
529445 :raises: ImageWriteError if writing the image fails.
530- :raises: ConfigDriveTooLargeError if the configdrive contents are too
446+ :raises: InstanceDeployFailure if failed to create config drive.
531447 large to store on the given device.
532448 """
533449 LOG .debug ('Preparing image %s' , image_info ['id' ])
@@ -551,8 +467,18 @@ def prepare_image(self,
551467 # work_on_disk().
552468 if image_info .get ('image_type' ) != 'partition' :
553469 if configdrive is not None :
554- _write_configdrive_to_partition (configdrive , device )
555-
470+ # Will use dummy value of 'local' for 'node_uuid',
471+ # if it is not available. This is to handle scenario
472+ # wherein new IPA is being used with older version
473+ # of Ironic that did not pass 'node_uuid' in 'image_info'
474+ node_uuid = image_info .get ('node_uuid' , 'local' )
475+ starttime = time .time ()
476+ disk_utils .create_config_drive_partition (node_uuid ,
477+ device ,
478+ configdrive )
479+ totaltime = time .time () - starttime
480+ LOG .info ('configdrive copied to {0} in {1} '
481+ 'seconds.' .format (device , totaltime ))
556482 msg = 'image ({}) written to device {} '
557483 result_msg = _message_format (msg , image_info , device ,
558484 self .partition_uuids )
0 commit comments