-
Notifications
You must be signed in to change notification settings - Fork 0
/
smount
executable file
·148 lines (105 loc) · 3.8 KB
/
smount
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env python3
import sys, os, subprocess, signal
import argparse, json
CONFIG_FILES = [ os.path.join(os.path.expanduser("~"), ".smountrc"), "/etc/smountrc" ]
AUTO_MKDIR = True
SSHFS = "/usr/bin/sshfs"
FUSERMOUNT = "/usr/bin/fusermount"
def signal_handler(signal, frame):
sys.exit(1)
signal.signal(signal.SIGINT, signal_handler)
class Mount:
def __init__(self, name, data):
self.name = name
self.data = data
def __contains__(self, key):
return key in self.data
def host(self, value = None):
return self.__data("host", value)
def mountdir(self, value = None):
return self.__data("mountdir", value)
def port(self, value = None):
return self.__data("port", value)
def user(self, value = None):
return self.__data("user", value)
def dir(self, value = None):
return self.__data("dir", value)
def __data(self, key, value):
if value:
self.data[key] = value
return self.data[key]
class Smount:
def __init__(self, config_files):
for conf in config_files:
if os.path.isfile(conf):
self.conf = json.load(open(conf, "r"))
self.automkdir = self.getconf("automkdir", AUTO_MKDIR)
self.sshfs = self.getconf("sshfs", SSHFS)
self.fusermount = self.getconf("fusermount", FUSERMOUNT)
self.basedir = self.getconf("basedir", required = True)
return
sys.exit("Configuration file is not available!")
def getconf(self, key, default = None, required = False):
if key not in self.conf:
if required:
sys.exit("Configuration key '%s' is required!" % key)
return default
else:
return self.conf[key]
def getmount(self, name, umountOnly = False):
if name not in self.conf["mounts"]:
sys.exit("Mount '%s' does not exist!" % name)
mount = Mount(name, self.conf["mounts"][name])
if "mountdir" not in mount:
mount.mountdir(name)
if umountOnly:
return mount
if "host" not in mount:
mount.host(name)
if "dir" not in mount:
mount.dir(".")
return mount
def getmountdir(self, mount):
return os.path.join(self.basedir, mount.mountdir())
def mount(self, name, username = None, remotedir = None):
selected = self.getmount(name)
mountdir = self.getmountdir(selected)
if not os.path.isdir(mountdir):
if self.automkdir:
os.mkdir(mountdir)
else:
sys.exit("Directory for mountpoint does not exist!")
if os.path.ismount(mountdir):
sys.exit("Mountpoint is already mounted...")
cmd = self.sshfs + " "
if "port" in selected:
cmd += "-p %d " % int(selected.port())
if username or "user" in selected:
cmd += "%s@" % (username if username else selected.user())
cmd += "%s:%s %s" % (selected.host(), selected.dir(), mountdir)
self.runcmd(cmd)
def umount(self, name, lazy = False):
mountdir = self.getmountdir(self.getmount(name, True))
cmd = "%s -u%s %s" % (self.fusermount, ("z" if lazy else ""), mountdir)
self.runcmd(cmd)
def runcmd(self, cmd):
print("### %s ###" % cmd)
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.PIPE)
print(p.stdout.read().decode("utf-8"), end="")
sys.exit(p.wait())
if __name__ == "__main__":
parser = argparse.ArgumentParser(prog="smount")
parser.add_argument("-c", "--config-file", help="use specified configuration file")
parser.add_argument("-u", "--unmount", help="unmount filesystem", action="store_true")
parser.add_argument("-z", "--lazy", help="lazy unmount", action="store_true")
parser.add_argument("-n", "--username", help="mount filesystem as user")
parser.add_argument("mountpoint", help="target mountpoint")
parser.add_argument("directory", nargs="?", help="specify directory")
args = parser.parse_args()
if args.config_file:
CONFIG_FILES = [args.config_file]
smount = Smount(CONFIG_FILES)
if args.unmount:
smount.umount(args.mountpoint)
else:
smount.mount(args.mountpoint, args.username, args.directory)