Esempio n. 1
0
File: vm.py Progetto: cloudcache/cc1
def create(caller_id, name, description, image_id, template_id, public_ip_id, iso_list, disk_list, vnc, groups, count=1, user_data=None,
           ssh_key=None, ssh_username=None):
    """
    Creates virtual machines.
    @cmview_user

    @parameter{name,string}
    @parameter{description,string}
    @parameter{image_id,int}
    @parameter{template_id,int}
    @parameter{ip_id,int}
    @parameter{iso_list,list(int)} ISOs' ids
    @parameter{vnc}
    @parameter{groups}
    @parameter{user_data} data accessible via ec2ctx
    @parameter{ssh_key}
    @parameter{ssh_username}

    @returns @asreturned{src.cm.views.utils.vm.create()}
    """
    user = User.get(caller_id)
    try:
        user.check_points()
    except:
        message.warn(caller_id, 'point_limit', {'used_points': user.used_points, 'point_limit': user.points})
    vms = VM.create(user, name=name, description=description, image_id=image_id,
                    template_id=template_id, public_ip_id=public_ip_id, iso_list=iso_list, disk_list=disk_list,
                    vnc=vnc, groups=groups, count=count, user_data=user_data, ssh_key=ssh_key, ssh_username=ssh_username)

    for vm in vms:
        thread = VMThread(vm, 'create')
        thread.start()

    return [vm.dict for vm in vms]
Esempio n. 2
0
File: vm.py Progetto: cloudcache/cc1
def create(caller_id, name, description, image_id, template_id, public_ip_id, iso_list, disk_list, vnc, node_id):
    user = User.get(caller_id)
    vms = VM.create(user, name=name, description=description, image_id=image_id,
                    template_id=template_id, public_ip_id=public_ip_id, iso_list=iso_list, disk_list=disk_list,
                    vnc=vnc, groups=[], node_id=node_id)
    for vm in vms:
        thread = VMThread(vm, 'create')
        thread.start()

    return [vm.dict for vm in vms]
Esempio n. 3
0
    def register_head(vm):
        """
        Head registration process:
        - Creates ssh keys and sets their values for WN;
        - Inserts VMs into the database;
        - Then starts VMThreads which create actual machines.

        Called when registering farms head.

        @parameter{vm,VM} instance of the VM to be registered as head
        """
        log.debug(vm.user_id, "machine %d: registered as head" % vm.id)

        log.debug(vm.user_id, "creating lock for machine %d in farm %d" % (vm.id, vm.farm_id))
        # skip if farm is already configured - reboot head
        if vm.is_head() == True and vm.farm.state == farm_states['running']:
            return

        vms = []
        if vm.farm.state == farm_states['init_head']:
            vm.farm.state = farm_states['running']
            vm.farm.save()

            log.info(vm.user_id, 'generating ssh keys on head %d' % vm.id)

            try:
                r = Command.execute('generate_key', vm.user_id, vm.id)
                r = json.loads(r)
                log.info(vm.user_id, 'generated key: %s for machine %d' % (r, vm.id))
                for wn in vm.farm.vms.all():
                    wn.ssh_username = '******'
                    wn.ssh_key = r
                    wn.save()
                    if not wn.is_head():
                        vms.append(wn)
                ssh_username = '******'
                ssh_key = r
                log.debug(vm.user_id, 'appended %d vms to farm [id:%d]' % (vm.farm.vms.count() - 1, vm.id))  # excluding head

                Command.add_command('add_ssh_key', vm.user_id, vm.id, user=ssh_username, ssh_key=ssh_key)
                Command.add_command('update_hosts', vm.user_id, vm.id, hosts_list=vm.farm.hosts(), user=ssh_username)
                Command.execute('set_hostname', vm.user_id, vm.id, hostname=vm.name.replace(vm.farm.name, 'farm'))

            except Exception:
                log.exception(vm.user_id, '')
                vm.farm.state = farm_states['unconfigured']
                message.error(vm.id, 'farm_create', {'id': vm.farm.id, 'name': vm.farm.name})
        log.info(vm.user_id, 'Head %d registered' % vm.id)
        shared = {"counter": len(vms), "lock": threading.Lock()}
        for vm in vms:
            thread = VMThread(vm, 'create', shared)
            thread.start()
            log.debug(vm.user_id, 'vm thread created [vm id:%d]' % vm.id)
Esempio n. 4
0
File: vm.py Progetto: cc1-cloud/cc1
    def reset(vms):
        from cm.utils.threads.vm import VMThread

        results = []
        for vm in vms:
            if vm.state in (vm_states['running'], vm_states['running ctx']):
                thread = VMThread(vm, 'reset')
                thread.start()
                results.append({'status': 'ok', 'data': ''})
            else:
                results.append({'status': 'vm_wrong_state', 'data': ''})

        return results
Esempio n. 5
0
    def reset(vms):
        from cm.utils.threads.vm import VMThread

        results = []
        for vm in vms:
            if vm.state in (vm_states['running'], vm_states['running ctx']):
                thread = VMThread(vm, 'reset')
                thread.start()
                results.append({'status': 'ok', 'data': ''})
            else:
                results.append({'status': 'vm_wrong_state', 'data': ''})

        return results
Esempio n. 6
0
File: vm.py Progetto: cc1-cloud/cc1
def update_state(remote_ip, vm_name, action, state):
    """
    @cmview_ci
    @param_post{remote_ip,string}
    @param_post{vm_name}
    @param_post{action}
    @param_post{state}
    """
    try:
        node = Node.objects.get(address=remote_ip)
    except:
        raise CMException('node_not_found')

    try:
        vm_id = int(vm_name.split('-')[1])
        user_id = int(vm_name.split('-')[2])
    except:
        log.debug(0, "Unknown vm from hook: %s" % vm_name)
        raise CMException('vm_not_found')

    if action != "stopped":
        log.debug(user_id, "Not updating vm state: action is %s" % str(action))
        return ''

    try:
        VM.objects.update()
        vm = VM.objects.get(id=vm_id)
    except:
        log.error(user_id, 'Cannot find vm in database!')
        raise CMException('vm_not_found')

    if not vm.state in [vm_states['running ctx'], vm_states['running']]:
        log.error(user_id, 'VM is not running!')
        raise CMException('vm_not_running')

    if vm.state == vm_states['restart']:
        raise CMException('vm_restart')

    thread = VMThread(vm, 'delete')
    thread.start()

    return ''
Esempio n. 7
0
def update_state(remote_ip, vm_name, action, state):
    """
    @cmview_ci
    @param_post{remote_ip,string}
    @param_post{vm_name}
    @param_post{action}
    @param_post{state}
    """
    try:
        node = Node.objects.get(address=remote_ip)
    except:
        raise CMException('node_not_found')

    try:
        vm_id = int(vm_name.split('-')[1])
        user_id = int(vm_name.split('-')[2])
    except:
        log.debug(0, "Unknown vm from hook: %s" % vm_name)
        raise CMException('vm_not_found')

    if action != "stopped":
        log.debug(user_id, "Not updating vm state: action is %s" % str(action))
        return ''

    try:
        VM.objects.update()
        vm = VM.objects.get(id=vm_id)
    except:
        log.error(user_id, 'Cannot find vm in database!')
        raise CMException('vm_not_found')

    if not vm.state in [vm_states['running ctx'], vm_states['running']]:
        log.error(user_id, 'VM is not running!')
        raise CMException('vm_not_running')

    if vm.state == vm_states['restart']:
        raise CMException('vm_restart')

    thread = VMThread(vm, 'delete')
    thread.start()

    return ''
Esempio n. 8
0
File: vm.py Progetto: cc1-cloud/cc1
def create(caller_id, name, description, image_id, template_id, public_ip_id, iso_list, disk_list, vnc, node_id):
    """
    Creates new VM with specified attributes.

    @cmview_admin_cm
    @param_post{name,string}
    @param_post{description,string}
    @param_post{image_id,int}
    @param_post{template_id,int}
    @param_post{public_ip_id,int}
    @param_post{iso_list,list(int)}
    @param_post{disk_list,list(int)}
    @param_post{vnc}
    @param_post{node_id}
    """
    user = User.get(caller_id)
    vms = VM.create(user, name=name, description=description, image_id=image_id,
                    template_id=template_id, public_ip_id=public_ip_id, iso_list=iso_list, disk_list=disk_list,
                    vnc=vnc, groups=[], node_id=node_id)
    for vm in vms:
        thread = VMThread(vm, 'create')
        thread.start()

    return [vm.dict for vm in vms]