def add_acl(target_name, acl):
    try:
        if not target_name:
            raise Exception('No Target Specified')
        if not acl:
            raise Exception('No ACL specified')

        target, err = get_target(target_name)
        if err:
            raise Exception(err)

        if not target:
            raise Exception('Specified target not found')

        acls = target['acl']

        # First remove ALL from the ACL list
        if acls and 'ALL' in acls:
            cmd = 'tgtadm --lld iscsi --mode target --op unbind --tid %d -I ALL' % target['tid']
            (ret, rc), err = command.execute_with_rc(cmd)
            if err:
                raise Exception(err)
            if rc != 0:
                err = ''
                tl, er = command.get_output_list(ret)
                if er:
                    raise Exception(er)
                if tl:
                    err = ','.join(tl)
                tl, er = command.get_error_list(ret)
                if er:
                    raise Exception(er)
                if tl:
                    err = err + ','.join(tl)
                raise Exception('Error removind wildcard ACL : %s' % err)

        cmd = 'tgtadm --lld iscsi --mode target --op bind --tid %d -I %s' % (
            target['tid'], acl)
        (ret, rc), err = command.execute_with_rc(cmd)
        if err:
            raise Exception(err)
        if rc != 0:
            err = ''
            tl, er = command.get_output_list(ret)
            if er:
                raise Exception(er)
            if tl:
                err = ','.join(tl)
            tl, er = command.get_error_list(ret)
            if er:
                raise Exception(er)
            if tl:
                err = err + ','.join(tl)
            raise Exception(err)
        conf, err = _generate_targets_conf()
        if err:
            raise Exception(err)

    except Exception, e:
        return False, 'Error adding ACL: %s' % str(e)
Example #2
0
def add_acl(target_name, acl):
    try:
        if not target_name:
            raise Exception('No Target Specified')
        if not acl:
            raise Exception('No ACL specified')

        target, err = get_target(target_name)
        if err:
            raise Exception(err)

        if not target:
            raise Exception('Specified target not found')

        acls = target['acl']

        # First remove ALL from the ACL list
        if acls and 'ALL' in acls:
            cmd = 'tgtadm --lld iscsi --mode target --op unbind --tid %d -I ALL' % target['tid']
            (ret, rc), err = command.execute_with_rc(cmd)
            if err:
                raise Exception(err)
            if rc != 0:
                err = ''
                tl, er = command.get_output_list(ret)
                if er:
                    raise Exception(er)
                if tl:
                    err = ','.join(tl)
                tl, er = command.get_error_list(ret)
                if er:
                    raise Exception(er)
                if tl:
                    err = err + ','.join(tl)
                raise Exception('Error removind wildcard ACL : %s' % err)

        cmd = 'tgtadm --lld iscsi --mode target --op bind --tid %d -I %s' % (
            target['tid'], acl)
        (ret, rc), err = command.execute_with_rc(cmd)
        if err:
            raise Exception(err)
        if rc != 0:
            err = ''
            tl, er = command.get_output_list(ret)
            if er:
                raise Exception(er)
            if tl:
                err = ','.join(tl)
            tl, er = command.get_error_list(ret)
            if er:
                raise Exception(er)
            if tl:
                err = err + ','.join(tl)
            raise Exception(err)
        conf, err = _generate_targets_conf()
        if err:
            raise Exception(err)

    except Exception, e:
        return False, 'Error adding ACL: %s' % str(e)
Example #3
0
def update_ssh_dir_permissions(user='******'):
    try:
        # /bin/su -s /bin/bash -c \"%s\" %s
        uid = pwd.getpwnam(user).pw_uid
        gid = grp.getgrnam("integralstor").gr_gid

        path = _get_ssh_dir(user)
        os.chown(path, uid, gid)
        ssh_perm = "/bin/su -s /bin/bash -c 'chmod 700 %s' %s" % (path, user)
        (ret, rc), err = command.execute_with_rc(ssh_perm, True)
        if err:
            raise Exception(err)

        path = _get_authorized_file(user)
        if not os.path.isfile(path):
            with open(path, 'w') as f:
                f.close()

        os.chown(path, uid, gid)
        authorized_key = "/bin/su -s /bin/bash -c 'chmod 640 %s' %s" % (
            path, user)
        (ret, rc), err = command.execute_with_rc(authorized_key, True)
        if err:
            raise Exception(err)

        path = _get_known_hosts(user)
        if not os.path.isfile(path):
            with open(path, 'w') as f:
                f.close()
        if os.path.isfile(path):
            os.chown(path, uid, gid)
        return True, None
    except Exception, e:
        return False, "Error updating ssh dir permissions: %s" % str(e)
def remove_acl(target_name, acl):
    try:
        if not target_name:
            raise Exception('No Target Specified')
        if not acl:
            raise Exception('No ACL specified')

        target, err = get_target(target_name)
        if err:
            raise Exception(err)

        if not target:
            raise Exception('Specified target not found')

        acls = target['acl']
        if not acls:
            raise Exception('No ACLs found')

        if acl not in acls:
            raise Exception('Specified ACL not found')

        cmd = 'tgtadm --lld iscsi --mode target --op unbind --tid %d -I %s' % (
            target['tid'], acl)
        (ret, rc), err = command.execute_with_rc(cmd)
        if err:
            raise Exception(err)
        if rc != 0:
            # Could be an initiator name so try this..
            cmd = 'tgtadm --lld iscsi --mode target --op unbind --tid %d --initiator-name %s' % (
                target['tid'], acl)
            ret, rc = command.execute_with_rc(cmd)
            if rc != 0:
                err = ''
                tl, er = command.get_output_list(ret)
                if er:
                    raise Exception(er)
                if tl:
                    err = ','.join(tl)
                tl, er = command.get_error_list(ret)
                if er:
                    raise Exception(er)
                if tl:
                    err = err + ','.join(tl)
                raise Exception(err)
        conf, err = _generate_targets_conf()
        if err:
            raise Exception(err)

    except Exception, e:
        return False, 'Error removing ACL: %s' % str(e)
def remove_acl(target_name, acl):
    try:
        if not target_name:
            raise Exception('No Target Specified')
        if not acl:
            raise Exception('No ACL specified')

        target, err = get_target(target_name)
        if err:
            raise Exception(err)

        if not target:
            raise Exception('Specified target not found')

        acls = target['acl']
        if not acls:
            raise Exception('No ACLs found')

        if acl not in acls:
            raise Exception('Specified ACL not found')

        cmd = 'tgtadm --lld iscsi --mode target --op unbind --tid %d -I %s' % (
            target['tid'], acl)
        (ret, rc), err = command.execute_with_rc(cmd)
        if err:
            raise Exception(err)
        if rc != 0:
            # Could be an initiator name so try this..
            cmd = 'tgtadm --lld iscsi --mode target --op unbind --tid %d --initiator-name %s' % (
                target['tid'], acl)
            ret, rc = command.execute_with_rc(cmd)
            if rc != 0:
                err = ''
                tl, er = command.get_output_list(ret)
                if er:
                    raise Exception(er)
                if tl:
                    err = ','.join(tl)
                tl, er = command.get_error_list(ret)
                if er:
                    raise Exception(er)
                if tl:
                    err = err + ','.join(tl)
                raise Exception(err)
        conf, err = _generate_targets_conf()
        if err:
            raise Exception(err)

    except Exception, e:
        return False, 'Error removing ACL: %s' % str(e)
def delete_target(name):
    try:
        target, err = get_target(name)
        if err:
            raise Exception(err)
        if not target:
            raise Exception('Specified target not found')
        cmd = 'tgtadm --lld iscsi --mode target --op delete --tid %d' % target[
            'tid']
        (ret, rc), err = command.execute_with_rc(cmd)
        if err:
            raise Exception(err)
        if rc != 0:
            err = ''
            tl, er = command.get_output_list(ret)
            if er:
                raise Exception(er)
            if tl:
                err = ','.join(tl)
            tl, er = command.get_error_list(ret)
            if er:
                raise Exception(er)
            if tl:
                err = err + ','.join(tl)
            raise Exception(err)
        conf, err = _generate_targets_conf()
        if err:
            raise Exception(err)
    except Exception, e:
        return False, 'Error deleting target : %s' % str(e)
def create_target(name):
    try:
        targets, err = get_targets()
        if err:
            raise Exception(err)
        highest_tid = 0
        if targets:
            for t in targets:
                if int(t['tid']) > highest_tid:
                    highest_tid = int(t['tid'])
        new_tid = highest_tid + 1
        target_name = 'com.integralstor.integralstor-gridcell:%s' % name
        cmd = 'tgtadm --lld iscsi --mode target --op new --tid %d -T %s' % (
            new_tid, target_name)
        (ret, rc), err = command.execute_with_rc(cmd)
        if err:
            raise Exception(err)
        if rc != 0:
            err = ''
            tl, er = command.get_output_list(ret)
            if er:
                raise Exception(er)
            if tl:
                err = ','.join(tl)
            tl, er = command.get_error_list(ret)
            if er:
                raise Exception(er)
            if tl:
                err = err + ','.join(tl)
            raise Exception(err)
        conf, err = _generate_targets_conf()
        if err:
            raise Exception(err)
    except Exception, e:
        return False, 'Error creating target : %s' % str(e)
def delete_target(name):
    try:
        target, err = get_target(name)
        if err:
            raise Exception(err)
        if not target:
            raise Exception('Specified target not found')
        cmd = 'tgtadm --lld iscsi --mode target --op delete --tid %d' % target['tid']
        (ret, rc), err = command.execute_with_rc(cmd)
        if err:
            raise Exception(err)
        if rc != 0:
            err = ''
            tl, er = command.get_output_list(ret)
            if er:
                raise Exception(er)
            if tl:
                err = ','.join(tl)
            tl, er = command.get_error_list(ret)
            if er:
                raise Exception(er)
            if tl:
                err = err + ','.join(tl)
            raise Exception(err)
        conf, err = _generate_targets_conf()
        if err:
            raise Exception(err)
    except Exception, e:
        return False, 'Error deleting target : %s' % str(e)
Example #9
0
def get_all_disks_by_name():
    """Get all disks by their sd* names

    Returns a list of all disks by name (sda/sbd, etc) in the sytem
    """
    dl = []
    try:
        cmd_dl = "/usr/sbin/smartctl --scan"
        (ret, rc), err = command.execute_with_rc(cmd_dl)
        if err:
            raise Exception(err)
        disk_list, err = command.get_output_list(ret)
        if err:
            raise Exception(err)

        if ret:
            # Regex to capture "/dev/sdX"
            reg_exp_dl = re.compile("(/dev/[a-z]+)")

            for line in disk_list:
                d = {}
                if reg_exp_dl.search(line):
                    result_dl = re.search(r'/dev/sd[a-z]+', line)
                    result_dl1 = re.search(r'/dev/(sd[a-z]+)', line)
                    if result_dl:
                        d["full_path"] = result_dl.group()
                        dname = result_dl1.groups()[0]
                        r = re.match('^sd[a-z]+', dname)
                        d["name"] = r.group()
                        dl.append(d)
        # print "disk list info: ", dl
    except Exception, e:
        return None, "Error retrieving disks by name : %s" % str(e)
Example #10
0
def get_all_disks_by_name():
    """Get all disks by their sd* names

    Returns a list of all disks by name (sda/sbd, etc) in the sytem
    """
    dl = []
    try:
        cmd_dl = "/usr/sbin/smartctl --scan"
        (ret, rc), err = command.execute_with_rc(cmd_dl)
        if err:
            raise Exception(err)
        disk_list, err = command.get_output_list(ret)
        if err:
            raise Exception(err)

        if ret:
            # Regex to capture "/dev/sdX"
            reg_exp_dl = re.compile("(/dev/[a-z]+)")

            for line in disk_list:
                d = {}
                if reg_exp_dl.search(line):
                    result_dl = re.search(r'/dev/sd[a-z]+', line)
                    result_dl1 = re.search(r'/dev/(sd[a-z]+)', line)
                    if result_dl:
                        d["full_path"] = result_dl.group()
                        dname = result_dl1.groups()[0]
                        r = re.match('^sd[a-z]+', dname)
                        d["name"] = r.group()
                        dl.append(d)
        # print "disk list info: ", dl
    except Exception, e:
        return None, "Error retrieving disks by name : %s" % str(e)
def create_target(name):
    try:
        targets, err = get_targets()
        if err:
            raise Exception(err)
        highest_tid = 0
        if targets:
            for t in targets:
                if int(t['tid']) > highest_tid:
                    highest_tid = int(t['tid'])
        new_tid = highest_tid + 1
        target_name = 'com.integralstor.integralstor-gridcell:%s' % name
        cmd = 'tgtadm --lld iscsi --mode target --op new --tid %d -T %s' % (
            new_tid, target_name)
        (ret, rc), err = command.execute_with_rc(cmd)
        if err:
            raise Exception(err)
        if rc != 0:
            err = ''
            tl, er = command.get_output_list(ret)
            if er:
                raise Exception(er)
            if tl:
                err = ','.join(tl)
            tl, er = command.get_error_list(ret)
            if er:
                raise Exception(er)
            if tl:
                err = err + ','.join(tl)
            raise Exception(err)
        conf, err = _generate_targets_conf()
        if err:
            raise Exception(err)
    except Exception, e:
        return False, 'Error creating target : %s' % str(e)
def get_services_status():
    """The status of various services."""
    s = {}
    try:
        platform, err = config.get_platform()
        if err:
            raise Exception(err)
        if platform == 'gridcell':
            # Commenting out ctdb for now as we wont use it for this release!
            #services = ['smb', 'winbind', 'ctdb', 'glusterd']
            services = ['smb', 'winbind', 'glusterd']
            for service_name in services:
                stat, err = command.get_command_output(
                    '/sbin/service %s status' % service_name)
                ret = None
                rc = -1
                tup, err = command.execute_with_rc('/sbin/service %s status' %
                                                   service_name)
                if tup:
                    (ret, rc) = tup
                if err:
                    raise Exception(err)
                if rc == 0:
                    lines, er = command.get_output_list(ret)
                    if er:
                        raise Exception(er)
                    s[service_name] = [0, ','.join(lines)]
                else:
                    err = ''
                    tl, er = command.get_output_list(ret)
                    if er:
                        raise Exception(er)
                    if tl:
                        err = ','.join(tl)
                    tl, er = command.get_error_list(ret)
                    if er:
                        raise Exception(er)
                    if tl:
                        err = err + ','.join(tl)
                    s[service_name] = [-1, err]
        else:
            service_dict, err = services_management.get_sysd_services_status()
            if err:
                raise Exception(err)
            for service_name, service_info in service_dict.items():
                if service_info['info']['status']['status_str'] in [
                        'Failed', 'Unknown State'
                ]:
                    s[service_name] = [
                        -1, service_info['info']['status']['output_str']
                    ]
                else:
                    s[service_name] = [
                        0, service_info['info']['status']['output_str']
                    ]
    except Exception, e:
        return None, 'Error retrieving services status: %s' % str(e)
Example #13
0
def reboot_or_shutdown(request):
    return_dict = {}
    audit_str = ""
    try:
        minutes_to_wait = 1
        return_dict['minutes_to_wait'] = minutes_to_wait
        ret, err = django_utils.get_request_parameter_values(request, ['do'])
        if err:
            raise Exception(err)
        if 'do' not in ret:
            raise Exception("Invalid request, please use the menus.")
        do = ret['do']
        return_dict['do'] = do
        if request.method == "GET":
            return django.shortcuts.render_to_response(
                "reboot_or_shutdown.html",
                return_dict,
                context_instance=django.template.context.RequestContext(
                    request))
        else:
            if 'conf' not in request.POST:
                raise Exception('Unknown action. Please use the menus')
            audit.audit('reboot_shutdown', 'System %s initiated' % do, request)
            if do == 'reboot':
                command.execute_with_rc('shutdown -r +%d' % minutes_to_wait)
            elif do == 'shutdown':
                command.execute_with_rc('shutdown -h +%d' % minutes_to_wait)
            return django.shortcuts.render_to_response(
                "reboot_or_shutdown_conf.html",
                return_dict,
                context_instance=django.template.context.RequestContext(
                    request))
    except Exception, e:
        return_dict['base_template'] = "system_base.html"
        return_dict["page_title"] = 'Reboot or Shutdown Failure'
        return_dict['tab'] = 'reboot_tab'
        return_dict["error"] = 'Error Rebooting'
        return_dict["error_details"] = str(e)
        return django.shortcuts.render_to_response(
            'logged_in_error.html',
            return_dict,
            context_instance=django.template.context.RequestContext(request))
Example #14
0
def generate_ssh_key(user='******'):
    try:
        cmd = "ssh-keygen -t rsa -f %s -N ''" % (
            _get_ssh_dir(user) + "/id_rsa")
        runuser = "******"%s\" %s " % (cmd, user)
        (ret, rc), err = command.execute_with_rc(runuser, True)
        if err:
            raise Exception(err)
        return True, None
    except Exception, e:
        return None, "Error while generating ssh file for user: %s. Error : %s" % (user, str(e))
def get_services_status():
    """The status of various services."""
    s = {}
    try:
        platform, err = config.get_platform()
        if err:
            raise Exception(err)
        if platform == 'gridcell':
            # Commenting out ctdb for now as we wont use it for this release!
            #services = ['smb', 'winbind', 'ctdb', 'glusterd']
            services = ['smb', 'winbind', 'glusterd']
            for service_name in services:
                stat, err = command.get_command_output(
                    '/sbin/service %s status' % service_name)
                ret = None
                rc = -1
                tup, err = command.execute_with_rc(
                    '/sbin/service %s status' % service_name)
                if tup:
                    (ret, rc) = tup
                if err:
                    raise Exception(err)
                if rc == 0:
                    lines, er = command.get_output_list(ret)
                    if er:
                        raise Exception(er)
                    s[service_name] = [0, ','.join(lines)]
                else:
                    err = ''
                    tl, er = command.get_output_list(ret)
                    if er:
                        raise Exception(er)
                    if tl:
                        err = ','.join(tl)
                    tl, er = command.get_error_list(ret)
                    if er:
                        raise Exception(er)
                    if tl:
                        err = err + ','.join(tl)
                    s[service_name] = [-1, err]
        else:
            service_dict, err = services_management.get_sysd_services_status()
            if err:
                raise Exception(err)
            for service_name, service_info in service_dict.items():
                if service_info['info']['status']['status_str'] in ['Failed', 'Unknown State']:
                    s[service_name] = [-1, service_info['info']
                                       ['status']['output_str']]
                else:
                    s[service_name] = [
                        0, service_info['info']['status']['output_str']]
    except Exception, e:
        return None, 'Error retrieving services status: %s' % str(e)
Example #16
0
def delete_route(name, netmask='255.255.255.0'):
    if not name:
        return False, "Interface name missing"
    else:
        if name == 'default':
            cmd = 'route del -net 0.0.0.0 netmask ' + netmask
        else:
            cmd = 'route del -net ' + name + ' netmask ' + netmask
        (ret, rc), err = command.execute_with_rc(cmd)
        if err:
            return False, err
        return (ret, rc), None
def get_logs(hostname=socket.getfqdn()):
    try:
        for logs in files:
            cmd = "cp -rf %s /tmp/logs" % logs
            ret, err = command.execute_with_rc(cmd, True)
            if err:
                raise Exception(err)
        for folder in folders:
            cmd = "cp -rf %s /tmp/logs" % folder
            ret, err = command.execute_with_rc(cmd, True)
            if err:
                raise Exception(err)
        if ("gridcell-pri" in hostname) or ("gridcell-sec" in hostname):
            for folder in master_logs:
                cmd = "cp -rf %s /tmp/logs" % folder
                ret, err = command.execute_with_rc(cmd, True)
                if err:
                    raise Exception(err)

    except Exception, e:
        return False, e
def get_logs(hostname=socket.getfqdn()):
    try:
        for logs in files:
            cmd = "cp -rf %s /tmp/logs" % logs
            ret, err = command.execute_with_rc(cmd, True)
            if err:
                raise Exception(err)
        for folder in folders:
            cmd = "cp -rf %s /tmp/logs" % folder
            ret, err = command.execute_with_rc(cmd, True)
            if err:
                raise Exception(err)
        if ("gridcell-pri" in hostname) or ("gridcell-sec" in hostname):
            for folder in master_logs:
                cmd = "cp -rf %s /tmp/logs" % folder
                ret, err = command.execute_with_rc(cmd, True)
                if err:
                    raise Exception(err)

    except Exception, e:
        return False, e
def delete_lun(target_name, backing_store):

    try:

        if not target_name:
            raise ('No target name specified')
        if not backing_store:
            raise ('No backing store path specified')

        target, err = get_target(target_name)
        if err:
            raise Exception(err)
        if not target:
            raise Exception('Specified target not found.')

        luns = target['luns']
        if not luns:
            raise Exception('Specified target does not have any LUNs.')
        print luns
        lun = None
        for tl in luns:
            if tl['path'] == backing_store:
                lun = tl

        if not lun:
            raise Exception('Specified LUN not found.')

        cmd = 'tgtadm --lld iscsi --mode logicalunit --op delete --tid %d --lun %d' % (
            target['tid'], lun['id'])
        (ret, rc), err = command.execute_with_rc(cmd)
        if err:
            raise Exception(err)
        if rc != 0:
            err = ''
            tl, er = command.get_output_list(ret)
            if er:
                raise Exception(er)
            if tl:
                err = ','.join(tl)
            tl, er = command.get_error_list(ret)
            if er:
                raise Exception(er)
            if tl:
                err = err + ','.join(tl)
            raise Exception(err)
        conf, err = _generate_targets_conf()
        if err:
            raise Exception(err)

    except Exception, e:
        return False, 'Error Deleting Logical Unit: %s' % str(e)
def create_lun(target_name, backing_store):
    try:
        if not target_name:
            raise Exception('No Target Specified')
        if not backing_store:
            raise Exception('No backing storage volume specified')

        target, err = get_target(target_name)
        if err:
            raise Exception(err)

        if not target:
            raise Exception('Specified target not found')

        luns = target['luns']
        if not luns:
            raise Exception('Error retrieving LUN list')

        highest_lun_id = 0
        for lun in luns:
            if lun['id'] > highest_lun_id:
                highest_lun_id = lun['id']

        new_lun_id = highest_lun_id + 1

        cmd = 'tgtadm --lld iscsi --mode logicalunit --op new --tid %d --lun %d --backing-store %s' % (
            int(target['tid']), new_lun_id, backing_store)
        print cmd
        (ret, rc), err = command.execute_with_rc(cmd, True)
        print ret, rc, err
        if err:
            raise Exception(err)
        if rc != 0:
            err = ''
            tl, er = command.get_output_list(ret)
            if er:
                raise Exception(er)
            if tl:
                err = ','.join(tl)
            tl, er = command.get_error_list(ret)
            if er:
                raise Exception(er)
            if tl:
                err = err + ','.join(tl)
            raise Exception(err)
        conf, err = _generate_targets_conf()
        if err:
            raise Exception(err)

    except Exception, e:
        return False, 'Error creating LUN : %s' % str(e)
def delete_lun(target_name, backing_store):

    try:

        if not target_name:
            raise ('No target name specified')
        if not backing_store:
            raise ('No backing store path specified')

        target, err = get_target(target_name)
        if err:
            raise Exception(err)
        if not target:
            raise Exception('Specified target not found.')

        luns = target['luns']
        if not luns:
            raise Exception('Specified target does not have any LUNs.')
        print luns
        lun = None
        for tl in luns:
            if tl['path'] == backing_store:
                lun = tl

        if not lun:
            raise Exception('Specified LUN not found.')

        cmd = 'tgtadm --lld iscsi --mode logicalunit --op delete --tid %d --lun %d' % (
            target['tid'], lun['id'])
        (ret, rc), err = command.execute_with_rc(cmd)
        if err:
            raise Exception(err)
        if rc != 0:
            err = ''
            tl, er = command.get_output_list(ret)
            if er:
                raise Exception(er)
            if tl:
                err = ','.join(tl)
            tl, er = command.get_error_list(ret)
            if er:
                raise Exception(er)
            if tl:
                err = err + ','.join(tl)
            raise Exception(err)
        conf, err = _generate_targets_conf()
        if err:
            raise Exception(err)

    except Exception, e:
        return False, 'Error Deleting Logical Unit: %s' % str(e)
def create_lun(target_name, backing_store):
    try:
        if not target_name:
            raise Exception('No Target Specified')
        if not backing_store:
            raise Exception('No backing storage volume specified')

        target, err = get_target(target_name)
        if err:
            raise Exception(err)

        if not target:
            raise Exception('Specified target not found')

        luns = target['luns']
        if not luns:
            raise Exception('Error retrieving LUN list')

        highest_lun_id = 0
        for lun in luns:
            if lun['id'] > highest_lun_id:
                highest_lun_id = lun['id']

        new_lun_id = highest_lun_id + 1

        cmd = 'tgtadm --lld iscsi --mode logicalunit --op new --tid %d --lun %d --backing-store %s' % (
            int(target['tid']), new_lun_id, backing_store)
        print cmd
        (ret, rc), err = command.execute_with_rc(cmd, True)
        print ret, rc, err
        if err:
            raise Exception(err)
        if rc != 0:
            err = ''
            tl, er = command.get_output_list(ret)
            if er:
                raise Exception(er)
            if tl:
                err = ','.join(tl)
            tl, er = command.get_error_list(ret)
            if er:
                raise Exception(er)
            if tl:
                err = err + ','.join(tl)
            raise Exception(err)
        conf, err = _generate_targets_conf()
        if err:
            raise Exception(err)

    except Exception, e:
        return False, 'Error creating LUN : %s' % str(e)
Example #23
0
def add_route(name, gw, netmask='255.255.255.0', interface=None):
    if not name or not gw:
        return False, "Interface name or gateway missing"
    else:
        if name == 'default':
            cmd = 'route add -net 0.0.0.0 netmask ' + netmask + ' gw ' + gw
        else:
            cmd = 'route add -net ' + name + ' netmask ' + netmask + ' gw ' + gw
        if interface:
            cmd = cmd + ' dev ' + interface[0]
        # print cmd
        (ret, rc), err = command.execute_with_rc(cmd)
        if err:
            return False, err
        return (ret, rc), None
def delete_iscsi_lun(request):
    return_dict = {}
    try:
        if 'target_name' not in request.REQUEST:
            raise Exception(
                "Malformed request. No target specified. Please use the menus."
            )
        if 'store' not in request.REQUEST:
            raise Exception(
                "Malformed request. No LUN specified. Please use the menus.")

        store = request.REQUEST['store']
        target_name = request.REQUEST['target_name']
        print store, target_name

        if request.method == "GET":
            return_dict["target_name"] = target_name
            return_dict["store"] = store
            return django.shortcuts.render_to_response(
                "delete_iscsi_lun_conf.html",
                return_dict,
                context_instance=django.template.context.RequestContext(
                    request))
        else:
            ret, err = iscsi_stgt.delete_lun(target_name, store)
            if not ret:
                if err:
                    raise Exception(err)
                else:
                    raise Exception("Unknown error.")
            audit_str = "Deleted ISCSI LUN %s from target %s" % (store,
                                                                 target_name)
            cmd = 'zfs destroy frzpool/%s' % (store.split("/")[-1])
            ret, err = command.execute_with_rc(cmd=cmd, shell=True)
            url = '/view_iscsi_target?name=%s&ack=lun_deleted' % target_name
            audit.audit("delete_iscsi_lun", audit_str, request)
            return django.http.HttpResponseRedirect(url)
    except Exception, e:
        return_dict['base_template'] = "shares_and_targets_base.html"
        return_dict["page_title"] = 'Remove an ISCSI LUN'
        return_dict['tab'] = 'view_iscsi_targets_tab'
        return_dict["error"] = 'Error removing an ISCSI LUN'
        return_dict["error_details"] = str(e)
        return django.shortcuts.render_to_response(
            "logged_in_error.html",
            return_dict,
            context_instance=django.template.context.RequestContext(request))
Example #25
0
def delete_ramdisk(path, pool):
    try:
        if not path:
            raise Exception('No RAMDisk path specified')
        res, err = delete_from_ramdisks_config(pool, path)
        if err:
            raise Exception(err)
        if not res:
            raise Exception("Error removing ramdisk info from the conf file")
        cmd = 'umount %s' % path
        lines, err = command.get_command_output(cmd)
        if err:
            raise Exception('Error creating ramdisk : %s' % err)
        (ret, rc), err = command.execute_with_rc(cmd)
        if err:
            raise Exception('Error unmounting ramdisk : %s' % err)
    except Exception, e:
        return False, 'Error destroying ramdisk : %s' % str(e)
def remove_user_authentication(target_name, username, authentication_type):
    try:
        if not target_name:
            raise "Target Not Specified"
        if not username:
            raise "Username Not Specified"

        target, err = get_target(target_name)
        if err:
            raise Exception(err)
        if not target:
            raise Exception('Specified target not found.')

        cmd = 'tgtadm --lld iscsi --mode account --op unbind --tid %d --user %s' % (
            target['tid'], username)
        if authentication_type == 'outgoing':
            cmd += ' --outgoing'
        (ret, rc), err = command.execute_with_rc(cmd)
        if err:
            raise Exception(err)
        if rc != 0:
            err = ''
            tl, er = command.get_output_list(ret)
            if er:
                raise Exception(er)
            if tl:
                err = ','.join(tl)
            tl, er = command.get_error_list(ret)
            if er:
                raise Exception(er)
            if tl:
                err = err + ','.join(tl)
            raise Exception(err)
        conf, err = _generate_targets_conf()
        if err:
            raise Exception(err)
    except Exception, e:
        return False, 'Error removing user autentication: %s' % str(e)
def remove_user_authentication(target_name, username, authentication_type):
    try:
        if not target_name:
            raise "Target Not Specified"
        if not username:
            raise "Username Not Specified"

        target, err = get_target(target_name)
        if err:
            raise Exception(err)
        if not target:
            raise Exception('Specified target not found.')

        cmd = 'tgtadm --lld iscsi --mode account --op unbind --tid %d --user %s' % (
            target['tid'], username)
        if authentication_type == 'outgoing':
            cmd += ' --outgoing'
        (ret, rc), err = command.execute_with_rc(cmd)
        if err:
            raise Exception(err)
        if rc != 0:
            err = ''
            tl, er = command.get_output_list(ret)
            if er:
                raise Exception(er)
            if tl:
                err = ','.join(tl)
            tl, er = command.get_error_list(ret)
            if er:
                raise Exception(er)
            if tl:
                err = err + ','.join(tl)
            raise Exception(err)
        conf, err = _generate_targets_conf()
        if err:
            raise Exception(err)
    except Exception, e:
        return False, 'Error removing user autentication: %s' % str(e)
def create_volume(request):
    """ Used to actually create the volume"""

    return_dict = {}
    try:
        gluster_lck, err = lock.get_lock('gluster_commands')
        if err:
            raise Exception(err)

        if not gluster_lck:
            raise Exception(
                'This action cannot be performed as an underlying storage command is being run. Please retry this operation after a few seconds.')

        return_dict['base_template'] = "volume_base.html"
        return_dict["page_title"] = 'Create a volume'
        return_dict['tab'] = 'view_volumes_tab'
        return_dict["error"] = 'Error creating a volume'

        if request.method != "POST":
            raise Exception("Invalid access method. Please use the menus.")

        if 'cmd' not in request.POST or 'dataset_list' not in request.POST:
            raise Exception('Required parameters not passed.')

        # cmd represents the actual gluster volume creation command built using
        # the wizard choices.
        cmd = request.POST['cmd']
        # print cmd

        # dataset_list is a list of hostname:dataset components of the datasets
        # that need to br created on various gridcells.
        dsl = request.POST.getlist('dataset_list')
        dataset_dict = {}
        for ds in dsl:
            tl = ds.split(':')
            dataset_dict[tl[0]] = tl[1]

        iv_logging.info("create volume command %s" % cmd)

        # First create the datasets on which the bricks will reside.
        # revert_list will consist of the set of node:command components to
        # perform to undo dataset creation in case of some failures.
        client = salt.client.LocalClient()
        revert_list = []
        errors = ""
        for node, dataset in dataset_dict.items():
            dataset_cmd = 'zfs create %s' % dataset
            dataset_revert_cmd = 'zfs destroy %s' % dataset
            r1 = client.cmd(node, 'cmd.run_all', [dataset_cmd])
            if r1:
                for node, ret in r1.items():
                    # print ret
                    if ret["retcode"] != 0:
                        errors += ", Error creating the underlying storage brick on %s" % node
                        # print errors
                    else:
                        revert_list.append({node: dataset_revert_cmd})

        if errors != "":
            # print errors
            if revert_list:
                # Undo the creation of the datasets
                for revert in revert_list:
                    for node, dsr_cmd in revert.items():
                        r1 = client.cmd(node, 'cmd.run_all', [dsr_cmd])
                        if r1:
                            for node, ret in r1.items():
                                # print ret
                                if ret["retcode"] != 0:
                                    errors += ", Error undoing the creating the underlying storage brick on %s" % node
            raise Exception(errors)

        # Underlying storage created so now create the volume

        d, errors = xml_parse.run_gluster_command("%s force" % cmd)
        # print d, errors
        if not errors:
            # All ok so mount and change the owner and group of the volume to
            # integralstor
            (ret, rc), err = command.execute_with_rc("gluster volume set " +
                                                     request.POST['vol_name'] + " storage.owner-gid 1000")
            if err:
                raise Exception('Error setting volume owner : %s' % err)

            # Now start the volume
            (ret, rc), err = command.execute_with_rc(
                "gluster volume start " + request.POST['vol_name'])
            if err:
                raise Exception('Error starting volume : %s' % err)

            '''
      #Set the client side quorum count
      cmd = "gluster volume set %s quorum-count 2 --xml"%request.POST['vol_name']
      d, err = xml_parse.run_gluster_command(cmd)
      if err:
        raise Exception('Error setting volume client side quorum count : %s'%err)

      #Set the client side quorum type
      cmd = "gluster volume set %s quorum-type fixed --xml"%request.POST['vol_name']
      d, err = xml_parse.run_gluster_command(cmd)
      if err:
        raise Exception('Errot setting volume client side quorum type : %s'%err)
      '''

            # Temporarily mount the volume
            (ret, rc), err = command.execute_with_rc(
                "mount -t glusterfs localhost:/" + request.POST['vol_name'] + " /mnt")
            if err:
                raise Exception(err)

            # Set the volume permissions
            (ret, rc), err = command.execute_with_rc("chmod 770 /mnt")
            if err:
                raise Exception(err)

            #..and unmount the volume
            (ret, rc), err = command.execute_with_rc("umount /mnt")
            if err:
                raise Exception(err)

            # If it is an ISCSI volume, then add it to the iscsi volume list..
            # print request.POST['vol_access']
            if request.POST["vol_access"] == "iscsi":
                ret, err = iscsi.add_iscsi_volume(request.POST["vol_name"])
                if err:
                    raise Exception(err)

            # Success so audit the change
            audit_str = "Create "
            if request.POST["vol_type"] in ["replicated"]:
                audit_str = audit_str + \
                    "replicated (count %d) " % int(request.POST["repl_count"])
            else:
                audit_str = audit_str + "distributed  "
            audit_str = audit_str + \
                " volume named %s" % request.POST["vol_name"]
            ret, err = audit.audit("create_volume", audit_str, request)
            if err:
                raise Exception(err)
        else:
            # Volume creation itself failed so try and delete the underlying
            # datasets if they were created..
            if not errors:
                errors = ""
            if revert_list:
                # Undo the creation of the datasets
                for revert in revert_list:
                    for node, dsr_cmd in revert.items():
                        r1 = client.cmd(node, 'cmd.run_all', [dsr_cmd])
                        if r1:
                            for node, ret in r1.items():
                                print ret
                                if ret["retcode"] != 0:
                                    errors += ", Error undoing the creating the underlying storage brick on %s" % node
        if errors:
            raise Exception(errors)

        if request.POST['vol_type'] == 'replicated':
            return_dict['repl_count'] = request.POST['repl_count']
        return_dict['vol_type'] = request.POST['vol_type']
        return_dict['vol_name'] = request.POST['vol_name']
        return_dict['node_list_str'] = request.POST['node_list_str']
        return_dict['cmd'] = cmd
        return_dict['result_dict'] = d

        return django.shortcuts.render_to_response('vol_create_wiz_result.html', return_dict, context_instance=django.template.context.RequestContext(request))
    except Exception, e:
        s = str(e)
        if "Another transaction is in progress".lower() in s.lower():
            return_dict["error_details"] = "An underlying storage operation has locked a volume so we are unable to process this request. Please try after a couple of seconds"
        else:
            return_dict["error_details"] = "An error occurred when processing your request : %s" % s
        return django.shortcuts.render_to_response("logged_in_error.html", return_dict, context_instance=django.template.context.RequestContext(request))
def create_iscsi_lun(request):

    return_dict = {}

    try:
        if 'target_name' not in request.REQUEST:
            raise Exception(
                "Malformed request. No target specified. Please use the menus."
            )
        target_name = request.REQUEST['target_name']
        zvols, err = zfs.get_all_zvols()
        if err:
            raise Exception(err)
        # if not zvols:
        #  raise Exception("There are no block device volumes created. Please create one first before adding a LUN.")
        if request.method == "GET":
            # Return the form
            initial = {}
            initial['target_name'] = target_name
            form = iscsi_stgt_forms.IscsiLunForm(initial=initial)
            return_dict["form"] = form
            return django.shortcuts.render_to_response(
                "create_iscsi_lun.html",
                return_dict,
                context_instance=django.template.context.RequestContext(
                    request))
        else:
            # Form submission so create
            return_dict = {}
            form = iscsi_stgt_forms.IscsiLunForm(request.POST)
            if form.is_valid():
                cd = form.cleaned_data
                # Create a zvol
                cmd = 'zfs create -s -b 64K -V %s frzpool/%s' % (
                    cd['lun_size'], cd['lun_name'])
                ret, err = command.execute_with_rc(cmd=cmd, shell=True)
                time.sleep(20)
                ret, err = iscsi_stgt.create_lun(
                    cd["target_name"], "/dev/frzpool/%s" % cd['lun_name'])
                if not ret:
                    if err:
                        raise Exception(err)
                    else:
                        raise Exception("Unknown error.")
                audit_str = "Created an ISCSI LUN in target %s with path %s" % (
                    cd["target_name"], cd['lun_name'])
                audit.audit("create_iscsi_lun", audit_str, request)
                url = '/view_iscsi_target?name=%s&ack=lun_created' % target_name
                return django.http.HttpResponseRedirect(url)
            else:
                return_dict["form"] = form
                return django.shortcuts.render_to_response(
                    "create_iscsi_lun.html",
                    return_dict,
                    context_instance=django.template.context.RequestContext(
                        request))
    except Exception, e:
        return_dict['base_template'] = "shares_and_targets_base.html"
        return_dict["page_title"] = 'Create an ISCSI LUN'
        return_dict['tab'] = 'view_iscsi_targets_tab'
        return_dict["error"] = 'Error creating an ISCSI LUN'
        return_dict["error_details"] = str(e)
        return django.shortcuts.render_to_response(
            "logged_in_error.html",
            return_dict,
            context_instance=django.template.context.RequestContext(request))
def _generate_targets_conf(new_user=None):
    try:
        targets, err = get_targets()
        if err:
            raise Exception(err)
        config_targets, err = _load_targets_conf()
        if err:
            raise Exception(err)

        with open('/tmp/targets.conf', 'w') as f:
            f.write('default-driver iscsi\n')
            for target in targets:
                f.write('\n<target %s>\n' % target['iqn'])
                for lun in target['luns']:
                    if lun['path'] and lun['path'] != 'None':
                        f.write('  backing-store %s\n' % lun['path'])
                for acl in target['acl']:
                    if acl != 'ALL':
                        f.write('  initiator-address %s\n' % acl)
                config_target = None
                # First process new users if any
                if new_user and new_user['iqn'] == target['iqn']:
                    if new_user['type'] == 'incoming':
                        f.write('  incominguser %s %s\n' %
                                (new_user['username'], new_user['pswd']))
                    else:
                        f.write('  outgoinguser %s %s\n' %
                                (new_user['username'], new_user['pswd']))
                for account in target['accounts']:
                    # Now process existing users. Take the list from tgtadm,
                    # get pswds from existing config file and write it out
                    # again
                    for ct in config_targets:
                        if ct['iqn'] == target['iqn']:
                            config_target = ct
                            break
                    if account['type'] == 'incoming':
                        for ctiu in config_target['incoming_users']:
                            if ctiu['username'] == account['user']:
                                f.write('  incominguser %s %s\n' %
                                        (account['user'], ctiu['pswd']))
                    else:
                        for ctiu in config_target['outgoing_users']:
                            if ctiu['username'] == account['user']:
                                f.write('  outgoinguser %s %s\n' %
                                        (account['user'], ctiu['pswd']))
                f.write('</target>\n\n')
            f.flush()
            f.close()
        shutil.move('/tmp/targets.conf', '/etc/tgt/targets.conf')
        cmd = 'tgt-admin -e'
        (ret, rc), err = command.execute_with_rc(cmd)
        if err:
            raise Exception(err)
        if rc != 0:
            err = ''
            tl, er = command.get_output_list(ret)
            if er:
                raise Exception(er)
            if tl:
                err = ','.join(tl)
            tl, er = command.get_error_list(ret)
            if er:
                raise Exception(er)
            if tl:
                err = err + ','.join(tl)
            raise Exception(err)

    except Exception, e:
        return False, 'Error generating ISCSI config file: %s' % str(e)
        for minion in minions:
            # check if the minion is up or not.
            status = local.cmd(minion, "test.ping")

            if minion in status:
                status = local.cmd(minion, "cp.push", ["/tmp/%s.zip" % minion])
                # If no response from minion, log a message
                if not status[minion]:
                    with open("/tmp/gridcell_logs/%s.log" % minion, "a+") as f:
                        f.write(
                            "Could not get the log file from minion: %s. Check the minion logs for any possible errors." % minion)

            else:
                with open("/tmp/gridcell_logs/%s.log" % minion, "a+") as f:
                    f.write(
                        "Minion %s did not respond. Minion possibly down." % minion)

            log_path = "/var/cache/salt/master/minions/%s/files/tmp/%s.zip" % (
                minion, minion)
            cmd = "cp -rf %s %s" % (log_path, path)
            ret, err = command.execute_with_rc(cmd, True)
            if err:
                raise Exception(err)
        if os.path.isdir(path):
            zipdir(path, "/tmp/gridcell.zip")

    except Exception, e:
        print e

# vim: tabstop=8 softtabstop=0 expandtab ai shiftwidth=4 smarttab
Example #32
0
def generate_default_primary_named_conf(primary_ip, primary_netmask, secondary_ip, generate_forwarders=False, forwarder_ip=None, generate_zone_file=True):
    """Generates the named.conf to be used in the primary

    primary_ip -- The IP address of the primary
    primary_netmask -- The netmask of the primary
    secondary_ip -- The IP address of the secondary
    generate_forwarders -- If True then generate a forwarders entry to <forwarder_ip>
    forwarder_ip -- The forwarder IP address if a forwarder entry is needed
    generate_zone_file -- If True then generate a zone file with the primary and secondary IP addresses
    """
    rc = 0
    try:
        primary_cidr_netmask, err = networking.get_subnet_in_cidr_format(
            primary_ip, primary_netmask)
        if err:
            raise Exception(err)

        with open('/etc/named.conf', 'w') as f:
            f.write('// Generated by the IntegralStor script\n')
            f.write("options {\n")
            f.write(" listen-on port 53 { any; };\n")
            f.write(" listen-on-v6 port 53 { ::1; };\n")
            f.write(' directory 	"/var/named";\n')
            f.write(' dump-file 	"/var/named/data/cache_dump.db";\n')
            f.write(' statistics-file "/var/named/data/named_stats.txt";\n')
            f.write(' memstatistics-file "/var/named/data/named_mem_stats.txt";\n')
            f.write(" allow-query     { localhost; any; };\n")
            f.write(" allow-transfer  { localhost; %s; };\n" % secondary_ip)
            if generate_forwarders:
                f.write(" forwarders    { %s; };\n" % forwarder_ip)
                f.write(" recursion yes;\n")
            else:
                f.write(" recursion no;\n")
            f.write("};\n")

            f.write("logging {\n")
            f.write(" channel default_debug {\n")
            f.write('   file "data/named.run";\n')
            f.write("   severity dynamic;\n")
            f.write(" };\n")
            f.write("};\n")

            f.write('zone "." IN {\n')
            f.write(" type hint;\n")
            f.write(' file "named.ca";\n')
            f.write("};\n")

            f.write('zone "integralstor.lan" IN {\n')
            f.write(" type master;\n")
            f.write(' file "integralstor.for";\n')
            f.write(" allow-update { localhost; %s; };\n" %
                    primary_cidr_netmask)
            f.write("};\n")

            f.write('include "/etc/named.rfc1912.zones";\n')
            f.flush()
        f.close()
        if generate_zone_file:
            rc, err = generate_default_zone_file(primary_ip, secondary_ip)
            if err:
                raise Exception(err)
            if rc != 0:
                raise Exception('Error generating the default zone file')
        (r, rc), err = command.execute_with_rc('service named reload')
        if err:
            raise Exception(err)
        if rc != 0:
            print "Error restarting the DNS server"
    except Exception, e:
        return False, 'Error generating primary named configuration : %s' % str(e)
Example #33
0
            status = local.cmd(minion, "test.ping")

            if minion in status:
                status = local.cmd(minion, "cp.push", ["/tmp/%s.zip" % minion])
                # If no response from minion, log a message
                if not status[minion]:
                    with open("/tmp/gridcell_logs/%s.log" % minion, "a+") as f:
                        f.write(
                            "Could not get the log file from minion: %s. Check the minion logs for any possible errors."
                            % minion)

            else:
                with open("/tmp/gridcell_logs/%s.log" % minion, "a+") as f:
                    f.write(
                        "Minion %s did not respond. Minion possibly down." %
                        minion)

            log_path = "/var/cache/salt/master/minions/%s/files/tmp/%s.zip" % (
                minion, minion)
            cmd = "cp -rf %s %s" % (log_path, path)
            ret, err = command.execute_with_rc(cmd, True)
            if err:
                raise Exception(err)
        if os.path.isdir(path):
            zipdir(path, "/tmp/gridcell.zip")

    except Exception, e:
        print e

# vim: tabstop=8 softtabstop=0 expandtab ai shiftwidth=4 smarttab
Example #34
0
def create_volume(request):
    """ Used to actually create the volume"""

    return_dict = {}
    try:
        gluster_lck, err = lock.get_lock('gluster_commands')
        if err:
            raise Exception(err)

        if not gluster_lck:
            raise Exception(
                'This action cannot be performed as an underlying storage command is being run. Please retry this operation after a few seconds.'
            )

        return_dict['base_template'] = "volume_base.html"
        return_dict["page_title"] = 'Create a volume'
        return_dict['tab'] = 'view_volumes_tab'
        return_dict["error"] = 'Error creating a volume'

        if request.method != "POST":
            raise Exception("Invalid access method. Please use the menus.")

        if 'cmd' not in request.POST or 'dataset_list' not in request.POST:
            raise Exception('Required parameters not passed.')

        # cmd represents the actual gluster volume creation command built using
        # the wizard choices.
        cmd = request.POST['cmd']
        # print cmd

        # dataset_list is a list of hostname:dataset components of the datasets
        # that need to br created on various gridcells.
        dsl = request.POST.getlist('dataset_list')
        dataset_dict = {}
        for ds in dsl:
            tl = ds.split(':')
            dataset_dict[tl[0]] = tl[1]

        iv_logging.info("create volume command %s" % cmd)

        # First create the datasets on which the bricks will reside.
        # revert_list will consist of the set of node:command components to
        # perform to undo dataset creation in case of some failures.
        client = salt.client.LocalClient()
        revert_list = []
        errors = ""
        for node, dataset in dataset_dict.items():
            dataset_cmd = 'zfs create %s' % dataset
            dataset_revert_cmd = 'zfs destroy %s' % dataset
            r1 = client.cmd(node, 'cmd.run_all', [dataset_cmd])
            if r1:
                for node, ret in r1.items():
                    # print ret
                    if ret["retcode"] != 0:
                        errors += ", Error creating the underlying storage brick on %s" % node
                        # print errors
                    else:
                        revert_list.append({node: dataset_revert_cmd})

        if errors != "":
            # print errors
            if revert_list:
                # Undo the creation of the datasets
                for revert in revert_list:
                    for node, dsr_cmd in revert.items():
                        r1 = client.cmd(node, 'cmd.run_all', [dsr_cmd])
                        if r1:
                            for node, ret in r1.items():
                                # print ret
                                if ret["retcode"] != 0:
                                    errors += ", Error undoing the creating the underlying storage brick on %s" % node
            raise Exception(errors)

        # Underlying storage created so now create the volume

        d, errors = xml_parse.run_gluster_command("%s force" % cmd)
        # print d, errors
        if not errors:
            # All ok so mount and change the owner and group of the volume to
            # integralstor
            (ret, rc), err = command.execute_with_rc("gluster volume set " +
                                                     request.POST['vol_name'] +
                                                     " storage.owner-gid 1000")
            if err:
                raise Exception('Error setting volume owner : %s' % err)

            # Now start the volume
            (ret, rc), err = command.execute_with_rc("gluster volume start " +
                                                     request.POST['vol_name'])
            if err:
                raise Exception('Error starting volume : %s' % err)
            '''
      #Set the client side quorum count
      cmd = "gluster volume set %s quorum-count 2 --xml"%request.POST['vol_name']
      d, err = xml_parse.run_gluster_command(cmd)
      if err:
        raise Exception('Error setting volume client side quorum count : %s'%err)

      #Set the client side quorum type
      cmd = "gluster volume set %s quorum-type fixed --xml"%request.POST['vol_name']
      d, err = xml_parse.run_gluster_command(cmd)
      if err:
        raise Exception('Errot setting volume client side quorum type : %s'%err)
      '''

            # Temporarily mount the volume
            (ret, rc), err = command.execute_with_rc(
                "mount -t glusterfs localhost:/" + request.POST['vol_name'] +
                " /mnt")
            if err:
                raise Exception(err)

            # Set the volume permissions
            (ret, rc), err = command.execute_with_rc("chmod 770 /mnt")
            if err:
                raise Exception(err)

            #..and unmount the volume
            (ret, rc), err = command.execute_with_rc("umount /mnt")
            if err:
                raise Exception(err)

            # If it is an ISCSI volume, then add it to the iscsi volume list..
            # print request.POST['vol_access']
            if request.POST["vol_access"] == "iscsi":
                ret, err = iscsi.add_iscsi_volume(request.POST["vol_name"])
                if err:
                    raise Exception(err)

            # Success so audit the change
            audit_str = "Create "
            if request.POST["vol_type"] in ["replicated"]:
                audit_str = audit_str + \
                    "replicated (count %d) " % int(request.POST["repl_count"])
            else:
                audit_str = audit_str + "distributed  "
            audit_str = audit_str + \
                " volume named %s" % request.POST["vol_name"]
            ret, err = audit.audit("create_volume", audit_str, request)
            if err:
                raise Exception(err)
        else:
            # Volume creation itself failed so try and delete the underlying
            # datasets if they were created..
            if not errors:
                errors = ""
            if revert_list:
                # Undo the creation of the datasets
                for revert in revert_list:
                    for node, dsr_cmd in revert.items():
                        r1 = client.cmd(node, 'cmd.run_all', [dsr_cmd])
                        if r1:
                            for node, ret in r1.items():
                                print ret
                                if ret["retcode"] != 0:
                                    errors += ", Error undoing the creating the underlying storage brick on %s" % node
        if errors:
            raise Exception(errors)

        if request.POST['vol_type'] == 'replicated':
            return_dict['repl_count'] = request.POST['repl_count']
        return_dict['vol_type'] = request.POST['vol_type']
        return_dict['vol_name'] = request.POST['vol_name']
        return_dict['node_list_str'] = request.POST['node_list_str']
        return_dict['cmd'] = cmd
        return_dict['result_dict'] = d

        return django.shortcuts.render_to_response(
            'vol_create_wiz_result.html',
            return_dict,
            context_instance=django.template.context.RequestContext(request))
    except Exception, e:
        s = str(e)
        if "Another transaction is in progress".lower() in s.lower():
            return_dict[
                "error_details"] = "An underlying storage operation has locked a volume so we are unable to process this request. Please try after a couple of seconds"
        else:
            return_dict[
                "error_details"] = "An error occurred when processing your request : %s" % s
        return django.shortcuts.render_to_response(
            "logged_in_error.html",
            return_dict,
            context_instance=django.template.context.RequestContext(request))
def _generate_targets_conf(new_user=None):
    try:
        targets, err = get_targets()
        if err:
            raise Exception(err)
        config_targets, err = _load_targets_conf()
        if err:
            raise Exception(err)

        with open('/tmp/targets.conf', 'w') as f:
            f.write('default-driver iscsi\n')
            for target in targets:
                f.write('\n<target %s>\n' % target['iqn'])
                for lun in target['luns']:
                    if lun['path'] and lun['path'] != 'None':
                        f.write('  backing-store %s\n' % lun['path'])
                for acl in target['acl']:
                    if acl != 'ALL':
                        f.write('  initiator-address %s\n' % acl)
                config_target = None
                # First process new users if any
                if new_user and new_user['iqn'] == target['iqn']:
                    if new_user['type'] == 'incoming':
                        f.write('  incominguser %s %s\n' %
                                (new_user['username'], new_user['pswd']))
                    else:
                        f.write('  outgoinguser %s %s\n' %
                                (new_user['username'], new_user['pswd']))
                for account in target['accounts']:
                    # Now process existing users. Take the list from tgtadm,
                    # get pswds from existing config file and write it out
                    # again
                    for ct in config_targets:
                        if ct['iqn'] == target['iqn']:
                            config_target = ct
                            break
                    if account['type'] == 'incoming':
                        for ctiu in config_target['incoming_users']:
                            if ctiu['username'] == account['user']:
                                f.write('  incominguser %s %s\n' %
                                        (account['user'], ctiu['pswd']))
                    else:
                        for ctiu in config_target['outgoing_users']:
                            if ctiu['username'] == account['user']:
                                f.write('  outgoinguser %s %s\n' %
                                        (account['user'], ctiu['pswd']))
                f.write('</target>\n\n')
            f.flush()
            f.close()
        shutil.move('/tmp/targets.conf', '/etc/tgt/targets.conf')
        cmd = 'tgt-admin -e'
        (ret, rc), err = command.execute_with_rc(cmd)
        if err:
            raise Exception(err)
        if rc != 0:
            err = ''
            tl, er = command.get_output_list(ret)
            if er:
                raise Exception(er)
            if tl:
                err = ','.join(tl)
            tl, er = command.get_error_list(ret)
            if er:
                raise Exception(er)
            if tl:
                err = err + ','.join(tl)
            raise Exception(err)

    except Exception, e:
        return False, 'Error generating ISCSI config file: %s' % str(e)
def mount_and_configure():
    lg = None
    try:
        lg, err = logger.get_script_logger('Admin volume mounter',
                                           '/var/log/integralstor/scripts.log',
                                           level=logging.DEBUG)

        logger.log_or_print('Admin volume mounter initiated.',
                            lg,
                            level='info')

        pog, err = grid_ops.is_part_of_grid()
        if err:
            raise Exception(err)

        # If the localhost is part of the gridcell, proceed
        if pog:
            logger.log_or_print('Checking glusterd service', lg, level='debug')
            service = 'glusterd'
            status, err = services_management.get_service_status([service])
            if err:
                raise Exception(err)
            logger.log_or_print('Service %s status is %s' %
                                (service, status['status_code']),
                                lg,
                                level='debug')
            if status['status_code'] != 0:
                logger.log_or_print('Service %s not started so restarting' %
                                    service,
                                    lg,
                                    level='error')
                out, err = command.get_command_output(
                    'service %s restart' % service, False, True)
                if not err and out:
                    logger.log_or_print('Service %s: %s' % (service, out),
                                        lg,
                                        level='debug')
                else:
                    logger.log_or_print('Service %s error : %s' %
                                        (service, err),
                                        lg,
                                        level='error')

            admin_vol_name, err = config.get_admin_vol_name()
            if err:
                raise Exception(err)

            # Get the config dir - the mount point.
            config_dir, err = config.get_config_dir()
            if err:
                raise Exception(err)

            ag, err = grid_ops.is_admin_gridcell()
            if err:
                raise Exception(err)

            admin_gridcells, err = grid_ops.get_admin_gridcells()
            if err:
                raise Exception(err)

            is_pooled = False
            peer_list, err = gluster_trusted_pools.get_peer_list()
            if peer_list:
                is_pooled = True

            is_mounted = False
            # mount only if the localhost is pooled
            if is_pooled:
                is_mounted, err = grid_ops.is_admin_vol_mounted_local()
                if not is_mounted:
                    str = 'Admin volume is not mounted. Will attempt to mount now.'
                    logger.log_or_print(str, lg, level='error')

                    # Try to mount
                    (ret, rc), err = command.execute_with_rc(
                        'mount -t glusterfs localhost:/%s %s' %
                        (admin_vol_name, config_dir))
                    if err:
                        str = 'Mount from localhost failed.'
                        logger.log_or_print(str, lg, level='error')
                    elif (not err) and (rc == 0):
                        is_access, err = assert_admin_vol_mount()
                        if err:
                            raise Exception(err)

                        sync, is_change, error = sync_ctdb_files()
                        if error:
                            # It's only a best-effort, it will try next
                            # minute again.
                            pass
                        if sync == False:
                            #raise Exception (err)
                            pass

                        # Restart nginx
                        out, err = command.get_command_output(
                            'service nginx restart', False, True)
                        if not err and out:
                            logger.log_or_print('Service nginx: %s' % out,
                                                lg,
                                                level='debug')
                        else:
                            logger.log_or_print('Service nginx error : %s' %
                                                err,
                                                lg,
                                                level='error')
                        # Restart uwsgi
                        out, err = command.get_command_output(
                            'service uwsgi restart', False, True)
                        if not err and out:
                            logger.log_or_print('Service uwsgi: %s' % out,
                                                lg,
                                                level='debug')
                        else:
                            logger.log_or_print('Service uwsgi error : %s' %
                                                err,
                                                lg,
                                                level='error')

                        if ag:
                            # Restart salt-master
                            out, err = command.get_command_output(
                                'service salt-master restart', False, True)
                            if not err and out:
                                logger.log_or_print('Service salt-master: %s' %
                                                    out,
                                                    lg,
                                                    level='debug')
                            else:
                                logger.log_or_print(
                                    'Service salt-master error : %s' % err,
                                    lg,
                                    level='error')
                            # Restart salt-minion
                        out, err = command.get_command_output(
                            'service salt-minion restart', False, True)
                        if not err and out:
                            logger.log_or_print('Service salt-minion: %s' %
                                                out,
                                                lg,
                                                level='debug')
                        else:
                            logger.log_or_print(
                                'Service salt-minion error : %s' % err,
                                lg,
                                level='error')

                    str = 'Admin vol is mounted'
                    logger.log_or_print(str, lg, level='info')

                # Admin volume is mounted, perform required checks
                else:
                    sync, is_change, err = sync_ctdb_files()
                    if err:
                        raise Exception(err)
                    if sync == False:
                        raise Exception(err)

                    logger.log_or_print('Checking services', lg, level='debug')
                    service_list = ['nginx', 'ctdb', 'salt-minion']
                    if ag:
                        service_list.append('salt-master')

                    for service in service_list:
                        status, err = services_management.get_service_status(
                            [service])
                        if err:
                            raise Exception(err)
                        logger.log_or_print('Service %s status is %s' %
                                            (service, status['status_code']),
                                            lg,
                                            level='debug')
                        if status['status_code'] != 0:
                            logger.log_or_print(
                                'Service %s is not active, restarting' %
                                service,
                                lg,
                                level='error')
                            out, err = command.get_command_output(
                                'service %s restart' % service, False, True)
                            if not err and out:
                                logger.log_or_print('Service %s: %s' %
                                                    (service, out),
                                                    lg,
                                                    level='debug')
                            else:
                                logger.log_or_print('Service %s error : %s' %
                                                    (service, err),
                                                    lg,
                                                    level='error')

                    # UWSGI service config not complete so need to check
                    # against the actual process name
                    (ret, rc), err = command.execute_with_rc('pidof uwsgi',
                                                             shell=True)
                    if rc != 0:
                        logger.log_or_print(
                            'Service uwsgi is not active, restarting',
                            lg,
                            level='error')
                        out, err = command.get_command_output(
                            'service uwsgi restart', False, True)
                        if not err and out:
                            logger.log_or_print('Service uwsgi: %s' % out,
                                                lg,
                                                level='debug')
                        else:
                            logger.log_or_print('Service uwsgi error : %s' %
                                                err,
                                                lg,
                                                level='error')

                    str = 'Admin volume is already mounted'
                    logger.log_or_print(str, lg, level='info')

    except Exception, e:
        st = 'Error mounting admin volume : %s' % e
        logger.log_or_print(st, lg, level='critical')
        return False, st
def add_user_authentication(target_name, authentication_type, username,
                            password):
    try:
        tid = -1
        if not authentication_type:
            raise Exception('No authentication type specified')
        if not target_name:
            raise Exception('No target specified')
        if not username:
            raise Exception('No username specified')
        if not password:
            raise Exception('No password specified')

        target, err = get_target(target_name)
        if err:
            raise Exception(err)
        if not target:
            raise Exception('Specified target not found.')

        cmd1 = 'tgtadm --lld iscsi --mode account --op new --user %s --password %s' % (
            username, password)
        (ret, rc), err = command.execute_with_rc(cmd1)
        if err:
            raise Exception(err)
        if rc != 0:
            err = ''
            tl, er = command.get_output_list(ret)
            if er:
                raise Exception(er)
            if tl:
                err = ','.join(tl)
            tl, er = command.get_error_list(ret)
            if er:
                raise Exception(er)
            if tl:
                err = err + ','.join(tl)
            raise Exception(err)

        cmd2 = 'tgtadm --lld iscsi --mode account --op bind --tid %d --user %s' % (
            target['tid'], username)
        if authentication_type == 'outgoing':
            cmd2 += ' --outgoing'

        (ret, rc), err = command.execute_with_rc(cmd2)
        if err:
            raise Exception(err)
        if rc != 0:
            err = ''
            tl, er = command.get_output_list(ret)
            if er:
                raise Exception(er)
            if tl:
                err = ','.join(tl)
            tl, er = command.get_error_list(ret)
            if er:
                raise Exception(er)
            if tl:
                err = err + ','.join(tl)
            raise Exception(err)
        new_user_dict = {}
        new_user_dict['iqn'] = target['iqn']
        new_user_dict['username'] = username
        new_user_dict['type'] = authentication_type
        new_user_dict['pswd'] = password
        conf, err = _generate_targets_conf(new_user_dict)
        if err:
            raise Exception(err)

    except Exception, e:
        return False, 'Error Adding User: %s' % str(e)
def configure_networking():

    try:
        os.system('clear')
        change_ip = False
        change_netmask = False
        change_default_gateway = False
        change_bonding_type = False
        change_jumbo_frames = False

        interfaces, err = networking.get_interfaces()
        if err:
            raise Exception(err)

        if 'bond0' not in interfaces.keys():
            if_list = interfaces.keys()
            ret, err = networking.create_bond('bond0', if_list, 6)
            if err:
                raise Exception(err)
            config_changed = True

        ip_info, err = networking.get_ip_info('bond0')
        if err:
            ip_info = None

        '''
    if not ip_info :
      raise Exception("No bonding configured! Incorrect configuration. : %s"%err)
    '''

        config_changed = False
        if ip_info:
            ip = ip_info["ipaddr"]
            str_to_print = 'Enter IP address (currently "%s", press enter to retain current value) : ' % ip
        else:
            ip = None
            str_to_print = "Enter IP address (currently not configured) : "

        valid_input = False
        while not valid_input:
            input = raw_input(str_to_print)
            if input:
                vi, err = networking.validate_ip(input)
                if vi:
                    valid_input = True
                    ip = input
                    change_ip = True
                    config_changed = True
            else:
                if ip:
                    # IP already existed and they are now not changing it so
                    # its ok.
                    valid_input = True
            if not valid_input:
                print "Invalid value. Please try again."
        print

        if ip_info:
            netmask = ip_info["netmask"]
            str_to_print = 'Enter netmask (currently "%s", press enter to retain current value) : ' % netmask
        else:
            netmask = None
            str_to_print = "Enter netmask (currently not set) : "

        valid_input = False
        while not valid_input:
            input = raw_input(str_to_print)
            if input:
                vi, err = networking.validate_netmask(input)
                if vi:
                    valid_input = True
                    netmask = input
                    change_netmask = True
                    config_changed = True
            else:
                if netmask:
                    # IP already existed and they are now not changing it so
                    # its ok.
                    valid_input = True
            if not valid_input:
                print "Invalid value. Please try again."
        print

        default_gateway = None
        if ip_info and "default_gateway" in ip_info:
            default_gateway = ip_info["default_gateway"]
        else:
            default_gateway = None
        if default_gateway:
            str_to_print = 'Enter the default gateway\'s IP address (currently "%s", press enter to retain current value) : ' % default_gateway
        else:
            str_to_print = "Enter the default gateway's IP address (currently not set, press enter to retain current value) : "
        valid_input = False
        while not valid_input:
            input = raw_input(str_to_print)
            if input:
                vi, err = networking.validate_ip(input)
                if vi:
                    valid_input = True
                    default_gateway = input
                    change_default_gateway = True
                    config_changed = True
            else:
                valid_input = True
            if not valid_input:
                print "Invalid value. Please try again."
        print

        hostname = socket.gethostname()
        str_to_print = 'Enter the GRIDCell hostname (currently "%s", press enter to retain current value) : ' % hostname
        valid_input = False
        change_hostname = False
        while not valid_input:
            input = raw_input(str_to_print)
            if input:
                vi, err = networking.validate_hostname(input)
                if vi:
                    valid_input = True
                    hostname = input
                    change_hostname = True
                    config_changed = True
            else:
                valid_input = True
            if not valid_input:
                print "Invalid value. Please try again."
        print

        bonding_type, err = networking.get_bonding_type('bond0')
        print "Ethernet NIC bonding configuration"
        print "----------------------------------"
        print "Ethernet bonding aggregates the bandwidth of all available ethernet ports giving high throughput and failover."
        print "We support two modes. "
        print "The first is LACP (also called 802.3ad) which requires configuration on any switch). The second is balance-alb which does not require switch configuration but may not be supported on all switches. "
        valid_input = False
        while not valid_input:
            print "Valid choices for this selection  are 4 (for 802.3ad or LACP) and 6 (for balance-alb)."
            print
            if bonding_type == -1:
                str_to_print = "Enter bonding mode (currently not configured, press enter to retain current value) : "
            else:
                str_to_print = "Enter bonding mode (currently %s, press enter to retain current value) : " % bonding_type
            input = raw_input(str_to_print)
            if input:
                if input.lower() in ['4', '6']:
                    valid_input = True
                    bonding_type = int(input)
                    change_bonding_type = True
                    config_changed = True
            else:
                valid_input = True
            if not valid_input:
                print "Invalid value. Please try again."
        print

        jfe, err = networking.is_enabled_jumbo_frames('bond0')
        if jfe:
            jumbo_frames = 'y'
            jfe_str = "enabled"
        else:
            jumbo_frames = 'n'
            jfe_str = "disabled"
        print "Jumbo frames support"
        print "--------------------"
        print "Enabling jumbo frames improves network throughput but requires configuration on the switch side."
        print "If you enable it here, please set the MTU size on the switch to 9000"
        valid_input = False
        while not valid_input:
            str_to_print = "Enable jumbo frames (currently %s, press enter to retain current value) (y/n)? : " % jfe_str
            print
            input = raw_input(str_to_print)
            if input:
                if input.lower() in ['y', 'n']:
                    valid_input = True
                    jumbo_frames = input.lower()
                    change_jumbo_frames = True
                    config_changed = True
            else:
                valid_input = True
            if not valid_input:
                print "Invalid value. Please try again."
        print

        print "Final confirmation"
        print "------------------"

        print
        print "The following are the choices that you have made :"
        print "IP address : %s" % ip
        print "Net Mask : %s" % netmask
        print "Hostname : %s" % hostname
        print "Default gateway : %s" % default_gateway
        if bonding_type == 4:
            print "NIC Bonding mode : LACP"
        elif bonding_type == 6:
            print "NIC Bonding mode : balance-alb"
        else:
            print "NIC Bonding mode (unsupported!!) : %d" % bonding_type

        print "Enable jumbo frames : %s" % jumbo_frames

        if not config_changed:
            print
            print
            raw_input(
                'No changes have been made to the configurations. Press enter to return to the main menu.')
            return 0

        str_to_print = 'Commit the above changes? (y/n) :'

        commit = 'n'
        valid_input = False
        while not valid_input:
            input = raw_input(str_to_print)
            if input:
                if input.lower() in ['y', 'n']:
                    valid_input = True
                    commit = input.lower()
            if not valid_input:
                print "Invalid value. Please try again."
        print

        if commit == 'y':
            print "Committing changes!"
        else:
            print "Discarding changes!"

        if commit != 'y':
            return 0

        restart_networking = False
        ip_dict = {}
        errors = []

        if change_ip or change_netmask or change_default_gateway or change_jumbo_frames:
            ip_dict["ip"] = ip
            ip_dict["netmask"] = netmask
            ip_dict["default_gateway"] = default_gateway
            if jumbo_frames == 'y':
                ip_dict["mtu"] = 9000
            else:
                ip_dict["mtu"] = 1500
            rc, err = networking.update_bond_ip(ip_dict)
            if not rc:
                if err:
                    errors.append("Error setting IP configuration : %s" % err)
                else:
                    errors.append("Error setting IP configuration ")
            restart_networking = True

        if change_hostname:
            ret, err = networking.update_hostname(hostname, 'integralstor.lan')
            if err:
                raise Exception(err)
            restart_networking = True

        if change_hostname or change_ip:
            ret, err = networking.update_hosts_file_entry(hostname, ip)
            if err:
                raise Exception(err)

        restart = False
        if restart_networking:
            print
            print
            valid_input = False
            while not valid_input:
                str_to_print = 'Restart network services now (y/n) :'
                print
                input = raw_input(str_to_print)
                if input:
                    if input.lower() in ['y', 'n']:
                        valid_input = True
                        if input.lower() == 'y':
                            restart = True
                if not valid_input:
                    print "Invalid value. Please try again."
            print
        if restart:
            (r, rc), err = command.execute_with_rc('service network restart')
            if err:
                raise Exception(err)
            if rc == 0:
                print "Network service restarted succesfully."
            else:
                print "Error restarting network services."
                raw_input('Press enter to return to the main menu')
                return -1
    except Exception, e:
        print "Error configuring network settings : %s" % e
        return -1
Example #39
0
def process_batch(d, file, logger=None):
    # Process each batch file

    try:
        batch_files_path, err = config.get_batch_files_path()
        if err:
            raise Exception(err)
        if not d:
            raise Exception("Error: No JSON info in %s/%s" %
                            (batch_files_path, file))

        # if not d["process"] in ["replace_sled", "volume_rebalance", "factory_defaults_reset"]:
        #  raise Exception("Error! Unknown process in %s/%s"%(batch_files_path, file))

        if not "start_time" in d:
            # Update when this process was started
            d["start_time"] = time.strftime("%a %b %d %H:%M:%S %Y",
                                            time.localtime())

        if d["status"] != "In progress":
            d["status"] = "In progress"

        # ADD CODE HERE TO MOVE IT TO COMPLETED DIR IF STATUS IS COMPLETE

        # Not committed so process the file
        for cd in d["command_list"]:

            # Remove old err_msg because we are reprocessing
            cd.pop("err_msg", None)

            # Status codes explanation -
            # 3 - complete
            # 0 - not yet run
            # 1 - in progress
            # -1 - error executing command

            if cd["status_code"] == 3:
                # Completed so skip to next command
                continue

            # Else failed or not done so do it

            if cd["type"] == "volume_heal_full":
                # No XML output so do some dirty text processing.
                try:
                    r = None
                    r, err = command.execute(cd["command"])
                    if err:
                        raise Exception(err)
                    lines = []
                    if r:
                        lines, err = command.get_output_list(r)
                        if err:
                            raise Exception(err)
                    else:
                        raise Exception('')

                    # Now start processing output of command
                    if lines:
                        for line in lines:
                            # print line
                            m = re.search("has been successful", line)
                            # print m
                            if m:
                                # print 'success'
                                # Successfully executed
                                ret, err = audit.audit(cd["type"], cd["desc"],
                                                       'Batch job')
                                cd["status_code"] = 3
                                break
                        if cd["status_code"] != 3:
                            # Stop executing more commands!
                            raise Exception("Got : %s" % line)
                    else:
                        # No output from command execution so flag error
                        raise Exception(
                            "Volume heal did not seem to kick off properly. Please make sure the volume is started."
                        )
                except Exception, e:
                    cd["status_code"] = -1
                    cd["err_msg"] = "Error executing volume heal command : %s" % str(
                        e)
                    # Stop executing more commands!
                    break

            elif cd["type"] == "brick_delete":
                # Need to execute a shell command to remotely delete a brick.
                try:
                    (ret, rc), err = command.execute_with_rc(cd["command"])
                    if rc == 0:
                        cd["status_code"] = 3
                    else:
                        raise Exception(
                            "Error deleting the volume brick : %s %s" %
                            (ret[0], ret[1]))
                except Exception, e:
                    cd["status_code"] = -1
                    cd["err_msg"] = "Error executing brick delete command : %s" % str(
                        e)
                    # Stop executing more commands!
                    break
def add_user_authentication(target_name, authentication_type, username, password):
    try:
        tid = -1
        if not authentication_type:
            raise Exception('No authentication type specified')
        if not target_name:
            raise Exception('No target specified')
        if not username:
            raise Exception('No username specified')
        if not password:
            raise Exception('No password specified')

        target, err = get_target(target_name)
        if err:
            raise Exception(err)
        if not target:
            raise Exception('Specified target not found.')

        cmd1 = 'tgtadm --lld iscsi --mode account --op new --user %s --password %s' % (
            username, password)
        (ret, rc), err = command.execute_with_rc(cmd1)
        if err:
            raise Exception(err)
        if rc != 0:
            err = ''
            tl, er = command.get_output_list(ret)
            if er:
                raise Exception(er)
            if tl:
                err = ','.join(tl)
            tl, er = command.get_error_list(ret)
            if er:
                raise Exception(er)
            if tl:
                err = err + ','.join(tl)
            raise Exception(err)

        cmd2 = 'tgtadm --lld iscsi --mode account --op bind --tid %d --user %s' % (
            target['tid'], username)
        if authentication_type == 'outgoing':
            cmd2 += ' --outgoing'

        (ret, rc), err = command.execute_with_rc(cmd2)
        if err:
            raise Exception(err)
        if rc != 0:
            err = ''
            tl, er = command.get_output_list(ret)
            if er:
                raise Exception(er)
            if tl:
                err = ','.join(tl)
            tl, er = command.get_error_list(ret)
            if er:
                raise Exception(er)
            if tl:
                err = err + ','.join(tl)
            raise Exception(err)
        new_user_dict = {}
        new_user_dict['iqn'] = target['iqn']
        new_user_dict['username'] = username
        new_user_dict['type'] = authentication_type
        new_user_dict['pswd'] = password
        conf, err = _generate_targets_conf(new_user_dict)
        if err:
            raise Exception(err)

    except Exception, e:
        return False, 'Error Adding User: %s' % str(e)
def process_tasks(node=socket.getfqdn()):
    """When called, processes/runs subtasks of each entry from tasks table if they satisfy/pass the required checks like status, last_run_time, retries, etc."""
    '''
    TODO
        - Needs a better docstring comment beriefly explaning what the function does
    '''
    try:

        db_path, err = config.get_db_path()
        if err:
            raise Exception(err)

        current_user = getpass.getuser()
        now = int(time.time())

        tasks_query = "select * from tasks where node == '" + node + \
            "' and (status == 'error-retrying' or status == 'queued') and (initiate_time <= '%d');" % (now)
        # print tasks_query
        tasks_to_process, err = db.get_multiple_rows(db_path, tasks_query)
        if err:
            raise Exception(err)
        # print tasks_to_process

        if tasks_to_process is not None:

            for task in tasks_to_process:
                # print 'Processing task ', task['task_id']

                if task['last_run_time']:
                    seconds_since_last_run = (now - task['last_run_time'])
                    # retry_interval is in minutes!
                    if seconds_since_last_run < task['retry_interval'] * 60:
                        continue

                # Mark the task as running
                cmd = "update tasks set status = 'running', last_run_time=%d where task_id = '%d'" % (
                    now, task['task_id'])
                status, err = db.execute_iud(db_path, [
                    [cmd],
                ],
                                             get_rowid=True)
                # print status, err
                if err:
                    raise Exception(err)

                audit_str = "%s" % task['description']
                audit.audit("task_start",
                            audit_str,
                            None,
                            system_initiated=True)

                attempts = task['attempts']
                run_as_user_name = task['run_as_user_name']

                # Now process subtasks for the task
                subtasks_query = "select * from subtasks where task_id == '%d' and (status == 'error-retrying' or status == 'queued') order by subtask_id" % task[
                    'task_id']
                subtasks, err = db.get_multiple_rows(db_path, subtasks_query)
                if err:
                    raise Exception(err)

                # Assume task is complete unless proven otherwise
                task_completed = True

                # Iteriate through all the unfinished subtasks related to the
                # task
                for subtask in subtasks:
                    # print 'subtask is ', subtask

                    subtask_id = subtask["subtask_id"]

                    status_update = "update subtasks set status = 'running' where subtask_id = '%d' and status is not 'cancelled'" % subtask_id
                    status, err = db.execute_iud(db_path, [
                        [status_update],
                    ],
                                                 get_rowid=True)
                    if err:
                        task_completed = False
                        break
                    # print subtask['command']
                    # print 'username is ', task['run_as_user_name']

                    # Now actually execute the command
                    # print 'not current user'
                    # This task is not to be meant to be executed by the current user so switch to that user
                    # print 'command is ', row['command']
                    (out, return_code), err = command.execute_with_rc(
                        subtask["command"],
                        shell=True,
                        run_as_user_name=run_as_user_name)
                    # print out, return_code, err

                    if out[0]:
                        output = re.sub("'", "", ''.join(out[0]))
                    else:
                        output = None
                    if out[1]:
                        error = re.sub("'", "", ''.join(out[1]))
                    else:
                        error = None

                    if return_code == 0:
                        # This means the command was successful. So update to
                        # completed
                        status_update = "update subtasks set status = 'completed', return_code='%d' where subtask_id = '%d' and status is not 'cancelled';" % (
                            return_code, subtask_id)
                        status, err = db.execute_iud(db_path, [
                            [status_update],
                        ],
                                                     get_rowid=True)
                        if err:
                            task_completed = False
                            break
                        else:
                            continue
                    else:
                        # Subtask command failed
                        if attempts > 1 or attempts == -2:
                            status_update = 'update subtasks set status = "error-retrying", return_code="%d" where subtask_id = "%d" and status is not "cancelled";' % (
                                return_code, subtask_id)
                        elif attempts in [0, 1]:
                            status_update = 'update scheduler_commands set status = "failed", return_code="%d"" where subtask_id = "%d" and status is not "cancelled";' % (
                                return_code, subtask_id)
                        execute, err = db.execute_iud(db_path, [
                            [status_update],
                        ],
                                                      get_rowid=True)
                        task_completed = False
                        break

                if task_completed:
                    status_update = "update tasks set status = 'completed' where task_id = '%d'" % task[
                        'task_id']
                else:
                    if attempts > 1:
                        status_update = "update tasks set status = 'error-retrying', attempts = %d where task_id = '%d' and status is not 'cancelled'" % (
                            attempts - 1, task['task_id'])
                    elif attempts == -2:
                        status_update = "update tasks set status = 'error-retrying', attempts = %d where task_id = '%d' and status is not 'cancelled'" % (
                            -2, task['task_id'])
                    else:
                        status_update = "update tasks set status = 'failed', attempts = '%d' where task_id = '%d' and status is not 'cancelled'" % (
                            0, task['task_id'])
                status, err = db.execute_iud(db_path, [
                    [status_update],
                ],
                                             get_rowid=True)
                if err:
                    raise Exception(err)

                if task_completed:
                    audit.audit("task_complete",
                                audit_str,
                                None,
                                system_initiated=True)
                else:
                    audit.audit("task_fail",
                                audit_str,
                                None,
                                system_initiated=True)

    except Exception as e:
        return False, 'Error processing tasks : %s' % e
    else:
        return True, None
def mount_and_configure():
    lg = None
    try:
        lg, err = logger.get_script_logger(
            'Admin volume mounter', '/var/log/integralstor/scripts.log', level=logging.DEBUG)

        logger.log_or_print(
            'Admin volume mounter initiated.', lg, level='info')

        pog, err = grid_ops.is_part_of_grid()
        if err:
            raise Exception(err)

        # If the localhost is part of the gridcell, proceed
        if pog:
            logger.log_or_print('Checking glusterd service', lg, level='debug')
            service = 'glusterd'
            status, err = services_management.get_service_status([service])
            if err:
                raise Exception(err)
            logger.log_or_print('Service %s status is %s' % (
                service, status['status_code']), lg, level='debug')
            if status['status_code'] != 0:
                logger.log_or_print(
                    'Service %s not started so restarting' % service, lg, level='error')
                out, err = command.get_command_output(
                    'service %s restart' % service, False, True)
                if not err and out:
                    logger.log_or_print('Service %s: %s' %
                                        (service, out), lg, level='debug')
                else:
                    logger.log_or_print('Service %s error : %s' % (
                        service, err), lg, level='error')

            admin_vol_name, err = config.get_admin_vol_name()
            if err:
                raise Exception(err)

            # Get the config dir - the mount point.
            config_dir, err = config.get_config_dir()
            if err:
                raise Exception(err)

            ag, err = grid_ops.is_admin_gridcell()
            if err:
                raise Exception(err)

            admin_gridcells, err = grid_ops.get_admin_gridcells()
            if err:
                raise Exception(err)

            is_pooled = False
            peer_list, err = gluster_trusted_pools.get_peer_list()
            if peer_list:
                is_pooled = True

            is_mounted = False
            # mount only if the localhost is pooled
            if is_pooled:
                is_mounted, err = grid_ops.is_admin_vol_mounted_local()
                if not is_mounted:
                    str = 'Admin volume is not mounted. Will attempt to mount now.' 
                    logger.log_or_print(str, lg, level='error')

                    # Try to mount
                    (ret, rc), err = command.execute_with_rc(
                        'mount -t glusterfs localhost:/%s %s' % (admin_vol_name, config_dir))
                    if err:
                        str = 'Mount from localhost failed.' 
                        logger.log_or_print(str, lg, level='error')
                    elif (not err) and (rc == 0):
                        is_access, err = assert_admin_vol_mount()
                        if err:
                            raise Exception(err)

                        sync, is_change, error = sync_ctdb_files()
                        if error:
                            # It's only a best-effort, it will try next
                            # minute again.
                            pass
                        if sync == False:
                            #raise Exception (err)
                            pass

                        # Restart nginx
                        out, err = command.get_command_output(
                            'service nginx restart', False, True)
                        if not err and out:
                            logger.log_or_print(
                                'Service nginx: %s' % out, lg, level='debug')
                        else:
                            logger.log_or_print(
                                'Service nginx error : %s' % err, lg, level='error')
                        # Restart uwsgi
                        out, err = command.get_command_output(
                            'service uwsgi restart', False, True)
                        if not err and out:
                            logger.log_or_print(
                                'Service uwsgi: %s' % out, lg, level='debug')
                        else:
                            logger.log_or_print(
                                'Service uwsgi error : %s' % err, lg, level='error')

                        if ag:
                            # Restart salt-master
                            out, err = command.get_command_output(
                                'service salt-master restart', False, True)
                            if not err and out:
                                logger.log_or_print(
                                    'Service salt-master: %s' % out, lg, level='debug')
                            else:
                                logger.log_or_print(
                                    'Service salt-master error : %s' % err, lg, level='error')
                            # Restart salt-minion
                        out, err = command.get_command_output(
                            'service salt-minion restart', False, True)
                        if not err and out:
                            logger.log_or_print(
                                'Service salt-minion: %s' % out, lg, level='debug')
                        else:
                            logger.log_or_print(
                                'Service salt-minion error : %s' % err, lg, level='error')

                    str = 'Admin vol is mounted'
                    logger.log_or_print(str, lg, level='info')

                # Admin volume is mounted, perform required checks
                else:
                    sync, is_change, err = sync_ctdb_files()
                    if err:
                        raise Exception(err)
                    if sync == False:
                        raise Exception(err)

                    logger.log_or_print('Checking services', lg, level='debug')
                    service_list = ['nginx','ctdb','salt-minion']
                    if ag:
                        service_list.append('salt-master')

                    for service in service_list:
                        status, err = services_management.get_service_status([
                                                                             service])
                        if err:
                            raise Exception(err)
                        logger.log_or_print('Service %s status is %s' % (
                            service, status['status_code']), lg, level='debug')
                        if status['status_code'] != 0:
                            logger.log_or_print(
                                'Service %s is not active, restarting' % service, lg, level='error')
                            out, err = command.get_command_output(
                                'service %s restart' % service, False, True)
                            if not err and out:
                                logger.log_or_print('Service %s: %s' % (
                                    service, out), lg, level='debug')
                            else:
                                logger.log_or_print('Service %s error : %s' % (
                                    service, err), lg, level='error')

                    # UWSGI service config not complete so need to check
                    # against the actual process name
                    (ret, rc), err = command.execute_with_rc(
                        'pidof uwsgi', shell=True)
                    if rc != 0:
                        logger.log_or_print(
                            'Service uwsgi is not active, restarting', lg, level='error')
                        out, err = command.get_command_output(
                            'service uwsgi restart', False, True)
                        if not err and out:
                            logger.log_or_print(
                                'Service uwsgi: %s' % out, lg, level='debug')
                        else:
                            logger.log_or_print(
                                'Service uwsgi error : %s' % err, lg, level='error')

                    str = 'Admin volume is already mounted'
                    logger.log_or_print(str, lg, level='info')

    except Exception, e:
        st = 'Error mounting admin volume : %s' % e
        logger.log_or_print(st, lg, level='critical')
        return False, st
def process_batch(d, file, logger=None):
    # Process each batch file

    try:
        batch_files_path, err = config.get_batch_files_path()
        if err:
            raise Exception(err)
        if not d:
            raise Exception("Error: No JSON info in %s/%s" %
                            (batch_files_path, file))

        # if not d["process"] in ["replace_sled", "volume_rebalance", "factory_defaults_reset"]:
        #  raise Exception("Error! Unknown process in %s/%s"%(batch_files_path, file))

        if not "start_time" in d:
            # Update when this process was started
            d["start_time"] = time.strftime(
                "%a %b %d %H:%M:%S %Y", time.localtime())

        if d["status"] != "In progress":
            d["status"] = "In progress"

        # ADD CODE HERE TO MOVE IT TO COMPLETED DIR IF STATUS IS COMPLETE

        # Not committed so process the file
        for cd in d["command_list"]:

            # Remove old err_msg because we are reprocessing
            cd.pop("err_msg", None)

            # Status codes explanation -
            # 3 - complete
            # 0 - not yet run
            # 1 - in progress
            # -1 - error executing command

            if cd["status_code"] == 3:
                # Completed so skip to next command
                continue

            # Else failed or not done so do it

            if cd["type"] == "volume_heal_full":
                # No XML output so do some dirty text processing.
                try:
                    r = None
                    r, err = command.execute(cd["command"])
                    if err:
                        raise Exception(err)
                    lines = []
                    if r:
                        lines, err = command.get_output_list(r)
                        if err:
                            raise Exception(err)
                    else:
                        raise Exception('')

                    # Now start processing output of command
                    if lines:
                        for line in lines:
                            # print line
                            m = re.search("has been successful", line)
                            # print m
                            if m:
                                # print 'success'
                                # Successfully executed
                                ret, err = audit.audit(
                                    cd["type"], cd["desc"], 'Batch job')
                                cd["status_code"] = 3
                                break
                        if cd["status_code"] != 3:
                            # Stop executing more commands!
                            raise Exception("Got : %s" % line)
                    else:
                        # No output from command execution so flag error
                        raise Exception(
                            "Volume heal did not seem to kick off properly. Please make sure the volume is started.")
                except Exception, e:
                    cd["status_code"] = -1
                    cd["err_msg"] = "Error executing volume heal command : %s" % str(
                        e)
                    # Stop executing more commands!
                    break

            elif cd["type"] == "brick_delete":
                # Need to execute a shell command to remotely delete a brick.
                try:
                    (ret, rc), err = command.execute_with_rc(cd["command"])
                    if rc == 0:
                        cd["status_code"] = 3
                    else:
                        raise Exception(
                            "Error deleting the volume brick : %s %s" % (ret[0], ret[1]))
                except Exception, e:
                    cd["status_code"] = -1
                    cd["err_msg"] = "Error executing brick delete command : %s" % str(
                        e)
                    # Stop executing more commands!
                    break
Example #44
0
def configure_minion():

    try:
        os.system('clear')
        minion_opts = salt.config.minion_config('/etc/salt/minion')
        masters = []
        if minion_opts and 'master' in minion_opts:
            masters = minion_opts['master']

        masters_list = []
        if type(masters) is not list:
            masters_list.append(masters)
        else:
            masters_list = masters

        print '\n\n\nBootstrap admin agent\n'
        print '----------------------\n\n\n'
        config_changed = False
        str_to_print = 'Enter the IP address of the initial Admin GRIDCell : '

        valid_input = False
        ip = None
        while not valid_input:
            input = raw_input(str_to_print)
            if input:
                vi, err = networking.validate_ip(input.strip())
                if err:
                    raise Exception(err)
                if vi:
                    ip = input.strip()
                    valid_input = True
                else:
                    print "Invalid value. Please try again."
            else:
                print "Invalid value. Please try again."
            print

        print "Final confirmation"
        print "------------------"

        print
        print "You have entered : %s" % input
        print

        str_to_print = 'Commit the above changes? (y/n) :'

        commit = 'n'
        valid_input = False
        while not valid_input:
            input = raw_input(str_to_print)
            if input:
                if input.lower() in ['y', 'n']:
                    valid_input = True
                    commit = input.lower()
            if not valid_input:
                print "Invalid value. Please try again."
        print

        if commit == 'y':
            print "Committing changes!"
        else:
            print "Discarding changes!"

        if commit == 'y':
            platform_root, err = config.get_platform_root()
            if err:
                raise Exception(err)

            f1 = open('%s/defaults/salt/minion_part_1' % platform_root, 'r')
            f2 = open('%s/defaults/salt/minion_part_2' % platform_root, 'r')
            with open('/tmp/minion', 'w') as fw:
                fw.write(f1.read())
                fw.write('master: %s\n' % ip)
                fw.write('\n')
                fw.write(f2.read())
            f2.close()
            f1.close()
            shutil.move('/tmp/minion', '/etc/salt/minion')
            print
            print 'Restarting admin agent service'
            (r,
             rc), err = command.execute_with_rc('service salt-minion restart')
            if err:
                raise Exception(err)
            if rc == 0:
                print "Admin agent (salt minion) service restarted succesfully."
            else:
                raise Exception(
                    "Error restarting admin agent (salt minion) services.")

    except Exception, e:
        print "Error configuring network settings : %s" % e
        return -1
def configure_minion():

    try:
        os.system('clear')
        minion_opts = salt.config.minion_config('/etc/salt/minion')
        masters = []
        if minion_opts and 'master' in minion_opts:
            masters = minion_opts['master']

        masters_list = []
        if type(masters) is not list:
            masters_list.append(masters)
        else:
            masters_list = masters

        print '\n\n\nBootstrap admin agent\n'
        print '----------------------\n\n\n'
        config_changed = False
        str_to_print = 'Enter the IP address of the initial Admin GRIDCell : '

        valid_input = False
        ip = None
        while not valid_input:
            input = raw_input(str_to_print)
            if input:
                vi, err = networking.validate_ip(input.strip())
                if err:
                    raise Exception(err)
                if vi:
                    ip = input.strip()
                    valid_input = True
                else:
                    print "Invalid value. Please try again."
            else:
                print "Invalid value. Please try again."
            print

        print "Final confirmation"
        print "------------------"

        print
        print "You have entered : %s" % input
        print

        str_to_print = 'Commit the above changes? (y/n) :'

        commit = 'n'
        valid_input = False
        while not valid_input:
            input = raw_input(str_to_print)
            if input:
                if input.lower() in ['y', 'n']:
                    valid_input = True
                    commit = input.lower()
            if not valid_input:
                print "Invalid value. Please try again."
        print

        if commit == 'y':
            print "Committing changes!"
        else:
            print "Discarding changes!"

        if commit == 'y':
            platform_root, err = config.get_platform_root()
            if err:
                raise Exception(err)

            f1 = open('%s/defaults/salt/minion_part_1' % platform_root, 'r')
            f2 = open('%s/defaults/salt/minion_part_2' % platform_root, 'r')
            with open('/tmp/minion', 'w') as fw:
                fw.write(f1.read())
                fw.write('master: %s\n' % ip)
                fw.write('\n')
                fw.write(f2.read())
            f2.close()
            f1.close()
            shutil.move('/tmp/minion', '/etc/salt/minion')
            print
            print 'Restarting admin agent service'
            (r, rc), err = command.execute_with_rc(
                'service salt-minion restart')
            if err:
                raise Exception(err)
            if rc == 0:
                print "Admin agent (salt minion) service restarted succesfully."
            else:
                raise Exception(
                    "Error restarting admin agent (salt minion) services.")

    except Exception, e:
        print "Error configuring network settings : %s" % e
        return -1
Example #46
0
def generate_default_secondary_named_conf(primary_ip, secondary_netmask, secondary_ip, generate_forwarders=False, forwarder_ip=None):
    """Generates the named.conf to be used in the secondary

    primary_ip -- The IP address of the primary
    secondary_ip -- The IP address of the secondary
    secondary_netmask -- The netmask of the primary
    generate_forwarders -- If True then generate a forwarders entry to <forwarder_ip>
    forwarder_ip -- The forwarder IP address if a forwarder entry is needed
    """
    rc = 0
    try:
        secondary_cidr_netmask, err = get_subnet_in_cidr_format(
            primary_ip, secondary_netmask)
        if err:
            raise Exception(err)

        with open('/etc/named.conf', 'w') as f:
            f.write('// Generated by the IntegralStor script\n')
            f.write('options {\n')
            f.write('  listen-on port 53 { any; };\n')
            f.write('  listen-on-v6 port 53 { ::1; };\n')
            f.write('  directory 	"/var/named";\n')
            f.write('  dump-file 	"/var/named/data/cache_dump.db";\n')
            f.write('  statistics-file "/var/named/data/named_stats.txt";\n')
            f.write('  memstatistics-file "/var/named/data/named_mem_stats.txt";\n')
            f.write(
                '  allow-query     { localhost; %s; };\n' % secondary_cidr_netmask)
            if generate_forwarders:
                f.write("   forwarders    { %s; };\n" % forwarder_ip)
                f.write('  recursion yes;\n')
            else:
                f.write('  recursion no;\n')
            f.write('};\n')

            f.write('logging {\n')
            f.write('  channel default_debug {\n')
            f.write('    file "data/named.run";\n')
            f.write('    severity dynamic;\n')
            f.write('  };\n')
            f.write('};\n')

            f.write('zone "." IN {\n')
            f.write('  type hint;\n')
            f.write('  file "named.ca";\n')
            f.write('};\n')

            f.write('zone "integralstor.lan" IN {\n')
            f.write('  type slave;\n')
            f.write('  file "slaves/integralstor.for";\n')
            f.write('  masters { %s; };\n' % primary_ip)
            f.write('};\n')
            f.write('include "/etc/named.rfc1912.zones";\n')
            f.flush()
        f.close()
        (r, rc), err = command.execute_with_rc('service named restart')
        if err:
            raise Exception(err)
        if rc != 0:
            print "Error restarting the DNS server"
    except Exception, e:
        return False,  "Error generating the DNS slave configuration file : %s" % e
Example #47
0
def configure_interface():

    try:
        os.system('clear')
        interfaces, err = networking.get_interfaces()
        if err:
            raise Exception(
                'Error retrieving interface information : %s' % err)
        if not interfaces:
            raise Exception('No interfaces detected')
        print
        print
        print 'IntegralSTOR interface configuration'
        print '--------------------------------------------'
        print
        print
        print 'Current network interfaces : '
        print
        for if_name, iface in interfaces.items():
            if if_name.startswith('lo'):
                continue
            print '- %s' % if_name
        print

        valid_input = False
        while not valid_input:
            ifname = raw_input(
                'Enter the name of the interface that you wish to configure : ')
            if ifname not in interfaces or ifname.startswith('lo'):
                print 'Invalid interface name'
            else:
                valid_input = True
        print
        ip_info, err = networking.get_ip_info(ifname)
        '''
    if err:
      raise Exception('Error retrieving interface information : %s'%err)
    '''
        if ip_info:
            ip = ip_info["ipaddr"]
            netmask = ip_info["netmask"]
            if "default_gateway" in ip_info:
                gateway = ip_info["default_gateway"]
            else:
                gateway = None
        else:
            ip = None
            netmask = None
            gateway = None

        old_boot_proto, err = networking.get_interface_bootproto(ifname)
        if err:
            raise Exception(
                'Error retrieving interface information : %s' % err)
            time.sleep(5)

        config_changed = False

        str_to_print = "Configure for DHCP or static addressing (dhcp/static)? : "
        valid_input = False
        while not valid_input:
            input = raw_input(str_to_print)
            if input:
                if input.lower() in ['static', 'dhcp']:
                    valid_input = True
                    boot_proto = input.lower()
                    if boot_proto != old_boot_proto:
                        config_changed = True
            if not valid_input:
                print "Invalid value. Please try again."
        print

        if boot_proto == 'static':
            if ip:
                str_to_print = "Enter IP address (currently %s, press enter to retain current value) : " % ip
            else:
                str_to_print = "Enter IP address (currently not set) : "
            valid_input = False
            while not valid_input:
                input = raw_input(str_to_print)
                if input:
                    ok, err = networking.validate_ip(input)
                    if err:
                        raise Exception('Error validating IP : %s' % err)
                    if ok:
                        valid_input = True
                        ip = input
                        config_changed = True
                elif ip:
                    valid_input = True
                if not valid_input:
                    print "Invalid value. Please try again."
            print

            if netmask:
                str_to_print = "Enter netmask (currently %s, press enter to retain current value) : " % netmask
            else:
                str_to_print = "Enter netmask (currently not set) : "
            valid_input = False
            while not valid_input:
                input = raw_input(str_to_print)
                if input:
                    ok, err = networking.validate_netmask(input)
                    if err:
                        raise Exception('Error validating netmask : %s' % err)
                    if ok:
                        valid_input = True
                        netmask = input
                        config_changed = True
                elif netmask:
                    valid_input = True
            if not valid_input:
                print "Invalid value. Please try again."
            print

            if gateway:
                str_to_print = "Enter gateway (currently %s, press enter to retain current value) : " % gateway
            else:
                str_to_print = "Enter gateway (currently not set) : "
            valid_input = False
            while not valid_input:
                input = raw_input(str_to_print)
                if input:
                    ok, err = networking.validate_ip(input)
                    if err:
                        raise Exception('Error validating gateway : %s' % err)
                    if ok:
                        valid_input = True
                        gateway = input
                        config_changed = True
                elif gateway:
                    valid_input = True
                if not valid_input:
                    print "Invalid value. Please try again."
            print
        if config_changed:
            d = {}
            d['addr_type'] = boot_proto
            if boot_proto == 'static':
                d['ip'] = ip
                d['netmask'] = netmask
                d['default_gateway'] = gateway
            ret, err = networking.update_interface_ip(ifname, d)
            if not ret:
                if err:
                    raise Exception(
                        'Error changing interface address : %s' % err)
                else:
                    raise Exception('Error changing interface address')

            restart = False
            print
            print
            valid_input = False
            while not valid_input:
                str_to_print = 'Restart network services now (y/n) :'
                print
                input = raw_input(str_to_print)
                if input:
                    if input.lower() in ['y', 'n']:
                        valid_input = True
                        if input.lower() == 'y':
                            restart = True
                if not valid_input:
                    print "Invalid value. Please try again."
            print

            if restart:
                ret, err = networking.restart_networking()
                if not ret:
                    if err:
                        raise Exception(err)
                    else:
                        raise Exception("Couldn't restart.")

                use_salt, err = config.use_salt()
                if err:
                    raise Exception(err)
                if use_salt:
                    (r, rc), err = command.execute_with_rc(
                        'service salt-minion restart')
                    if err:
                        raise Exception(err)
                    if rc == 0:
                        print "Salt minion service restarted succesfully."
                    else:
                        print "Error restarting salt minion services."
                        raw_input('Press enter to return to the main menu')
                        return -1
        else:
            print
            print
            raw_input(
                'No changes have been made to the configurations. Press enter to return to the main menu.')
            return 0

    except Exception, e:
        print "Error configuring network settings : %s" % e
        return -1
def display_status():

    try:
        hostname = socket.gethostname()
        use_salt, err = config.use_salt()
        if err:
            raise Exception(err)
        if use_salt:
            print "Salt master service status :",
            (r,
             rc), err = command.execute_with_rc('service salt-master status')
            if err:
                raise Exception(err)
            l, err = command.get_output_list(r)
            if err:
                raise Exception(err)
            if l:
                print '\n'.join(l)
            else:
                l, err = command.get_error_list(r)
                if err:
                    raise Exception(err)
                if l:
                    print '\n'.join(l)
            print "Salt minion service status :",
            (r,
             rc), err = command.execute_with_rc('service salt-minion status')
            if err:
                raise Exception(err)
            l, err = command.get_output_list(r)
            if err:
                raise Exception(err)
            if l:
                print '\n'.join(l)
            else:
                l, err = command.get_error_list(r)
                if err:
                    raise Exception(err)
                print l
                if l:
                    print '\n'.join(l)
        print "Samba service status :",
        (r, rc), err = command.execute_with_rc('service smb status')
        if err:
            raise Exception(err)
        l, err = command.get_output_list(r)
        if err:
            raise Exception(err)
        if l:
            print '\n'.join(l)
        else:
            l, err = command.get_error_list(r)
            if err:
                raise Exception(err)
            if l:
                print '\n'.join(l)
        print "Winbind service status :",
        (r, rc), err = command.execute_with_rc('service winbind status')
        if err:
            raise Exception(err)
        l, err = command.get_output_list(r)
        if err:
            raise Exception(err)
        if l:
            print '\n'.join(l)
        else:
            l, err = command.get_error_list(r)
            if err:
                raise Exception(err)
            if l:
                print '\n'.join(l)
    except Exception, e:
        print "Error displaying system status : %s" % e
        return -1
def generate_default_secondary_named_conf(primary_ip,
                                          secondary_netmask,
                                          secondary_ip,
                                          generate_forwarders=False,
                                          forwarder_ip=None):
    """Generates the named.conf to be used in the secondary

    primary_ip -- The IP address of the primary
    secondary_ip -- The IP address of the secondary
    secondary_netmask -- The netmask of the primary
    generate_forwarders -- If True then generate a forwarders entry to <forwarder_ip>
    forwarder_ip -- The forwarder IP address if a forwarder entry is needed
    """
    rc = 0
    try:
        secondary_cidr_netmask, err = get_subnet_in_cidr_format(
            primary_ip, secondary_netmask)
        if err:
            raise Exception(err)

        with open('/etc/named.conf', 'w') as f:
            f.write('// Generated by the IntegralStor script\n')
            f.write('options {\n')
            f.write('  listen-on port 53 { any; };\n')
            f.write('  listen-on-v6 port 53 { ::1; };\n')
            f.write('  directory 	"/var/named";\n')
            f.write('  dump-file 	"/var/named/data/cache_dump.db";\n')
            f.write('  statistics-file "/var/named/data/named_stats.txt";\n')
            f.write(
                '  memstatistics-file "/var/named/data/named_mem_stats.txt";\n'
            )
            f.write('  allow-query     { localhost; %s; };\n' %
                    secondary_cidr_netmask)
            if generate_forwarders:
                f.write("   forwarders    { %s; };\n" % forwarder_ip)
                f.write('  recursion yes;\n')
            else:
                f.write('  recursion no;\n')
            f.write('};\n')

            f.write('logging {\n')
            f.write('  channel default_debug {\n')
            f.write('    file "data/named.run";\n')
            f.write('    severity dynamic;\n')
            f.write('  };\n')
            f.write('};\n')

            f.write('zone "." IN {\n')
            f.write('  type hint;\n')
            f.write('  file "named.ca";\n')
            f.write('};\n')

            f.write('zone "integralstor.lan" IN {\n')
            f.write('  type slave;\n')
            f.write('  file "slaves/integralstor.for";\n')
            f.write('  masters { %s; };\n' % primary_ip)
            f.write('};\n')
            f.write('include "/etc/named.rfc1912.zones";\n')
            f.flush()
        f.close()
        (r, rc), err = command.execute_with_rc('service named restart')
        if err:
            raise Exception(err)
        if rc != 0:
            print "Error restarting the DNS server"
    except Exception, e:
        return False, "Error generating the DNS slave configuration file : %s" % e