Jump To …

JSTACK.Nova.js

/*
The MIT License

Copyright (c) 2012 Universidad Politecnica de Madrid

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/


JStack Nova Module



This module provides Nova API functions.

JSTACK.Nova = (function (JS, undefined) {
    "use strict";

    var params, configure, check, postAction, getserverlist, getserverdetail, getserverips,
        updateserver, createserver, deleteserver, changepasswordserver,
        rebootserverhard, rebootserversoft, resizeserver, confirmresizedserver,
        revertresizedserver, startserver, stopserver, pauseserver,
        unpauseserver, suspendserver, resumeserver, createimage, getflavorlist,
        getflavordetail, createflavor, deleteflavor, getimagelist,
        getimagedetail, deleteimage, getkeypairlist, createkeypair,
        deletekeypair, getvncconsole, getconsoleoutput, getattachedvolumes,
        attachvolume, detachvolume, getattachedvolume;


This modules stores the urlto which it will send every request.

    params = {
        url : undefined,
        state : undefined,
        endpointType : "publicURL"
    };


Private functions



Function _check internally confirms that Keystone module is authenticated and it has the URL of the Nova service.

    check = function () {
        if (JS.Keystone !== undefined &&
                JS.Keystone.params.currentstate === JS.Keystone.STATES.AUTHENTICATED) {
            var service = JS.Keystone.getservice("compute");
            params.url = service.endpoints[0][params.endpointType];
            return true;
        }
        return false;
    };

This function is used internally to send Actions to server identified with id. In data we pass the corresponding information about the action.

    postAction = function (id, data, query, callback) {
        var url, onOk, onError;

        if (!check()) {
            return;
        }

        url = params.url + '/servers/' + id + '/action';

        onOk = function (result) {
            if (callback !== undefined) {
                callback(result);
            }
        };
        onError = function (message) {
            throw new Error(message);
        };

        JS.Comm.post(url, data, JS.Keystone.params.token, onOk, onError);
    };

Public functions



This function sets the endpoint type for making requests to Glance. It could take one of the following values: * "adminURL" * "internalURL" * "publicURL" You can use this function to change the default endpointURL, which is publicURL.

    configure = function (endpointType) {
        if (endpointType === "adminURL" || endpointType === "internalURL" || endpointType === "publicURL") {
            params.endpointType = endpointType;
        }
    };



Server Operations



This operation provides a list of servers associated with the account. In Create Server List there is more information about the JSON object that is returned.

    getserverlist = function (detailed, allTenants, callback) {
        var url, onOK, onError;
        if (!check()) {
            return;
        }
        url = params.url + '/servers';
        if (detailed !== undefined && detailed) {
            url += '/detail';
        }

        if (allTenants) {
            url += '?all_tenants=' + allTenants;
        }

        onOK = function (result) {
            if (callback !== undefined) {
                callback(result);
            }
        };
        onError = function (message) {
            throw new Error(message);
        };

        JS.Comm.get(url, JS.Keystone.params.token, onOK, onError);
    };

This operation returns the details of a specific server by its id. In Get Server Details there is more information about the JSON object that is returned.

    getserverdetail = function (id, callback) {
        var url, onOK, onError;
        if (!check()) {
            return;
        }
        url = params.url + '/servers/' + id;

        onOK = function (result) {
            if (callback !== undefined) {
                callback(result);
            }
        };
        onError = function (message) {
            throw new Error(message);
        };

        JS.Comm.get(url, JS.Keystone.params.token, onOK, onError);
    };

This operation returns the ip address of a specific server by its id. In List Addresses and in List Addresses by Network there is more information about the JSON object that is returned.

    getserverips = function (id, networkID, callback) {
        var url, onOK, onError;
        if (!check()) {
            return;
        }
        url = params.url + '/servers/' + id + '/ips';

        if (networkID !== undefined) {
            url += '/' + networkID;
        }

        onOK = function (result) {
            if (callback !== undefined) {
                callback(result);
            }
        };
        onError = function (message) {
            throw new Error(message);
        };

        JS.Comm.get(url, JS.Keystone.params.token, onOK, onError);
    };

This operation updates the the name of the server given by its id. In Server Update there is more information about the JSON object that is returned.

    updateserver = function (id, name, callback) {
        var url, onOK, onError, data;
        if (!check()) {
            return;
        }
        url = params.url + '/servers/' + id;

        if (name === undefined) {
            return;
        }

        data = {
            "server" : {
                "name" : name
            }
        };

        onOK = function (result) {
            if (callback !== undefined) {
                callback(result);
            }
        };
        onError = function (message) {
            throw new Error(message);
        };

        JS.Comm.put(url, data, JS.Keystone.params.token, onOK, onError);
    };

This operation asynchronously provisions a new server. The progress of this operation depends on several factors including location of the requested image, network i/o, host load, and the selected flavor. The progress of the request can be checked by performing a getserverdetail, which will return a progress attribute (0-100% completion).

Arguments in this function are:

a. Mandatory

  • The name we want to put to the new server

  • The imageRef, that is the id of the image we will instantiate

  • The flavorReg, that is the id of the flavor we will use

b. Optional

  • The key_name that corresponds to the name of the key we will later use to access the instance by SSH (default is None)

  • Some raw data as user_data (default is None)

  • An array with names of the security_groups in which we want to put our instance (default is none)

  • The minimum number of instances to be started as min_count ( default is 1)

  • The maximum number of instances as max_count (default is 1)

  • And the availability_zone (default is None)

In Create Servers there is more information about the JSON object that is returned.

    createserver = function (name, imageRef, flavorRef, key_name, user_data, security_groups, min_count, max_count, availability_zone, callback) {
        var url, onOK, onError, data, groups = [], i, group;
        if (!check()) {
            return;
        }

        data = {
            "server" : {
                "name" : name,
                "imageRef" : imageRef,
                "flavorRef" : flavorRef
            }
        };

        if (key_name !== undefined) {
            data.server.key_name = key_name;
        }

        if (user_data !== undefined) {
            data.server.user_data = JS.Utils.encode(user_data);
        }

        if (security_groups !== undefined) {
            for (i in security_groups) {
                if (security_groups[i] !== undefined) {
                    group = {
                        "name" : security_groups[i]
                    };
                    groups.push(group);
                }
            }

            data.server.security_groups = groups;
        }

        if (min_count === undefined) {
            min_count = 1;
        }

        data.server.min_count = min_count;

        if (max_count === undefined) {
            max_count = 1;
        }

        data.server.max_count = max_count;

        if (availability_zone !== undefined) {
            data.server.availability_zone = JS.Utils.encode(availability_zone);
        }

        onOK = function (result) {
            if (callback !== undefined) {
                callback(result);
            }
        };
        onError = function (message) {
            throw new Error(message);
        };

        JS.Comm.post(params.url + '/servers', data, JS.Keystone.params.token, onOK, onError);

    };

This operation deletes a cloud server instance from the system. In Delete Server there is more information.

    deleteserver = function (id, callback) {
        var url, onOK, onError;
        if (!check()) {
            return;
        }
        url = params.url + '/servers/' + id;

        onOK = function (result) {
            if (callback !== undefined) {
                callback(result);
            }
        };
        onError = function (message) {
            throw new Error(message);
        };

        JS.Comm.del(url, JS.Keystone.params.token, onOK, onError);
    };

Server Actions

Take a look at postAction for detailed information about requests.



This operation changes the server's administrator password. In Change Password there is more information.

    changepasswordserver = function (id, adminPass, callback) {
        var data;
        if (adminPass === undefined) {
            return;
        }

        data = {
            "changePassword" : {
                "adminPass" : adminPass
            }
        };

        postAction(id, data, callback);
    };

This operation allows for a hard reboot that is the equivalent of power cycling the server.

    rebootserverhard = function (id, callback) {
        postAction(id, {
            "reboot" : {
                "type" : "HARD"
            }
        }, callback);
    };

This operation allows for a soft reboot, which allows for a graceful shutdown of all processes. In Reboot Server there is more information about hard and soft reboots.

    rebootserversoft = function (id, callback) {
        postAction(id, {
            "reboot" : {
                "type" : "SOFT"
            }
        }, callback);
    };

The resize function converts an existing server to a different flavor, in essence, scaling the server up or down. The original server is saved for a period of time to allow rollback if there is a problem. All resizes should be tested and explicitly confirmed with confirmresizedserver, at which time the original server is removed. All resizes are automatically confirmed after 24 hours if they are not explicitly confirmed or reverted. In Resize Server there is more information.

    resizeserver = function (id, flavorRef, callback) {
        postAction(id, {
            "resize" : {
                "flavorRef" : flavorRef
            }
        }, callback);
    };

During a resize operation, the original server is saved for a period of time to allow roll back if there is a problem. Once the newly resized server is tested and has been confirmed to be functioning properly, use this operation to confirm the resize. After confirmation, the original server is removed and cannot be rolled back to. All resizes are automatically confirmed after 24 hours if they are not explicitly confirmed or reverted. In Confirm Resized Server there is more information.

    confirmresizedserver = function (id, callback) {
        postAction(id, {
            "confirmResize" : null
        }, callback);
    };

In Revert Resized Server there is more information.

    revertresizedserver = function (id, callback) {
        postAction(id, {
            "revertResize" : null
        }, callback);
    };

It halts a running server. Changes status to STOPPED. In Start Server there is more information.

    startserver = function (id, callback) {
        postAction(id, {
            "os-start" : null
        }, callback);
    };

Returns a STOPPED server to ACTIVE status. In Stop Server there is more information.

    stopserver = function (id, callback) {
        postAction(id, {
            "os-stop" : null
        }, callback);
    };

It pauses a running server. Changes status to PAUSED.

    pauseserver = function (id, callback) {
        postAction(id, {
            "pause" : null
        }, callback);
    };

Returns a PAUSED server to ACTIVE status.

    unpauseserver = function (id, callback) {
        postAction(id, {
            "unpause" : null
        }, callback);
    };

It pauses a running server. Changes status to SUSPENDED.

    suspendserver = function (id, callback) {
        postAction(id, {
            "suspend" : null
        }, callback);
    };

Returns a SUSPENDED server to ACTIVE status.

    resumeserver = function (id, callback) {
        postAction(id, {
            "resume" : null
        }, callback);
    };

This action creates a new image for the given server. Once complete, a new image will be available that can be used to rebuild or create servers. In Create Image there is more information.

    createimage = function (id, name, metadata, callback) {
        var data = {
            "createImage" : {
                'name' : name
            }
        };

        data.createImage.metadata = {};

        if (metadata !== undefined) {
            data.createImage.metadata = metadata;
        }

        postAction(id, data, callback);
    };

Flavor Operations



This operation will list all available flavors. In List Flavors there is more information.

    getflavorlist = function (detailed, callback) {
        var url, onOK, onError;
        if (!check()) {
            return;
        }
        url = params.url + '/flavors';
        if (detailed !== undefined && detailed) {
            url += '/detail';
        }

        onOK = function (result) {
            if (callback !== undefined) {
                callback(result);
            }
        };
        onError = function (message) {
            throw new Error(message);
        };
        JS.Comm.get(url, JS.Keystone.params.token, onOK, onError);
    };

This operation returns details of the specified flavor. In Get Flavor Details there is more information.

    getflavordetail = function (id, callback) {
        var url, onOK, onError;
        if (!check()) {
            return;
        }
        url = params.url + '/flavors/' + id;

        onOK = function (result) {
            if (callback !== undefined) {
                callback(result);
            }
        };
        onError = function (message) {
            throw new Error(message);
        };
        JS.Comm.get(url, JS.Keystone.params.token, onOK, onError);
    };

This operation creates a new flavor, using information given in arguments: the name of the new flavor, the number of MB of ram, the id of the new flavor, the number of GB of root disk, the number of GB of ephemeral disk, the number of MB of swap space, and the rxtx_factor. Arguments ephemeral, swap, rxtx_factor and callback are optional.

    createflavor = function (name, ram, vcpus, disk, flavorid, ephemeral, swap, rxtx_factor, callback) {
        var url, onOK, onError, data;
        if (!check()) {
            return;
        }
        url = params.url + '/flavors';
        data = {
            "flavor" : {
                "name" : name,
                "ram" : ram,
                "vcpus" : vcpus,
                "disk" : disk,
                "id" : flavorid,
                "swap" : 0,
                "OS-FLV-EXT-DATA:ephemeral" : 0,
                "rxtx_factor" : 0
            }
        };

        if (ephemeral !== undefined) {
            data.flavor["OS-FLV-EXT-DATA:ephemeral"] = ephemeral;
        }

        if (swap !== undefined) {
            data.flavor.swap = swap;
        }

        if (rxtx_factor !== undefined) {
            data.flavor.rxtx_factor = rxtx_factor;
        }

        onOK = function (result) {
            if (callback !== undefined) {
                callback(result);
            }
        };
        onError = function (message) {
            throw new Error(message);
        };
        JS.Comm.post(url, data, JS.Keystone.params.token, onOK, onError);
    };

This operation deletes flavor, specified by its id. In Get Flavor Details there is more information.

    deleteflavor = function (id, callback) {
        var url, onOK, onError;
        if (!check()) {
            return;
        }
        url = params.url + '/flavors/' + id;

        onOK = function (result) {
            if (callback !== undefined) {
                callback(result);
            }
        };
        onError = function (message) {
            throw new Error(message);
        };

        JS.Comm.del(url, JS.Keystone.params.token, onOK, onError);
    };

Image Operations



This operation will list all images visible by the account. In-flight images will have the status attribute set to SAVING and the conditional progress element (0-100% completion) will also be returned. Other possible values for the status attribute include: UNKNOWN, ACTIVE, SAVING, ERROR, and DELETED. Images with an ACTIVE status are available for install. In List Images there is more information.

    getimagelist = function (detailed, callback) {
        var url, onOK, onError;
        if (!check()) {
            return;
        }
        url = params.url + '/images';
        if (detailed !== undefined && detailed) {
            url += '/detail';
        }

        onOK = function (result) {
            if (callback !== undefined) {
                callback(result);
            }
        };
        onError = function (message) {
            throw new Error(message);
        };

        JS.Comm.get(url, JS.Keystone.params.token, onOK, onError);
    };

This operation returns details of the image specified by its id. In Get Image Details there is more information.

    getimagedetail = function (id, callback) {
        var url, onOK, onError;
        if (!check()) {
            return;
        }
        url = params.url + '/images/' + id;

        onOK = function (result) {
            if (callback !== undefined) {
                callback(result);
            }
        };
        onError = function (message) {
            throw new Error(message);
        };
        JS.Comm.get(url, JS.Keystone.params.token, onOK, onError);
    };

This operation deletes an image from the system, specified by its id. Images are immediately removed. Currently, there are no state transitions to track the delete operation. In Delete Image there is more information.

    deleteimage = function (id, callback) {
        var url, onOK, onError;
        if (!check()) {
            return;
        }
        url = params.url + '/images/' + id;

        onOK = function (result) {
            if (callback !== undefined) {
                callback(result);
            }
        };
        onError = function (message) {
            throw new Error(message);
        };
        JS.Comm.del(url, JS.Keystone.params.token, onOK, onError);
    };

This operation retrieves a list of available Key-pairs.

    getkeypairlist = function (callback) {
        var url, onOK, onError;
        if (!check()) {
            return;
        }
        url = params.url + '/os-keypairs';

        onOK = function (result) {
            if (callback !== undefined) {
                callback(result);
            }
        };
        onError = function (message) {
            throw new Error(message);
        };

        JS.Comm.get(url, JS.Keystone.params.token, onOK, onError);
    };

This operation creates a new Key-pair.

    createkeypair = function (name, pubkey, callback) {
        var url, onOK, onError, body;
        if (!check()) {
            return;
        }
        url = params.url + '/os-keypairs';

        onOK = function (result) {
            if (callback !== undefined) {
                callback(result);
            }
        };
        onError = function (message) {
            throw new Error(message);
        };
        body = {
            'keypair' : {
                'name' : name
            }
        };

        if (pubkey !== undefined) {
            body.keypair.public_key = pubkey;

        }

        JS.Comm.post(url, body, JS.Keystone.params.token, onOK, onError);
    };

This operation retrieves a list of available Key-pairs.

    deletekeypair = function (id, callback) {
        var url, onOK, onError;
        if (!check()) {
            return;
        }
        url = params.url + '/os-keypairs/' + id;

        onOK = function (result) {
            if (callback !== undefined) {
                callback(result);
            }
        };
        onError = function (message) {
            throw new Error(message);
        };

        JS.Comm.del(url, JS.Keystone.params.token, onOK, onError);
    };

Get a vnc console for an instance id: The server's ID to get the vnc console from. console_type: Type of vnc console to get ('novnc' or 'xvpvnc')

    getvncconsole = function (id, console_type, callback) {
        var data;
        if (!check()) {
            return;
        }

        if (console_type === undefined || !console_type) {
            console_type = "novnc";
        }

        data = {
            "os-getVNCConsole" : {
                'type' : console_type
            }
        };

        postAction(id, data, null, callback);
    };

Get text console log output from Server. id: The server's ID to get the vnc console from. length: The number of tail loglines you would like to retrieve.

    getconsoleoutput = function (id, length, callback) {
        var data;
        if (!check()) {
            return;
        }

        if (length === undefined || !length) {
            length = 35;
        }

        data = {
            "os-getConsoleOutput" : {
                'length' : length
            }
        };

        postAction(id, data, null, callback);
    };

Lists the volume attachments for the specified server. id: The server's ID to get the volume attachments from.

    getattachedvolumes = function (id, callback) {
        var url, onOK, onError;
        if (!check()) {
            return;
        }
        url = params.url + '/servers/' + id + '/os-volume_attachments';

        onOK = function (result) {
            if (callback !== undefined) {
                callback(result);
            }
        };
        onError = function (message) {
            throw new Error(message);
        };

        JS.Comm.get(url, JS.Keystone.params.token, onOK, onError);
    };

Attaches a volume to the specified server. id: The server's ID. volume_id: The volume's ID to be attached to the server. device: The device where we want to attach this volume.

    attachvolume = function (id, volume_id, device, callback) {
        var url, onOK, onError, data;
        if (!check()) {
            return;
        }

        url = params.url + '/servers/' + id + '/os-volume_attachments';

        if (volume_id === undefined || device === undefined) {
            return;
        }

        data = {
            'volumeAttachment' : {
                'volumeId' : volume_id,
                'device' : device
            }
        };

        onOK = function (result) {
            if (callback !== undefined) {
                callback(result);
            }
        };
        onError = function (message) {
            throw new Error(message);
        };

        JS.Comm.post(url, data, JS.Keystone.params.token, onOK, onError);

    };

Deletes the specified volume attachment from the specified server. id: The server's ID. volume_id: The volume's ID to be detached from the server.

    detachvolume = function (id, volume_id, callback) {
        var url, onOK, onError;
        if (!check()) {
            return;
        }

        url = params.url + '/servers/' + id + '/os-volume_attachments/' + volume_id;

        if (volume_id === undefined) {
            return;
        }

        onOK = function (result) {
            if (callback !== undefined) {
                callback(result);
            }
        };
        onError = function (message) {
            throw new Error(message);
        };

        JS.Comm.del(url, JS.Keystone.params.token, onOK, onError);

    };

Lists volume details for the specified volume attachment ID. id: The server's ID. volume_id: The volume's ID.

    getattachedvolume = function (id, volume_id, callback) {
        var url, onOK, onError;
        if (!check()) {
            return;
        }

        url = params.url + '/servers/' + id + '/os-volume_attachments/' + volume_id;

        if (volume_id === undefined) {
            return;
        }

        onOK = function (result) {
            if (callback !== undefined) {
                callback(result);
            }
        };
        onError = function (message) {
            throw new Error(message);
        };

        JS.Comm.get(url, JS.Keystone.params.token, onOK, onError);

    };

Public Functions and Variables

This is the list of available public functions and variables

    return {


Functions:

        configure : configure,
        getserverlist : getserverlist,
        getserverdetail : getserverdetail,
        getserverips : getserverips,
        updateserver : updateserver,
        createserver : createserver,
        deleteserver : deleteserver,
        changepasswordserver : changepasswordserver,
        rebootserverhard : rebootserverhard,
        rebootserversoft : rebootserversoft,
        resizeserver : resizeserver,
        confirmresizedserver : confirmresizedserver,
        revertresizedserver : revertresizedserver,
        startserver : startserver,
        stopserver : stopserver,
        pauseserver : pauseserver,
        unpauseserver : unpauseserver,
        suspendserver : suspendserver,
        resumeserver : resumeserver,
        createimage : createimage,
        getflavorlist : getflavorlist,
        getflavordetail : getflavordetail,
        createflavor : createflavor,
        deleteflavor : deleteflavor,
        getimagelist : getimagelist,
        getimagedetail : getimagedetail,
        deleteimage : deleteimage,
        getkeypairlist : getkeypairlist,
        createkeypair : createkeypair,
        deletekeypair : deletekeypair,
        getvncconsole : getvncconsole,
        getconsoleoutput : getconsoleoutput,
        getattachedvolumes : getattachedvolumes,
        attachvolume : attachvolume,
        detachvolume : detachvolume,
        getattachedvolume : getattachedvolume
    };

}(JSTACK));