Example #1
0
def writeLastMntFile(client_mount_path, server_mount_path):
	try:
		f = open(LAST_MNT_FILENAME, 'w')
		f.write(client_mount_path)
		f.write(server_mount_path)
		f.close()
	except Exception as err:
		np.print_msg("An error occured writing the previous mount file: {0}".format(err), MessageLevel.ERROR)
		result = ''
Example #2
0
def move(src, dest):
    result = True
    try:
        shutil.move(src, dest)
        np.print_msg("Moved {0} to {1}".format(src, dest), MessageLevel.INFO)
    except Exception as err:
        np.print_msg("Failed to move {0} to {1} ...\n\t{2}".format(src, dest, err), MessageLevel.ERROR)
        result = False

    return result
Example #3
0
def writeLastMntFile(client_mount_path, server_mount_path):
    try:
        f = open(LAST_MNT_FILENAME, 'w')
        f.write(client_mount_path)
        f.write(server_mount_path)
        f.close()
    except Exception as err:
        np.print_msg(
            "An error occured writing the previous mount file: {0}".format(
                err), MessageLevel.ERROR)
        result = ''
Example #4
0
def bindServerSocket(port):
	server_sock = None
	try:
		server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		server_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
		server_sock.bind((HOST, port))
		np.print_msg("Successfully bound server socket to port:{0}".format(PORT), MessageLevel.INFO)
	except Exception as err:
		np.print_msg("Failed to bind server socket to port:{0}".format(PORT), MessageLevel.ERROR)
		server_sock = None

	return server_sock
Example #5
0
def move(src, dest):
    result = True
    try:
        shutil.move(src, dest)
        np.print_msg("Moved {0} to {1}".format(src, dest), MessageLevel.INFO)
    except Exception as err:
        np.print_msg(
            "Failed to move {0} to {1} ...\n\t{2}".format(src, dest, err),
            MessageLevel.ERROR)
        result = False

    return result
Example #6
0
def getLastMntFileContents():
	result = ''
	try:
		if os.path.isfile(LAST_MNT_FILENAME):
			f = open(LAST_MNT_FILENAME, 'r')
			result = f.read()
			f.close()
	except Exception as err:
		np.print_msg("An error occured retrieving the previous mount file: {0}".format(err), MessageLevel.ERROR)
		result = ''

	return result
Example #7
0
def processConnection(conn, args):
    ncmd = conn.recv(ncmds.MAX_CMD_SIZE)
    quit, cmd_success = processCmd(ncmd, args)

    resp = processResponse(ncmd, cmd_success)
    if len(resp) > 0:
        try:
            conn.send(resp)
        except Exception as err:
            np.print_msg(msg, MessageLevel.ERROR)

    conn.close()
    return quit
Example #8
0
def processConnection(conn, args):
	ncmd = conn.recv(ncmds.MAX_CMD_SIZE)
	quit, cmd_success = processCmd(ncmd, args)

	resp = processResponse(ncmd, cmd_success)
	if len(resp) > 0:
		try:
			conn.send(resp)
		except Exception as err:
			np.print_msg(msg, MessageLevel.ERROR)

	conn.close()
	return quit
Example #9
0
def getLastMntFileContents():
    result = ''
    try:
        if os.path.isfile(LAST_MNT_FILENAME):
            f = open(LAST_MNT_FILENAME, 'r')
            result = f.read()
            f.close()
    except Exception as err:
        np.print_msg(
            "An error occured retrieving the previous mount file: {0}".format(
                err), MessageLevel.ERROR)
        result = ''

    return result
Example #10
0
def bindServerSocket(port):
    server_sock = None
    try:
        server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        server_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        server_sock.bind((HOST, port))
        np.print_msg(
            "Successfully bound server socket to port:{0}".format(PORT),
            MessageLevel.INFO)
    except Exception as err:
        np.print_msg("Failed to bind server socket to port:{0}".format(PORT),
                     MessageLevel.ERROR)
        server_sock = None

    return server_sock
Example #11
0
def main():
    # Get the port
    args = getArgs()
    server_port = PORT
    if args.port:
        server_port = args.port

    # Bind the sever socket
    server_sock = bindServerSocket(server_port)
    if server_sock:
        while True:
            conn = None
            try:
                conn, addr = acceptConnection(server_sock)
                np.print_msg(
                    "Successfully connected to client: {0}:{1}".format(
                        addr[0], PORT), MessageLevel.INFO)
            except socket.error as msg:
                np.print_msg(msg, MessageLevel.ERROR)
                conn = None

            if conn:
                quit = processConnection(conn, args)
                if quit:
                    np.print_msg(
                        "Server shutdown requested @ {0}...".format(
                            datetime.datetime.now()), MessageLevel.INFO)
                    break

    # Keep this at the end for safety!
    if server_sock:
        server_sock.close()
Example #12
0
def processCmd(ncmd, args):
	quit = False
	cmd_success = True
	np.print_msg("Received command: {0}".format(ncmd), MessageLevel.INFO)

	dest = ncmds.getCommandDest(ncmd)
	srcs = ncmds.getCommandSrcs(ncmd)
	
	if ncmds.isQuitSequence(ncmd):
		quit = True

	else:
		if args.validate_server_mount:
			srcs_valid = validatePaths(srcs, args.validate_server_mount)
			dest_valid = validatePath(dest, args.validate_server_mount)
			cmd_success = srcs_valid and dest_valid
	
		# Only try and conduct file operations when validation is disabled,
		# or if validation is enabled, and it passes.
		if cmd_success:	
			if ncmds.isMove(ncmd):
				for src in srcs:
					if not nfops.move(src, dest):
						cmd_success = False
	
			elif ncmds.isCopy(ncmd):
				for src in srcs:
					if not nfops.copy(src, dest):
						cmd_success = False
	
			elif ncmds.isRemove(ncmd):
				# The naming here isn't ideal, but this code gets the job done!
				for src in srcs:
					if not nfops.remove(src):
						cmd_success = False
	
				if not nfops.remove(dest):
					cmd_success = False

	return quit, cmd_success
Example #13
0
def processCmd(ncmd, args):
    quit = False
    cmd_success = True
    np.print_msg("Received command: {0}".format(ncmd), MessageLevel.INFO)

    dest = ncmds.getCommandDest(ncmd)
    srcs = ncmds.getCommandSrcs(ncmd)

    if ncmds.isQuitSequence(ncmd):
        quit = True

    else:
        if args.validate_server_mount:
            srcs_valid = validatePaths(srcs, args.validate_server_mount)
            dest_valid = validatePath(dest, args.validate_server_mount)
            cmd_success = srcs_valid and dest_valid

        # Only try and conduct file operations when validation is disabled,
        # or if validation is enabled, and it passes.
        if cmd_success:
            if ncmds.isMove(ncmd):
                for src in srcs:
                    if not nfops.move(src, dest):
                        cmd_success = False

            elif ncmds.isCopy(ncmd):
                for src in srcs:
                    if not nfops.copy(src, dest):
                        cmd_success = False

            elif ncmds.isRemove(ncmd):
                # The naming here isn't ideal, but this code gets the job done!
                for src in srcs:
                    if not nfops.remove(src):
                        cmd_success = False

                if not nfops.remove(dest):
                    cmd_success = False

    return quit, cmd_success
Example #14
0
def main():
	# Get the port
	args = getArgs()
	server_port = PORT
	if args.port:
		server_port = args.port

	# Bind the sever socket
	server_sock = bindServerSocket(server_port)
	if server_sock:
		while True:
			conn = None
			try:
				conn, addr = acceptConnection(server_sock)
				np.print_msg("Successfully connected to client: {0}:{1}".format(addr[0], PORT), MessageLevel.INFO)
			except socket.error as msg:
				np.print_msg(msg, MessageLevel.ERROR)
				conn = None
		
			if conn:
				quit = processConnection(conn, args)
				if quit:
					np.print_msg("Server shutdown requested @ {0}...".format(datetime.datetime.now()), MessageLevel.INFO)
					break
				
	# Keep this at the end for safety!
	if server_sock:
		server_sock.close()
Example #15
0
def copy(src, dest):
    result = True
    try:
        if isFile(src):
            shutil.copy(src, dest)
            np.print_msg("Copied file {0} to {1}".format(src, dest), MessageLevel.INFO)
        elif isDirectory(src):
            shutil.copytree(src, dest)
            np.print_msg("Copied folder {0} to {1}".format(src, dest), MessageLevel.INFO)
        else:
            np.print_msg("Failed to remove {0} ...\n\tNot a recognized file or folder.".format(src), MessageLevel.ERROR)
            result = False
    except Exception as err:
        np.print_msg("Failed to copy {0} to {1} ...\n\t{2}".format(src, dest, err), MessageLevel.ERROR)
        result = False

    return result
Example #16
0
def remove(target):
    result = True
    try:
        if isFile(target):
            os.remove(target)
            np.print_msg("Removed file {0}".format(target), MessageLevel.INFO)
        elif isDirectory(target):
            shutil.rmtree(target)
            np.print_msg("Removed folder {0}".format(target), MessageLevel.INFO)
        else:
            np.print_msg(
                "Failed to remove {0} ...\n\tNot a recognized file or folder.".format(target), MessageLevel.ERROR
            )
            result = False
    except Exception as err:
        np.print_msg("Failed to remove {0} ...\n\t{2}".format(target, err), MessageLevel.ERROR)
        result = False

    return result
Example #17
0
def remove(target):
    result = True
    try:
        if isFile(target):
            os.remove(target)
            np.print_msg("Removed file {0}".format(target), MessageLevel.INFO)
        elif isDirectory(target):
            shutil.rmtree(target)
            np.print_msg("Removed folder {0}".format(target),
                         MessageLevel.INFO)
        else:
            np.print_msg(
                "Failed to remove {0} ...\n\tNot a recognized file or folder.".
                format(target), MessageLevel.ERROR)
            result = False
    except Exception as err:
        np.print_msg("Failed to remove {0} ...\n\t{2}".format(target, err),
                     MessageLevel.ERROR)
        result = False

    return result
Example #18
0
def copy(src, dest):
    result = True
    try:
        if isFile(src):
            shutil.copy(src, dest)
            np.print_msg("Copied file {0} to {1}".format(src, dest),
                         MessageLevel.INFO)
        elif isDirectory(src):
            shutil.copytree(src, dest)
            np.print_msg("Copied folder {0} to {1}".format(src, dest),
                         MessageLevel.INFO)
        else:
            np.print_msg(
                "Failed to remove {0} ...\n\tNot a recognized file or folder.".
                format(src), MessageLevel.ERROR)
            result = False
    except Exception as err:
        np.print_msg(
            "Failed to copy {0} to {1} ...\n\t{2}".format(src, dest, err),
            MessageLevel.ERROR)
        result = False

    return result
Example #19
0
def main():
    args = getArgs()
    HOST = "192.168.1.189"
    PORT = 10123
    blocking = '1'
    if args.non_blocking:
        blocking = '0'

    # Parse the command.
    if not args.cmd in recognized_cmds:
        np.print_msg("Unrecognized command: {0}".format(args.cmd),
                     MessageLevel.ERROR)
        sys.exit()

    client_mnt = getClientMount(args.client_mount)
    server_mnt = getServerMount(args.server_mount)

    # Some debug messages to help the user figure out what their mounts are if not explicitly set.
    np.print_msg("Using client mount: {0}".format(client_mnt),
                 MessageLevel.DEBUG)
    np.print_msg("Using server mount: {0}".format(server_mnt),
                 MessageLevel.DEBUG)

    # Convert the client path to a server path, if possible
    sources = []
    destination = ''
    if (client_mnt and server_mnt):
        # Add sources we can parse -- in the future maybe we should error out and warn the user if we can't
        for src in args.src:
            converted_source = ncmd_fileops.convertPath(
                client_mnt, server_mnt, src)
            if converted_source:
                sources.append(converted_source)

        destination = ncmd_fileops.convertPath(client_mnt, server_mnt,
                                               args.dest)

    else:
        sources = args.src
        destination = args.dest

    quit = ncmds.isQuitCmd(args.cmd)
    if quit:
        ncmd = ncmds.getQuitSequence()
    else:
        ncmd = ncmds.genCommand(args.cmd, sources, destination, blocking)

    np.print_msg("Command to send: {0}".format(ncmd), MessageLevel.DEBUG)

    if args.port:
        PORT = args.port

    if args.host:
        HOST = args.host

    client_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    # Try the connection.
    try:
        client_sock.connect((HOST, PORT))
    except socket.error as msg:
        np.print_msg(msg, MessageLevel.ERROR)
        client_sock = None

    if client_sock:
        np.print_msg(
            "Successfully connected to host: {0}:{1}".format(HOST, PORT),
            MessageLevel.INFO)
        client_sock.sendall(ncmd)

        # Close the connection on the client when quitting to allow socket reuse!
        if quit:
            client_sock.close()
            client_sock = None

        # We only expect responses for blocking commands, and expect none for the quit cmd.
        if not args.non_blocking and not quit:
            nrsp = client_sock.recv(ncmds.MAX_CMD_SIZE)

            # If the recieved response is legit.
            if ncmds.isResponse(ncmd, nrsp):
                if ncmds.isSuccessfulRsp(nrsp):
                    np.print_msg(
                        "Command: {0} completed successfully.".format(ncmd),
                        MessageLevel.INFO)
                elif ncmds.isFailureRsp(nrsp):
                    np.print_msg(
                        "Command: {0} completed unsuccessfully.".format(ncmd),
                        MessageLevel.ERROR)
            else:
                np.print_msg(
                    "Something odd recieved, not a recognized response: {0}".
                    format(nrsp), MessageLevel.ERROR)

    # Keep this at the end for safety!
    if client_sock:
        client_sock.close()

    # Maybe a more sophisticated decesion here later -- like if the mount paths exist.
    writeLastMntFile(client_mnt, server_mnt)
Example #20
0
def main():
	args = getArgs()
	HOST="192.168.1.189"
	PORT=10123
	blocking = '1'
	if args.non_blocking:
		blocking = '0'

	# Parse the command.
	if not args.cmd in recognized_cmds:
		np.print_msg("Unrecognized command: {0}".format(args.cmd), MessageLevel.ERROR)
		sys.exit()
	
	client_mnt = getClientMount(args.client_mount)
	server_mnt = getServerMount(args.server_mount)
	
	# Some debug messages to help the user figure out what their mounts are if not explicitly set.
	np.print_msg("Using client mount: {0}".format(client_mnt), MessageLevel.DEBUG)
	np.print_msg("Using server mount: {0}".format(server_mnt), MessageLevel.DEBUG)

	# Convert the client path to a server path, if possible
	sources = []
	destination = ''
	if (client_mnt and server_mnt):
		# Add sources we can parse -- in the future maybe we should error out and warn the user if we can't
		for src in args.src:
			converted_source = ncmd_fileops.convertPath(client_mnt, server_mnt, src)
			if converted_source:
				sources.append(converted_source)
	
		destination = ncmd_fileops.convertPath(client_mnt, server_mnt, args.dest)

	else:
		sources = args.src
		destination = args.dest
		
	quit = ncmds.isQuitCmd(args.cmd)
	if quit:
		ncmd = ncmds.getQuitSequence()
	else:
		ncmd = ncmds.genCommand(args.cmd, sources, destination, blocking)

	np.print_msg("Command to send: {0}".format(ncmd), MessageLevel.DEBUG)

	if args.port:
		PORT = args.port

	if args.host:
		HOST = args.host

	client_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

	# Try the connection.
	try:
		client_sock.connect((HOST, PORT))
	except socket.error as msg:
		np.print_msg(msg, MessageLevel.ERROR)
		client_sock = None

	if client_sock:
		np.print_msg("Successfully connected to host: {0}:{1}".format(HOST, PORT), MessageLevel.INFO)
		client_sock.sendall(ncmd)

		# Close the connection on the client when quitting to allow socket reuse!
		if quit:
			client_sock.close()
			client_sock = None

		# We only expect responses for blocking commands, and expect none for the quit cmd.
		if not args.non_blocking and not quit:
			nrsp = client_sock.recv(ncmds.MAX_CMD_SIZE)

			# If the recieved response is legit.
			if ncmds.isResponse(ncmd, nrsp):
				if ncmds.isSuccessfulRsp(nrsp):
					np.print_msg("Command: {0} completed successfully.".format(ncmd), MessageLevel.INFO)
				elif ncmds.isFailureRsp(nrsp):
					np.print_msg("Command: {0} completed unsuccessfully.".format(ncmd), MessageLevel.ERROR)
			else:
				np.print_msg("Something odd recieved, not a recognized response: {0}".format(nrsp), MessageLevel.ERROR)

	# Keep this at the end for safety!
	if client_sock:
		client_sock.close()

	# Maybe a more sophisticated decesion here later -- like if the mount paths exist.
	writeLastMntFile(client_mnt, server_mnt)