forked from avichal314e/BashProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcourse_mount.py
More file actions
87 lines (69 loc) · 2.37 KB
/
course_mount.py
File metadata and controls
87 lines (69 loc) · 2.37 KB
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
from subprocess import PIPE, run, STDOUT
import sys
courses = [
"Linux_course/Linux_course1",
"Linux_course/Linux_course2",
"machinelearning/ml1",
"machinelearning/ml2",
"SQL1",
"SQL2",
"SQL3"
]
def usage():
print("Usage:")
print("\t./course_mount.py -h To print this help message")
print("\t./course_mount.py -m -c [course] For mounting a given course")
print("\t./course_mount.py -u -c [course] For unmounting a given course")
print("If course name is ommited all courses will be (un)mounted")
def check_mount(file):
res = run(['mountpoint', '/home/trainee/courses/'+file],
stdin=PIPE, stdout=PIPE, stderr=STDOUT, text=True)
return (1-res.returncode)
# 0 for Not Mount, 1 for Mount
def mount_course(file):
if(file in courses):
if(check_mount(file)):
print(f"{file} Mount Already Exists.")
else:
run(['sudo', 'mkdir', '-p', '/home/trainee/courses/'+file],
stdin=PIPE, stdout=PIPE, stderr=STDOUT, text=True)
bind = run(['sudo', 'bindfs', '-p', '550', '-u', 'trainee', '-g', 'ftpaccess', '/data/courses/'+file, '/home/trainee/courses/'+file],
stdin=PIPE, stdout=PIPE, stderr=STDOUT, text=True)
if(bind.returncode == 0):
print(f"Mounted {file}")
else:
print("Some Error Occured.")
else:
print("Course Not Found!")
def mount_all():
for course in courses:
mount_course(course)
def unmount_course(file):
if(check_mount(file)):
unbind = run(['sudo', 'umount', '/home/trainee/courses/'+file],
stdin=PIPE, stdout=PIPE, stderr=STDOUT, text=True)
if(unbind.returncode == 0):
run(['sudo', 'rm', '-rf', '/home/trainee/courses/'+file],
stdin=PIPE, stdout=PIPE, stderr=STDOUT, text=True)
print(f"Unmounted {file}")
else:
print("Some Error Occured")
else:
print(f"{file} Mount does not Exist.")
def unmount_all():
for course in courses:
unmount_course(course)
inputs = sys.argv[1:]
val = inputs[0]
if(val == "-h"):
usage()
elif(val == "-m"):
if(len(inputs) == 1):
mount_all()
else:
mount_course(inputs[2])
elif(val == "-u"):
if(len(inputs) == 1):
unmount_all()
else:
unmount_course(inputs[2])