Esempio n. 1
0
def download(caller_id, description, name, path, disk_controller, network_device, platform, video_device):
    """
    Downloads image depending on the \c data parameter.
    @cmview_user

    @parameter{path,string} HTTP or FTP path to image to download
    @parameter{name,string}
    @parameter{description,string}

    @parameter{type,image_types} type of image, automatically set, type is in the URL requested

    @response{None}

    @raises{image_not_found,CMException}
    @raises{image_create,CMException}
    """
    user = User.get(caller_id)

    if not any([path.startswith("http://"), path.startswith("https://"), path.startswith("ftp://")]):
        path = "http://" + path.strip()

    # size value is taken
    try:
        connection = urllib.urlopen(path)
        size = int(connection.info()["Content-Length"])
    except IOError:
        log.exception(caller_id, "Cannot find image")
        raise CMException("image_not_found")
    except KeyError:
        log.exception(caller_id, "Cannot calculate size")
        raise CMException("image_calculate_size")

    user = User.get(caller_id)
    user.check_storage(size / (1024 * 1024))

    image = SystemImage.create(
        name=name,
        description=description,
        user=user,
        platform=platform,
        disk_controller=disk_controller,
        network_device=network_device,
        video_device=video_device,
    )

    try:
        image.save()
    except Exception, e:
        log.error(caller_id, "Unable to save image to DB: %s" % str(e))
        raise CMException("image_create")
Esempio n. 2
0
def add(caller_id, user_id):
    """
    Adds specified CC1 User to current CM.

    @cmview_user
    @note This method is decorated by user_log decorator, not by admin_cm_log.
    This is caused by the fact that CLMAdmin doesn't need to be CMAdmin and
    despite not having rights to call CMAdmin functions he needs to call it on
    CMAdmin privileges.

    @param_post{user_id}
    """

    User.create(user_id)
Esempio n. 3
0
def multiple_change_quota(caller_id, user_ids, memory=None, cpu=None, storage=None, public_ip=None, points=None):
    """
    Method changes quota of multiple users.
    @cmview_admin_cm

    @dictkey{users,list(int)} ids of the users to change quota
    @dictkey{cpu,int} cpu to set
    @dictkey{storage,int} storage to set
    @dictkey{public_ip,int} number of public_ips to set
    @dictkey{points,int} points to set

    @response{None}
    """

    for user_id in user_ids:
        user = User.get(user_id)
        user.memory = memory or user.memory
        user.cpu = cpu or user.cpu
        user.storage = storage or user.storage
        user.public_ip = public_ip or user.public_ip
        user.points = points or user.points
        try:
            user.save()
        except:
            raise CMException('user_change_quota')
Esempio n. 4
0
def change_quota(caller_id, user_id, memory, cpu, storage, public_ip, points):
    """
    Function changes quota for user @prm{user_id}.
    @cmview_admin_cm

    @parameter{user_id}
    @parameter{memory}
    @parameter{cpu,int}
    @parameter{storage,int}
    @parameter{public_ip,int}
    @parameter{points,int}
    """

    user = User.get(user_id)

    user.memory = memory
    user.cpu = cpu
    user.storage = storage
    user.public_ip = public_ip
    user.points = points

    try:
        user.save()
    except:
        raise CMException('user_change_quota')
Esempio n. 5
0
def add(caller_id, user_id):
    """
    Function adds new user to DB and creates its home directory.
    @cmview_user

    @note This method is decorated by user_log decorator, not by admin_cm_log.
    This is caused by the fact that CLMAdmin doesn't need to be CMAdmin and
    despite not having rights to call CMAdmin functions he needs to call it on
    CMAdmin priviledges.

    @parameter{user_id}

    @response{None}
    """

    User.create(user_id)
Esempio n. 6
0
def change_quota(caller_id, user_id, memory, cpu, storage, public_ip, points):
    """
    Changes specified User's quota.

    @cmview_admin_cm
    @param_post{user_id}
    @param_post{memory}
    @param_post{cpu,int}
    @param_post{storage,int}
    @param_post{public_ip,int}
    @param_post{points,int}
    """

    user = User.get(user_id)

    user.memory = memory
    user.cpu = cpu
    user.storage = storage
    user.public_ip = public_ip
    user.points = points

    try:
        user.save()
    except:
        raise CMException('user_change_quota')
Esempio n. 7
0
def download(caller_id, description, name, path, disk_dev, disk_controller):
    """
    Downloads specified StorateImage from remote path.

    @cmview_admin_cm
    @param_post{description,string}
    @param_post{name,string} how to name newly downloaded storage image
    @param_post{path,string} HTTP or FTP path to download StorageImage.
    @param_post{disk_dev}
    @param_post{disk_controller}
    """

    # size value is taken
    try:
        connection = urllib.urlopen(path)
        size = int(connection.info()["Content-Length"])
    except IOError:
        log.exception('Cannot find image')
        raise CMException('image_not_found')
    except KeyError:
        log.exception(caller_id, 'Cannot calculate size')
        raise CMException('image_calculate_size')

    user = User.get(caller_id)

    image = StorageImage.create(name=name, description=description, user=user, disk_dev=disk_dev,  disk_controller=disk_controller)

    try:
        image.save()
    except Exception, e:
        log.error(caller_id, "Unable to save image to DB: %s" % str(e))
        raise CMException('image_create')
Esempio n. 8
0
def request(caller_id, mask, name):
    """
    Tries to allocate network with specified mask for caller.

    @cmview_user
    @param_post{mask}
    @param_post{name}

    @response{dict} UserNetwork.dict property of the newly created UserNetwork
    """
    new_net = None
    user = User.get(caller_id)
    for available_network in AvailableNetwork.objects.all():
        log.debug(user.id, "Trying to allocate in %s" % str(available_network.to_ipnetwork()))
        if available_network.state == available_network_states['ok']:
            try:
                net = available_network.get_unused_ipnetwork(mask)

                new_net = UserNetwork()
                new_net.address = str(net.network)
                new_net.mask = mask
                new_net.name = name
                new_net.available_network = available_network
                new_net.user = user
                new_net.save()

                new_net.allocate()

                return new_net.dict
            except:
                continue
    if new_net == None:
        raise CMException('available_network_not_found')
Esempio n. 9
0
def download(caller_id, name, description, path, disk_controller):
    """
    Downloads specified IsoImage and saves it with specified name and description.

    @cmview_user
    @param_post{name,string}
    @param_post{description,string}
    @param_post{path,string} HTTP or FTP path to IsoImage to download
    @param_post{disk_controller}
    """
    user = User.get(caller_id)

    if not any([path.startswith('http://'), path.startswith('https://'), path.startswith('ftp://')]):
        path = 'http://' + path.strip()

    # size value is taken
    try:
        connection = urllib.urlopen(path)
        size = int(connection.info()["Content-Length"])
    except IOError:
        log.exception('Cannot find image')
        raise CMException('image_not_found')
    except KeyError:
        log.exception(caller_id, 'Cannot calculate size')
        raise CMException('image_calculate_size')

    user.check_storage(size / (1024 * 1024))

    image = IsoImage.create(user=user, description=description, name=name, disk_controller=disk_controller, disk_dev=1)

    try:
        image.save()
    except Exception, e:
        log.error(caller_id, "Unable to save image to DB: %s" % str(e))
        raise CMException('image_create')
Esempio n. 10
0
def multiple_change_quota(caller_id,
                          user_ids,
                          memory=None,
                          cpu=None,
                          storage=None,
                          public_ip=None,
                          points=None):
    """
    Changes quota of multiple users.

    @cmview_admin_cm
    @param_post{user_ids,list(int)}
    @param_post{memory,int} new RAM memory limit [MB]
    @param_post{cpu,int} new CPU's limit
    @param_post{storage,int} new storage space's limit [MB]
    @param_post{public_ip,int} new limit of public_ips
    @param_post{points,int} new monthly points limit
    """

    for user_id in user_ids:
        user = User.get(user_id)
        user.memory = memory or user.memory
        user.cpu = cpu or user.cpu
        user.storage = storage or user.storage
        user.public_ip = public_ip or user.public_ip
        user.points = points or user.points
        try:
            user.save()
        except:
            raise CMException('user_change_quota')
Esempio n. 11
0
def request(caller_id, mask, name):
    """
    Tries to allocate network with specified mask for caller.

    @cmview_user
    @param_post{mask}
    @param_post{name}

    @response{dict} UserNetwork.dict property of the newly created UserNetwork
    """
    new_net = None
    user = User.get(caller_id)
    for available_network in AvailableNetwork.objects.all():
        log.debug(
            user.id,
            "Trying to allocate in %s" % str(available_network.to_ipnetwork()))
        if available_network.state == available_network_states['ok']:
            try:
                net = available_network.get_unused_ipnetwork(mask)

                new_net = UserNetwork()
                new_net.address = str(net.network)
                new_net.mask = mask
                new_net.name = name
                new_net.available_network = available_network
                new_net.user = user
                new_net.save()

                new_net.allocate()

                return new_net.dict
            except:
                continue
    if new_net == None:
        raise CMException('available_network_not_found')
Esempio n. 12
0
def download(caller_id, description, name, path, disk_controller, network_device, platform, video_device):
    """
    Downloads image depending on the \c data parameter.
    @cmview_admin_cm

    @parameter{description,string}
    @parameter{name,string}
    @parameter{path,string} HTTP or FTP path to image to download
    @parameter{type,image_types} type of image, automatically set, type is in the URL requested

    @response{None}
    """

    # size value is taken
    try:
        connection = urllib.urlopen(path)
        size = int(connection.info()["Content-Length"])
    except IOError:
        log.exception(caller_id, 'Cannot find image')
        raise CMException('image_not_found')
    except KeyError:
        log.exception(caller_id, 'Cannot calculate size')
        raise CMException('image_calculate_size')

    user = User.get(caller_id)

    image = SystemImage.create(name=name, description=description, user=user, platform=platform,
                               disk_controller=disk_controller, network_device=network_device,
                               video_device=video_device)

    try:
        image.save()
    except Exception, e:
        log.error(caller_id, "Unable to save image to DB: %s" % str(e))
        raise CMException('image_create')
Esempio n. 13
0
def points_history(caller_id):
    """
    @cmview_user

    @response caller's point history
    """
    return User.get(caller_id).points_history()
Esempio n. 14
0
def create(caller_id, name, description, filesystem, size, disk_controller):
    """
    Creates new StorageImage.

    @cmview_user
    @param_post{name,string}
    @param_post{description,string}
    @param_post{filesystem,int} id of the filesystem. Supported filesystems are
    common.hardware.disk_filesystems
    @param_post{size,int} size of the SystemImage to create [MB]
    @param_post{disk_controller}

    @response{dict} StorageImage.dict property of newly created StorageImage
    """
    if size < 1:
        raise CMException('image_invalid_size')

    user = User.get(caller_id)
    user.check_storage(size)
    image = StorageImage.create(user=user, disk_controller=disk_controller, description=description, name=name,
                                size=size)

    try:
        image.save()
    except Exception, e:
        log.error(caller_id, "Unable to save image to DB: %s" % str(e))
        raise CMException('image_create')
Esempio n. 15
0
def request(caller_id):
    """
    Method requests single PublicIP address for caller. If caller's quota
    is exceeded, exception is raised. Otherwise caller obtains a new PublicIP
    address.

    @cmview_user
    @response{string} newly obtained PublicIP's address

    @raises{public_lease_not_found,CMException}
    @raises{public_lease_request,CMException}
    """
    user = User.get(caller_id)

    if len(user.public_ips.all()) >= user.public_ip:
        raise CMException('public_lease_limit')

    ips = PublicIP.objects.filter(user=None).all()
    if len(ips) == 0:
        raise CMException('public_lease_not_found')

    ip = ips[0]
    ip.user = user
    ip.request_time = datetime.now()
    ip.release_time = None
    try:
        ip.save()
    except Exception:
        raise CMException('public_lease_request')
    return ip.address
Esempio n. 16
0
def list_user_networks(caller_id, user_id=None):
    """
    @cmview_admin_cm
    @param_post{user_id,int} (optional) if specified, only networks belonging
    to specigied User are fetched.
    @param_post{only_unused,bool} (optional) if @val{True}, only unused
    networks are returned

    @response{list(dict)} UserNetwork.dict property for each requested
    UserNetwork
    """
    try:
        user_networks = []
        if user_id:
            user = User.get(user_id)
            user_networks = UserNetwork.objects.filter(user=user)
        else:
            user_networks = UserNetwork.objects.all()
    except:
        raise CMException('network_not_found')

    response = []
    for network in user_networks:
        response.append(network.dict)
    return response
Esempio n. 17
0
def request(caller_id):
    """
    Method requests single PublicIP address for caller. If caller's quota
    is exceeded, exception is raised. Otherwise caller obtains a new PublicIP
    address.

    @cmview_user
    @response{string} newly obtained PublicIP's address

    @raises{public_lease_not_found,CMException}
    @raises{public_lease_request,CMException}
    """
    user = User.get(caller_id)

    if len(user.public_ips.all()) >= user.public_ip:
        raise CMException('public_lease_limit')

    ips = PublicIP.objects.filter(user=None).all()
    if len(ips) == 0:
        raise CMException('public_lease_not_found')

    ip = ips[0]
    ip.user = user
    ip.request_time = datetime.now()
    ip.release_time = None
    try:
        ip.save()
    except Exception:
        raise CMException('public_lease_request')
    return ip.address
Esempio n. 18
0
def points_history(caller_id):
    """
    @cmview_user

    @response caller's point history
    """
    return User.get(caller_id).points_history()
Esempio n. 19
0
def create(caller_id, name, description, filesystem, size, disk_controller):
    """
    Method creates new Image. It's only used by disk_volume type (only url view with create)
    @cmview_user

    @parameter{name,string}
    @parameter{description,string}
    @parameter{filesystem,int} id of the filesystem. Supported filesystems are listed in settings
    @parameter{size,int} size of the Image to create [MB]
    @parameter{disk_controller}

    @response{dict} Image's dictionary
    """
    if size < 1:
        raise CMException('image_invalid_size')

    user = User.get(caller_id)
    user.check_storage(size)
    image = StorageImage.create(user=user, disk_controller=disk_controller, description=description, name=name,
                                size=size)

    try:
        image.save()
    except Exception, e:
        log.error(caller_id, "Unable to save image to DB: %s" % str(e))
        raise CMException('image_create')
Esempio n. 20
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. 21
0
def list_user_networks(caller_id, user_id=None):
    """
    @cmview_admin_cm
    @param_post{user_id,int} (optional) if specified, only networks belonging
    to specigied User are fetched.
    @param_post{only_unused,bool} (optional) if @val{True}, only unused
    networks are returned

    @response{list(dict)} UserNetwork.dict property for each requested
    UserNetwork
    """
    try:
        user_networks = []
        if user_id:
            user = User.get(user_id)
            user_networks = UserNetwork.objects.filter(user=user)
        else:
            user_networks = UserNetwork.objects.all()
    except:
        raise CMException('network_not_found')

    response = []
    for network in user_networks:
        response.append(network.dict)
    return response
Esempio n. 22
0
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]
Esempio n. 23
0
def check_quota(caller_id):
    """
    Check quota for caller user.

    @cmview_user

    @response{dict} extended caller's data
    """
    return User.get(caller_id).long_dict
Esempio n. 24
0
def get_list(caller_id):
    """
    @cmview_user
    @response{list(dict)} PublicIP.dict property for each caller's PublicIP
    """
    user = User.get(caller_id)

    ips = PublicIP.objects.filter(user=user).all()
    return [ip.dict for ip in ips]
Esempio n. 25
0
def check_quota(caller_id):
    """
    Check quota for caller user.

    @cmview_user

    @response{dict} extended caller's data
    """
    return User.get(caller_id).long_dict
Esempio n. 26
0
def get_list(caller_id):
    """
    @cmview_user
    @response{list(dict)} PublicIP.dict property for each caller's PublicIP
    """
    user = User.get(caller_id)

    ips = PublicIP.objects.filter(user=user).all()
    return [ip.dict for ip in ips]
Esempio n. 27
0
def list_leases(caller_id, network_id):
    user = User.get(caller_id)
    try:
        user_network = UserNetwork.objects.filter(user=user).get(id=network_id)
    except:
        raise CMException('network_not_found')
    response = []
    for lease in user_network.lease_set.all():
        response.append(lease.dict)
    return response
Esempio n. 28
0
def download(caller_id, description, name, path, disk_controller, network_device, platform, video_device):
    """
    Downloads image depending on the \c data parameter.
    @cmview_user

    @parameter{path,string} HTTP or FTP path to image to download
    @parameter{name,string}
    @parameter{description,string}

    @parameter{type,image_types} type of image, automatically set, type is in the URL requested

    @response{None}

    @raises{image_not_found,CMException}
    @raises{image_create,CMException}
    """
    user = User.get(caller_id)

    if not any([path.startswith('http://'), path.startswith('https://'), path.startswith('ftp://')]):
        path = 'http://' + path.strip()

    # size value is taken
    try:
        connection = urllib.urlopen(path)
        size = int(connection.info()["Content-Length"])
    except IOError:
        log.exception(caller_id, 'Cannot find image')
        raise CMException('image_not_found')
    except KeyError:
        log.exception(caller_id, 'Cannot calculate size')
        raise CMException('image_calculate_size')

    user = User.get(caller_id)
    user.check_storage(size / (1024 * 1024))

    image = SystemImage.create(name=name, description=description, user=user, platform=platform,
                        disk_controller=disk_controller, network_device=network_device, video_device=video_device)

    try:
        image.save()
    except Exception, e:
        log.error(caller_id, "Unable to save image to DB: %s" % str(e))
        raise CMException('image_create')
Esempio n. 29
0
def add_missing(caller_id, remote):
    """
    Adds Users whose ids are listed in @prm{remote} and who are locally
    missing.

    @cmview_user
    @param_post{remote,list(int)} ids of the remote Users
    """

    # remote must be passed through POST as a list
    # it is a list of users id
    try:
        # put all the user ids in a list
        local = User.objects.all().values_list('id', flat=True)
        # create user for all the ids which are in remote but not in the local cm
        for user_id in [uid for uid in remote if uid not in local]:
            User.create(user_id)
    except:
        raise CMException('user_create')
Esempio n. 30
0
def check_quota(caller_id, user_id):
    """
    Get specified User's qouta.

    @cmview_admin_cm
    @param_post{user_id,int}

    @response{dict} Full User's quota
    """
    return User.get(user_id).long_dict
Esempio n. 31
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. 32
0
def check_quota(caller_id, user_id):
    """
    Check quota of the user @prm{user_id}.
    @cmview_admin_cm

    @parameter{user_id,int}

    @response{dict} extended user's data
    """
    return User.get(user_id).long_dict
Esempio n. 33
0
def list_user_networks(caller_id):
    user = User.get(caller_id)
    try:
        user_networks = UserNetwork.objects.filter(user=user)
    except:
        raise CMException('network_not_found')
    response = []
    for network in user_networks:
        if network.available_network.state == available_network_states['ok']:
            response.append(network.dict)
    return response
Esempio n. 34
0
def release(caller_id, network_id):
    user = User.get(caller_id)
    try:
        user_network = UserNetwork.objects.filter(user=user).get(id=network_id)
    except:
        raise CMException('network_not_found')

    if user_network.is_in_use():
        raise CMException('network_in_use')
    user_network.release()
    user_network.delete()
Esempio n. 35
0
def add_missing(caller_id, remote):
    """
    Function adds missing users listed in remote that don't belong to local.

    @cmview_user

    @parameter{remote,list of int}

    @response{None}
    """

    # remote must be passed through POST as a list
    # it is a list of users id
    try:
        # put all the user ids in a list
        local = User.objects.all().values_list('id', flat=True)
        # create user for all the ids which are in remote but not in the local cm
        for user_id in [uid for uid in remote if uid not in local]:
            User.create(user_id)
    except:
        raise CMException('user_create')
Esempio n. 36
0
def create(caller_id, name, description, image_id, head_template_id, worker_template_id, public_ip_id, iso_list, disk_list, vnc, groups, count):
    """
    Method creates new Farm for caller:

    -#. Creates VMs described by \c machine dict.
    -#. Creates farm named by \c machine[name] consisting of those VMs.
    -#. Creates thread for this farm.

    @decoratedby{src.cm.utils.decorators.user_log}
    @parameter{machine,dict}
    \n fields:
    @dictkey{name,string} farm's name
    @dictkey{count,int} number of Worker Nodes
    @dictkey{template_id,int} Worker Node's template
    @dictkey{head_template_id,int} Head's template
    @dictkey{image_id,int} image for WNs and Head
    @dictkey{groups,list} optional
    @dictkey{node_id} optional on which node farm is to be created
    @dictkey{description,string} description of the farm

    @response{None}

    @raises{farm_create,CMException}
    """
    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})

    farm = Farm.create(user=user, name=name, description=description)

    vms = VM.create(user, name=name, description=description, image_id=image_id, template_id=worker_template_id,
                    head_template_id=head_template_id, public_ip_id=public_ip_id, iso_list=iso_list, disk_list=disk_list,
                    vnc=vnc, groups=groups, farm=farm, count=count)

    farm.save()
    for vm in vms:
        vm.farm = farm
        if not vm.is_head():
            # disable autosave
            vm.save_vm = 0
        vm.save()

    try:
        farm.save()
    except Exception:
        log.exception(caller_id, 'farm_create')
        raise CMException('farm_create')

    VMThread(vms[0], 'create').start()
    return [vm.dict for vm in vms]
Esempio n. 37
0
def add(new_user_id):
    """
    Adds existing CLM User to CM Users.
    @cmview_guest
    @param_post{new_user_id,int} id of the existing CLM User

    @response{None}
    """
    user = User.create(new_user_id)
    try:
        user.save()
    except Exception, e:
        log.debug(0, "Adding to DB: %s" % str(e))
        raise CMException('user_create')
Esempio n. 38
0
def add(new_user_id):
    """
    Adds existing CLM User to CM Users.
    @cmview_guest
    @param_post{new_user_id,int} id of the existing CLM User

    @response{None}
    """
    user = User.create(new_user_id)
    try:
        user.save()
    except Exception, e:
        log.debug(0, "Adding to DB: %s" % str(e))
        raise CMException('user_create')
Esempio n. 39
0
def add(new_user_id):
    """
    Function adds new user to DB and creates its home directory.
    @cmview_guest

    @dictkey{new_user_id,int}

    @response{None}
    """
    user = User.create(new_user_id)
    try:
        user.save()
    except Exception, e:
        log.debug(0, "Adding to DB: %s" % str(e))
        raise CMException('user_create')
Esempio n. 40
0
def list_user_networks(caller_id):
    """
    @cmview_user
    @response{list(dict)} UserNetwork.dict property for each caller's
    UserNetwork
    """
    user = User.get(caller_id)
    try:
        user_networks = UserNetwork.objects.filter(user=user)
    except:
        raise CMException('network_not_found')
    response = []
    for network in user_networks:
        if network.available_network.state == available_network_states['ok']:
            response.append(network.dict)
    return response
Esempio n. 41
0
def list_user_networks(caller_id):
    """
    @cmview_user
    @response{list(dict)} UserNetwork.dict property for each caller's
    UserNetwork
    """
    user = User.get(caller_id)
    try:
        user_networks = UserNetwork.objects.filter(user=user)
    except:
        raise CMException('network_not_found')
    response = []
    for network in user_networks:
        if network.available_network.state == available_network_states['ok']:
            response.append(network.dict)
    return response
Esempio n. 42
0
def create(caller_id, name, description, image_id, head_template_id, worker_template_id, public_ip_id, iso_list, disk_list, vnc, groups, count):
    """
    Method creates new caller's Farm.

    @cmview_user
    @param_post{name,string} Farm's name
    @param_post{description,string}
    @param_post{image_id,int} image for WNs and Head
    @param_post{head_template_id,int} Head's template
    @param_post{worker_template_id,int} Worker Node's template
    @param_post{public_ip_id,int} Worker Node's template
    @param_post{iso_list,list}
    @param_post{disk_list,list}
    @param_post{vnc,list}
    @param_post{groups,list}
    @param_post{count,int} number of Worker Nodes

    @raises{farm_create,CMException}
    """
    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})

    farm = Farm.create(user=user, name=name, description=description)

    vms = VM.create(user, name=name, description=description, image_id=image_id, template_id=worker_template_id,
                    head_template_id=head_template_id, public_ip_id=public_ip_id, iso_list=iso_list, disk_list=disk_list,
                    vnc=vnc, groups=groups, farm=farm, count=count)

    farm.save()
    for vm in vms:
        vm.farm = farm
        if not vm.is_head():
            # disable autosave
            vm.save_vm = 0
        vm.save()

    try:
        farm.save()
    except Exception:
        log.exception(caller_id, 'farm_create')
        raise CMException('farm_create')

    VMThread(vms[0], 'create').start()
    return [vm.dict for vm in vms]
Esempio n. 43
0
File: vm.py Progetto: cc1-cloud/cc1
def save_and_shutdown(caller_id, vm_id, name, description):
    """
    Calls VM.save_and_shutdown() on specified VM

    @cmview_user
    @param_post{vm_id,int} id of the VM to save and shutdown.
    @param_post{name,string} name of the new SystemImage VM should be saved to
    @param_post{description,string} description of the new SystemImage VM
    should be saved to
    """
    user = User.get(caller_id)
    vm = VM.get(caller_id, vm_id)

    if user.used_storage + vm.system_image.size > user.storage:
        raise CMException('user_storage_limit')

    VM.save_and_shutdown(caller_id, vm, name, description)
Esempio n. 44
0
def save_and_shutdown(caller_id, vm_id, name, description):
    """
    Calls VM.save_and_shutdown() on specified VM

    @cmview_user
    @param_post{vm_id,int} id of the VM to save and shutdown.
    @param_post{name,string} name of the new SystemImage VM should be saved to
    @param_post{description,string} description of the new SystemImage VM
    should be saved to
    """
    user = User.get(caller_id)
    vm = VM.get(caller_id, vm_id)

    if user.used_storage + vm.system_image.size > user.storage:
        raise CMException('user_storage_limit')

    VM.save_and_shutdown(caller_id, vm, name, description)
Esempio n. 45
0
File: vm.py Progetto: cloudcache/cc1
def save_and_shutdown(caller_id, vm_id, name, description):
    """
    Calls src.cm.views.utils.image.save_and_shutdown() for the VM selected.

    @cmview_user

    @parameter{vm_id,int} id of the VM to save and shutdown.
    @parameter{name,string}
    @parameter{description,string}
    """
    user = User.get(caller_id)
    vm = VM.get(caller_id, vm_id)

    if user.used_storage + vm.system_image.size > user.storage:
        raise CMException('user_storage_limit')

    VM.save_and_shutdown(caller_id, vm, name, description)
Esempio n. 46
0
def save_and_shutdown(caller_id, vm_id, name, description):
    """
    Saves and shutdowns specified VM, without checking User quota.

    @cmview_admin_cm
    @param_post{vm_id,string} id of the VM to save
    @param_post{name,string}
    @param_post{description,string}
    """

    vm = VM.admin_get(vm_id)
    user = User.get(vm.user.id)

    if user.used_storage + vm.system_image.size > user.storage:
        raise CMException('user_storage_limit')

    VM.save_and_shutdown(user.id, vm, name, description)
Esempio n. 47
0
def list_leases(caller_id, network_id):
    """
    Returns all Leases in specified UserNetwork

    @cmview_user
    @param_post{network_id} id of the UserNetwork which Leases should be listed
    from
    @response{list(dict)} Lease.dict property for each Lease in specified UserNetwork
    """
    user = User.get(caller_id)
    try:
        user_network = UserNetwork.objects.filter(user=user).get(id=network_id)
    except:
        raise CMException('network_not_found')
    response = []
    for lease in user_network.lease_set.all():
        response.append(lease.dict)
    return response
Esempio n. 48
0
def copy(caller_id, src_image_id, dest_user_id):
    """
    Copy selected StorageImage to user's StorageImages

    @cmview_admin_cm
    @param_post{src_image_id,int}
    @param_post{dest_user_id,int}
    """
    src_image = StorageImage.admin_get(src_image_id)
    dest_user = User.get(dest_user_id)
    dest_image = StorageImage.create(name=src_image.name, description=src_image.description, user=dest_user,
                                    disk_controller=src_image.disk_controller, size=src_image.size)

    try:
        dest_image.save()
    except Exception, e:
        log.error(caller_id, "Unable to commit: %s" % str(e))
        raise CMException('image_create')
Esempio n. 49
0
def unassign(caller_id, lease_id):
    """
    Method detaches PublicIP from caller's VM.

    @cmview_user
    @param_post{lease_id,int} id of the VM's Lease from which PublicIP should
    be detached.

    @raises{lease_not_found,CMException}
    @raises{public_lease_unassign,CMException}
    """
    user = User.get(caller_id)

    try:
        lease = Lease.objects.get(id=lease_id)
    except Exception, e:
        log.exception(caller_id, str(e))
        raise CMException("lease_not_found")
Esempio n. 50
0
def release(caller_id, network_id):
    """
    When UserNetwork isn't needed anymore, it should be explicitly released
    by User. If UserNetwork is in use, exception is thrown. Released
    UserNetwork is deleted.

    @cmview_user
    @param_post{network_id}
    """
    user = User.get(caller_id)
    try:
        user_network = UserNetwork.objects.filter(user=user).get(id=network_id)
    except:
        raise CMException('network_not_found')

    if user_network.is_in_use():
        raise CMException('network_in_use')
    user_network.release()
    user_network.delete()
Esempio n. 51
0
def release(caller_id, public_ip_id):
    """
    Removes PublicIP from caller's pool and returns it to publicly available
    pool, provided PublicIP isn't in use.

    @note There's very low probability of obtaining the same PublicIP address
    once again.

    @cmview_user
    @param_post{public_ip_id,int} id of the PublicIP to release
    """
    user = User.get(caller_id)
    public_lease = PublicIP.objects.filter(user=user).get(id=public_ip_id)

    if public_lease.lease:
        raise CMException('public_lease_assigned')

    public_lease.user = None
    public_lease.save()
Esempio n. 52
0
def download(caller_id, name, description, path, disk_controller):
    """
    Downloads specified IsoImage and saves it with specified name and description.

    @cmview_user
    @param_post{name,string}
    @param_post{description,string}
    @param_post{path,string} HTTP or FTP path to IsoImage to download
    @param_post{disk_controller}
    """
    user = User.get(caller_id)

    if not any([
            path.startswith('http://'),
            path.startswith('https://'),
            path.startswith('ftp://')
    ]):
        path = 'http://' + path.strip()

    # size value is taken
    try:
        connection = urllib.urlopen(path)
        size = int(connection.info()["Content-Length"])
    except IOError:
        log.exception('Cannot find image')
        raise CMException('image_not_found')
    except KeyError:
        log.exception(caller_id, 'Cannot calculate size')
        raise CMException('image_calculate_size')

    user.check_storage(size / (1024 * 1024))

    image = IsoImage.create(user=user,
                            description=description,
                            name=name,
                            disk_controller=disk_controller,
                            disk_dev=1)

    try:
        image.save()
    except Exception, e:
        log.error(caller_id, "Unable to save image to DB: %s" % str(e))
        raise CMException('image_create')
Esempio n. 53
0
def first_admin_add(caller_id, new_password, clm_address):
    """
    Creates first admin of the cluster. It should be called right after
    submiting the form for adding new CM. System will not operate properly
    with no CM admin existing.

    @note It can be run only if no CM admin exists in the CM database.

    @cmview_user
    @param_post{new_password,string} first *CM admin password* to set
    @param_post{clm_address,string}
    """
    user = User.create(1)
    user.save()

    # creates a new admin, which is the caller
    admin = Admin()
    admin.user = user
    admin.password = new_password

    try:
        admin.save()
    except:
        raise CMException('admin_add')

    # Update config and setup CLM address
    try:
        lines = []
        config = open('/usr/lib/cc1/cm/config.py', 'r')
        for line in config.readlines():
            if line.startswith('CLM_ADDRESS') and 'NOT_CONFIGURED' in line:
                lines.append('CLM_ADDRESS = "https://%s:8000/"\n' %
                             clm_address)
            else:
                lines.append(line)
        config.close()

        config = open('/usr/lib/cc1/cm/config.py', 'w')
        config.write(''.join(lines))
        config.close()
    except:
        log.exception(caller_id, 'config_update')
        raise CMException('config_update')
Esempio n. 54
0
def download(caller_id, description, name, path, disk_controller,
             network_device, platform, video_device):
    """
    Downloads specified SystemImage.

    @cmview_admin_cm
    @param_post{description,string}
    @param_post{name,string}
    @param_post{path,string} HTTP or FTP path to image to download
    @param_post{disk_controller}
    @param_post{network_device}
    @param_post{platform}
    @param_post{video_device}
    """

    # size value is taken
    try:
        connection = urllib.urlopen(path)
        size = int(connection.info()["Content-Length"])
    except IOError:
        log.exception(caller_id, 'Cannot find image')
        raise CMException('image_not_found')
    except KeyError:
        log.exception(caller_id, 'Cannot calculate size')
        raise CMException('image_calculate_size')

    user = User.get(caller_id)

    image = SystemImage.create(name=name,
                               description=description,
                               user=user,
                               platform=platform,
                               disk_controller=disk_controller,
                               network_device=network_device,
                               video_device=video_device)

    try:
        image.save()
    except Exception, e:
        log.error(caller_id, "Unable to save image to DB: %s" % str(e))
        raise CMException('image_create')
Esempio n. 55
0
def assign(caller_id, lease_id, public_ip_id):
    """
    Method attaches caller's PublicIP to his VM. VM's Lease instance's is
    assigned to PublicIP.

    @cmview_user
    @param_post{lease_id} id of the Lease in caller's UserNetwork
    @param_post{public_ip_id} id of the Public_IP to be attached to VM

    @raises{lease_not_found,CMException}
    @raises{lease_not_assigned,CMException}
    @raises{public_lease_assign,CMException}
    """
    user = User.get(caller_id)

    try:
        lease = Lease.objects.filter(user_network__user=user).get(id=lease_id)
        public_ip = PublicIP.objects.filter(user=user).get(id=public_ip_id)
    except Exception, e:
        log.exception(caller_id, str(e))
        raise CMException("lease_not_found")
Esempio n. 56
0
def copy(caller_id, src_image_id, dest_user_id):
    """
    Copy selected image to user's images

    @cmview_admin_cm
    @param_post{src_image_id,int}
    @param_post{dest_user_id,int}
    """
    src_image = SystemImage.admin_get(src_image_id)
    dest_user = User.get(dest_user_id)
    dest_image = SystemImage.create(name=src_image.name,
                                    description=src_image.description,
                                    user=dest_user,
                                    platform=src_image.platform,
                                    disk_controller=src_image.disk_controller,
                                    network_device=src_image.network_device,
                                    video_device=src_image.video_device)

    try:
        dest_image.save()
    except Exception, e:
        log.error(caller_id, "Unable to commit: %s" % str(e))
        raise CMException('image_create')
Esempio n. 57
0
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
    @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)} ISOs' ids
    @param_post{disk_list,list(int)}
    @param_post{vnc}
    @param_post{count}
    @param_post{groups}
    @param_post{user_data} data accessible via ec2ctx
    @param_post{ssh_key}
    @param_post{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]