Esempio n. 1
0
def _get_placement_inner(request):
    """
    Perform the actual work of figuring out the possible cluster placement.

    .. seealso::

     See :ref:`get_placements`.
    """
    if request.is_ajax():
        if request.method == 'POST':
            cluster_name = request.POST.get('cluster_name', '')
            cloud_id = request.POST.get('cloud_id', '')
            a_key = request.POST.get('a_key', '')
            s_key = request.POST.get('s_key', '')
            inst_type = request.POST.get('instance_type', '')
            placements = []
            if cloud_id != '' and a_key != '' and s_key != '':
                # Needed to get the cloud connection
                cloud = models.Cloud.objects.get(pk=cloud_id)
                cml = CloudManLauncher(a_key, s_key, cloud)
                placements = cml.find_placements(cml.ec2_conn, inst_type,
                                                 cloud.cloud_type, cluster_name)
                return {'placements': placements}
        else:
            log.error("Not a POST request")
    else:
        log.error("No XHR")
    return {"error": "Please specify access and secret keys", "placements": []}
Esempio n. 2
0
    def launch_instance(cfg, **kwargs):
        """
        Launches a new instance of CloudMan on the specified cloud infrastructure.

        :type cfg: CloudManConfig
        :param cfg: A CloudManConfig object containing the initial parameters
                    for this launch.
        """
        validation_result = cfg.validate()
        if validation_result is not None:
            raise VMLaunchException(
                "Invalid CloudMan configuration provided: {0}".format(
                    validation_result))
        launcher = CloudManLauncher(cfg.access_key, cfg.secret_key,
                                    cfg.cloud_metadata)
        result = launcher.launch(cfg.cluster_name, cfg.image_id,
                                 cfg.instance_type, cfg.password,
                                 cfg.kernel_id, cfg.ramdisk_id, cfg.key_name,
                                 cfg.security_groups, cfg.placement,
                                 **cfg.kwargs)
        if result['error'] is not None:
            raise VMLaunchException(
                "Error launching cloudman instance: {0}".format(
                    result['error']))
        instance = CloudManInstance(None,
                                    None,
                                    launcher=launcher,
                                    launch_result=result,
                                    cloudman_config=cfg)
        if cfg.block_until_ready and cfg.cluster_type:
            instance.get_status(
            )  # this will indirect result in initialize being invoked
        return instance
Esempio n. 3
0
def get_key_pairs(request):
    """
    Retrieve a list of key pairs available in the user's account on the given cloud.
    """
    response = {}
    if request.is_ajax():
        if request.method == 'POST':
            cloud_id = request.POST.get('cloud_id', '')
            a_key = request.POST.get('a_key', '')
            s_key = request.POST.get('s_key', '')
            key_pairs = []
            if a_key != '' and s_key != '':
                # Needed to get the cloud connection
                cloud = models.Cloud.objects.get(pk=cloud_id)
                cml = CloudManLauncher(a_key, s_key, cloud)
                kps = cml.ec2_conn.get_all_key_pairs()
                key_pairs = []
                for kp in kps:
                    key_pairs.append(kp.name)
                response = {'key_pairs': key_pairs}
        else:
            response = {"error": "Not a POST request", "key_pairs": []}
            log.error("Not a POST request")
    else:
        response = {"error": "Not an AJAX request", "key_pairs": []}
        log.error("No XHR")
    if not response:
        response = {"error": "Please specify access and secret keys", "key_pairs": []}
    return HttpResponse(simplejson.dumps(response), mimetype="application/json")
Esempio n. 4
0
def fetch_clusters(cloud, a_key, s_key):
    """
    Given a cloud object and appropriate credentials, retrieve a list of
    clusters associated with the given account. Return a dict of clusters'
    persistent data.
    """
    cml = CloudManLauncher(a_key, s_key, cloud)
    return cml.get_clusters_pd()
Esempio n. 5
0
def instance_state(cloud, a_key, s_key, instance_id):
    """
    Check on the state of an instance until and return the state.
    """
    if not instance_id:
        state = {'instance_state': "",
                 'public_ip': "",
                 'placement': "",
                 'error': "Missing instance ID, cannot check the state."}
        return state
    cml = CloudManLauncher(a_key, s_key, cloud)
    return cml.get_status(instance_id)
Esempio n. 6
0
    def get_saved_clusters(self, obj):
        """
        Fetch a list of saved CloudMan clusters from AWS.

        This only fetches saved clusters that used AWS since it appears that
        was the only place this feature was actively used.
        """
        provider = view_helpers.get_cloud_provider(self.context.get('view'))
        if provider.cloud_type != 'aws':
            return []
        # Since we're only working with the AWS, there's no need to specify
        # the cloud argument as it defaults to AWS in BioBlend.
        cml = CloudManLauncher(provider.a_key, provider.s_key, None)
        return cml.get_clusters_pd().get('clusters', [])
Esempio n. 7
0
def userdata(request):
    """
    Provide file download of user-data to enable re-start an instance from
    cloud's console or the API.
    """
    ec2data = request.session["ec2data"]
    response = HttpResponse(mimetype='text/plain')
    response['Content-Disposition'] = 'attachment; filename={cluster_name}-userdata.txt'.format(
        **ec2data)
    form = request.session["ec2data"]
    cml = CloudManLauncher(form["access_key"], form["secret_key"], form['cloud'])
    ud = cml._compose_user_data(ec2data)
    response.write(ud)
    return response
Esempio n. 8
0
def _get_placement_inner(request):
    if request.is_ajax():
        if request.method == 'POST':
            cloud_id = request.POST.get('cloud_id', '')
            a_key = request.POST.get('a_key', '')
            s_key = request.POST.get('s_key', '')
            inst_type = request.POST.get('instance_type', '')
            placements = []
            if cloud_id != '' and a_key != '' and s_key != '':
                # Needed to get the cloud connection
                cloud = models.Cloud.objects.get(pk=cloud_id)
                cml = CloudManLauncher(a_key, s_key, cloud)
                placements = cml._find_placements(cml.ec2_conn, inst_type,
                                                  cloud.cloud_type)
                return {'placements': placements}
        else:
            log.error("Not a POST request")
    else:
        log.error("No XHR")
    return {"error": "Please specify access and secret keys", "placements": []}
Esempio n. 9
0
def instance_state(cloud, a_key, s_key, instance_id):
    """
    Check on the state of an instance until and return the state.
    """
    # Dev code
    # if instance_id == 'i-l0cal':
    #     state = {'instance_state': "ready",
    #              'public_ip': "127.0.0.1:42284",
    #              'placement': "desktop",
    #              'error': ""}
    #     return state
    # End dev code
    if not instance_id:
        state = {
            'instance_state': "",
            'public_ip': "",
            'placement': "",
            'error': "Missing instance ID, cannot check the state."
        }
        return state
    cml = CloudManLauncher(a_key, s_key, cloud)
    return cml.get_status(instance_id)
Esempio n. 10
0
def get_subnets(request):
    """
    Fetch all subnets available under the supplied account.
    """
    response = {}
    if request.is_ajax():
        if request.method == 'POST':
            cloud_id = request.POST.get('cloud_id', '')
            a_key = request.POST.get('a_key', '')
            s_key = request.POST.get('s_key', '')
            subnets = []
            if a_key != '' and s_key != '':
                # Needed to get the cloud connection
                cloud = models.Cloud.objects.get(pk=cloud_id)
                cml = CloudManLauncher(a_key, s_key, cloud)
                snl = cml.vpc_conn.get_all_subnets()
                for sn in snl:
                    subnets.append({
                        'id': sn.id,
                        'name': sn.tags.get('Name'),
                        'vpc_id': sn.vpc_id
                    })
                response = {'subnets': subnets}
        else:
            response = {"error": "Not a POST request", "subnets": []}
            log.error("Not a POST request")
    else:
        response = {"error": "Not an AJAX request", "subnets": []}
        log.error("No XHR")
    if not response:
        response = {
            "error": "Please specify access and secret keys",
            "subnets": []
        }
    return HttpResponse(simplejson.dumps(response),
                        mimetype="application/json")
Esempio n. 11
0
                "Couldn't set the storage size for the filesystem. Reason: {0}. Ignoring... "
                .format(e))

    # Handle extra_user_data after flavor data so that flavor data can be overridden
    extra_user_data = form['extra_user_data']
    if extra_user_data:
        for key, value in yaml.load(extra_user_data).iteritems():
            form[key] = value
    del form['extra_user_data']

    instance_type = (form['custom_instance_type'] if
                     form['custom_instance_type'] else form['instance_type'])
    ebs_optimized = True if form.get('ebs_optimized', None) == 'on' else False
    subnet_id = form.get('subnet_id') if form.get('subnet_id') else None
    # Create cloudman connection with provided creds
    cml = CloudManLauncher(form["access_key"], form["secret_key"],
                           form['cloud'])
    form["freenxpass"] = form["password"]

    # Compose kwargs from form data making sure the named arguments are not included
    kwargs = copy.deepcopy(form)
    # key_name is the parameter name for the key pair in the launch method so
    # ensure it's there as a kwarg if provided in the form
    if form.get('key_pair', None):
        kwargs['key_name'] = form['key_pair']
    for key in form.iterkeys():
        if key in [
                'cluster_name', 'image_id', 'instance_type', 'password',
                'placement', 'access_key', 'secret_key', 'cloud', 'key_pair',
                'ebs_optimized', 'subnet_id'
        ]:
            del kwargs[key]
from bioblend.util import Bunch
from bioblend.cloudman.launch import CloudManLauncher

import logging

logging.basicConfig(filename="bioblend.log", level=logging.DEBUG)

cloud = Bunch(id='1',
              name='climb',
              cloud_type="openstack",
              bucket_default="cloudman_os",
              region_name="nova",
              region_endpoint="147.188.173.10",
              ec2_port="8773",
              ec2_conn_path="/services/Cloud",
              cidr_range="147.188.173.0/24",
              is_secure=False,
              s3_host="swift.rc.nectar.org.au",
              s3_port="8888",
              s3_conn_path='/')

cml = CloudManLauncher('ACCESS_KEY', 'SECRET_KEY', cloud)

response = cml.launch(cluster_name='test',
                      image_id='ami-00000039',
                      instance_type='m1.large',
                      password='******',
                      placement='nova')

print response
Esempio n. 13
0
def runinstance(request):
    """Run a CloudBioLinux/CloudMan instance with current session credentials.
    """
    form = request.session["ec2data"]

    # Handle extra_user_data
    extra_user_data = form['extra_user_data']
    if extra_user_data:
        for key, value in yaml.load(extra_user_data).iteritems():
            form[key] = value
    del form['extra_user_data']

    rs = None
    instance_type = form['instance_type']
    # Create cloudman connection with provided creds
    cml = CloudManLauncher(form["access_key"], form["secret_key"],
                           form['cloud'])
    form["freenxpass"] = form["password"]
    if form['image_id']:
        image = models.Image.objects.get(pk=form['image_id'])
    else:
        try:
            image = models.Image.objects.get(cloud=form['cloud'], default=True)
        except models.Image.DoesNotExist:
            log.error("Cannot find an image to launch for cloud {0}".format(
                form['cloud']))
            return False
    # Compose kwargs from form data making sure the named arguments are not included
    kwargs = copy.deepcopy(form)
    for key in form.iterkeys():
        if key in [
                'cluster_name', 'image_id', 'instance_type', 'password',
                'placement', 'access_key', 'secret_key', 'cloud'
        ]:
            del kwargs[key]
    response = cml.launch(
        cluster_name=form['cluster_name'],
        image_id=image.image_id,
        instance_type=instance_type,
        password=form["password"],
        kernel_id=image.kernel_id if image.kernel_id != '' else None,
        ramdisk_id=image.ramdisk_id if image.ramdisk_id != '' else None,
        placement=form['placement'],
        **kwargs)
    if response["error"]:
        return response
    elif response["rs"]:
        rs = response["rs"]
        request.session['ec2data']['instance_id'] = rs.instances[0].id
        request.session['ec2data']['public_ip'] = rs.instances[
            0].ip_address  # public_dns_name
        request.session['ec2data']['image_id'] = rs.instances[0].image_id
        request.session['ec2data']['kp_name'] = response['kp_name']
        request.session['ec2data']['kp_material'] = response['kp_material']
        request.session['ec2data']['sg_name'] = response['sg_names'][0]

        # Add an entry to the Usage table
        try:
            u = models.Usage(cloud_name=form["cloud_name"],
                             cloud_type=form["cloud_type"],
                             image_id=image.image_id,
                             instance_type=instance_type,
                             user_id=form["access_key"])
            u.save()
        except Exception, e:
            log.debug("Trouble saving Usage data: {0}".format(e))
Esempio n. 14
0
def instancestate(request):
    form = request.session["ec2data"]
    cml = CloudManLauncher(form["access_key"], form["secret_key"],
                           form['cloud'])
    state = cml.get_status(form["instance_id"])
    return HttpResponse(simplejson.dumps(state), mimetype="application/json")