Browse Source

Worked on integrating the new configuration system with the mount script for ssh drives

pull/11/head
jrtechs 6 years ago
parent
commit
2e7ceecbea
6 changed files with 146 additions and 56 deletions
  1. +15
    -12
      src/configuration.py
  2. +38
    -2
      src/module.py
  3. +75
    -25
      src/mount_ssh_drive.py
  4. +10
    -13
      src/quote.py
  5. +5
    -3
      src/roosay.py
  6. +3
    -1
      src/ssh_manager.py

+ 15
- 12
src/configuration.py View File

@ -39,18 +39,16 @@ def single_conf_input(param):
helper function for create_config() which reads the value of a single
file location from the user
"""
print("\nPlease enter the absolute path for your " + param + " file")
print("if you leave this blank, by default it will be "
print("\nPlease enter the absolute path for your " + param + " file if you leave this blank,")
print("by default it will be "
+ os.path.dirname(__file__) + "/" + param + ".txt")
# i = input("Enter selection:")
i = input("Enter selection:")
# if i.strip() == "":
# return os.path.dirname(__file__) + "/" + param + ".txt"
# else:
# return i
return param + ": " + os.path.dirname(__file__) + "/" + param + ".txt"
if i.strip() == "":
return param + ": " + os.path.dirname(__file__) + "/" + param + ".txt"
else:
return param + ": " + i
def create_config():
@ -65,6 +63,7 @@ def create_config():
f.write(single_conf_input("mounts") + '\n')
f.close()
def read_config():
"""
Reads the config file and creates a config dictionary
@ -74,6 +73,10 @@ def read_config():
with open(CONFIG_FILE) as file:
for line in file:
temp = line.split(" ")
if len(temp) >= 1:
temp[1] = temp[1].strip('\n')
if line.find("servers:") != -1:
if len(temp) <= 1:
print("Error reading servers file from config")
@ -102,13 +105,13 @@ def create_config_dependent_files(config):
"""
Finds missing files and creates them
"""
if not os.path.exists(config["servers"]):
if os.path.isfile(config["servers"]) == False:
print("Creating missing servers file in " + config["servers"])
module.create_empty_file(config["servers"])
if not os.path.exists(config["quotes"]):
if os.path.isfile(config["quotes"]) == False:
print("Creating missing quotes file in " + config["quotes"])
module.create_empty_file(config["quotes"])
if not os.path.exists(config["mounts"]):
if os.path.isfile(config["mounts"]) == False:
print("Creating missing mounts file in " + config["mounts"])
module.create_empty_file(config["mounts"])

+ 38
- 2
src/module.py View File

@ -12,7 +12,7 @@ def input_file(file_name):
:return: a string array containing the lines of INPUT_FILE
"""
f = []
with open(file_name) as file:
with open(file_name.strip('\n')) as file:
for line in file:
f.append(line.strip(' \t\n\r'))
return f
@ -49,4 +49,40 @@ def create_empty_file(file_name):
"""
simple function to mimic touch command
"""
subprocess.call(['touch', file_name])
file_name = file_name.replace('\n', '')
subprocess.call(['touch', file_name])
TOP_BAR = "**************************************"
def print_magenta(prt): return"\033[95m {}\033[00m" .format(prt)
def print_green(prt): return "\033[92m {}\033[00m" .format(prt)
def print_red(prt): return "\033[91m {}\033[00m" .format(prt)
def print_menu_option(s):
"""
Prints each host option
:param s:
:return:
"""
space = " " * (len(TOP_BAR) - 4 - len(s))
print(print_magenta("* ") + s + space + print_magenta("*"))
def print_menu(name, lines):
"""
"""
print(print_magenta(TOP_BAR))
print(print_magenta("*") + " " +
print_green(name) + " " + print_magenta("*"))
for s in lines:
print_menu_option(s)
print(print_magenta(TOP_BAR))

+ 75
- 25
src/mount_ssh_drive.py View File

@ -21,15 +21,6 @@ user@remote.server.address
MOUNT_FILE = configuration.get_config()["mounts"]
def add_drive_to_config(remote_connection, remote_mount_point, local_mount_point):
"""
Adds a new network drive to the default mount config file
"""
module.append_file(MOUNT_FILE, remote_connection)
module.append_file(MOUNT_FILE, remote_mount_point)
module.append_file(MOUNT_FILE, local_mount_point)
def mount_drive(remote_connection, remote_mount_point, local_mount_point):
"""
Calls sshfs to mount a remote connection on a local mount point over ssh
@ -47,40 +38,97 @@ def mount_drives():
"""
Mounts all the ssh drives in the configuration file
"""
with open(MOUNT_FILE) as f:
lines = f.readlines()
i = 0
while i < len(lines):
mount_drive(lines[i], lines[i + 1], lines[i + 2])
i+=3
file = module.input_file(MOUNT_FILE)
if len(file) == 0:
print(MOUNT_FILE + " is empty")
else:
for i in range(0, len(file), 3):
mount_drive(file[i], file[i + 1], file[i + 2])
def unmount_drive(local_mount_point):
"""
UnMounts a drive from a computer
"""
subprocess.call(["umount", local_mount_point])
subprocess.call(["fusermount", "-u", local_mount_point])
def unmount_drives():
def unmount_all_drives():
"""
UnMounts the ssh drives from the computer
"""
with open(MOUNT_FILE) as f:
lines = f.readlines()
i = 0
while i < len(lines):
unmount_drive(lines[i + 2])
i+=3
file = module.input_file(MOUNT_FILE)
for i in range(0, len(file), 3):
unmount_drive(file[i + 2])
def remove_drive():
"""
"""
options = []
file = module.input_file(MOUNT_FILE)
for i in range(0, len(file), 3):
options.append(str(len(options) + 1) + ") " + file[i])
options.append("A) Exit")
module.print_menu("SSH Drive Manager", options)
def add_drive_to_config(remote_connection, remote_mount_point, local_mount_point):
"""
Adds a new network drive to the default mount config file
"""
module.append_file(MOUNT_FILE, remote_connection)
module.append_file(MOUNT_FILE, remote_mount_point)
module.append_file(MOUNT_FILE, local_mount_point)
def add_drive():
"""
Prompts the user to enter information to add ssh mount drive to config
"""
ssh_acct = input("Enter your ssh account:")
remote_mount = input("Enter the remote mount point:")
local_mount = input("Enter the local mount point:")
add_drive_to_config(ssh_acct, remote_mount, local_mount)
def print_mount_menu():
"""
Displays box which has mount menu options
"""
module.print_menu("SSH Drive Manager", ["1) Remove Remote Drive",
"2) Add Drive to Mount",
"3) Exit"])
def manage_mount_file():
"""
Method which prompts user which action to take with mounts file
"""
print_mount_menu()
i = input("Enter Option:")
while i != '3':
if i == '2':
add_drive()
elif i == '1':
remove_drive()
else:
print("Invalid Option")
print_mount_menu()
i = input("Enter Option:")
def print_usage():
"""
Prints the usage message to the terminal
"""
print("Usage -m, or -u")
print("Usage:")
print("\t-m mounts drives to computer")
print("\t-u unmounts drives from the computer")
print("\t-e manages config file with drives to mount")
def main():
@ -91,7 +139,9 @@ def main():
if sys.argv[1].lower() == "-m":
mount_drives()
elif sys.argv[1].lower() == "-u":
unmount_drives()
unmount_all_drives()
elif sys.argv[1].lower() == "-e":
manage_mount_file()
else:
print_usage()
else:

+ 10
- 13
src/quote.py View File

@ -8,20 +8,14 @@ Jeffery Russell
import subprocess
import random
import module
import roosay
"""Path to a text file containing quotes"""
INPUT_FILE = "/home/jeff/scripts/quotes.txt"
import configuration
def print_cowsay_message(message):
"""
Runs the cowsay command and passes it message to print
:param message: The message to print
:return: None
"""
subprocess.call(["cowsay", message])
"""Path to a text file containing quotes"""
INPUT_FILE = configuration.get_config()["quotes"]
def print_roosay_message(message):
@ -32,15 +26,18 @@ def print_roosay_message(message):
"""
roosay.roo_say(message)
def main():
"""
This function calls the welcome function, then it calls the cowsay function with a random quote.
:return: None
"""
quotes = module.input_file(INPUT_FILE)
#print_cowsay_message(quotes[random.randint(0,(len(quotes) -1))])
print_roosay_message(quotes[random.randint(0,(len(quotes) -1))])
if len(quotes) == 0:
print("Quotes file : " + INPUT_FILE + " is empty.")
else:
print_roosay_message(quotes[random.randint(0,(len(quotes) -1))])
"""

+ 5
- 3
src/roosay.py View File

@ -18,8 +18,9 @@ def main():
message += " "
message += sys.argv[i]
for line in sys.stdin:
message+= line
if len(message) == 0:
for line in sys.stdin:
message+= line
message = " ".join(message.split())
roo_say(message)
@ -108,7 +109,8 @@ def print_roo():
"""
If ran from command line, this will call the main which looks for command line arguments
If ran from command line, this will call the main which
looks for command line arguments
"""
if __name__ == '__main__':
main()

+ 3
- 1
src/ssh_manager.py View File

@ -7,8 +7,10 @@ import subprocess
import collections
import module
import configuration
INPUT_FILE = "/home/jeff/scripts/servers.txt"
#INPUT_FILE = "/home/jeff/scripts/servers.txt"
INPUT_FILE = configuration.get_config()["servers"]
Computer = collections.namedtuple("Computer", ('host', 'menu_id'))

Loading…
Cancel
Save