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)
def change_password(userid, pswd): """Change the password for a local user account in samba. userid -- The username of the account pswd -- The password for the username """ try: (ret, rc), err = command.execute_with_conf_and_rc( r'smbpasswd -s %s' % (userid), "%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) if err: raise Exception(err) except Exception, e: return False, 'Error changing local users password :%s' % str(e)
def volume_start_stop(vol_name, operation): try: cmd = 'gluster volume %s %s' % (operation, vol_name) print cmd if operation == 'stop': (ret, rc), err = command.execute_with_conf_and_rc(cmd) 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) else: lines, err = command.get_command_output(cmd) # print 'a', ret, err if err: raise Exception(err) if lines: print '\n'.join(lines) except Exception, e: return False, 'Error performing volume %s : %s' % (operation, 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)
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)
def get_heal_count(cmd, type): # Gets the number of files healed so far rl = [] try: batch_files_path, err = config.get_batch_files_path() if err: raise Exception(err) r = None if type: cmd = cmd + " " + type r, err = command.execute(cmd) if err: raise Exception(err) lines = [] if r: lines, err = command.get_output_list(r) if err: raise Exception(err) if lines: for line in lines: m = re.search("Number of entries:[\s]*([\d]+)", line) # print m if m: found = True # print line try: count = int(m.groups()[0]) except Exception, e: return rl continue rl.append(count) # print rl except Exception, e: return None, 'Error getting heal count : %s' % str(e)
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 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 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 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 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 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_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 run_command_get_xml_output_tree(cmd): """Run a command that is expected to generate an XML output and return the XML tree cmd -- The command to run """ tree = None try: r, err = command.execute(cmd) if err: raise Exception(err) l, err = command.get_output_list(r) if err: raise Exception(err) el, err = command.get_error_list(r) if err: raise Exception(err) if el: raise Exception('\n'.join(el)) # No errors so proceed st = '\n'.join(l) tree = ElementTree.fromstring(st) except Exception, e: return None, 'Error obtaining XML tree from command output : %s' % str(e)
def run_command_get_xml_output_tree(cmd): """Run a command that is expected to generate an XML output and return the XML tree cmd -- The command to run """ tree = None try: r, err = command.execute(cmd) if err: raise Exception(err) l, err = command.get_output_list(r) if err: raise Exception(err) el, err = command.get_error_list(r) if err: raise Exception(err) if el: raise Exception('\n'.join(el)) # No errors so proceed st = '\n'.join(l) tree = ElementTree.fromstring(st) except Exception, e: return None, 'Error obtaining XML tree from command output : %s' % str( e)
e) # Stop executing more commands! break else: # Commands that have valid XML outputs temp = tempfile.TemporaryFile() try: print cd["command"] r, err = command.execute(cd["command"]) if err: raise Exception("Error executing %s" % cd["command"]) # print r if r: # print "command = %s"%cd["command"] l, err = command.get_output_list(r) if err: raise Exception(err) for line in l: temp.write(line) temp.seek(0) tree = ElementTree.parse(temp) else: raise Exception("Error executing %s" % cd["command"]) op_status = {} # Mark it as in progress cd["status_code"] = 1 root = tree.getroot()
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 create_local_user(userid, name, pswd): """Create a local user account in the linux system as well as in samba. This account cannot be used to ssh/login as the system pswd is not whats provided. userid -- The username of the account name -- The real world name associated with the account pswd -- The password for the username """ try: error_list = [] # First check if samba user exists. if so kick out ul, err = get_local_users() if err: raise Exception(err) if ul: for ud in ul: if ud["userid"] == userid: raise Exception( "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: # 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, 1000]) for hostname, status in rc.items(): if not status: error_list.append( "Error creating the userid on GRIDCell %s" % hostname) if error_list: raise Exception(','.join(error_list)) 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) if error_list: raise Exception(','.join(error_list)) 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) # Now all set to create samba user (ret, rc), err = 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: 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 creating local user :%s' % str(e)
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 _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(userid, name, pswd): """Create a local user account in the linux system as well as in samba. This account cannot be used to ssh/login as the system pswd is not whats provided. userid -- The username of the account name -- The real world name associated with the account pswd -- The password for the username """ try: error_list = [] # First check if samba user exists. if so kick out ul, err = get_local_users() if err: raise Exception(err) if ul: for ud in ul: if ud["userid"] == userid: raise Exception("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: # 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, 1000]) for hostname, status in rc.items(): if not status: error_list.append( "Error creating the userid on GRIDCell %s" % hostname) if error_list: raise Exception(','.join(error_list)) 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) if error_list: raise Exception(','.join(error_list)) 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) # Now all set to create samba user (ret, rc), err = 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: 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 creating local user :%s' % str(e)
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 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 = config.use_salt() if err: raise Exception(err) if use_salt: import salt.client 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)