试图在Bluemix上使用FUSE的问题

时间:2015-05-26 22:35:14

标签: ibm-cloud sshfs

我一直在寻找一种方法来添加一个可以访问Bluemix的远程文件系统。在这个post中,我被告知要使用最近版本的Cloud Foundry支持的cflinuxfs2堆栈。

我能够从我的Java应用程序为挂载点执行mkdir命令并执行sshfs命令,但最后一个失败的是:“read:Connection reset by peer”。

关键是在家里的Linux机箱中使用的相同命令工作正常,所以我理解命令,ssh密钥和已知主机文件都可以。

这是在Bluemix中部署到Liberty运行时的Java EE代码:

String s = null;
Process p = null;
BufferedReader br = null;
try 
{
    p = Runtime.getRuntime().exec("mkdir -p /home/vcap/misc");
    br = new BufferedReader(new InputStreamReader(p.getInputStream()));
    while((s = br.readLine()) != null)
        System.out.println("line: " + s);
    p.waitFor();
    System.out.println ("#### Executing command mkdir with exit: " + p.exitValue());
    p.destroy();
    br.close();

    p = Runtime.getRuntime().exec("sshfs ibmcloud@129.41.133.34:/ /home/vcap/misc -o IdentityFile=/home/vcap/app/wlp/usr/servers/defaultServer/apps/myapp.ear/cloud.key -o StrictHostKeyChecking=yes -o UserKnownHostsFile=/home/vcap/app/wlp/usr/servers/defaultServer/apps/myapp.ear/known_hosts -o idmap=user -o compression=no -o sshfs_debug");
    br = new BufferedReader(new InputStreamReader(p.getErrorStream()));
    while((s = br.readLine()) != null)
        System.out.println("line: " + s);
    p.waitFor();
    System.out.println ("#### Executing command sshfs with exit: " + p.exitValue());
    p.destroy();
    br.close();
}
catch(IOException ex)
{
    ex.printStackTrace();
}
catch(InterruptedException ex)
{
    ex.printStackTrace();
}
finally
{
    try 
    {
        if(br != null)
            br.close();
    }
    catch(IOException ex) 
    {
        ex.printStackTrace();
    }
}

命令中的引用文件包含在EAR文件中,并作为应用程序的一部分进行推送。我可以看到他们和他们的内容从Bluemix仪表板浏览文件系统。

浏览网页时,我发现了大量关于错误消息的帖子:“读取:连接由同行重置”但似乎它们不适用于我的情况,或者它们与我在Bluemix中无法访问的防火墙和配置文件有关。正如我所说,在家里的linux盒子里执行的两个命令工作正常。

任何想法或建议让它运作?有人在Bluemix之前测试过这个想法吗?

谢谢!

1 个答案:

答案 0 :(得分:3)

好的,最后我在团队同事的帮助下找到了问题的原因。问题在于私有ssh密钥的权限。它必须是600,默认情况下它是cf push后的644。

所以这里是最终的代码(快速和肮脏),以防万一它对其他人有用......

  1. 在应用程序中包含私钥和known_hosts文件。

  2. 推送应用添加-s cflinuxfs2参数。

  3. 在运行时启动一些代码,如下所示:

    String s = null;
    Process p = null;
    BufferedReader br = null;
    try 
    {
        p = Runtime.getRuntime().exec("mkdir -p /home/vcap/misc");
        br = new BufferedReader(new InputStreamReader(p.getInputStream()));
        while((s = br.readLine()) != null)
            System.out.println("line: " + s);
        p.waitFor();
        System.out.println ("#### Executing command mkdir with exit: " + p.exitValue());
        p.destroy();
        br.close();
    
        p = Runtime.getRuntime().exec("chmod 600 /home/vcap/app/wlp/usr/servers/defaultServer/apps/myapp.ear/cloud.key");
        br = new BufferedReader(new InputStreamReader(p.getInputStream()));
        while((s = br.readLine()) != null)
            System.out.println("line: " + s);
        p.waitFor();
        System.out.println ("#### Executing command chmod with exit: " + p.exitValue());
        p.destroy();
        br.close();
    
        p = Runtime.getRuntime().exec("chmod 600 /home/vcap/app/wlp/usr/servers/defaultServer/apps/myapp.ear/known_hosts");
        br = new BufferedReader(new InputStreamReader(p.getInputStream()));
        while((s = br.readLine()) != null)
            System.out.println("line: " + s);
        p.waitFor();
        System.out.println ("#### Executing command chmod with exit: " + p.exitValue());
        p.destroy();
        br.close();
    
        p = Runtime.getRuntime().exec("sshfs ibmcloud@129.41.133.34:/home/ibmcloud /home/vcap/misc -o IdentityFile=/home/vcap/app/wlp/usr/servers/defaultServer/apps/myapp.ear/cloud.key -o StrictHostKeyChecking=yes -o UserKnownHostsFile=/home/vcap/app/wlp/usr/servers/defaultServer/apps/myapp.ear/known_hosts -o idmap=user -o compression=no -o sshfs_debug");
        br = new BufferedReader(new InputStreamReader(p.getInputStream()));
        while((s = br.readLine()) != null)
            System.out.println("line: " + s);
        p.waitFor();
        System.out.println ("#### Executing command sshfs with exit: " + p.exitValue());
        p.destroy();
        br.close();
    
        p = Runtime.getRuntime().exec("ls -l /home/vcap/misc");
        br = new BufferedReader(new InputStreamReader(p.getInputStream()));
        while((s = br.readLine()) != null)
            System.out.println("line: " + s);
        p.waitFor();
        System.out.println ("#### Executing command ls with exit: " + p.exitValue());
        p.destroy();
        br.close();
    }
    catch(IOException ex)
    {
        ex.printStackTrace();
    }
    catch(InterruptedException ex)
    {
        ex.printStackTrace();
    }
    finally
    {
        try 
        {
            if(br != null)
                br.close();
        }
        catch(IOException ex) 
        {
            ex.printStackTrace();
        }
    }
    

    此代码段应创建一个文件夹,将远程文件系统安装到该文件夹​​中,并列出远程文件系统的内容。