def add_machine_wrapper(self, name, fail_on_error=True, fail_on_invalid_params=True, monitoring=False, **kwargs): """Wrapper around add_machine for kwargs backwards compatibity FIXME: This wrapper should be deprecated """ # Sanitize params. rename_kwargs(kwargs, 'machine_ip', 'host') rename_kwargs(kwargs, 'machine_user', 'ssh_user') rename_kwargs(kwargs, 'machine_key', 'ssh_key') rename_kwargs(kwargs, 'machine_port', 'ssh_port') rename_kwargs(kwargs, 'remote_desktop_port', 'rdp_port') if kwargs.get('operating_system') == 'windows': kwargs['os_type'] = 'windows' else: kwargs['os_type'] = 'unix' kwargs.pop('operating_system', None) errors = {} for key in list(kwargs.keys()): if key not in ('host', 'ssh_user', 'ssh_port', 'ssh_key', 'os_type', 'rdp_port'): error = "Invalid parameter %s=%r." % (key, kwargs[key]) if fail_on_invalid_params: errors[key] = error else: log.warning(error) kwargs.pop(key) if 'host' not in kwargs: errors['host'] = "Required parameter host missing" log.error(errors['host']) if not name: name = kwargs['host'] if errors: log.error("Invalid parameters %s." % list(errors.keys())) raise BadRequestError({ 'msg': "Invalid parameters %s." % list(errors.keys()), 'errors': errors, }) # Add the machine. machine = self.add_machine(name, fail_on_error=fail_on_error, **kwargs) # Enable monitoring. if monitoring: from mist.api.monitoring.methods import enable_monitoring from mist.api.machines.models import KeyMachineAssociation enable_monitoring( self.cloud.owner, self.cloud.id, machine.machine_id, no_ssh=not (machine.os_type == 'unix' and KeyMachineAssociation.objects( machine=machine).count()) ) return machine
def add_machine(request): """ Tags: machines --- Add a machine to an OtherServer Cloud. This works for bare_metal clouds. --- cloud: in: path required: true type: string machine_ip: type: string required: true operating_system: type: string machine_name: type: string machine_key: type: string machine_user: type: string machine_port: type: string remote_desktop_port: type: string monitoring: type: boolean """ cloud_id = request.matchdict.get('cloud') params = params_from_request(request) machine_ip = params.get('machine_ip') if not machine_ip: raise RequiredParameterMissingError("machine_ip") operating_system = params.get('operating_system', '') machine_name = params.get('machine_name', '') machine_key = params.get('machine_key', '') machine_user = params.get('machine_user', '') machine_port = params.get('machine_port', '') remote_desktop_port = params.get('remote_desktop_port', '') monitoring = params.get('monitoring', '') job_id = params.get('job_id') if not job_id: job = 'add_machine' job_id = uuid.uuid4().hex else: job = None auth_context = auth_context_from_request(request) auth_context.check_perm("cloud", "read", cloud_id) if machine_key: auth_context.check_perm("key", "read", machine_key) try: Cloud.objects.get(owner=auth_context.owner, id=cloud_id, deleted=None) except Cloud.DoesNotExist: raise NotFoundError('Cloud does not exist') log.info('Adding bare metal machine %s on cloud %s' % (machine_name, cloud_id)) cloud = Cloud.objects.get(owner=auth_context.owner, id=cloud_id, deleted=None) try: machine = cloud.ctl.add_machine(machine_name, host=machine_ip, ssh_user=machine_user, ssh_port=machine_port, ssh_key=machine_key, os_type=operating_system, rdp_port=remote_desktop_port, fail_on_error=True) except Exception as e: raise MachineCreationError("OtherServer, got exception %r" % e, exc=e) # Enable monitoring if monitoring: monitor = enable_monitoring( auth_context.owner, cloud.id, machine.machine_id, no_ssh=not (machine.os_type == 'unix' and KeyMachineAssociation.objects(machine=machine))) ret = { 'id': machine.id, 'name': machine.name, 'extra': {}, 'public_ips': machine.public_ips, 'private_ips': machine.private_ips, 'job_id': job_id, 'job': job } if monitoring: ret.update({'monitoring': monitor}) return ret
def add_machine(request): """ Tags: machines --- Add a machine to an OtherServer/Libvirt Cloud. READ permission required on cloud. EDIT permission required on cloud. READ permission required on key. --- cloud: in: path required: true type: string machine_hostname: type: string required: true operating_system: type: string machine_name: type: string machine_key: type: string machine_user: type: string machine_port: type: string remote_desktop_port: type: string monitoring: type: boolean images_location: type: string """ cloud_id = request.matchdict.get('cloud') auth_context = auth_context_from_request(request) try: cloud = Cloud.objects.get(owner=auth_context.owner, id=cloud_id, deleted=None) except Cloud.DoesNotExist: raise NotFoundError('Cloud does not exist') if cloud.ctl.provider not in ['libvirt', 'bare_metal']: raise MistNotImplementedError() params = params_from_request(request) machine_hostname = params.get('machine_hostname') if not machine_hostname: raise RequiredParameterMissingError("machine_hostname") operating_system = params.get('operating_system', '') machine_name = params.get('machine_name', '') machine_key = params.get('machine_key', '') machine_user = params.get('machine_user', '') machine_port = params.get('machine_port', 22) remote_desktop_port = params.get('remote_desktop_port', '') images_location = params.get('images_location', '') monitoring = params.get('monitoring', False) job_id = params.get('job_id') if not job_id: job = 'add_machine' job_id = uuid.uuid4().hex else: job = None auth_context.check_perm("cloud", "read", cloud_id) auth_context.check_perm("cloud", "edit", cloud_id) if machine_key: auth_context.check_perm("key", "read", machine_key) log.info('Adding host machine %s on cloud %s' % (machine_name, cloud_id)) try: machine = cloud.ctl.add_machine(host=machine_hostname, ssh_user=machine_user, ssh_port=machine_port, ssh_key=machine_key, name=machine_name, os_type=operating_system, rdp_port=remote_desktop_port, images_location=images_location) except Exception as e: raise MachineCreationError("Adding host got exception %r" % e, exc=e) # Enable monitoring if monitoring: monitor = enable_monitoring( auth_context.owner, cloud.id, machine.machine_id, no_ssh=not (machine.os_type == 'unix' and KeyMachineAssociation.objects(machine=machine))) ret = { 'id': machine.id, 'name': machine.name, 'extra': {}, 'public_ips': machine.public_ips, 'private_ips': machine.private_ips, 'job_id': job_id, 'job': job } if monitoring: ret.update({'monitoring': monitor}) return ret