Browse Source

Started working on configuration file and mounting ssh drives

pull/11/head
jrtechs 6 years ago
parent
commit
bc8897e5a0
3 changed files with 240 additions and 0 deletions
  1. +124
    -0
      src/configuration.py
  2. +9
    -0
      src/module.py
  3. +107
    -0
      src/mount_ssh_drive.py

+ 124
- 0
src/configuration.py View File

@ -0,0 +1,124 @@
"""
Jeffery Russell
4-27-18
Deals with the configuration file for bash manager
Config file:
servers: /path/to/servers
quotes: /path/to/quotes.txt
mounts: /path/to/ssh/mounts.txt
config dictionary
{servers: "/", quotes: "/", mounts:"/"}
"""
import subprocess
import collections
import os.path
import module
CONFIG_FILE = os.path.abspath(__file__) + "/config.txt"
def config_exists():
"""
Function which checks to see if the config file exists
:return: whether file returns
"""
if not os.path.exists(CONFIG_FILE):
print("config file not found in " + CONFIG_FILE)
def single_conf_input(param):
"""
helper function for create_config() which reads the value of a single
file location from the user
"""
print("Please enter the absolute path for your " + param + " file")
print("if you leave this blank, by default it will be "
+ os.path.abspath(__file__) + "/" + param + ".txt")
i = input(":")
if i.strip() == "":
return os.path.abspath(__file__) + "/" + param + ".txt"
else:
return i
def create_config():
"""
Creates a new configuration file
"""
print("Creating new configuration file under " + CONFIG_FILE)
f = open(CONFIG_FILE, "W")
f.write(single_conf_input("servers") + "\n")
f.write(single_conf_input("quotes") + "\n")
f.write(single_conf_input("mounts") + "\n")
f.close()
def read_config():
"""
Reads the config file and creates a config dictionary
"""
config = {}
temp = []
with open(CONFIG_FILE) as file:
for line in file:
if line.find("servers:") != -1:
temp = line.split(" ")
if len(temp) <= 1:
print("Error reading servers file from config")
return
config.servers = temp[1]
if line.find("quotes:") != -1:
if len(temp) <= 1:
print("Error reading quotes file from config")
return
config.quotes = temp[1]
if line.find("mounts:") != -1:
if len(temp) <= 1:
print("Error reading mounts file from config")
return
config.mounts = temp[1]
def valid_config(config):
"""
Checks to see if a configuration is valid
"""
return 'servers' in config and 'quotes' in config and 'mounts' in config
def create_config_dependent_files(config):
"""
Finds missing files and creates them
"""
if not os.path.exists(config.servers):
print("Creating missing servers file in " + config.servers)
module.create_empty_file(config.servers)
if not os.path.exists(config.quotes):
print("Creating missing quotes file in " + config.quotes)
module.create_empty_file(config.quotes)
if not os.path.exists(config.mounts):
print("Creating missing mounts file in " + config.mounts)
module.create_empty_file(config.mounts)
def get_config():
"""
Returns the config file for the main to use
"""
if not config_exists():
module.create_empty_file(CONFIG_FILE)
create_config()
create_config_dependent_files()
config = read_config()
if valid_config(config):
return config

+ 9
- 0
src/module.py View File

@ -3,6 +3,8 @@ Jeffery Russell
9/29/17 9/29/17
""" """
import subprocess
def input_file(file_name): def input_file(file_name):
""" """
@ -41,3 +43,10 @@ def remove_line_from_file(file_name, remove):
if remove not in host: if remove not in host:
f.write(host + "\n") f.write(host + "\n")
f.close() f.close()
def create_empty_file(file_name):
"""
simple function to mimic touch command
"""
subprocess.call(['touch', file_name])

+ 107
- 0
src/mount_ssh_drive.py View File

@ -0,0 +1,107 @@
"""
Jeffery Russell
4-27-18
"""
import subprocess
import collections
import module
import configuration
"""
The mounts.txt file is a sequence of three strings
user@remote.server.address
/remote/mount/point
/local/mount/point
"""
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
"""
runCode = subprocess.call(["sshfs", "-o", "allow_other",
remote_connection + ":" + remote_mount_point, local_mount_point])
if runCode == 0:
print("Mounted " + remote_connection + ":" + remote_mount_point +
" to " + local_mount_point)
else:
print("Failed to mount " + remote_connection + ":" + remote_mount_point)
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
def unmount_drive(local_mount_point):
"""
UnMounts a drive from a computer
"""
subprocess.call(["umount", local_mount_point])
def unmount_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
def print_usage():
"""
Prints the usage message to the terminal
"""
print("Usage -m, or -u")
print("\t-m mounts drives to computer")
print("\t-u unmounts drives from the computer")
def main():
"""
Parses cmd line input and calls appropriate functions
"""
if len(sys.argv) > 1:
if sys.argv[1].lower() == "-m":
mount_drives()
elif sys.argv[1].lower() == "-u":
unmount_drives()
else:
print_usage()
else:
print_usage()
"""
Makes sure that other programs don't execute the main
"""
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
exit_program()

Loading…
Cancel
Save