-
Notifications
You must be signed in to change notification settings - Fork 0
/
sshfs_mounter.py
executable file
·111 lines (87 loc) · 3.76 KB
/
sshfs_mounter.py
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
import os
import sys
import time
import subprocess
from pprint import pprint, pformat
poll_intervall = 2
def get_config_data(config_folder_path):
import json
print ('reading config files from ' + config_folder_path)
data = dict()
data['config_folder_path'] = config_folder_path
if not os.path.isdir(config_folder_path):
return data
config_files = os.listdir(config_folder_path)
if not config_files:
return data
for config_file_name in config_files:
if not config_file_name.endswith('.json'):
continue
config_file_path = os.path.join(
config_folder_path,
config_file_name
)
try:
with open(config_file_path, 'r') as config_file:
config = json.load(config_file)
config_file.close()
except Exception as e:
print('[WARNING] Unable to read config file %s' % config_file_path)
print(e)
name, ext = os.path.splitext(config_file_name)
data[name] = config
return data
if __name__ == "__main__":
app_location = os.path.dirname(os.path.abspath(__file__))
config_folder_path = os.path.join(app_location, 'config')
while True:
current_config = get_config_data(config_folder_path)
locations = current_config.get('locations')
if not isinstance(locations, list):
print ('locations should be list but found %s' % type(locations))
for location in locations:
if not isinstance(location, dict):
print ('location should be a dictionary but found %s' % type(location))
pprint (location)
continue
mount_options = location.get('mount_options')
identity_file = location.get('identity_file')
if mount_options and identity_file:
if "IdentityFile" not in mount_options:
mount_options += ',IdentityFile=' + identity_file
print (mount_options)
args = ['ssh']
if identity_file:
args.append('-i')
args.append(identity_file)
args.append(location.get('user@machine'))
args.append('ls -1 ' + location.get('remote_folder'))
p = subprocess.Popen(args, stdout=subprocess.PIPE)
output = p.communicate()[0].decode()
remote_folders = []
for line in output.splitlines():
if line in location.get('exclude_folders'):
continue
remote_folders.append(line)
local_folders = [x for x in os.listdir(location.get('local_folder'))]
for local_folder in local_folders:
if local_folder not in remote_folders:
try:
local_folder_path = os.path.join(location.get('local_folder'), local_folder)
cmd = 'sudo umount "' + local_folder_path + '"; sudo rmdir "' + local_folder_path + '"'
os.system(cmd)
except:
pass
for remote_folder in remote_folders:
try:
cmd = 'mkdir -p "' + os.path.join(location.get('local_folder'), remote_folder) + '"'
os.system(cmd)
cmd = 'sshfs ' + location.get('user@machine') + ':"'
cmd += os.path.join(location.get('remote_folder'), remote_folder) + '" '
cmd += '"' + os.path.join(location.get('local_folder'), remote_folder) + '" '
cmd += '-o ' + mount_options
cmd += ' 2>/dev/null'
os.system(cmd)
except:
pass
time.sleep(poll_intervall)