def launch(user, name_prefix, provider, machines, size, host, skip_deploy, count): ident = user.identity_set.get(provider_id=provider.id) instances = [] kwargs = {} if host: kwargs['ex_availability_zone'] = host machine_count = 0 for c in range(0, count): for machine in machines: machine_count += 1 gen_name = "%s v.%s" % (machine.application.name, machine.application_version.name) if name_prefix: name = "%s %s" % (name_prefix, machine_count) else: name = "%s %s" % (gen_name, machine_count) try: instance_id = launch_instance( user, ident.uuid, size.alias, machine.instance_source.identifier, name=name, deploy=(not skip_deploy), **kwargs) print "Successfully launched Machine %s : %s" \ % (machine.instance_source.identifier, instance_id) instances.append(instance_id) except Exception as exc: print "Error on launch of Machine %s : %s" \ % (machine.instance_source.identifier, exc) return instances
def post(self, request, provider_id, identity_id, format=None): """ Instance Class: Launches an instance based on the params Returns a single instance Parameters: machine_alias, size_alias, username TODO: Create a 'reverse' using the instance-id to pass the URL for the newly created instance I.e: url = "/provider/1/instance/1/i-12345678" """ data = request.DATA user = request.user #Check the data is valid missing_keys = valid_post_data(data) if missing_keys: return keys_not_found(missing_keys) #Pass these as args size_alias = data.pop('size_alias') machine_alias = data.pop('machine_alias') hypervisor_name = data.pop('hypervisor',None) try: core_instance = launch_instance( user, provider_id, identity_id, size_alias, machine_alias, ex_availability_zone=hypervisor_name, **data) except OverQuotaError, oqe: return over_quota(oqe)
def launch(user, name_prefix, provider, machines, size, host, skip_deploy, count): ident = user.identity_set.get(provider_id=provider.id) instances = [] kwargs = {} if host: kwargs["ex_availability_zone"] = host machine_count = 0 for c in range(0, count): for machine in machines: machine_count += 1 gen_name = "%s v.%s" % (machine.application.name, machine.application_version.name) if name_prefix: name = "%s %s" % (name_prefix, machine_count) else: name = "%s %s" % (gen_name, machine_count) try: instance_id = launch_instance( user, ident.uuid, size.alias, machine.instance_source.identifier, name=name, deploy=(not skip_deploy), **kwargs ) print "Successfully launched Machine %s : %s" % (machine.instance_source.identifier, instance_id) instances.append(instance_id) except Exception as exc: print "Error on launch of Machine %s : %s" % (machine.instance_source.identifier, exc) return instances
def post(self, request, provider_id, identity_id, format=None): """ Instance Class: Launches an instance based on the params Returns a single instance Parameters: machine_alias, size_alias, username TODO: Create a 'reverse' using the instance-id to pass the URL for the newly created instance I.e: url = "/provider/1/instance/1/i-12345678" """ data = request.DATA user = request.user #Check the data is valid missing_keys = valid_post_data(data) if missing_keys: return keys_not_found(missing_keys) #Pass these as args size_alias = data.pop('size_alias') machine_alias = data.pop('machine_alias') hypervisor_name = data.pop('hypervisor', None) try: core_instance = launch_instance( user, provider_id, identity_id, size_alias, machine_alias, ex_availability_zone=hypervisor_name, **data) except OverQuotaError, oqe: return over_quota(oqe)
def create(self, request): user = request.user data = request.data try: self.validate_input(user, data) except Exception as exc: return failure_response(status.HTTP_400_BAD_REQUEST, exc.message) # Create a mutable dict and start modifying. data = data.copy() name = data.get('name') identity_uuid = data.get('identity') source_alias = data.get('source_alias') size_alias = data.get('size_alias') allocation_source_id = data.get('allocation_source_id') boot_scripts = data.pop("scripts", []) deploy = data.get('deploy') extra = data.get('extra') try: identity = Identity.objects.get(uuid=identity_uuid) allocation_source = AllocationSource.objects.get( source_id=allocation_source_id) core_instance = launch_instance(user, identity_uuid, size_alias, source_alias, name, deploy, **extra) # Faking a 'partial update of nothing' to allow call to 'is_valid' serialized_instance = InstanceSerializer( core_instance, context={'request': self.request}, data={}, partial=True) if not serialized_instance.is_valid(): return Response(serialized_instance.errors, status=status.HTTP_400_BAD_REQUEST) instance = serialized_instance.save() if boot_scripts: _save_scripts_to_instance(instance, boot_scripts) instance.change_allocation_source(allocation_source) return Response(serialized_instance.data, status=status.HTTP_201_CREATED) except UnderThresholdError as ute: return under_threshold(ute) except (OverQuotaError, OverAllocationError) as oqe: return over_quota(oqe) except ProviderNotActive as pna: return inactive_provider(pna) except SizeNotAvailable as snae: return size_not_available(snae) except HypervisorCapacityError as hce: return over_capacity(hce) except SecurityGroupNotCreated: return connection_failure(identity) except (socket_error, ConnectionFailure): return connection_failure(identity) except LibcloudInvalidCredsError: return invalid_creds(identity) except Exception as exc: logger.exception("Encountered a generic exception. " "Returning 409-CONFLICT") return failure_response(status.HTTP_409_CONFLICT, str(exc.message))
def create(self, request): user = request.user data = request.data try: self.validate_input(user, data) except Exception as exc: return failure_response( status.HTTP_400_BAD_REQUEST, exc.message) # Create a mutable dict and start modifying. data = data.copy() name = data.get('name') identity_uuid = data.get('identity') source_alias = data.get('source_alias') size_alias = data.get('size_alias') boot_scripts = data.pop("scripts", []) deploy = data.get('deploy') extra = data.get('extra') try: identity = Identity.objects.get(uuid=identity_uuid) core_instance = launch_instance( user, identity_uuid, size_alias, source_alias, name, deploy, **extra) # Faking a 'partial update of nothing' to allow call to 'is_valid' serialized_instance = InstanceSerializer( core_instance, context={'request': self.request}, data={}, partial=True) if not serialized_instance.is_valid(): return Response(serialized_instance.errors, status=status.HTTP_400_BAD_REQUEST) instance = serialized_instance.save() if boot_scripts: _save_scripts_to_instance(instance, boot_scripts) return Response( serialized_instance.data, status=status.HTTP_201_CREATED) except UnderThresholdError as ute: return under_threshold(ute) except (OverQuotaError, OverAllocationError) as oqe: return over_quota(oqe) except ProviderNotActive as pna: return inactive_provider(pna) except SizeNotAvailable as snae: return size_not_available(snae) except HypervisorCapacityError as hce: return over_capacity(hce) except SecurityGroupNotCreated: return connection_failure(identity) except (socket_error, ConnectionFailure): return connection_failure(identity) except InvalidCredsError: return invalid_creds(identity) except Exception as exc: logger.exception("Encountered a generic exception. " "Returning 409-CONFLICT") return failure_response(status.HTTP_409_CONFLICT, str(exc.message))
def launch(user, name, provider, machine, size, host, skip_deploy, count): ident = user.identity_set.get(provider_id=provider.id) instances = [] for c in range(0, count): instances.append(launch_instance( user, provider.uuid, ident.uuid, size.alias, machine.instance_source.identifier, deploy=(not skip_deploy), ex_availability_zone=host, **{"name": name})) return instances
def launch(user, name, provider, machine, size, host, skip_deploy, count): ident = user.identity_set.get(provider_id=provider.id) instances = [] for c in range(0, count): instances.append( launch_instance(user, provider.uuid, ident.uuid, size.alias, machine.instance_source.identifier, deploy=(not skip_deploy), ex_availability_zone=host, **{"name": name})) return instances
def _multi_create( self, user, identity_uuid, size_alias, source_alias, name, deploy, allocation_source, project, boot_scripts, extra ): """ 1. Launch multiple instances 2. Serialize the launched instances 3. Return a list of serialized instances """ core_instances = launch_instance( user, identity_uuid, size_alias, source_alias, name, deploy, allocation_source=allocation_source, **extra ) serialized_data = [] # Serialize all instances launched for core_instance in core_instances: # Faking a 'partial update of nothing' to allow call to 'is_valid' serialized_instance = InstanceSerializer( core_instance, context={'request': self.request}, data={}, partial=True ) if not serialized_instance.is_valid(): logger.error( "multi-instance-launch, serialized instance is invalid, {}". format(serialized_instance) ) instance = serialized_instance.save() instance.project = project instance.save() if boot_scripts: _save_scripts_to_instance(instance, boot_scripts) instance.change_allocation_source(allocation_source) # append to result serialized_data.append(serialized_instance.data) # return a list of instances in the response return Response(serialized_data, status=status.HTTP_201_CREATED)
def post(self, request, provider_uuid, identity_uuid, format=None): """ Instance Class: Launches an instance based on the params Returns a single instance Parameters: machine_alias, size_alias, username TODO: Create a 'reverse' using the instance-id to pass the URL for the newly created instance I.e: url = "/provider/1/instance/1/i-12345678" """ data = request.DATA user = request.user #Check the data is valid missing_keys = valid_post_data(data) if missing_keys: return keys_not_found(missing_keys) #Pass these as args size_alias = data.pop("size_alias") machine_alias = data.pop("machine_alias") hypervisor_name = data.pop("hypervisor", None) deploy = data.pop("deploy", True) if type(deploy) in [str, unicode] and deploy.lower() == "false": deploy = False elif not type(deploy) is bool: deploy = True data["deploy"] = deploy logger.debug("deploy type = %s" % type(deploy)) logger.debug("deploy = %s" % deploy) logger.debug("data = %s" % str(data)) boot_scripts = data.pop("boot_scripts", []) try: logger.debug(data) core_instance = launch_instance( user, provider_uuid, identity_uuid, size_alias, machine_alias, ex_availability_zone=hypervisor_name, deploy=deploy, **data) except UnderThresholdError, ute: return under_threshold(ute)
def launch(user, name, provider, machines, size, host, skip_deploy, count): ident = user.identity_set.get(provider_id=provider.id) instances = [] kwargs = {} if host: kwargs['ex_availability_zone'] = host for c in range(0, count): for machine in machines: try: instance_id = launch_instance( user, ident.uuid, size.alias, machine.instance_source.identifier, name=name, deploy=(not skip_deploy), **kwargs) print "Successfully launched Machine %s : %s" \ % (machine.instance_source.identifier, instance_id) instances.append(instance_id) except Exception as exc: print "Error on launch of Machine %s : %s" \ % (machine.instance_source.identifier, exc) return instances
def post(self, request, provider_uuid, identity_uuid, format=None): """ Instance Class: Launches an instance based on the params Returns a single instance Parameters: machine_alias, size_alias, username TODO: Create a 'reverse' using the instance-id to pass the URL for the newly created instance I.e: url = "/provider/1/instance/1/i-12345678" """ data = request.DATA user = request.user # Check the data is valid missing_keys = valid_post_data(data) if missing_keys: return keys_not_found(missing_keys) #Pass these as args size_alias = data.pop("size_alias") machine_alias = data.pop("machine_alias") hypervisor_name = data.pop("hypervisor", None) deploy = data.pop("deploy", True) if type(deploy) in [str, unicode] and deploy.lower() == "false": deploy = False elif not type(deploy) is bool: deploy = True boot_scripts = data.pop("boot_scripts", []) try: logger.debug(data) core_instance = launch_instance( user, provider_uuid, identity_uuid, size_alias, machine_alias, ex_availability_zone=hypervisor_name, deploy=deploy, **data) except UnderThresholdError, ute: return under_threshold(ute)
def post(self, request, provider_uuid, identity_uuid, format=None): """ Instance Class: Launches an instance based on the params Returns a single instance Parameters: machine_alias, size_alias, username TODO: Create a 'reverse' using the instance-id to pass the URL for the newly created instance I.e: url = "/provider/1/instance/1/i-12345678" """ data = request.data user = request.user # Check the data is valid missing_keys = valid_post_data(data) if missing_keys: return keys_not_found(missing_keys) # Pass these as args size_alias = data.pop("size_alias") allocation_source_id = data.pop("allocation_source_id", None) machine_alias = data.pop("machine_alias") hypervisor_name = data.pop("hypervisor", None) if hypervisor_name: # Previous method passed this with 'None' but that fails now. # This check will only add the ex_ value if it is 'truthy'. data['ex_hypervisor_name'] = hypervisor_name deploy = data.pop("deploy", True) if type(deploy) in [str, unicode] and deploy.lower() == "false": deploy = False elif not isinstance(deploy, bool): deploy = True boot_scripts = data.pop("scripts", []) try: logger.debug(data) if not settings.USE_ALLOCATION_SOURCE: allocation_source = None else: allocation_source = AllocationSource.objects.get( source_id=allocation_source_id) core_instance = launch_instance(user, identity_uuid, size_alias, machine_alias, deploy=deploy, **data) except UnderThresholdError as ute: return under_threshold(ute) except OverQuotaError as oqe: return over_quota(oqe) except OverAllocationError as oae: return over_quota(oae) except SizeNotAvailable as snae: return size_not_available(snae) except SecurityGroupNotCreated: return connection_failure(provider_uuid, identity_uuid) except (socket_error, ConnectionFailure): return connection_failure(provider_uuid, identity_uuid) except LibcloudInvalidCredsError: return invalid_creds(provider_uuid, identity_uuid) except Exception as exc: logger.exception("Encountered a generic exception. " "Returning 409-CONFLICT") return failure_response(status.HTTP_409_CONFLICT, str(exc.message)) serializer = InstanceSerializer(core_instance, context={"request": request}, data=data) if serializer.is_valid(): instance = serializer.save() if boot_scripts: _save_scripts_to_instance(instance, boot_scripts) instance.change_allocation_source(allocation_source) return Response(serializer.data, status=status.HTTP_201_CREATED) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request, provider_uuid, identity_uuid, format=None): """ Instance Class: Launches an instance based on the params Returns a single instance Parameters: machine_alias, size_alias, username TODO: Create a 'reverse' using the instance-id to pass the URL for the newly created instance I.e: url = "/provider/1/instance/1/i-12345678" """ data = request.data user = request.user # Check the data is valid missing_keys = valid_post_data(data) if missing_keys: return keys_not_found(missing_keys) # Pass these as args size_alias = data.pop("size_alias") machine_alias = data.pop("machine_alias") hypervisor_name = data.pop("hypervisor", None) if hypervisor_name: # Previous method passed this with 'None' but that fails now. # This check will only add the ex_ value if it is 'truthy'. data['ex_hypervisor_name'] = hypervisor_name deploy = data.pop("deploy", True) if type(deploy) in [str, unicode] and deploy.lower() == "false": deploy = False elif not isinstance(deploy, bool): deploy = True boot_scripts = data.pop("scripts", []) try: logger.debug(data) core_instance = launch_instance( user, identity_uuid, size_alias, machine_alias, deploy=deploy, **data) except UnderThresholdError as ute: return under_threshold(ute) except OverQuotaError as oqe: return over_quota(oqe) except OverAllocationError as oae: return over_quota(oae) except SizeNotAvailable as snae: return size_not_available(snae) except SecurityGroupNotCreated: return connection_failure(provider_uuid, identity_uuid) except (socket_error, ConnectionFailure): return connection_failure(provider_uuid, identity_uuid) except InvalidCredsError: return invalid_creds(provider_uuid, identity_uuid) except Exception as exc: logger.exception("Encountered a generic exception. " "Returning 409-CONFLICT") return failure_response(status.HTTP_409_CONFLICT, str(exc.message)) serializer = InstanceSerializer(core_instance, context={"request": request}, data=data) if serializer.is_valid(): instance = serializer.save() if boot_scripts: _save_scripts_to_instance(instance, boot_scripts) return Response(serializer.data, status=status.HTTP_201_CREATED) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request, provider_uuid, identity_uuid, format=None): """ Instance Class: Launches an instance based on the params Returns a single instance Parameters: machine_alias, size_alias, username TODO: Create a 'reverse' using the instance-id to pass the URL for the newly created instance I.e: url = "/provider/1/instance/1/i-12345678" """ data = request.data user = request.user # Check the data is valid missing_keys = valid_post_data(data) if missing_keys: return keys_not_found(missing_keys) identity = Identity.shared_with_user(user, is_leader=True).filter(uuid=identity_uuid).first() if not identity: failure_msg = "User %s does not have permission to POST with this identity. Promote user to leader or use a different Identity." % (user,) return failure_response(status.HTTP_403_FORBIDDEN, failure_msg) # Pass these as args size_alias = data.pop("size_alias") allocation_source_uuid = data.pop("allocation_source_uuid",None) machine_alias = data.pop("machine_alias") hypervisor_name = data.pop("hypervisor", None) if hypervisor_name: # Previous method passed this with 'None' but that fails now. # This check will only add the ex_ value if it is 'truthy'. data['ex_hypervisor_name'] = hypervisor_name deploy = data.pop("deploy", True) if type(deploy) in [str, unicode] and deploy.lower() == "false": deploy = False elif not isinstance(deploy, bool): deploy = True boot_scripts = data.pop("scripts", []) try: logger.debug(data) allocation_source = AllocationSource.objects.get( uuid=allocation_source_uuid) core_instance = launch_instance( user, identity_uuid, size_alias, machine_alias, deploy=deploy, allocation_source=allocation_source, **data) except UnderThresholdError as ute: return under_threshold(ute) except OverQuotaError as oqe: return over_quota(oqe) except OverAllocationError as oae: return over_quota(oae) except AllocationBlacklistedError as e: return failure_response( status.HTTP_403_FORBIDDEN, e.message) except Unauthorized: return invalid_creds(provider_uuid, identity_uuid) except SizeNotAvailable as snae: return size_not_available(snae) except SecurityGroupNotCreated: return connection_failure(provider_uuid, identity_uuid) except (socket_error, ConnectionFailure): return connection_failure(provider_uuid, identity_uuid) except LibcloudInvalidCredsError: return invalid_creds(provider_uuid, identity_uuid) except Exception as exc: logger.exception("Encountered a generic exception. " "Returning 409-CONFLICT") return failure_response(status.HTTP_409_CONFLICT, str(exc.message)) serializer = InstanceSerializer(core_instance, context={"request": request}, data=data) if serializer.is_valid(): instance = serializer.save() if boot_scripts: _save_scripts_to_instance(instance, boot_scripts) instance.change_allocation_source(allocation_source) logger.info("DEBUG- Instance launch completed - Returning instance %s (%s) to user %s" % (instance, instance.created_by_identity, request.user)) return Response(serializer.data, status=status.HTTP_201_CREATED) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)