def remove_file(filename, server=''): """ This module removes the given file @paramater: * filename - <str> absolute path name of the file to be created * server - <str> (optional) name of the server to write the file. If not given, the function takes the first node from config file @Returns: True, on success False, on failure """ if server == '': server = tc.nodes[0] try: conn = tc.get_connection(server, 'root') if conn == -1: tc.logger.error("Unable to get connection to 'root' of node %s" \ % server) return False if conn.modules.os.path.exists(filename): conn.modules.os.remove(filename) except: tc.logger.error("Exception occured while removing file %s" % filename) return False finally: conn.close() return True
def create_geo_passwordless_ssh(mnode, snode, gsuser=''): """ Sets up the password less ssh between two specified nodes Returns True if successfull and False on failure """ if gsuser == '': gsuser = '******' loc = "/var/lib/glusterd/geo-replication/" mconn = tc.get_connection(mnode, user='******') sconn = tc.get_connection(snode, user=gsuser) if not mconn.modules.os.path.isfile('/root/.ssh/id_rsa'): if not mconn.modules.os.path.isfile('%s/secret.pem' % loc): tc.logger.debug("id_rsa not present. Generating with gsec_create") ret = tc.run(mnode, "gluster system:: execute gsec_create") if ret[0] != 0: tc.logger.error("Unable to generate the secret pem file") return False tc.logger.debug("Copying the secret.pem to id_rsa") mconn.modules.shutil.copyfile("%s/secret.pem" % loc, \ "/root/.ssh/id_rsa") tc.logger.debug("Copying the secret.pem.pub to id_rsa.pub") mconn.modules.shutil.copyfile("%s/secret.pem.pub" % loc, \ "/root/.ssh/id_rsa.pub") try: slocal = sconn.modules.os.path.expanduser('~') sfh = sconn.builtin.open("%s/.ssh/authorized_keys" % slocal, "a") with mconn.builtin.open("/root/.ssh/id_rsa.pub", 'r') as f: for line in f: sfh.write(line) except: tc.logger.error("Unable to establish passwordless ssh %s@%s to %s@%s" \ % ('root', mnode, gsuser, snode)) return False finally: sfh.close() mconn.close() sconn.close() tc.logger.debug("Password less ssh setup from %s@%s to %s@%s is %s" \ % ('root', mnode, gsuser, snode, 'successfull')) return True
def write_file(filename, file_contents=" ", create_mode='', filesize='', server=''): """ This module writes the file along with file contents @paramater: * filename - <str> absolute path name of the file to be created * file_contents - <str> (optional) file content * create_mode - <str> (optional) mode to create the file * filesize - <str> (optional) filesize * server - <str> (optional) name of the server to write the file. If not given, the function takes the first node from config file @Returns: True, on success False, on failure """ if server == '': server = tc.nodes[0] if create_mode == '': create_mode = 'open' if create_mode != 'open': try: conn = tc.get_connection(server, 'root') if conn == -1: tc.logger.error("Unable to get connection to 'root' of node %s" \ % server) return False if not conn.modules.os.path.exists(conn.modules.os.path.dirname(filename)): conn.modules.os.makedirs(conn.modules.os.path.dirname(filename)) except: tc.logger.error("Exception occured while creating directory for file %s" % filename) return False finally: conn.close() if create_mode == 'open': try: conn = tc.get_connection(server, 'root') if conn == -1: tc.logger.error("Unable to get connection to 'root' of node %s" \ % server) return False if not conn.modules.os.path.exists(conn.modules.os.path.dirname(filename)): conn.modules.os.makedirs(conn.modules.os.path.dirname(filename)) with conn.builtin.open(filename, 'w') as _filehandle: _filehandle.write(file_contents) except: tc.logger.error("Exception occured while writing file %s" % filename) return False finally: conn.close() elif create_mode == 'echo': cmd = "echo " + file_contents + " > " + filename ret, _, _ = tc.run(server, cmd) if ret != 0: return False elif create_mode == 'touch': cmd = "touch " + filename ret, _, _ = tc.run(server, cmd) if ret != 0: return False elif create_mode == 'dd': if filesize == '': tc.logger.error("Invalid argument for dd cmd. Pass correct \ number of parameters") return False cmd = "dd if=/dev/zero of=%s bs=%s count=1" % (filename, filesize) ret, _, _ = tc.run(server, cmd) if ret != 0: return False return True