def mount(self): if self.status == self.STATUS_UP: return None sys.stdout.write( "Mounting {0}:{1} at {2}...".format( self, colorize(self.remote_path, COLOR_CYAN), colorize(self.local_path, COLOR_YELLOW) ) ) sys.stdout.flush() if not os.path.exists(self.local_path): os.mkdir(self.local_path) return_code = subprocess.call( [ "sshfs", "{0}:{1}".format(self.name, self.remote_path), self.local_path, "-o", "ConnectTimeout={0}".format(self.CONNECT_TIMEOUT_SECONDS), ] ) if return_code == 0: self.status = self.STATUS_UP sys.stdout.write(colorize("ok\n", COLOR_GREEN)) else: os.rmdir(self.local_path) self.status = self.STATUS_DOWN sys.stdout.write(colorize("fail\n", COLOR_RED)) self.save()
def forget(self): if self.status == self.STATUS_UP: self.unmount() print("Forgetting about {0}...".format(self), end="") hosts = self._get_state() hosts.pop(self.name, None) self._save_state(hosts) print(colorize("ok", COLOR_GREEN))
def unmount(self): if self.status in (self.STATUS_DOWN, self.STATUS_UNKNOWN): return None sys.stdout.write("Unmounting {0}...".format(self)) sys.stdout.flush() if WE_ARE_OSX: unmount_command = ["umount", self.local_path] else: unmount_command = ["fusermount", "-u", self.local_path] return_code = subprocess.call(unmount_command) if return_code == 0: # This typically fails only if the connection is down anyway pass os.rmdir(self.local_path) self.status = self.STATUS_DOWN sys.stdout.write(colorize("ok\n", COLOR_GREEN)) self.save()
def __str__(self): return colorize(self.name, COLOR_BLUE)
if ssh_host: remote_path = args.remote_path rel_path = args.directory host = Host(ssh_host, remote_path=remote_path, rel_path=rel_path) if action == ACTION_FORGET: host.forget() elif action == ACTION_STATUS: table = PPTable(["host", "status", "local_path", "remote_path"]) table.add_row([str(host), host.status, host.local_path, host.remote_path]) print(table) elif action == ACTION_UP: host.mount() elif action == ACTION_DOWN: host.unmount() else: if action == ACTION_STATUS: table = PPTable(["host", "status", "local_path", "remote_path"]) for host in Host.all(): table.add_row([str(host), host.status, host.local_path, host.remote_path]) print(table) elif action == ACTION_DOWN: for host in Host.all(): host.unmount() else: print(colorize("You must specify a host.", COLOR_RED)) sys.exit(5) sys.exit(0)