예제 #1
0
def connect_svm(in_args):

    # type and syntax checking on command line args
    obj_regexp = re.compile(
        r'^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$|^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9](\.[a-zA-Z0-9\-]{2,}){0,6}$'
    )
    if (not (obj_regexp.match(in_args.svm))):
        sys.exit(
            "error: the svm parameter must be either an IP address or a hostname"
        )

    # connect to svm
    obj_svm = NaServer(in_args.svm, 1, 3)
    obj_svm.set_transport_type('HTTPS')
    if (in_args.cert is None):
        obj_svm.set_style('LOGIN')
        string_username = raw_input("login as: ")
        string_password = getpass.getpass()
        obj_svm.set_admin_user(string_username, string_password)
    else:
        result = obj_svm.set_style('CERTIFICATE')
        obj_svm.set_server_cert_verification(0)
        obj_svm.set_client_cert_and_key(in_args.cert[0], in_args.cert[1])

    # check that the api is reachable
    result = obj_svm.invoke("system-get-version")
    if (result.results_status() == "failed"):
        sys.exit("error: cannot connect to filer; " + result.results_reason())

    return obj_svm
예제 #2
0
    pass
else:
    ssl._create_default_https_context = _create_unverified_https_context

with open('config.yml') as stream:
    config = yaml.safe_load(stream)

try:
    s = NaServer(config['filer'], 1, 110)
    s.set_server_type("FILER")
    s.set_transport_type("HTTPS")
    s.set_port(443)
    s.set_style("LOGIN")
    s.set_admin_user(config['username'], config['password'])
    #s.set_vserver(config['vserver'])
    s.set_server_cert_verification("False")
    s.set_hostname_verification("False")

    api = NaElement("lun-get-iter")

    xi = NaElement("desired-attributes")
    api.child_add(xi)

    xi1 = NaElement("lun-info")
    xi.child_add(xi1)

    xi1.child_add_string("path", "<path>")
    xi1.child_add_string("volume", "<volume>")
    xi1.child_add_string("multiprotocol-type", "<multiprotocol-type>")
    xi1.child_add_string("mapped", "<mapped>")
예제 #3
0
파일: apitest.py 프로젝트: raykuan/docs
if(host_equiv == 1) :
    s.set_style("HOSTS")


if (use_cba == 1) :
    response = s.set_style("CERTIFICATE")
    if (response):
        print("Unable to set style: " + response.results_reason() + "\n")
        sys.exit(2)
    response = s.set_client_cert_and_key(cert_file, client_key)
    if (response):
        print(response.results_reason() + "\n")
        sys.exit(2)

if (dossl or need_server_cert_verification):
    response = s.set_server_cert_verification(need_server_cert_verification)
    if (response):
        print(response.results_reason() + "\n")
        sys.exit(2)
    if (need_server_cert_verification):
        response = s.set_hostname_verification(need_hostname_verification)
        if (response):
            print(response.results_reason() + "\n")
            sys.exit(2)

if (ca_file):
    response = s.set_ca_certs(ca_file)
    if (response):
        print(response.results_reason() + "\n")
        sys.exit(2)
예제 #4
0
class Netapp(SnapHandler):
    _exceptionbase = "netapp"
    _blocksize = 1024
    _filer = None
    _cacert = None

    def __init__(self, configname):
        zfscredfilename = os.path.join(scriptpath(), 'netappcredentials.cfg')
        if not os.path.isfile(zfscredfilename):
            raise Exception(
                self._exceptionbase,
                "Configuration file %s not found" % zfscredfilename)
        # Authentication information
        zfscredconfig = SafeConfigParser()
        zfscredconfig.read(zfscredfilename)
        zfsauth = (zfscredconfig.get('netappcredentials', 'user'),
                   zfscredconfig.get('netappcredentials', 'password'))
        #
        self._filer = Configuration.get('filer', 'netapp')
        self._srv = NaServer(self._filer, 1, 1)
        # Check if CA certificate validation is needed
        try:
            self._cacert = os.path.join(scriptpath(), 'certs',
                                        Configuration.get('cacert', 'netapp'))
        except NoOptionError:
            self._cacert = None
        if self._cacert:
            self._srv.set_ca_certs(self._cacert)
            self._srv.set_server_cert_verification(True)
            self._srv.set_hostname_verification(False)
        #
        self._srv.set_admin_user(
            zfscredconfig.get('netappcredentials', 'user'),
            zfscredconfig.get('netappcredentials', 'password'))
        self._volprefix = Configuration.get('volumeprefix', 'netapp')
        self._volname = "%s%s" % (self._volprefix, configname)
        super(Netapp, self).__init__(configname)

    def _check_netapp_error(self, output, errmsg):
        if output.results_errno() != 0:
            raise Exception(self._exceptionbase,
                            "%s. %s" % (errmsg, output.results_reason()))

    def _volsize_to_num(self, volsize):
        unit = volsize[-1].lower()
        factor = 1
        if unit == "m":
            factor = 20
        elif unit == "g":
            factor = 30
        elif unit == "t":
            factor = 40
        elif unit == "p":
            factor = 50
        return round(float(volsize[:-1]) * (2**factor))

    # Public interfaces
    def filesystem_info(self, filesystemname=None):
        info = {}
        output = self._srv.invoke("volume-clone-get", "volume", filesystemname)
        self._check_netapp_error(output, "Getting clone info failed")
        attrlist = output.child_get("attributes")
        if (attrlist is None or attrlist == ""):
            raise Exception(self._exceptionbase,
                            "No attributes found for clone.")
        for ss in attrlist.children_get():
            info['origin'] = ss.child_get_string('parent-volume')
            info['clonename'] = filesystemname
            info['mountpoint'] = ss.child_get_string('junction-path')
        return info

    def listclones(self):
        elem = NaElement("volume-clone-get-iter")
        elem.child_add_string("max-records", "50")
        query = NaElement("query")
        query.child_add_string("parent-volume", self._volname)
        elem.child_add(query)
        #
        output = self._srv.invoke_elem(elem)
        self._check_netapp_error(output, "List clones failed")
        attrlist = output.child_get("attributes-list")
        if (attrlist is not None and attrlist):
            for ss in attrlist.children_get():
                info = {}
                info['origin'] = ss.child_get_string('parent-volume')
                info['clonename'] = ss.child_get_string('volume')
                info['mountpoint'] = ss.child_get_string('junction-path')
                yield info

    def mountstring(self, filesystemname):
        info = self.filesystem_info(filesystemname)
        return "%s:%s" % (self._filer, info['mountpoint'])

    def snap(self):
        snapname = "%s_%s" % (self._volname,
                              datetime.now().strftime('%Y%m%dT%H%M%S'))
        output = self._srv.invoke("snapshot-create", "volume", self._volname,
                                  "snapshot", snapname)
        self._check_netapp_error(output, "Creating snapshot failed")
        return snapname

    def dropsnap(self, snapid):
        output = self._srv.invoke("snapshot-delete", "volume", self._volname,
                                  "snapshot", snapid)
        self._check_netapp_error(output, "Failed to drop snapshot %s" % snapid)

    def getsnapinfo(self, snapstruct):
        return snapstruct

    def listsnapshots(self, sortbycreation=False, sortreverse=False):
        output = self._srv.invoke("volume-size", "volume", self._volname)
        self._check_netapp_error(output,
                                 "Failed to get volume size information")
        volsize = self._volsize_to_num(output.child_get_string("volume-size"))
        pct_limit = round(2147483648 * 100 / (volsize / self._blocksize))
        output = self._srv.invoke("snapshot-list-info", "volume",
                                  self._volname)
        self._check_netapp_error(output, "Failed to list snapshots")
        snapshotlist = output.child_get("snapshots")
        snapshots = []
        if (snapshotlist is not None and snapshotlist):
            for ss in snapshotlist.children_get():
                snapshots.append({
                    'id':
                    ss.child_get_string("name"),
                    'creation':
                    datetime.utcfromtimestamp(
                        float(ss.child_get_int("access-time"))),
                    'numclones':
                    1 if ss.child_get_string("busy") == "true" else 0,
                    'space_total':
                    ss.child_get_int("cumulative-total") * self._blocksize if
                    ss.child_get_int("cumulative-percentage-of-total-blocks") <
                    pct_limit else round(volsize * ss.child_get_int(
                        "cumulative-percentage-of-total-blocks") / 100),
                    'space_unique':
                    ss.child_get_int("total") * self._blocksize if
                    ss.child_get_int("percentage-of-total-blocks") < pct_limit
                    else round(volsize *
                               ss.child_get_int("percentage-of-total-blocks") /
                               100)
                })
        if not sortbycreation:
            return snapshots
        else:
            return sorted(snapshots,
                          key=operator.itemgetter('creation'),
                          reverse=sortreverse)

    def clone(self, snapid, clonename):
        output = self._srv.invoke("volume-clone-create", "parent-volume",
                                  self._volname, "parent-snapshot", snapid,
                                  "volume", clonename)
        self._check_netapp_error(output, "Creating clone failed")
        output = self._srv.invoke("volume-mount", "junction-path",
                                  "/%s" % clonename, "volume-name", clonename)
        self._check_netapp_error(output, "Mounting clone failed")
        output = self._srv.invoke("volume-set-option", "option-name",
                                  "nosnapdir", "option-value", "on", "volume",
                                  clonename)
        self._check_netapp_error(output, "Setting attribute on clone failed")

    def dropclone(self, cloneid):
        info = self.filesystem_info(cloneid)
        if info['origin'] != self._volname:
            raise Exception(
                self._exceptionbase,
                "This clone does not belong to parent %s" % self._volname)
        output = self._srv.invoke("volume-unmount", "volume-name", cloneid)
        self._check_netapp_error(output,
                                 "Unmounting volume %s failed" % cloneid)
        output = self._srv.invoke("volume-offline", "name", cloneid)
        self._check_netapp_error(output,
                                 "Offlining volume %s failed" % cloneid)
        output = self._srv.invoke("volume-destroy", "name", cloneid)
        self._check_netapp_error(output, "Dropping volume %s failed" % cloneid)

    def createvolume(self):
        uid = os.getuid()
        gid = os.getgid()
        permissions = "0770"
        #
        ui = UIElement()
        aggregate = ui.ask_string("Aggregate name:", 50)
        volume_size = ui.ask_size("Volume size:")
        path = ui.ask_string("Parent namespace:", 50)
        export_policy = ui.ask_string("Export policy:", 50)
        #
        output = self._srv.invoke(
            "volume-create", "volume", self._volname, "containing-aggr-name",
            aggregate, "efficiency-policy", "default",
            "export-policy", export_policy, "group-id", gid, "user-id",
            os.getuid(), "unix-permissions", permissions, "junction-path",
            os.path.join(path, self.configname), "percentage-snapshot-reserve",
            0, "size", volume_size, "volume-state", "online")
        self._check_netapp_error(output, "Creating volume failed")
        #
        rootelem = NaElement("volume-modify-iter")
        attrelem1 = NaElement("attributes")
        attrelem = NaElement("volume-attributes")
        attrelem1.child_add(attrelem)
        queryelem1 = NaElement("query")
        queryelem = NaElement("volume-attributes")
        queryelem1.child_add(queryelem)
        volid = NaElement("volume-id-attributes")
        volid.child_add_string("name", self._volname)
        queryelem.child_add(volid)
        snapattr = NaElement("volume-snapshot-attributes")
        snapattr.child_add_string("auto-snapshots-enabled", "false")
        snapattr.child_add_string("snapdir-access-enabled", "false")
        autosizeattr = NaElement("volume-autosize-attributes")
        autosizeattr.child_add_string("mode", "grow")
        attrelem.child_add(snapattr)
        attrelem.child_add(autosizeattr)
        rootelem.child_add(attrelem1)
        rootelem.child_add(queryelem1)
        rootelem.child_add_string("max-records", "1")
        output = self._srv.invoke_elem(rootelem)
        self._check_netapp_error(output, "Setting volume options failed.")
        print "Volume created. Please disable automatic snapshot creation through GUI, for some reason it does not work through API."
예제 #5
0
def login():
    if 'username' in session:
        return redirect(url_for('vols'))

    error = None
    try:
        if request.method == 'POST':
            session['username'] = request.form['username']
            session['pass'] = request.form['password']
            session['vserver'] = request.form['vserver']
            if session['vserver'] == "KW1PESANV01":
                IP = '10.200.5.100'
            elif session['vserver'] == "SG1PESANV01":
                IP = '10.201.18.70'

            try:

                s = NaServer(IP, 1, 30)
                s.set_server_type("FILER")
                s.set_transport_type("HTTPS")
                s.set_port(443)
                s.set_style("LOGIN")
                s.set_admin_user(session['username'], session['pass'])
                s.set_vserver(session['vserver'])
                s.set_server_cert_verification("False")
                s.set_hostname_verification("False")

                api = NaElement("volume-get-iter")

                xi = NaElement("desired-attributes")
                api.child_add(xi)

                xi = NaElement("desired-attributes")
                api.child_add(xi)

                xi1 = NaElement("volume-attributes")
                xi.child_add(xi1)
                xi11 = NaElement("volume-id-attributes")
                xi1.child_add(xi11)
                xi11.child_add_string("name", "name")

                xi27 = NaElement("volume-space-attributes")
                xi1.child_add(xi27)
                xi27.child_add_string("size-total", "<size-total>")
                xi27.child_add_string("percentage-size-used",
                                      "<percentage-size-used>")
                api.child_add_string("max-records", "1000")
                xo = s.invoke_elem(api)
                if (xo.results_status() == "failed"):
                    print("Error:\n")
                    print(xo.sprintf())
                    sys.exit(1)
                else:
                    # print (xo.sprintf())
                    vols = xo.child_get("attributes-list").children_get()
                    data = []
                    for vol in vols:
                        mydict = xmltodict.parse(vol.sprintf())
                        if 'volume-space-attributes' not in mydict[
                                'volume-attributes']:
                            continue

                        dicty = mydict['volume-attributes'][
                            'volume-id-attributes'].copy()
                        dicty.update(mydict['volume-attributes']
                                     ['volume-space-attributes'])
                        dicty['size-total'] = (
                            '%.0f' % (float(int(dicty['size-total'].strip())) /
                                      1024 / 1024 / 1024))
                        del dicty['owning-vserver-name']
                        dictyy = OrderedDict()
                        dictyy['name'] = dicty['name']
                        dictyy['size-total'] = dicty['size-total'] + ' GB'
                        dictyy['percentage-size-used'] = dicty[
                            'percentage-size-used'] + ' %'
                        data.append(dictyy)

            except Exception, err:
                print "Error!"
                print Exception, err

    except ServerError as e:
        error = str(e)

    return render_template('login.html', error=error)
if (host_equiv == 1):
    s.set_style("HOSTS")

if (use_cba == 1):
    response = s.set_style("CERTIFICATE")
    if (response):
        print("Unable to set style: " + response.results_reason() + "\n")
        sys.exit(2)
    response = s.set_client_cert_and_key(cert_file, client_key)
    if (response):
        print(response.results_reason() + "\n")
        sys.exit(2)

if (dossl or need_server_cert_verification):
    response = s.set_server_cert_verification(need_server_cert_verification)
    if (response):
        print(response.results_reason() + "\n")
        sys.exit(2)
    if (need_server_cert_verification):
        response = s.set_hostname_verification(need_hostname_verification)
        if (response):
            print(response.results_reason() + "\n")
            sys.exit(2)

if (ca_file):
    response = s.set_ca_certs(ca_file)
    if (response):
        print(response.results_reason() + "\n")
        sys.exit(2)
예제 #7
0
#!/usr/bin/env python

import sys
sys.path.append("NMSDKpy")
from NaServer import *

cluster = "svlngen4-c01-trad-gen001"
transport = "HTTPS"
port = 443 
style = "CERTIFICATE"
cert = "devops_cert.pem"
key = "devops_cert.key"

s = NaServer(cluster, 1, 30)
s.set_transport_type(transport)
s.set_port(port)
s.set_style(style)
s.set_server_cert_verification(0)
s.set_client_cert_and_key(cert, key)

api = NaElement("system-get-version")
output = s.invoke_elem(api)
if (output.results_status() == "failed"):
    r = output.results_reason()
    print("Failed: " + str(r))
    sys.exit(2)

ontap_version = output.child_get_string("version")
print ("V: " + ontap_version)