1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
| @classmethod def create(cls, context, name, flavor_id, image_id, databases, users, datastore, datastore_version, volume_size, backup_id, availability_zone=None, nics=None, configuration_id=None, slave_of_id=None, cluster_config=None, replica_count=None, volume_type=None, modules=None, locality=None, region_name=None):
region_name = region_name or CONF.os_region_name
call_args = { 'name': name, 'flavor_id': flavor_id, 'datastore': datastore.name if datastore else None, 'datastore_version': datastore_version.name, 'image_id': image_id, 'availability_zone': availability_zone, 'region_name': region_name, }
bound_flavors = DBDatastoreVersionMetadata.find_all( datastore_version_id=datastore_version.id, key='flavor', deleted=False ) if bound_flavors.count() > 0: valid_flavors = tuple(f.value for f in bound_flavors) if flavor_id not in valid_flavors: raise exception.DatastoreFlavorAssociationNotFound( datastore=datastore.name, datastore_version=datastore_version.name, flavor_id=flavor_id)
datastore_cfg = CONF.get(datastore_version.manager)
client = create_nova_client(context) try: flavor = client.flavors.get(flavor_id) except nova_exceptions.NotFound: raise exception.FlavorNotFound(uuid=flavor_id)
if region_name and region_name != CONF.os_region_name: cls._validate_remote_datastore(context, region_name, flavor, datastore, datastore_version)
deltas = {'instances': 1} volume_support = datastore_cfg.volume_support if volume_support: call_args['volume_type'] = volume_type dvm.validate_volume_type(context, volume_type, datastore.name, datastore_version.name) call_args['volume_size'] = volume_size validate_volume_size(volume_size) deltas['volumes'] = volume_size target_size = volume_size else: target_size = flavor.disk if volume_size is not None: raise exception.VolumeNotSupported() if datastore_cfg.device_path: if flavor.ephemeral == 0: raise exception.LocalStorageNotSpecified(flavor=flavor_id) target_size = flavor.ephemeral
if backup_id: call_args['backup_id'] = backup_id backup_info = Backup.get_by_id(context, backup_id) if not backup_info.is_done_successfuly: raise exception.BackupNotCompleteError( backup_id=backup_id, state=backup_info.state)
if backup_info.size > target_size: raise exception.BackupTooLarge( backup_size=backup_info.size, disk_size=target_size)
if not backup_info.check_swift_object_exist( context, verify_checksum=CONF.verify_swift_checksum_on_restore): raise exception.BackupFileNotFound( location=backup_info.location)
if (backup_info.datastore_version_id and backup_info.datastore.name != datastore.name): raise exception.BackupDatastoreMismatchError( datastore1=backup_info.datastore.name, datastore2=datastore.name)
if slave_of_id: call_args['replica_of'] = slave_of_id call_args['replica_count'] = replica_count replication_support = datastore_cfg.replication_strategy if not replication_support: raise exception.ReplicationNotSupported( datastore=datastore.name) try: replica_source = DBInstance.find_by( context, id=slave_of_id, deleted=False) if replica_source.slave_of_id: raise exception.Forbidden( _("Cannot create a replica of a replica %(id)s.") % {'id': slave_of_id}) load_simple_instance_server_status( context, replica_source) replica_source_instance = Instance( context, replica_source, None, InstanceServiceStatus.find_by( context, instance_id=slave_of_id)) replica_source_instance.validate_can_perform_action() except exception.ModelNotFoundError: LOG.exception( _("Cannot create a replica of %(id)s " "as that instance could not be found."), {'id': slave_of_id}) raise exception.NotFound(uuid=slave_of_id) elif replica_count and replica_count != 1: raise exception.Forbidden(_( "Replica count only valid when creating replicas. Cannot " "create %(count)d instances.") % {'count': replica_count}) multi_replica = slave_of_id and replica_count and replica_count > 1 instance_count = replica_count if multi_replica else 1 if locality: call_args['locality'] = locality
if not nics: nics = [] if CONF.default_neutron_networks: nics = [{"net-id": net_id} for net_id in CONF.default_neutron_networks] + nics if nics: call_args['nics'] = nics if cluster_config: call_args['cluster_id'] = cluster_config.get("id", None)
if not modules: modules = [] module_ids = [mod['id'] for mod in modules] modules = module_models.Modules.load_by_ids(context, module_ids) auto_apply_modules = module_models.Modules.load_auto_apply( context, datastore.id, datastore_version.id) for aa_module in auto_apply_modules: if aa_module.id not in module_ids: modules.append(aa_module) module_models.Modules.validate( modules, datastore.id, datastore_version.id) module_list = module_views.convert_modules_to_list(modules)
def _create_resources():
if cluster_config: cluster_id = cluster_config.get("id", None) shard_id = cluster_config.get("shard_id", None) instance_type = cluster_config.get("instance_type", None) else: cluster_id = shard_id = instance_type = None
ids = [] names = [] root_passwords = [] root_password = None for instance_index in range(0, instance_count): db_info = DBInstance.create( name=name, flavor_id=flavor_id, tenant_id=context.tenant, volume_size=volume_size, datastore_version_id=datastore_version.id, task_status=InstanceTasks.BUILDING, configuration_id=configuration_id, slave_of_id=slave_of_id, cluster_id=cluster_id, shard_id=shard_id, type=instance_type, region_id=region_name) LOG.debug("Tenant %(tenant)s created new Trove instance " "%(db)s in region %(region)s.", {'tenant': context.tenant, 'db': db_info.id, 'region': region_name})
instance_id = db_info.id cls.add_instance_modules(context, instance_id, modules) instance_name = name ids.append(instance_id) names.append(instance_name) root_passwords.append(None) if multi_replica: replica_number = instance_index + 1 names[instance_index] += '-' + str(replica_number) setattr(db_info, 'name', names[instance_index]) db_info.save()
config = Configuration(context, configuration_id) overrides = config.get_configuration_overrides() service_status = InstanceServiceStatus.create( instance_id=instance_id, status=tr_instance.ServiceStatuses.NEW)
if CONF.trove_dns_support: dns_client = create_dns_client(context) hostname = dns_client.determine_hostname(instance_id) db_info.hostname = hostname db_info.save() if cls.get_root_on_create( datastore_version.manager) and not backup_id: root_password = utils.generate_random_password() root_passwords[instance_index] = root_password
if instance_count > 1: instance_id = ids instance_name = names root_password = root_passwords
task_api.API(context).create_instance( instance_id, instance_name, flavor, image_id, databases, users, datastore_version.manager, datastore_version.packages, volume_size, backup_id, availability_zone, root_password, nics, overrides, slave_of_id, cluster_config, volume_type=volume_type, modules=module_list, locality=locality)
return SimpleInstance(context, db_info, service_status, root_password, locality=locality)
with StartNotification(context, **call_args): return run_with_quotas(context.tenant, deltas, _create_resources)
|