-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver-mount.js
executable file
·173 lines (157 loc) · 5.24 KB
/
server-mount.js
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env node
const fs = require('fs');
const cmd = require('node-cmd');
const Promise = require('promise');
let config = {}; // Configuration settings to be set from process_args()
let isMounting = true;
/** process_args(args)
* Splices an array of strings into usable options
* @param {Array} args
*/
const process_args = () => {
// I promise to return an array of objects
return new Promise((resolve) => {
let cli_args = [];
if (process.argv.indexOf('config') === -1) {
config = JSON.parse(fs.readFileSync('sm-config.json'));
}
// If more args than the defaults
if (process.argv.length > 2) {
process.argv.map((arg, index, arr) => {
// Convert "key=value" to ['key', 'value']
let argArr = arg.split('=');
switch (argArr[0]) {
case 'unmount':
isMounting = false;
break;
case 'config':
isMounting = true;
config = JSON.parse(fs.readFileSync(argArr[1]));
console.log('config: ', config);
break;
default:
// TODO: Add console.log message saying that the config cannot be found
break;
}
// Resolve the promise if done
if (arr.length - index === 1) {
resolve();
}
});
}
else {
isMounting = true;
config = JSON.parse(fs.readFileSync('sm-config.json'));
resolve();
}
});
};
/** create_dirs(dirs)
* Create directories to be used as mount points.
* @param {Array} dirs
*/
const create_dirs = (dirs) => {
console.log('Create needed directories:');
// I promise to return an array of mountable directories
return new Promise((resolve) => {
let mountable_dirs = [];
dirs.map((dir) => {
fs.readdir(dir, (err, files) => {
if (err) {
// Create mount directories if missing
console.log(`Creating directory: ${dir} does not yet exist.`)
cmd.run(`mkdir ${config.dir_path}/${dir}`);
}
else {
console.log(`Skipping directory: ${dir} already exists.`);
}
mountable_dirs.push(dir);
if ( mountable_dirs.length === dirs.length) {
resolve( mountable_dirs);
}
});
});
});
};
/** mount_dirs(dirs, host)
* Mount to remote server with matching directories as mount points.
* @param {Object} config
* @param {Array} dirs
*/
const mount_dirs = (config, dirs) => {
console.log('Mount to directories:');
let c = config;
dirs.map((dir) => {
console.log(`Mounting to directory: ${dir}`);
let opts = `auto_cache,defer_permissions,follow_symlinks,reconnect,noappledouble,volname=${dir}`;
let command = `sshfs ${c.user}@${c.host}:${c.server_path}/${dir} ${c.dir_path}/${dir} -o ${opts}`;
cmd.get(command, (err, data, stderr) => {
if (stderr) console.log(stderr);
});
});
console.log('Mounting complete.');
};
/** remove_dirs(dirs)
* Create directories to be used as mount points.
* @param {Array} dirs
*/
const remove_dirs = (dirs) => {
console.log('Remove old directories:');
dirs.map((dir) => {
fs.readdir(dir, (err, files) => {
if (!err) {
// Create mount directories if missing
console.log(`Deleting directory: ${dir}`)
cmd.run(`rm -r ${config.dir_path}/${dir}`);
}
else {
console.log(`Skipping directory: ${dir} already removed.`);
}
});
});
};
/** mount_dirs(dirs, host)
* Mount to remote server with matching directories as mount points.
* @param {Object} config
* @param {Array} dirs
*/
const unmount_dirs = (config) => {
let removable_dirs = [];
// I promise to return an array of directories that can be removed
return new Promise((resolve) => {
let c = config;
c.dirs.map((dir, index, arr) => {
console.log(`Unmounting directory: ${dir}`);
let command = `umount ${c.dir_path}/${dir}`;
cmd.get(command, (err, data, stderr) => {
if (!err && !stderr) {
removable_dirs.push(dir);
if (arr.length - index === 1) {
console.log('Unmounting complete.');
resolve(removable_dirs);
}
}
});
});
});
};
// RUN THE THINGS
process_args()
.then(() => {
if (isMounting) {
// Create the directories
create_dirs(config.dirs)
.then((resp_dirs) =>{
// Mount to directories that were either just created or already existed
mount_dirs(config, resp_dirs);
});
}
else {
// Unmount each mount point
unmount_dirs(config)
.then((resp_dirs) => {
// Remove old directories
remove_dirs(resp_dirs);
});
}
});