Coverage for nova/virt/zvm/utils.py: 0%

55 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-04-24 11:16 +0000

1# Copyright 2017,2018 IBM Corp. 

2# 

3# Licensed under the Apache License, Version 2.0 (the "License"); you may 

4# not use this file except in compliance with the License. You may obtain 

5# a copy of the License at 

6# 

7# http://www.apache.org/licenses/LICENSE-2.0 

8# 

9# Unless required by applicable law or agreed to in writing, software 

10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 

11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 

12# License for the specific language governing permissions and limitations 

13# under the License. 

14 

15import os 

16from urllib import parse as urlparse 

17 

18from oslo_log import log as logging 

19from zvmconnector import connector 

20 

21from oslo_utils import fileutils 

22 

23from nova.api.metadata import base as instance_metadata 

24from nova import conf 

25from nova import exception 

26from nova.virt import configdrive 

27 

28 

29CONF = conf.CONF 

30LOG = logging.getLogger(__name__) 

31 

32 

33class ConnectorClient(object): 

34 """Request handler to zVM cloud connector""" 

35 

36 def __init__(self, zcc_url, ca_file=None): 

37 _url = urlparse.urlparse(zcc_url) 

38 

39 _ssl_enabled = False 

40 

41 if _url.scheme == 'https': 

42 _ssl_enabled = True 

43 elif ca_file: 

44 LOG.warning("url is %(url)s which is not https " 

45 "but ca_file is configured to %(ca_file)s", 

46 {'url': zcc_url, 'ca_file': ca_file}) 

47 

48 if _ssl_enabled and ca_file: 

49 self._conn = connector.ZVMConnector(_url.hostname, _url.port, 

50 ssl_enabled=_ssl_enabled, 

51 verify=ca_file) 

52 else: 

53 self._conn = connector.ZVMConnector(_url.hostname, _url.port, 

54 ssl_enabled=_ssl_enabled, 

55 verify=False) 

56 

57 def call(self, func_name, *args, **kwargs): 

58 results = self._conn.send_request(func_name, *args, **kwargs) 

59 

60 if results['overallRC'] != 0: 

61 LOG.error("zVM Cloud Connector request %(api)s failed with " 

62 "parameters: %(args)s %(kwargs)s . Results: %(results)s", 

63 {'api': func_name, 'args': str(args), 

64 'kwargs': str(kwargs), 

65 'results': str(results)}) 

66 raise exception.ZVMConnectorError(results=results) 

67 

68 return results['output'] 

69 

70 

71def _get_instance_path(instance_uuid): 

72 instance_folder = os.path.join(os.path.normpath(CONF.instances_path), 

73 instance_uuid) 

74 fileutils.ensure_tree(instance_folder) 

75 return instance_folder 

76 

77 

78def _create_config_drive(context, instance_path, instance, 

79 injected_files, network_info, admin_password): 

80 if CONF.config_drive_format != 'iso9660': 

81 raise exception.ConfigDriveUnsupportedFormat( 

82 format=CONF.config_drive_format) 

83 

84 LOG.debug('Using config drive', instance=instance) 

85 

86 extra_md = {} 

87 if admin_password: 

88 extra_md['admin_pass'] = admin_password 

89 

90 inst_md = instance_metadata.InstanceMetadata(instance, 

91 content=injected_files, 

92 extra_md=extra_md, 

93 network_info=network_info) 

94 

95 configdrive_iso = os.path.join(instance_path, 'cfgdrive.iso') 

96 LOG.debug('Creating config drive at %s', configdrive_iso, 

97 instance=instance) 

98 with configdrive.ConfigDriveBuilder(instance_md=inst_md) as cdb: 

99 cdb.make_drive(configdrive_iso) 

100 

101 return configdrive_iso 

102 

103 

104# Prepare and create configdrive for instance 

105def generate_configdrive(context, instance, injected_files, 

106 network_info, admin_password): 

107 # Create network configuration files 

108 LOG.debug('Creating config drive configuration files ' 

109 'for instance: %s', instance.name, instance=instance) 

110 

111 instance_path = _get_instance_path(instance.uuid) 

112 

113 transportfiles = None 

114 if configdrive.required_by(instance): 

115 transportfiles = _create_config_drive(context, instance_path, 

116 instance, 

117 injected_files, 

118 network_info, 

119 admin_password) 

120 return transportfiles 

121 

122 

123def clean_up_file(filepath): 

124 if os.path.exists(filepath): 

125 os.remove(filepath)