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)
示例#2
0
def services_status():
    s = {}
    try:
        platform, err = common.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)
def restart_samba_services():
  try:
    use_salt, err = common.use_salt()
    if err:
      raise Exception(err)
    if use_salt:
      client = salt.client.LocalClient()
      rc = client.cmd('*', 'service.reload', ['smbd'] )
      print rc
      rc = client.cmd('*', 'service.reload', ['winbind'] )
      print rc
      #rc = client.cmd('*', 'service.reload', ['nmbd'] )
      #print rc
    else:
      (ret, rc), err = command.execute_with_rc('service smb reload')
      if err:
        raise Exception(err)
      (ret, rc), err = command.execute_with_rc('service winbind reload')
      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("Return code : %d. Error : %s"%(rc, err))
      (ret, rc), err = command.execute_with_rc('service nmb reload')
      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("Return code : %d. Error : %s"%(rc, err))
  except Exception, e:
    return False, 'Error restarting samba services: %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.fractalio.integralstor-unicell:%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 change_password(userid, pswd):
  ret, rc = command.execute_with_conf_and_rc(r'smbpasswd -s %s'%(userid), "%s\n%s"%(pswd, pswd))
  if rc != 0:
    print ret
    #print command.get_error_list(ret)
    raise Exception("Error changing password. Return code : %d. "%rc)
  ul = command.get_output_list(ret)
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 _get_service_status(service):
  d = {}
  try:
    (ret, rc), err = command.execute_with_rc('service %s status'%service[0])
    if err:
      raise Exception(err)
    d['status_code'] = rc
    if rc == 0:
      d['status_str'] = 'Running'
    elif rc == 3:
      d['status_str'] = 'Stopped'
    elif rc == 1:
      d['status_str'] = 'Error'
    d['output_str'] = ''
    out, err = command.get_output_list(ret)
    if err:
      raise Exception(err)
    if out:
      d['output_str'] += ','.join(out)
    err, e = command.get_error_list(ret)
    if e:
      raise Exception(e)
    if err:
      d['output_str'] += ','.join(err)
  except Exception, e:
    return None, 'Error retrieving service status : %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.fractalio.integralstor_unicell:%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_ad_users_or_groups(type):
  d = load_auth_settings()
  workgroup = d['workgroup']
  if type and type=="users":
    c = command.execute_with_rc("wbinfo -u --domain=%s"%workgroup)
  elif type and type=="groups":
    c = command.execute_with_rc("wbinfo -g --domain=%s"%workgroup)
  else:
    raise Exception("Unknown type specified to retrieve AD users or groups.")

  o = command.get_output_list(c[0])
  #print "wbinfo output = "
  #print o
  e = command.get_error_list(c[0])
  #print "error = "
  #print e
  if c[1] != 0:
    err = ""
    if o:
      err += " ".join(o)
      err += ". "
    if e:
      err += " ".join(e)
    raise Exception("Error getting AD users: %s."%err)
  else:
    return o
示例#10
0
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)
示例#11
0
def get_all_disks_by_name():

    #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)
示例#12
0
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.')

        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 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.')

    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)
示例#14
0
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 create_local_user(userid, name, pswd):

  error_list = []
  #First check if samba user exists. if so kick out
  ul = get_local_users()
  if ul:
    for ud in ul:
      if ud["userid"] == userid:
        raise Exception("Error creating user. The user \"%s\" already exists. "%userid)

  # Now check if system user exists. If not create..
  create_system_user = False
  try:
    pwd.getpwnam(userid)
  except KeyError:
    create_system_user = True

  if create_system_user:
    #enc_pswd = crypt.crypt(pswd, "28")
    #Set a standard system password - not the one given by the user as the user should not have access to the system
    enc_pswd = crypt.crypt("integralstor_pswd_%s"%userid, "28")
    client = salt.client.LocalClient()
    rc = client.cmd('*', 'user.add', [userid,None,501])
    for hostname, status in rc.items():
      if not status:
        error_list.append("Error creating the userid on GRIDCell %s"%hostname)
    rc = client.cmd('*', 'shadow.set_password', [userid, enc_pswd] )
    for hostname, status in rc.items():
      if not status:
        error_list.append("Error setting the password for userid on GRIDCell %s"%hostname)
    rc = client.cmd('*', 'user.chfullname', [userid, "integralstor_user_%s"%name] )
    for hostname, status in rc.items():
      if not status:
        error_list.append("Error setting the name for userid on GRIDCell %s"%hostname)
    '''
    ret, rc = command.execute_with_rc(r'useradd -p %s -c integralstor_user_%s %s'%(enc_pswd, name, userid))
    if rc != 0:
      raise Exception("Error creating system user. Return code : %d. "%rc)
    '''

  print '/usr/bin/pdbedit  -d 1 -t -a  -u %s -f %s'%(userid, name), "%s\n%s"%(pswd, pswd)
  # Now all set to create samba user
  ret, rc = command.execute_with_conf_and_rc(r'/usr/bin/pdbedit  -d 1 -t -a  -u %s -f %s'%(userid, name), "%s\n%s"%(pswd, pswd))
  if rc != 0:
    #print command.get_error_list(ret)
    raise Exception("Error creating user. Return code : %d. "%rc)
  ul = command.get_output_list(ret)
  #print ul
  return error_list
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)
示例#17
0
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)
        (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 LUN : %s' % str(e)
示例#18
0
def change_password(username, pswd):
  try:
    if not username:
      raise Exception('No username specified')
    d, err = get_local_user(username)
    if not d:
      if err:
        raise Exception('Error locating user : %s'%err)
      else:
        raise Exception('Error locating user')
    lines, err = command.get_command_output(r'echo %s:%s|chpasswd'%(username, pswd), shell=True)
    if err:
      raise Exception(err)

    if 'smb_user' in d and d['smb_user']:
      #lines, err = command.get_command_output(r'smbpasswd -s %s'%(username), "%s\n%s"%(pswd, pswd))
      (ret, rc), err = command.execute_with_conf_and_rc(r'smbpasswd -s %s'%(username), "%s\n%s"%(pswd, pswd))
      if rc == 0:
        lines, er = command.get_output_list(ret)
        if er:
          raise Exception(er)
      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)
        raise Exception(err)
  except Exception, e:
    return False, 'Error changing local user password : %s'%str(e)
def change_password(username, pswd):
  try:
    if not username:
      raise Exception('No username specified')
    d, err = get_local_user(username)
    if not d:
      if err:
        raise Exception('Error locating user : %s'%err)
      else:
        raise Exception('Error locating user')
    lines, err = command.get_command_output(r'echo \"%s:%s\"|chpasswd'%(username, pswd))
    if err:
      raise Exception(err)

    if 'smb_user' in d and d['smb_user']:
      #lines, err = command.get_command_output(r'smbpasswd -s %s'%(username), "%s\n%s"%(pswd, pswd))
      (ret, rc), err = command.execute_with_conf_and_rc(r'smbpasswd -s %s'%(username), "%s\n%s"%(pswd, pswd))
      if rc == 0:
        lines, er = command.get_output_list(ret)
        if er:
          raise Exception(er)
      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)
        raise Exception(err)
  except Exception, e:
    return False, 'Error changing local user password : %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)
    (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 LUN : %s'%str(e)
def get_local_users():
  print "Comes Here"
  ret, rc = command.execute_with_rc("/usr/bin/pdbedit -d 1 -L")
  print ret
  if rc != 0:
    raise "Error retrieving user list. Return code : %d"%rc

  ul = command.get_output_list(ret)
  user_list = []
  for u in ul:
    l = u.split(':')
    if l:
      d = {}
      d["userid"] = l[0]
      if len(l) > 1:
        d["name"] = l[2]
      user_list.append(d)
  print user_list
  return user_list
示例#22
0
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)
示例#24
0
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)
示例#25
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)
示例#26
0
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 create_local_user(username, name, pswd, gid = None, smb_user=True):

  try:

    #First check if user exists. if so kick out
    ul, err = get_local_users()
    if ul:
      for ud in ul:
        if ud["username"] == username:
          raise Exception("Error creating user. The user \"%s\" already exists. "%username)
    elif err:
      raise Exception("Error retrieving user list : %s"%err)

  
    enc_pswd = crypt.crypt(pswd, "28")
    use_salt, err = common.use_salt()
    if err:
      raise Exception(err)
    if use_salt:
      client = salt.client.LocalClient()
      if gid:
        rc = client.cmd('*', 'user.add', [username, None, gid])
      else:
        rc = client.cmd('*', 'user.add', [username])
      #print rc
      if not rc:
        error_list.append("Error creating the username")
      for hostname, status in rc.items():
        if not status:
          error_list.append("Error creating the username on node"%hostname)
      rc = client.cmd('*', 'shadow.set_password', [username, enc_pswd] )
      for hostname, status in rc.items():
        if not status:
          error_list.append("Error setting the password for username on GRIDCell %s"%hostname)
      rc = client.cmd('*', 'user.chfullname', [username, "integralstor_user_%s"%name] )
      for hostname, status in rc.items():
        if not status:
          error_list.append("Error setting the name for username on node %s"%hostname)
    else:
      #print '1'
      if gid:
        cmd_to_run = 'useradd -g %s -p %s -c integralstor_user_%s %s'%(gid, enc_pswd, name, username)
      else:
        cmd_to_run = 'useradd  -p %s -c integralstor_user_%s %s'%(enc_pswd, name, username)
      lines, err = command.get_command_output(cmd_to_run)
      #print '2'
      #print lines, err
      if err:
        raise Exception(err)
  
    if smb_user:
      #print '/usr/bin/pdbedit  -d 1 -t -a  -u %s -f %s'%(username, name), "%s\n%s"%(pswd, pswd)
      # Now all set to create samba user
      #print '3'
      #lines, err = command.get_command_output(r'/usr/bin/pdbedit  -d 1 -t -a  -u %s -f %s'%(username, name), "%s\n%s"%(pswd, pswd))
      (ret, rc), err = command.execute_with_conf_and_rc(r'/usr/bin/pdbedit  -d 1 -t -a  -u %s -f %s'%(username, name), "%s\n%s"%(pswd, pswd))
      if rc == 0:
        lines, er = command.get_output_list(ret)
        if er:
          raise Exception(er)
      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)
        raise Exception(err)
      #print '4'
      #print lines, err
  except Exception, e:
    return False, 'Error creating local user : %s'%str(e)
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 display_status():

  try :
    hostname = socket.gethostname()
    use_salt, err = common.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 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)
示例#31
0
def create_local_user(username, name, pswd, gid = None, smb_user=True):

  try:

    #First check if user exists. if so kick out
    ul, err = get_local_users()
    if ul:
      for ud in ul:
        if ud["username"] == username:
          raise Exception("Error creating user. The user \"%s\" already exists. "%username)
    elif err:
      raise Exception("Error retrieving user list : %s"%err)

  
    enc_pswd = crypt.crypt(pswd, "28")
    use_salt, err = common.use_salt()
    if err:
      raise Exception(err)
    if use_salt:
      client = salt.client.LocalClient()
      if gid:
        rc = client.cmd('*', 'user.add', [username, None, gid])
      else:
        rc = client.cmd('*', 'user.add', [username])
      #print rc
      if not rc:
        error_list.append("Error creating the username")
      for hostname, status in rc.items():
        if not status:
          error_list.append("Error creating the username on node"%hostname)
      rc = client.cmd('*', 'shadow.set_password', [username, enc_pswd] )
      for hostname, status in rc.items():
        if not status:
          error_list.append("Error setting the password for username on GRIDCell %s"%hostname)
      rc = client.cmd('*', 'user.chfullname', [username, "integralstor_user_%s"%name] )
      for hostname, status in rc.items():
        if not status:
          error_list.append("Error setting the name for username on node %s"%hostname)
    else:
      #print '1'
      if gid:
        cmd_to_run = 'useradd -g %s -p %s -c integralstor_user_%s %s'%(gid, enc_pswd, name, username)
      else:
        cmd_to_run = 'useradd  -p %s -c integralstor_user_%s %s'%(enc_pswd, name, username)
      lines, err = command.get_command_output(cmd_to_run)
      #print '2'
      #print lines, err
      if err:
        raise Exception(err)
  
    if smb_user:
      #print '/usr/bin/pdbedit  -d 1 -t -a  -u %s -f %s'%(username, name), "%s\n%s"%(pswd, pswd)
      # Now all set to create samba user
      #print '3'
      #lines, err = command.get_command_output(r'/usr/bin/pdbedit  -d 1 -t -a  -u %s -f %s'%(username, name), "%s\n%s"%(pswd, pswd))
      (ret, rc), err = command.execute_with_conf_and_rc(r'/usr/bin/pdbedit  -d 1 -t -a  -u %s -f %s'%(username, name), "%s\n%s"%(pswd, pswd))
      if rc == 0:
        lines, er = command.get_output_list(ret)
        if er:
          raise Exception(er)
      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)
        raise Exception(err)
      #print '4'
      #print lines, err
  except Exception, e:
    return False, 'Error creating local user : %s'%str(e)
def display_status():

    try:
        hostname = socket.gethostname()
        use_salt, err = common.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