def unmount_handler(args, config): Sshfs.ensure_sshfs_installed() question = "What instances would you like to have unmounted?" if args['-a']: instances = inventory.instances(config) sshfs_objs = [Sshfs(config, instance, dry_run=args['-d']) for instance in instances] mounted_targets = [obj.instance for obj in sshfs_objs if obj.is_mounted] target_instances = mounted_targets else: desired_targets = args['<host>'] instances = inventory.search(config, desired_targets) sshfs_objs = [Sshfs(config, instance, dry_run=args['-d']) for instance in instances] mounted_targets = [obj.instance for obj in sshfs_objs if obj.is_mounted] target_instances = prompt_targets(question, instances=mounted_targets, multiple=False, config=config) if len(target_instances) == 0: logger.error("No matching mounts found") if args['-a']: logger.warn("Did you select targets with <space> and confirm with <enter>?") sys.exit(1) for sshfsObj in sshfs_objs: if sshfsObj.instance in target_instances: if sshfsObj.unmount(): logger.info("Unmounted %s" % sshfsObj.instance.name) else: logger.error("Unable to unmount %s" % sshfsObj.instance.name)
def mount_handler(args, config): Sshfs.ensure_sshfs_installed() if config.dig('inventory', 'update_at_start') or args['-u']: update_handler(args, config) fields = args['<host>:<remotedir>'].split(':') if len(fields) != 2: logger.error("Requires exactly 2 arguments: host:remotedir") sys.exit(1) desired_target, remotedir = fields instances = inventory.search(config, [desired_target]) sshfs_objs = [Sshfs(config, instance, remotedir, dry_run=args['-d']) for instance in instances] unmounted_targets = [obj.instance for obj in sshfs_objs if not obj.is_mounted] question = "What instances would you like to have mounted?" target_instances = prompt_targets(question, instances=unmounted_targets, multiple=False, config=config) if len(target_instances) == 0: logger.info("No matching instances found") sys.exit(1) for sshfsObj in sshfs_objs: if sshfsObj.instance in target_instances: if sshfsObj.mount(): logger.info("Mounted %s at %s" % (sshfsObj.instance.name, sshfsObj.mountpoint)) else: logger.error("Unable to mount %s" % sshfsObj.instance.name)
def list_mounts_handler(args, config): Sshfs.ensure_sshfs_installed() if args['-d']: return for mountpoint in Sshfs.mounts(config.mount_root_dir): logger.info(mountpoint)
def test_sshfs_unmount_fuse_failure(mocker): mock_rmdir = mocker.patch.object(os, 'rmdir') mock_system = mocker.patch.object(os, 'system') mock_exists = mocker.patch.object(os.path, 'exists') mock_exists.return_value = True mock_system.return_value = 1 mock_rmdir.return_value = True config = Config({}) sshfsObj = Sshfs(config, instance, remotedir='/tmp/test') success = sshfsObj.unmount() assert mock_rmdir.call_count == 0 assert success == False
def test_sshfs_command_go_case_no_options(): config = Config({}) remotedir = '/tmp' sshObj = Sshfs(config, instance, remotedir) mount_arg = '%s/%s@%s' % (config.mount_root_dir, instance.name, instance.address) assert_command_results(sshObj.command, 'sshfs address.com:/tmp %s' % mount_arg)
def test_sshfs_command_user(): config = Config({'ssh': {'user': '******'}}) remotedir = '/tmp' sshObj = Sshfs(config, instance, remotedir) mount_arg = '%s/%s@%s' % (config.mount_root_dir, instance.name, instance.address) assert_command_results(sshObj.command, 'sshfs [email protected]:/tmp %s' % mount_arg)
def test_sshfs_mount_failed(mocker): mock_ls = mocker.patch.object(os, 'listdir') mock_rmdir = mocker.patch('os.rmdir', side_effect=lambda x: True) mock_system = mocker.patch('os.system', side_effect=lambda x: 1) mock_mkdir = mocker.patch('os.mkdir', side_effect=lambda x: True) mock_exists = mocker.patch('os.path.exists', side_effect=lambda x: False) mock_ls.return_value = [ '/home/dummy/.bridgy/mounts/baddir', '/home/dummy/.bridgy/mounts/awesomebox@devbox' ] config = Config({}) sshObj = Sshfs(config, instance, remotedir='/tmp/test') sshObj.mount() assert mock_exists.called assert mock_mkdir.called assert mock_system.called assert mock_rmdir.called
def test_sshfs_command_no_user(): config = Config({'sshfs': {'options': '-C -o ServerAliveInterval=255'}}) remotedir = '/tmp' sshObj = Sshfs(config, instance, remotedir) mount_arg = '%s/%s@%s' % (config.mount_root_dir, instance.name, instance.address) assert_command_results( sshObj.command, 'sshfs -C -o ServerAliveInterval=255 address.com:/tmp %s' % mount_arg)
def test_sshfs_command_mountoptions(): config = Config({ 'sshfs': { 'options': '-o auto_cache,reconnect,defer_permissions,noappledouble,nolocalcaches,no_readahead' } }) remotedir = '/tmp' sshObj = Sshfs(config, instance, remotedir) mount_arg = '%s/%s@%s' % (config.mount_root_dir, instance.name, instance.address) assert_command_results( sshObj.command, "sshfs %s address.com:/tmp %s" % (config.dig('sshfs', 'options'), mount_arg))
def test_sshfs_command_bastion_user(): config = Config( {'bastion': { 'address': 'bastion.com', 'user': '******' }}) remotedir = '/tmp' sshObj = Sshfs(config, instance, remotedir) mount_arg = '%s/%s@%s' % (config.mount_root_dir, instance.name, instance.address) assert_command_results( sshObj.command, "sshfs -o ProxyCommand='ssh -W %%h:%%p [email protected]' address.com:/tmp %s" % mount_arg)
def test_sshfs_command_bastion_options(): config = Config({ 'bastion': { 'address': 'bastion.com', 'options': '-C -o ServerAliveInterval=255' } }) remotedir = '/tmp' sshObj = Sshfs(config, instance, remotedir) mount_arg = '%s/%s@%s' % (config.mount_root_dir, instance.name, instance.address) assert_command_results( sshObj.command, "sshfs -o ProxyCommand='ssh -C -o ServerAliveInterval=255 -W %%h:%%p bastion.com' address.com:/tmp %s" % mount_arg)
def test_sshfs_mounts(mocker): mock_ls = mocker.patch.object(os, 'listdir') mock_open = mocker.patch.object(builtin_module, 'open') mock_open.return_value = StringIO(MTAB) mock_ls.return_value = [ '/home/dummy/.bridgy/mounts/baddir', '/home/dummy/.bridgy/mounts/awesomebox@devbox' ] filename = '/etc/mtab' mounts_root_dir = '/home/dummy/.bridgy/mounts' owned_mount = os.path.join(mounts_root_dir, 'awesomebox@devbox') result = Sshfs.mounts(mounts_root_dir) assert len(result) == 1 assert owned_mount in result
def test_sshfs_options(): config = Config({ 'ssh': { 'user': '******', 'options': '-o ForwardAgent=yes' }, 'sshfs': { 'options': '-C -o ServerAliveInterval=255' } }) remotedir = '/tmp' sshObj = Sshfs(config, instance, remotedir) mount_arg = '%s/%s@%s' % (config.mount_root_dir, instance.name, instance.address) assert_command_results( sshObj.command, 'sshfs -C -o ServerAliveInterval=255 [email protected]:/tmp %s' % mount_arg)
def test_sshfs_mount_remotedir_missing(): config = Config({}) sshObj = Sshfs(config, instance) with pytest.raises(BadRemoteDir): sshObj.mount()
def test_sshfs_command_null_config(): remotedir = '/tmp' with pytest.raises(BadConfigError): sshObj = Sshfs(None, instance, remotedir) sshObj.command
def test_sshfs_command_null_instance(): config = Config({}) remotedir = '/tmp' with pytest.raises(BadInstanceError): sshObj = Sshfs(config, None) sshObj.command
def test_sshfs_command_bastion_missing_address(): config = Config({'bastion': {}}) remotedir = '/tmp' with pytest.raises(MissingBastionHost): sshObj = Sshfs(config, instance, remotedir) sshObj.command